2313
|
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 |
2311
|
20 ## Save a matrix to disk in image format. |
|
21 ## |
|
22 ## saveimage (filename, x) saves matrix x to file filename in octave's |
|
23 ## image format. The current colormap is saved in the file also. |
|
24 ## |
|
25 ## saveimage (filename, x, "img") saves the image in the default format |
|
26 ## and is the same as saveimage (filename, x). |
|
27 ## |
|
28 ## saveimage (filename, x, "ppm") saves the image in ppm format instead |
|
29 ## of the default octave image format. |
|
30 ## |
|
31 ## saveimage (filename, x, "ps") saves the image in PostScript format |
|
32 ## instead of the default octave image format. (Note: images saved in |
|
33 ## PostScript format can not be read back into octave with loadimage.) |
|
34 ## |
|
35 ## saveimage (filename, x, format, map) saves the image along with the |
|
36 ## specified colormap in the specified format. |
|
37 ## |
|
38 ## Note: If the colormap contains only two entries and these entries |
|
39 ## are black and white, the bitmap ppm and PostScript formats are used. |
|
40 ## If the image is a gray scale image (the entries within each row of |
|
41 ## the colormap are equal) the gray scale ppm and PostScript image |
|
42 ## formats are used, otherwise the full color formats are used. |
|
43 ## |
|
44 ## The conversion to PostScript is based on pbmtolps.c, which was |
2325
|
45 ## written by |
2311
|
46 ## |
|
47 ## George Phillips <phillips@cs.ubc.ca> |
|
48 ## Department of Computer Science |
|
49 ## University of British Columbia |
|
50 ## |
|
51 ## and is part of the portable bitmap utilities, |
|
52 ## |
|
53 ## SEE ALSO: loadimage, save, load, colormap |
904
|
54 |
2312
|
55 ## Author: Tony Richardson <amr@mpl.ucsd.edu> |
|
56 ## Created: July 1994 |
|
57 ## Adapted-By: jwe |
1024
|
58 |
2312
|
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 |
2325
|
61 ## function. |
2312
|
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. |
|
74 |
|
75 function saveimage (filename, img, img_form, map) |
1507
|
76 |
1024
|
77 if (nargin < 2 || nargin > 4) |
|
78 usage ("saveimage (filename, matrix, [format, [colormap]])"); |
1499
|
79 endif |
|
80 |
|
81 if (nargin < 4) |
|
82 map = colormap (); |
|
83 endif |
1507
|
84 |
|
85 [map_nr, map_nc] = size (map); |
|
86 |
|
87 if (map_nc != 3) |
1499
|
88 error ("colormap should be an N x 3 matrix"); |
|
89 endif |
|
90 |
|
91 if (nargin < 3) |
559
|
92 img_form = "img"; |
1499
|
93 elseif (! isstr (img_form)) |
|
94 error ("image format specification must be a string"); |
|
95 elseif (! (strcmp (img_form, "img") |
|
96 || strcmp (img_form, "ppm") |
|
97 || strcmp (img_form, "ps"))) |
1507
|
98 error ("unsupported image format specification"); |
559
|
99 endif |
|
100 |
1507
|
101 if (! is_matrix (img)) |
1499
|
102 warning ("image variable is not a matrix"); |
|
103 endif |
1024
|
104 |
1499
|
105 if (! isstr (filename)) |
|
106 error ("file name must be a string"); |
559
|
107 endif |
|
108 |
2303
|
109 ## If we just want Octave image format, save and return. |
1499
|
110 |
|
111 if (strcmp (img_form, "img")) |
1507
|
112 eval (strcat ("save -ascii ", filename, " map img")); |
1499
|
113 return; |
|
114 endif |
1024
|
115 |
2303
|
116 ## Convert to another format if requested. |
559
|
117 |
1887
|
118 grey = all (map(:,1) == map(:,2) && map(:,1) == map (:,3)); |
1507
|
119 |
|
120 pbm = pgm = ppm = 0; |
|
121 |
|
122 map_sz = map_nr * map_nc; |
|
123 |
|
124 map = reshape (map, map_sz, 1); |
|
125 |
|
126 idx = find (map > 1); |
|
127 map (idx) = ones (size (idx)); |
|
128 |
|
129 idx = find (map < 0); |
|
130 map (idx) = zeros (size (idx)); |
|
131 |
|
132 map = round (255 * map); |
|
133 |
|
134 bw = (map_nr == 2 |
|
135 && ((map(1,1) == 0 && map(2,1) == 255) |
|
136 || (map(1,1) == 255 && map(2,1) == 0))); |
|
137 |
1539
|
138 img = round (img'); |
1507
|
139 [img_nr, img_nc] = size (img); |
|
140 |
|
141 img_sz = img_nr * img_nc; |
|
142 img = reshape (img, img_sz, 1); |
|
143 |
2676
|
144 idx = find (img > map_nr); |
1507
|
145 img (idx) = ones (size (idx)) * map_nr; |
|
146 |
|
147 idx = find (img <= 0); |
|
148 img (idx) = ones (size (idx)); |
|
149 |
|
150 if (strcmp (img_form, "ppm")) |
|
151 if (grey && map_nr == 2 && bw) |
|
152 |
|
153 map = map(:,1); |
|
154 |
|
155 if (map(1) != 0) |
|
156 map(1) = 1; |
|
157 else |
|
158 map(2) = 1; |
|
159 endif |
|
160 |
|
161 img = map(img); |
|
162 n_long = rem (img_sz, 8); |
|
163 if (n_long == 0) |
|
164 n_long = 8; |
|
165 else |
2660
|
166 n_long++; |
1507
|
167 endif |
|
168 |
|
169 idx = 1:8:img_sz; |
|
170 s_len = length (idx) - 1; |
|
171 |
|
172 tmp = img (1:8:img_sz) * 128; |
|
173 for i = 2:n_long |
|
174 tmp = tmp + img (i:8:img_sz) * 2^(8-i); |
|
175 endfor |
|
176 for i = (n_long+1):8 |
|
177 tmp(1:s_len) = tmp(1:s_len) + img (i:8:img_sz) * 2^(8-i); |
|
178 endfor |
|
179 |
|
180 fid = fopen (filename, "w"); |
|
181 fprintf (fid, "P4\n%d %d\n", img_nr, img_nc); |
|
182 fwrite (fid, tmp, "char"); |
|
183 fprintf (fid, "\n"); |
|
184 fclose (fid); |
|
185 |
|
186 elseif (grey) |
|
187 |
|
188 fid = fopen (filename, "w"); |
|
189 fprintf (fid, "P5\n%d %d\n255\n", img_nr, img_nc); |
|
190 fwrite (fid, map(img), "uchar"); |
|
191 fprintf (fid, "\n"); |
|
192 fclose (fid); |
|
193 |
|
194 else |
|
195 |
1886
|
196 img_idx = ((1:3:3*img_sz)+2)'; |
|
197 map_idx = ((2*map_nr+1):map_sz)'; |
1507
|
198 |
|
199 tmap = map(map_idx); |
|
200 tmp(img_idx--) = tmap(img); |
|
201 |
|
202 map_idx = map_idx - map_nr; |
1887
|
203 tmap = map(map_idx); |
1507
|
204 tmp(img_idx--) = tmap(img); |
|
205 |
|
206 map_idx = map_idx - map_nr; |
|
207 tmap = map(map_idx); |
|
208 tmp(img_idx--) = tmap(img); |
|
209 |
|
210 fid = fopen (filename, "w"); |
|
211 fprintf (fid, "P6\n%d %d\n255\n", img_nr, img_nc); |
|
212 fwrite (fid, tmp, "uchar"); |
|
213 fprintf (fid, "\n"); |
|
214 fclose (fid); |
|
215 |
1499
|
216 endif |
|
217 |
1507
|
218 elseif (strcmp (img_form, "ps") == 1) |
|
219 |
|
220 if (! grey) |
1518
|
221 error ("must have a greyscale color map for conversion to PostScript"); |
1507
|
222 endif |
|
223 |
|
224 bps = 8; |
|
225 dpi = 300; |
|
226 pagewid = 612; |
|
227 pagehgt = 762; |
|
228 MARGIN = 0.95; |
|
229 devpix = dpi / 72.0 + 0.5; |
|
230 pixfac = 72.0 / dpi * devpix; |
|
231 |
2303
|
232 ## Compute padding to round cols * bps up to the nearest multiple of 8 |
|
233 ## (nr and nc are switched because we transposed the image above). |
1507
|
234 |
|
235 padright = (((img_nr * bps + 7) / 8) * 8 - img_nr * bps) / bps; |
|
236 |
|
237 scols = img_nr * pixfac; |
|
238 srows = img_nc * pixfac; |
|
239 |
|
240 if (scols > pagewid * MARGIN || srows > pagehgt * MARGIN) |
|
241 if (scols > pagewid * MARGIN) |
|
242 scale = scale * (pagewid / scols * MARGIN); |
|
243 scols = scale * img_nr * pixfac; |
|
244 srows = scale * img_nc * pixfac; |
|
245 endif |
|
246 if (srows > pagehgt * MARGIN) |
|
247 scale = scale * (pagehgt / srows * MARGIN); |
|
248 scols = scale * img_nr * pixfac; |
|
249 srows = scale * img_nc * pixfac; |
|
250 endif |
|
251 warning ("image too large for page, rescaling to %g", scale); |
|
252 endif |
|
253 |
|
254 llx = (pagewid - scols) / 2; |
|
255 lly = (pagehgt - srows) / 2; |
|
256 urx = llx + fix (scols + 0.5); |
|
257 ury = lly + fix (srows + 0.5); |
|
258 |
|
259 fid = fopen (filename, "w"); |
1499
|
260 |
1507
|
261 fprintf (fid, "%%!PS-Adobe-2.0 EPSF-2.0\n"); |
2485
|
262 fprintf (fid, "%%%%Creator: Octave %s (saveimage.m)\n", OCTAVE_VERSION); |
1507
|
263 fprintf (fid, "%%%%Title: %s\n", filename); |
|
264 fprintf (fid, "%%%%Pages: 1\n"); |
|
265 fprintf (fid, "%%%%BoundingBox: %d %d %d %d\n", |
|
266 fix (llx), fix (lly), fix (urx), fix (ury)); |
|
267 fprintf (fid, "%%%%EndComments\n" ); |
|
268 fprintf (fid, "/readstring {\n"); |
|
269 fprintf (fid, " currentfile exch readhexstring pop\n"); |
|
270 fprintf (fid, "} bind def\n"); |
|
271 fprintf (fid, "/picstr %d string def\n", |
|
272 fix ((img_nr + padright) * bps / 8)); |
|
273 fprintf (fid, "%%%%EndProlog\n"); |
|
274 fprintf (fid, "%%%%Page: 1 1\n"); |
|
275 fprintf (fid, "gsave\n"); |
|
276 fprintf (fid, "%g %g translate\n", llx, lly); |
|
277 fprintf (fid, "%g %g scale\n", scols, srows); |
|
278 fprintf (fid, "%d %d %d\n", img_nr, img_nc, bps); |
|
279 fprintf (fid, "[ %d 0 0 -%d 0 %d ]\n", img_nr, img_nc, img_nc); |
|
280 fprintf (fid, "{ picstr readstring }\n" ); |
|
281 fprintf (fid, "image\n" ); |
1499
|
282 |
1507
|
283 img = map(img); |
|
284 |
2485
|
285 fmt = "%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x\n"; |
|
286 fprintf (fid, fmt, img); |
1507
|
287 |
2485
|
288 if (rem (img_sz, 30) != 0) |
|
289 fprintf (fid, "\n" ); |
|
290 endif |
1507
|
291 |
|
292 fprintf (fid, "grestore\n" ); |
|
293 fprintf (fid, "showpage\n" ); |
|
294 fprintf (fid, "%%%%Trailer\n" ); |
|
295 fclose (fid); |
|
296 |
|
297 else |
|
298 error ("saveimage: what happened to the image type?"); |
|
299 endif |
559
|
300 |
|
301 endfunction |