7017
|
1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 |
|
2 ## 2004, 2005, 2006, 2007 John W. Eaton |
2313
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
2313
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
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. |
5642
|
45 ## @seealso{loadimage, save, load, colormap} |
3373
|
46 ## @end deftypefn |
|
47 |
2311
|
48 ## The conversion to PostScript is based on pbmtolps.c, which was |
2325
|
49 ## written by |
2311
|
50 ## |
|
51 ## George Phillips <phillips@cs.ubc.ca> |
|
52 ## Department of Computer Science |
|
53 ## University of British Columbia |
|
54 ## |
|
55 ## and is part of the portable bitmap utilities, |
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 |
6653
|
62 ## people who don't have 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) |
6046
|
80 print_usage (); |
1499
|
81 endif |
|
82 |
|
83 if (nargin < 4) |
6967
|
84 if (size(img, 3) == 3) |
|
85 [img, map] = rgb2ind(img); |
|
86 else |
|
87 map = colormap (); |
|
88 endif |
1499
|
89 endif |
1507
|
90 |
|
91 [map_nr, map_nc] = size (map); |
|
92 |
|
93 if (map_nc != 3) |
1499
|
94 error ("colormap should be an N x 3 matrix"); |
|
95 endif |
|
96 |
|
97 if (nargin < 3) |
559
|
98 img_form = "img"; |
5443
|
99 elseif (! ischar (img_form)) |
1499
|
100 error ("image format specification must be a string"); |
|
101 elseif (! (strcmp (img_form, "img") |
|
102 || strcmp (img_form, "ppm") |
3426
|
103 || strcmp (img_form, "ps"))) |
1507
|
104 error ("unsupported image format specification"); |
559
|
105 endif |
|
106 |
4030
|
107 if (! ismatrix (img)) |
1499
|
108 warning ("image variable is not a matrix"); |
|
109 endif |
1024
|
110 |
5443
|
111 if (! ischar (filename)) |
1499
|
112 error ("file name must be a string"); |
559
|
113 endif |
|
114 |
2303
|
115 ## If we just want Octave image format, save and return. |
1499
|
116 |
|
117 if (strcmp (img_form, "img")) |
7061
|
118 save ("-text", filename, "map", "img"); |
1499
|
119 return; |
|
120 endif |
1024
|
121 |
2303
|
122 ## Convert to another format if requested. |
559
|
123 |
1887
|
124 grey = all (map(:,1) == map(:,2) && map(:,1) == map (:,3)); |
1507
|
125 |
|
126 pbm = pgm = ppm = 0; |
|
127 |
|
128 map_sz = map_nr * map_nc; |
|
129 |
|
130 map = reshape (map, map_sz, 1); |
|
131 |
5962
|
132 map (map > 1) = 1; |
|
133 map (map < 0) = 0; |
1507
|
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 |
5962
|
147 img (img > map_nr) = map_nr; |
|
148 img (img <= 0) = 1; |
1507
|
149 |
|
150 if (strcmp (img_form, "ppm")) |
3618
|
151 |
|
152 ## Would be nice to make this consistent with the line used by the |
|
153 ## load/save functions, but we need a good way to get username and |
|
154 ## hostname information. |
|
155 |
|
156 time_string = ctime (time ()); |
|
157 time_string = time_string (1:length (time_string)-1); |
|
158 tagline = sprintf ("# Created by Octave %s, %s", |
4011
|
159 OCTAVE_VERSION, time_string); |
3618
|
160 |
3757
|
161 if (grey && bw) |
|
162 |
|
163 if (map(1) != 0) |
|
164 map = [0; 1]; |
|
165 else |
|
166 map = [1; 0]; |
|
167 endif |
|
168 |
|
169 n_long = rem (img_nc, 8); |
|
170 tmp = zeros (ceil (img_nc/8), img_nr); |
|
171 |
|
172 k = ceil (img_nr/8); |
|
173 tmp = zeros (k, img_nc); |
|
174 |
|
175 ## Append columns with zeros to original image so that |
|
176 ## mod (cols, 8) = 0. |
|
177 |
|
178 bwimg = postpad (reshape (map(img), img_nr, img_nc), k * 8, 0); |
|
179 |
|
180 b = kron (pow2 (7:-1:0)', ones (1, img_nc)); |
|
181 |
|
182 for i = 1:k |
|
183 tmp(i,:) = sum (bwimg(8*(i-1)+1:8*i,:) .* b); |
|
184 endfor |
|
185 |
5431
|
186 fid = fopen (filename, "wb"); |
3757
|
187 fprintf (fid, "P4\n%s\n%d %d\n", tagline, img_nr, img_nc); |
5446
|
188 fwrite (fid, tmp, "uchar"); |
3757
|
189 fprintf (fid, "\n"); |
|
190 fclose (fid); |
|
191 |
|
192 elseif (grey) |
1507
|
193 |
5431
|
194 fid = fopen (filename, "wb"); |
3618
|
195 fprintf (fid, "P5\n%s\n%d %d\n255\n", tagline, img_nr, img_nc); |
1507
|
196 fwrite (fid, map(img), "uchar"); |
|
197 fprintf (fid, "\n"); |
|
198 fclose (fid); |
|
199 |
|
200 else |
|
201 |
1886
|
202 img_idx = ((1:3:3*img_sz)+2)'; |
|
203 map_idx = ((2*map_nr+1):map_sz)'; |
1507
|
204 |
|
205 tmap = map(map_idx); |
|
206 tmp(img_idx--) = tmap(img); |
|
207 |
|
208 map_idx = map_idx - map_nr; |
1887
|
209 tmap = map(map_idx); |
1507
|
210 tmp(img_idx--) = tmap(img); |
|
211 |
|
212 map_idx = map_idx - map_nr; |
|
213 tmap = map(map_idx); |
|
214 tmp(img_idx--) = tmap(img); |
|
215 |
5431
|
216 fid = fopen (filename, "wb"); |
3618
|
217 fprintf (fid, "P6\n%s\n%d %d\n255\n", tagline, img_nr, img_nc); |
1507
|
218 fwrite (fid, tmp, "uchar"); |
|
219 fprintf (fid, "\n"); |
|
220 fclose (fid); |
|
221 |
1499
|
222 endif |
|
223 |
1507
|
224 elseif (strcmp (img_form, "ps") == 1) |
|
225 |
|
226 if (! grey) |
1518
|
227 error ("must have a greyscale color map for conversion to PostScript"); |
1507
|
228 endif |
|
229 |
|
230 bps = 8; |
|
231 dpi = 300; |
|
232 pagewid = 612; |
|
233 pagehgt = 762; |
|
234 MARGIN = 0.95; |
|
235 devpix = dpi / 72.0 + 0.5; |
|
236 pixfac = 72.0 / dpi * devpix; |
|
237 |
2303
|
238 ## Compute padding to round cols * bps up to the nearest multiple of 8 |
|
239 ## (nr and nc are switched because we transposed the image above). |
1507
|
240 |
|
241 padright = (((img_nr * bps + 7) / 8) * 8 - img_nr * bps) / bps; |
|
242 |
|
243 scols = img_nr * pixfac; |
|
244 srows = img_nc * pixfac; |
3268
|
245 scale = 1; |
1507
|
246 |
|
247 if (scols > pagewid * MARGIN || srows > pagehgt * MARGIN) |
|
248 if (scols > pagewid * MARGIN) |
3426
|
249 scale = scale * (pagewid / scols * MARGIN); |
|
250 scols = scale * img_nr * pixfac; |
|
251 srows = scale * img_nc * pixfac; |
1507
|
252 endif |
|
253 if (srows > pagehgt * MARGIN) |
3426
|
254 scale = scale * (pagehgt / srows * MARGIN); |
|
255 scols = scale * img_nr * pixfac; |
|
256 srows = scale * img_nc * pixfac; |
1507
|
257 endif |
|
258 warning ("image too large for page, rescaling to %g", scale); |
|
259 endif |
|
260 |
|
261 llx = (pagewid - scols) / 2; |
|
262 lly = (pagehgt - srows) / 2; |
|
263 urx = llx + fix (scols + 0.5); |
|
264 ury = lly + fix (srows + 0.5); |
|
265 |
5431
|
266 fid = fopen (filename, "wb"); |
1499
|
267 |
1507
|
268 fprintf (fid, "%%!PS-Adobe-2.0 EPSF-2.0\n"); |
2485
|
269 fprintf (fid, "%%%%Creator: Octave %s (saveimage.m)\n", OCTAVE_VERSION); |
1507
|
270 fprintf (fid, "%%%%Title: %s\n", filename); |
|
271 fprintf (fid, "%%%%Pages: 1\n"); |
|
272 fprintf (fid, "%%%%BoundingBox: %d %d %d %d\n", |
|
273 fix (llx), fix (lly), fix (urx), fix (ury)); |
3457
|
274 fprintf (fid, "%%%%EndComments\n"); |
1507
|
275 fprintf (fid, "/readstring {\n"); |
|
276 fprintf (fid, " currentfile exch readhexstring pop\n"); |
|
277 fprintf (fid, "} bind def\n"); |
|
278 fprintf (fid, "/picstr %d string def\n", |
|
279 fix ((img_nr + padright) * bps / 8)); |
|
280 fprintf (fid, "%%%%EndProlog\n"); |
|
281 fprintf (fid, "%%%%Page: 1 1\n"); |
|
282 fprintf (fid, "gsave\n"); |
|
283 fprintf (fid, "%g %g translate\n", llx, lly); |
|
284 fprintf (fid, "%g %g scale\n", scols, srows); |
|
285 fprintf (fid, "%d %d %d\n", img_nr, img_nc, bps); |
|
286 fprintf (fid, "[ %d 0 0 -%d 0 %d ]\n", img_nr, img_nc, img_nc); |
3457
|
287 fprintf (fid, "{ picstr readstring }\n"); |
|
288 fprintf (fid, "image\n"); |
1499
|
289 |
1507
|
290 img = map(img); |
|
291 |
3836
|
292 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
|
293 fprintf (fid, fmt, img); |
1507
|
294 |
2485
|
295 if (rem (img_sz, 30) != 0) |
3457
|
296 fprintf (fid, "\n"); |
2485
|
297 endif |
1507
|
298 |
3457
|
299 fprintf (fid, "grestore\n"); |
|
300 fprintf (fid, "showpage\n"); |
|
301 fprintf (fid, "%%%%Trailer\n"); |
1507
|
302 fclose (fid); |
|
303 |
|
304 else |
|
305 error ("saveimage: what happened to the image type?"); |
|
306 endif |
559
|
307 |
|
308 endfunction |