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