2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
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 |
3202
|
55 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312
|
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 if (map(1) != 0) |
3225
|
154 map = [1; 0]; |
1507
|
155 else |
3225
|
156 map = [0; 1]; |
1507
|
157 endif |
|
158 |
3225
|
159 n_long = rem (img_nc, 8); |
|
160 tmp = zeros (ceil (img_nc/8), img_nr); |
1507
|
161 |
3225
|
162 for i = 1:img_nr |
|
163 idx = (i-1)*img_nc+1:i*img_nc; |
|
164 if (n_long > 0) |
|
165 img_row = [map(img(idx)); zeros (8-n_long, 1)]; |
|
166 else |
|
167 img_row = map(img(idx)); |
|
168 endif |
|
169 img_row |
|
170 tmp |
|
171 if (img_nc < 8) |
|
172 for j = 1:8 |
|
173 tmp(:,i) = tmp(:,i) + img_row (j) * 2^(8-j); |
|
174 endfor |
|
175 else |
|
176 for j = 1:8 |
|
177 tmp(:,i) = tmp(:,i) + img_row (j:8:img_nc) * 2^(8-j); |
|
178 endfor |
|
179 endif |
1507
|
180 endfor |
|
181 |
|
182 fid = fopen (filename, "w"); |
|
183 fprintf (fid, "P4\n%d %d\n", img_nr, img_nc); |
|
184 fwrite (fid, tmp, "char"); |
|
185 fprintf (fid, "\n"); |
|
186 fclose (fid); |
|
187 |
|
188 elseif (grey) |
|
189 |
|
190 fid = fopen (filename, "w"); |
|
191 fprintf (fid, "P5\n%d %d\n255\n", img_nr, img_nc); |
|
192 fwrite (fid, map(img), "uchar"); |
|
193 fprintf (fid, "\n"); |
|
194 fclose (fid); |
|
195 |
|
196 else |
|
197 |
1886
|
198 img_idx = ((1:3:3*img_sz)+2)'; |
|
199 map_idx = ((2*map_nr+1):map_sz)'; |
1507
|
200 |
|
201 tmap = map(map_idx); |
|
202 tmp(img_idx--) = tmap(img); |
|
203 |
|
204 map_idx = map_idx - map_nr; |
1887
|
205 tmap = map(map_idx); |
1507
|
206 tmp(img_idx--) = tmap(img); |
|
207 |
|
208 map_idx = map_idx - map_nr; |
|
209 tmap = map(map_idx); |
|
210 tmp(img_idx--) = tmap(img); |
|
211 |
|
212 fid = fopen (filename, "w"); |
|
213 fprintf (fid, "P6\n%d %d\n255\n", img_nr, img_nc); |
|
214 fwrite (fid, tmp, "uchar"); |
|
215 fprintf (fid, "\n"); |
|
216 fclose (fid); |
|
217 |
1499
|
218 endif |
|
219 |
1507
|
220 elseif (strcmp (img_form, "ps") == 1) |
|
221 |
|
222 if (! grey) |
1518
|
223 error ("must have a greyscale color map for conversion to PostScript"); |
1507
|
224 endif |
|
225 |
|
226 bps = 8; |
|
227 dpi = 300; |
|
228 pagewid = 612; |
|
229 pagehgt = 762; |
|
230 MARGIN = 0.95; |
|
231 devpix = dpi / 72.0 + 0.5; |
|
232 pixfac = 72.0 / dpi * devpix; |
|
233 |
2303
|
234 ## Compute padding to round cols * bps up to the nearest multiple of 8 |
|
235 ## (nr and nc are switched because we transposed the image above). |
1507
|
236 |
|
237 padright = (((img_nr * bps + 7) / 8) * 8 - img_nr * bps) / bps; |
|
238 |
|
239 scols = img_nr * pixfac; |
|
240 srows = img_nc * pixfac; |
3268
|
241 scale = 1; |
1507
|
242 |
|
243 if (scols > pagewid * MARGIN || srows > pagehgt * MARGIN) |
|
244 if (scols > pagewid * MARGIN) |
|
245 scale = scale * (pagewid / scols * MARGIN); |
|
246 scols = scale * img_nr * pixfac; |
|
247 srows = scale * img_nc * pixfac; |
|
248 endif |
|
249 if (srows > pagehgt * MARGIN) |
|
250 scale = scale * (pagehgt / srows * MARGIN); |
|
251 scols = scale * img_nr * pixfac; |
|
252 srows = scale * img_nc * pixfac; |
|
253 endif |
|
254 warning ("image too large for page, rescaling to %g", scale); |
|
255 endif |
|
256 |
|
257 llx = (pagewid - scols) / 2; |
|
258 lly = (pagehgt - srows) / 2; |
|
259 urx = llx + fix (scols + 0.5); |
|
260 ury = lly + fix (srows + 0.5); |
|
261 |
|
262 fid = fopen (filename, "w"); |
1499
|
263 |
1507
|
264 fprintf (fid, "%%!PS-Adobe-2.0 EPSF-2.0\n"); |
2485
|
265 fprintf (fid, "%%%%Creator: Octave %s (saveimage.m)\n", OCTAVE_VERSION); |
1507
|
266 fprintf (fid, "%%%%Title: %s\n", filename); |
|
267 fprintf (fid, "%%%%Pages: 1\n"); |
|
268 fprintf (fid, "%%%%BoundingBox: %d %d %d %d\n", |
|
269 fix (llx), fix (lly), fix (urx), fix (ury)); |
|
270 fprintf (fid, "%%%%EndComments\n" ); |
|
271 fprintf (fid, "/readstring {\n"); |
|
272 fprintf (fid, " currentfile exch readhexstring pop\n"); |
|
273 fprintf (fid, "} bind def\n"); |
|
274 fprintf (fid, "/picstr %d string def\n", |
|
275 fix ((img_nr + padright) * bps / 8)); |
|
276 fprintf (fid, "%%%%EndProlog\n"); |
|
277 fprintf (fid, "%%%%Page: 1 1\n"); |
|
278 fprintf (fid, "gsave\n"); |
|
279 fprintf (fid, "%g %g translate\n", llx, lly); |
|
280 fprintf (fid, "%g %g scale\n", scols, srows); |
|
281 fprintf (fid, "%d %d %d\n", img_nr, img_nc, bps); |
|
282 fprintf (fid, "[ %d 0 0 -%d 0 %d ]\n", img_nr, img_nc, img_nc); |
|
283 fprintf (fid, "{ picstr readstring }\n" ); |
|
284 fprintf (fid, "image\n" ); |
1499
|
285 |
1507
|
286 img = map(img); |
|
287 |
2485
|
288 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"; |
|
289 fprintf (fid, fmt, img); |
1507
|
290 |
2485
|
291 if (rem (img_sz, 30) != 0) |
|
292 fprintf (fid, "\n" ); |
|
293 endif |
1507
|
294 |
|
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 |