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