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