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 |
|
17 # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
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]])"); |
|
51 elseif (nargin == 2) |
|
52 if (! isstr (filename)) |
|
53 error ("file name must be a string"); |
559
|
54 endif |
|
55 map = colormap; |
|
56 img_form = "img"; |
1024
|
57 elseif (nargin == 3) |
|
58 if (! isstr (img_form)) |
|
59 error ("image format specification must be a string"); |
559
|
60 endif |
|
61 map = colormap; |
|
62 endif |
|
63 |
1024
|
64 # XXX FIXME XXX -- we should check the remaining args. |
|
65 |
|
66 # XXX FIXME XXX -- we should use octave_tmp_file_name here. |
|
67 |
|
68 if (strcmp (img_form, "img") == 1) |
559
|
69 oct_file = filename; |
1024
|
70 elseif (strcmp (img_form, "ppm") == 1) |
|
71 oct_file = sprintf ("image.%s.img", num2str (fix (rand * 10000))); |
559
|
72 ppm_file = filename; |
1024
|
73 elseif (strcmp (img_form, "ps") == 1) |
|
74 oct_file = sprintf ("image.%s.img", num2str (fix (rand *10000))); |
559
|
75 ps_file = filename; |
|
76 endif |
|
77 |
1024
|
78 # Save image in octave image file format |
|
79 |
1136
|
80 eval (['save -ascii ', oct_file, ' map X']); |
1024
|
81 |
|
82 # Convert to another format if requested. |
559
|
83 |
1024
|
84 if (strcmp (img_form, "ppm") == 1) |
|
85 octtopnm = sprintf ("octtopnm %s > %s", oct_file, filename); |
|
86 rm = sprintf("rm -f %s", oct_file); |
|
87 shell_cmd (octtopnm); |
|
88 shell_cmd (rm); |
|
89 elseif (strcmp (img_form, "ps") == 1) |
|
90 octtopnm = sprintf ("octtopnm %s", oct_file); |
|
91 ppmtops = sprintf ("pnmtops > %s 2> /dev/null", filename); |
559
|
92 octtops = [ octtopnm, " | ", ppmtops ]; |
1024
|
93 rm = sprintf ("rm -f %s", oct_file); |
|
94 shell_cmd (octtops); |
|
95 shell_cmd (rm); |
559
|
96 endif |
|
97 |
|
98 endfunction |