559
|
1 function saveimage(filename,X,img_form,map) |
904
|
2 |
|
3 # Save a matrix to disk in image format. |
|
4 # |
|
5 # saveimage(filename,x) saves matrix x to file filename in octave's image |
|
6 # format. The current colormap is saved in the file also. |
|
7 # |
|
8 # saveimage(filename,x,"img") saves the image in the default format and |
|
9 # is the same as saveimage(filename,x). |
|
10 # |
|
11 # saveimage(filename,x,"ppm") saves the image in ppm format instead of |
|
12 # the default octave image format. |
|
13 # |
|
14 # saveimage(filename,x,"ps") saves the image in PostScript format instead |
|
15 # of the default octave image format. (Note: images saved in PostScript format |
|
16 # can not be read back into octave with loadimage.) |
|
17 # |
|
18 # saveimage(filename,x,format,map) saves the image along with the specified |
|
19 # colormap in the specified format. |
|
20 # |
|
21 # Note: If the colormap contains only two entries and these entries are black |
|
22 # and white, the bitmap ppm and PostScript formats are used. If the image is |
|
23 # a gray scale image (the entries within each row of the colormap are equal) |
|
24 # the gray scale ppm and PostScript image formats are used, otherwise the full |
|
25 # color formats are used. |
|
26 # |
|
27 # SEE ALSO: loadimage, save, load, colormap |
559
|
28 |
|
29 if(nargin < 2) |
|
30 error("usage: saveimage(filename,matrix,[format, [colormap]])"); |
|
31 elseif(nargin == 2) |
|
32 if(!isstr(filename)) |
|
33 error("File name must be a string."); |
|
34 endif |
|
35 map = colormap; |
|
36 img_form = "img"; |
|
37 elseif(nargin == 3) |
|
38 if(!isstr(img_form)) |
|
39 error("Image format specification must be a string"); |
|
40 endif |
|
41 map = colormap; |
|
42 endif |
|
43 |
|
44 if (strcmp(img_form,"img") == 1) |
|
45 oct_file = filename; |
|
46 elseif (strcmp(img_form,"ppm") == 1) |
|
47 # We need a temporary octave image file name. |
|
48 oct_file = sprintf("image.%s.img",num2str(fix(rand*10000))); |
|
49 ppm_file = filename; |
|
50 elseif (strcmp(img_form,"ps") == 1) |
|
51 # We need a temporary octave image file name. |
|
52 oct_file = sprintf("image.%s.img",num2str(fix(rand*10000))); |
|
53 ps_file = filename; |
|
54 endif |
|
55 |
|
56 # Save image in octave image file format |
|
57 eval(['save ', oct_file, ' map X']); |
|
58 |
|
59 # Convert to another format if requested. |
|
60 if (strcmp(img_form,"ppm") == 1) |
686
|
61 octtopnm = sprintf("octtopnm %s > %s",oct_file,filename); |
559
|
62 rm = sprintf("rm -f %s",oct_file); |
|
63 shell_cmd(octtopnm); |
|
64 shell_cmd(rm); |
|
65 elseif (strcmp(img_form,"ps") == 1) |
686
|
66 octtopnm = sprintf("octtopnm %s",oct_file); |
559
|
67 ppmtops = sprintf("pnmtops > %s 2> /dev/null", filename); |
|
68 octtops = [ octtopnm, " | ", ppmtops ]; |
|
69 rm = sprintf("rm -f %s",oct_file); |
|
70 shell_cmd(octtops); |
|
71 shell_cmd(rm); |
|
72 endif |
|
73 |
|
74 endfunction |