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