annotate scripts/image/saveimage.m @ 1499:47bab2f5363f

[project @ 1995-10-02 10:10:44 by jwe]
author jwe
date Mon, 02 Oct 1995 10:10:44 +0000
parents 611d403c7f3d
children 79ca6398b78e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
1 # Copyright (C) 1995 John W. Eaton
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
2 #
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
3 # This file is part of Octave.
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
4 #
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
5 # Octave is free software; you can redistribute it and/or modify it
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
6 # under the terms of the GNU General Public License as published by the
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
7 # Free Software Foundation; either version 2, or (at your option) any
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
8 # later version.
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
9 #
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
10 # Octave is distributed in the hope that it will be useful, but WITHOUT
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
13 # for more details.
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
14 #
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
15 # You should have received a copy of the GNU General Public License
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
16 # along with Octave; see the file COPYING. If not, write to the Free
1315
611d403c7f3d [project @ 1995-06-25 19:56:32 by jwe]
jwe
parents: 1136
diff changeset
17 # Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
18
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
19 function saveimage (filename, X, img_form, map)
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 686
diff changeset
20
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 686
diff changeset
21 # Save a matrix to disk in image format.
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 686
diff changeset
22 #
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
23 # saveimage (filename, x) saves matrix x to file filename in octave's
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
24 # image format. The current colormap is saved in the file also.
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 686
diff changeset
25 #
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
26 # saveimage (filename, x, "img") saves the image in the default format
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
27 # and is the same as saveimage (filename, x).
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 686
diff changeset
28 #
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
29 # saveimage (filename, x, "ppm") saves the image in ppm format instead
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
30 # of the default octave image format.
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 686
diff changeset
31 #
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
32 # saveimage (filename, x, "ps") saves the image in PostScript format
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
33 # instead of the default octave image format. (Note: images saved in
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
34 # PostScript format can not be read back into octave with loadimage.)
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 686
diff changeset
35 #
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
36 # saveimage (filename, x, format, map) saves the image along with the
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
37 # specified colormap in the specified format.
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 686
diff changeset
38 #
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
39 # Note: If the colormap contains only two entries and these entries
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
40 # are black and white, the bitmap ppm and PostScript formats are used.
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
41 # If the image is a gray scale image (the entries within each row of
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
42 # the colormap are equal) the gray scale ppm and PostScript image
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
43 # formats are used, otherwise the full color formats are used.
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 686
diff changeset
44 #
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 686
diff changeset
45 # SEE ALSO: loadimage, save, load, colormap
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
46
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
47 # Written by Tony Richardson (amr@mpl.ucsd.edu) July 1994.
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
48
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
49 if (nargin < 2 || nargin > 4)
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
50 usage ("saveimage (filename, matrix, [format, [colormap]])");
1499
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
51 endif
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
52
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
53 if (nargin < 4)
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
54 map = colormap ();
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
55 endif
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
56 if (columns (map) != 3)
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
57 error ("colormap should be an N x 3 matrix");
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
58 endif
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
59
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
60 if (nargin < 3)
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
61 img_form = "img";
1499
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
62 elseif (! isstr (img_form))
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
63 error ("image format specification must be a string");
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
64 elseif (! (strcmp (img_form, "img")
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
65 || strcmp (img_form, "ppm")
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
66 || strcmp (img_form, "ps")))
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
67 error ("unsupported image format specification");
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
68 endif
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
69
1499
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
70 if (! is_matrix (X))
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
71 warning ("image variable is not a matrix");
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
72 endif
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
73
1499
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
74 if (! isstr (filename))
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
75 error ("file name must be a string");
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
76 endif
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
77
1499
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
78 # If we just want Octave image format, save and return.
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
79
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
80 if (strcmp (img_form, "img"))
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
81 eval (strcat ("save -ascii ", filename, " map X"));
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
82 return;
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
83 endif
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
84
1499
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
85 unwind_protect
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
86
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
87 oct_file = octave_tmp_file_name ();
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
88
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
89 # Save image in Octave image file format
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
90
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
91 eval (strcat ("save -ascii ", oct_file, " map X"));
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
92
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 904
diff changeset
93 # Convert to another format if requested.
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
94
1499
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
95 if (strcmp (img_form, "ppm"))
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
96 shell_cmd (sprintf ("octtopnm %s > %s", oct_file, filename));
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
97 elseif (strcmp (img_form, "ps") == 1)
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
98 shell_cmd (sprintf ("octtopnm %s | pnmtops > %s 2> /dev/null",
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
99 oct_file, filename));
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
100 endif
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
101
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
102 unwind_protect_cleanup
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
103
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
104 shell_cmd (sprintf ("rm -f %s", oct_file));
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
105
47bab2f5363f [project @ 1995-10-02 10:10:44 by jwe]
jwe
parents: 1315
diff changeset
106 end_unwind_protect
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
107
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
108 endfunction