2303
|
1 ### Copyright (C) 1996 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 |
|
7 ### the Free Software Foundation; either version 2, or (at your option) |
|
8 ### any later version. |
|
9 ### |
|
10 ### Octave is distributed in the hope that it will be useful, but |
|
11 ### WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ### General Public License 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, 59 Temple Place - Suite 330, Boston, MA |
|
18 ### 02111-1307, USA. |
1024
|
19 |
1507
|
20 function saveimage (filename, img, img_form, map) |
904
|
21 |
2303
|
22 ## Save a matrix to disk in image format. |
|
23 ## |
|
24 ## saveimage (filename, x) saves matrix x to file filename in octave's |
|
25 ## image format. The current colormap is saved in the file also. |
|
26 ## |
|
27 ## saveimage (filename, x, "img") saves the image in the default format |
|
28 ## and is the same as saveimage (filename, x). |
|
29 ## |
|
30 ## saveimage (filename, x, "ppm") saves the image in ppm format instead |
|
31 ## of the default octave image format. |
|
32 ## |
|
33 ## saveimage (filename, x, "ps") saves the image in PostScript format |
|
34 ## instead of the default octave image format. (Note: images saved in |
|
35 ## PostScript format can not be read back into octave with loadimage.) |
|
36 ## |
|
37 ## saveimage (filename, x, format, map) saves the image along with the |
|
38 ## specified colormap in the specified format. |
|
39 ## |
|
40 ## Note: If the colormap contains only two entries and these entries |
|
41 ## are black and white, the bitmap ppm and PostScript formats are used. |
|
42 ## If the image is a gray scale image (the entries within each row of |
|
43 ## the colormap are equal) the gray scale ppm and PostScript image |
|
44 ## formats are used, otherwise the full color formats are used. |
|
45 ## |
|
46 ## The conversion to PostScript is based on pbmtolps.c, which was |
|
47 ## written by |
|
48 ## |
|
49 ## George Phillips <phillips@cs.ubc.ca> |
|
50 ## Department of Computer Science |
|
51 ## University of British Columbia |
|
52 ## |
|
53 ## and is part of the portable bitmap utilities, |
|
54 ## |
|
55 ## SEE ALSO: loadimage, save, load, colormap |
559
|
56 |
2303
|
57 ## Written by Tony Richardson (amr@mpl.ucsd.edu) July 1994. |
1024
|
58 |
2303
|
59 ## Rewritten by jwe to avoid using octoppm and pbm routines so that |
|
60 ## people who don't have the the pbm stuff installed can still use this |
|
61 ## function. |
|
62 ## |
|
63 ## The conversion to PostScript is based on pnmtops.c, which is part of |
|
64 ## the portable bitmap utilties and bears this copyright notice: |
|
65 ## |
|
66 ## Copyright (C) 1989 by Jef Poskanzer. |
|
67 ## |
|
68 ## Permission to use, copy, modify, and distribute this software and its |
|
69 ## documentation for any purpose and without fee is hereby granted, provided |
|
70 ## that the above copyright notice appear in all copies and that both that |
|
71 ## copyright notice and this permission notice appear in supporting |
|
72 ## documentation. This software is provided "as is" without express or |
|
73 ## implied warranty. |
1507
|
74 |
1024
|
75 if (nargin < 2 || nargin > 4) |
|
76 usage ("saveimage (filename, matrix, [format, [colormap]])"); |
1499
|
77 endif |
|
78 |
|
79 if (nargin < 4) |
|
80 map = colormap (); |
|
81 endif |
1507
|
82 |
|
83 [map_nr, map_nc] = size (map); |
|
84 |
|
85 if (map_nc != 3) |
1499
|
86 error ("colormap should be an N x 3 matrix"); |
|
87 endif |
|
88 |
|
89 if (nargin < 3) |
559
|
90 img_form = "img"; |
1499
|
91 elseif (! isstr (img_form)) |
|
92 error ("image format specification must be a string"); |
|
93 elseif (! (strcmp (img_form, "img") |
|
94 || strcmp (img_form, "ppm") |
|
95 || strcmp (img_form, "ps"))) |
1507
|
96 error ("unsupported image format specification"); |
559
|
97 endif |
|
98 |
1507
|
99 if (! is_matrix (img)) |
1499
|
100 warning ("image variable is not a matrix"); |
|
101 endif |
1024
|
102 |
1499
|
103 if (! isstr (filename)) |
|
104 error ("file name must be a string"); |
559
|
105 endif |
|
106 |
2303
|
107 ## If we just want Octave image format, save and return. |
1499
|
108 |
|
109 if (strcmp (img_form, "img")) |
1507
|
110 eval (strcat ("save -ascii ", filename, " map img")); |
1499
|
111 return; |
|
112 endif |
1024
|
113 |
2303
|
114 ## Convert to another format if requested. |
559
|
115 |
1887
|
116 grey = all (map(:,1) == map(:,2) && map(:,1) == map (:,3)); |
1507
|
117 |
|
118 pbm = pgm = ppm = 0; |
|
119 |
|
120 map_sz = map_nr * map_nc; |
|
121 |
|
122 map = reshape (map, map_sz, 1); |
|
123 |
|
124 idx = find (map > 1); |
|
125 map (idx) = ones (size (idx)); |
|
126 |
|
127 idx = find (map < 0); |
|
128 map (idx) = zeros (size (idx)); |
|
129 |
|
130 map = round (255 * map); |
|
131 |
|
132 bw = (map_nr == 2 |
|
133 && ((map(1,1) == 0 && map(2,1) == 255) |
|
134 || (map(1,1) == 255 && map(2,1) == 0))); |
|
135 |
1539
|
136 img = round (img'); |
1507
|
137 [img_nr, img_nc] = size (img); |
|
138 |
|
139 img_sz = img_nr * img_nc; |
|
140 img = reshape (img, img_sz, 1); |
|
141 |
|
142 idx = find (img >= map_nr - 1); |
|
143 img (idx) = ones (size (idx)) * map_nr; |
|
144 |
|
145 idx = find (img <= 0); |
|
146 img (idx) = ones (size (idx)); |
|
147 |
|
148 if (strcmp (img_form, "ppm")) |
|
149 if (grey && map_nr == 2 && bw) |
|
150 |
|
151 map = map(:,1); |
|
152 |
|
153 if (map(1) != 0) |
|
154 map(1) = 1; |
|
155 else |
|
156 map(2) = 1; |
|
157 endif |
|
158 |
|
159 img = map(img); |
|
160 n_long = rem (img_sz, 8); |
|
161 if (n_long == 0) |
|
162 n_long = 8; |
|
163 else |
|
164 n_long = 1 + nlong; |
|
165 endif |
|
166 |
|
167 idx = 1:8:img_sz; |
|
168 s_len = length (idx) - 1; |
|
169 |
|
170 tmp = img (1:8:img_sz) * 128; |
|
171 for i = 2:n_long |
|
172 tmp = tmp + img (i:8:img_sz) * 2^(8-i); |
|
173 endfor |
|
174 for i = (n_long+1):8 |
|
175 tmp(1:s_len) = tmp(1:s_len) + img (i:8:img_sz) * 2^(8-i); |
|
176 endfor |
|
177 |
|
178 fid = fopen (filename, "w"); |
|
179 fprintf (fid, "P4\n%d %d\n", img_nr, img_nc); |
|
180 fwrite (fid, tmp, "char"); |
|
181 fprintf (fid, "\n"); |
|
182 fclose (fid); |
|
183 |
|
184 elseif (grey) |
|
185 |
|
186 fid = fopen (filename, "w"); |
|
187 fprintf (fid, "P5\n%d %d\n255\n", img_nr, img_nc); |
|
188 fwrite (fid, map(img), "uchar"); |
|
189 fprintf (fid, "\n"); |
|
190 fclose (fid); |
|
191 |
|
192 else |
|
193 |
1886
|
194 img_idx = ((1:3:3*img_sz)+2)'; |
|
195 map_idx = ((2*map_nr+1):map_sz)'; |
1507
|
196 |
|
197 tmap = map(map_idx); |
|
198 tmp(img_idx--) = tmap(img); |
|
199 |
|
200 map_idx = map_idx - map_nr; |
1887
|
201 tmap = map(map_idx); |
1507
|
202 tmp(img_idx--) = tmap(img); |
|
203 |
|
204 map_idx = map_idx - map_nr; |
|
205 tmap = map(map_idx); |
|
206 tmp(img_idx--) = tmap(img); |
|
207 |
|
208 fid = fopen (filename, "w"); |
|
209 fprintf (fid, "P6\n%d %d\n255\n", img_nr, img_nc); |
|
210 fwrite (fid, tmp, "uchar"); |
|
211 fprintf (fid, "\n"); |
|
212 fclose (fid); |
|
213 |
1499
|
214 endif |
|
215 |
1507
|
216 elseif (strcmp (img_form, "ps") == 1) |
|
217 |
|
218 if (! grey) |
1518
|
219 error ("must have a greyscale color map for conversion to PostScript"); |
1507
|
220 endif |
|
221 |
|
222 bps = 8; |
|
223 dpi = 300; |
|
224 pagewid = 612; |
|
225 pagehgt = 762; |
|
226 MARGIN = 0.95; |
|
227 devpix = dpi / 72.0 + 0.5; |
|
228 pixfac = 72.0 / dpi * devpix; |
|
229 |
2303
|
230 ## Compute padding to round cols * bps up to the nearest multiple of 8 |
|
231 ## (nr and nc are switched because we transposed the image above). |
1507
|
232 |
|
233 padright = (((img_nr * bps + 7) / 8) * 8 - img_nr * bps) / bps; |
|
234 |
|
235 scols = img_nr * pixfac; |
|
236 srows = img_nc * pixfac; |
|
237 |
|
238 if (scols > pagewid * MARGIN || srows > pagehgt * MARGIN) |
|
239 if (scols > pagewid * MARGIN) |
|
240 scale = scale * (pagewid / scols * MARGIN); |
|
241 scols = scale * img_nr * pixfac; |
|
242 srows = scale * img_nc * pixfac; |
|
243 endif |
|
244 if (srows > pagehgt * MARGIN) |
|
245 scale = scale * (pagehgt / srows * MARGIN); |
|
246 scols = scale * img_nr * pixfac; |
|
247 srows = scale * img_nc * pixfac; |
|
248 endif |
|
249 warning ("image too large for page, rescaling to %g", scale); |
|
250 endif |
|
251 |
|
252 llx = (pagewid - scols) / 2; |
|
253 lly = (pagehgt - srows) / 2; |
|
254 urx = llx + fix (scols + 0.5); |
|
255 ury = lly + fix (srows + 0.5); |
|
256 |
|
257 fid = fopen (filename, "w"); |
1499
|
258 |
1507
|
259 fprintf (fid, "%%!PS-Adobe-2.0 EPSF-2.0\n"); |
|
260 fprintf (fid, "%%%%Creator: pnmtops\n"); |
|
261 fprintf (fid, "%%%%Title: %s\n", filename); |
|
262 fprintf (fid, "%%%%Pages: 1\n"); |
|
263 fprintf (fid, "%%%%BoundingBox: %d %d %d %d\n", |
|
264 fix (llx), fix (lly), fix (urx), fix (ury)); |
|
265 fprintf (fid, "%%%%EndComments\n" ); |
|
266 fprintf (fid, "/readstring {\n"); |
|
267 fprintf (fid, " currentfile exch readhexstring pop\n"); |
|
268 fprintf (fid, "} bind def\n"); |
|
269 fprintf (fid, "/picstr %d string def\n", |
|
270 fix ((img_nr + padright) * bps / 8)); |
|
271 fprintf (fid, "%%%%EndProlog\n"); |
|
272 fprintf (fid, "%%%%Page: 1 1\n"); |
|
273 fprintf (fid, "gsave\n"); |
|
274 fprintf (fid, "%g %g translate\n", llx, lly); |
|
275 fprintf (fid, "%g %g scale\n", scols, srows); |
|
276 fprintf (fid, "%d %d %d\n", img_nr, img_nc, bps); |
|
277 fprintf (fid, "[ %d 0 0 -%d 0 %d ]\n", img_nr, img_nc, img_nc); |
|
278 fprintf (fid, "{ picstr readstring }\n" ); |
|
279 fprintf (fid, "image\n" ); |
1499
|
280 |
1507
|
281 img = map(img); |
|
282 |
2303
|
283 ## XXX FIXME XXX -- this would be much faster if fprintf knew about |
|
284 ## vector arguments. |
1507
|
285 |
|
286 count = 0; |
|
287 for i = 1:img_sz |
1518
|
288 fprintf (fid, "%x", img (i)); |
1507
|
289 if (++count == 30) |
|
290 count = 0; |
|
291 fprintf (fid, "\n"); |
|
292 endif |
|
293 endfor |
|
294 |
|
295 fprintf (fid, "\n" ); |
|
296 fprintf (fid, "grestore\n" ); |
|
297 fprintf (fid, "showpage\n" ); |
|
298 fprintf (fid, "%%%%Trailer\n" ); |
|
299 fclose (fid); |
|
300 |
|
301 else |
|
302 error ("saveimage: what happened to the image type?"); |
|
303 endif |
559
|
304 |
|
305 endfunction |