1 ## Copyright (C) 1994-2012 John W. Eaton
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} saveimage (@var{fname}, @var{img}, @var{fmt})
21 ## @deftypefnx {Function File} {} saveimage (@var{fname}, @var{img}, @var{fmt}, @var{map})
22 ## Save the matrix @var{img} to file @var{fname} in image format @var{fmt}.
23 ## Valid values for @var{fmt} are
27 ## Octave's image format. The current colormap is also saved in the file.
30 ## Portable pixmap format.
36 ## If the fourth argument is supplied, the specified colormap will also be
37 ## saved along with the image.
39 ## Note: if the colormap contains only two entries and these entries are
40 ## black and white, the bitmap ppm and PostScript formats are used. If the
41 ## image is a gray scale image (the entries within each row of the colormap
42 ## are equal) the gray scale ppm and PostScript image formats are used,
43 ## otherwise the full color formats are used.
44 ## @seealso{imread, save, load, colormap}
47 ## The conversion to PostScript is based on pbmtolps.c, which was
50 ## George Phillips <phillips@cs.ubc.ca>
51 ## Department of Computer Science
52 ## University of British Columbia
54 ## and is part of the portable bitmap utilities,
56 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
60 ## Rewritten by jwe to avoid using octoppm and pbm routines so that
61 ## people who don't have the pbm stuff installed can still use this
64 ## The conversion to PostScript is based on pnmtops.c, which is part of
65 ## the portable bitmap utilties and bears this copyright notice:
67 ## Copyright (C) 1989 by Jef Poskanzer.
69 ## Permission to use, copy, modify, and distribute this software and its
70 ## documentation for any purpose and without fee is hereby granted, provided
71 ## that the above copyright notice appear in all copies and that both that
72 ## copyright notice and this permission notice appear in supporting
73 ## documentation. This software is provided "as is" without express or
76 function saveimage (fname, img, fmt, map)
78 persistent warned = false;
81 warning ("Octave:deprecated-function",
82 "saveimage is obsolete and will be removed from a future version of Octave; please use imwrite instead");
85 if (nargin < 2 || nargin > 4)
90 if (size(img, 3) == 3)
91 [img, map] = rgb2ind(img);
97 [map_nr, map_nc] = size (map);
100 error ("saveimage: MAP must be an N x 3 matrix");
105 elseif (! ischar (fmt))
106 error ("saveimage: FMT specification must be a string");
107 elseif (! (strcmp (fmt, "img")
108 || strcmp (fmt, "ppm")
109 || strcmp (fmt, "ps")))
110 error ("saveimage: unsupported image format specification");
113 if (! ismatrix (img))
114 warning ("IMG variable is not a matrix");
117 if (! ischar (fname))
118 error ("saveimage: FNAME must be a string");
121 ## If we just want Octave image format, save and return.
123 if (strcmp (fmt, "img"))
124 save ("-text", fname, "map", "img");
128 ## Convert to another format if requested.
130 grey = all (map(:,1) == map(:,2) && map(:,1) == map (:,3));
134 map_sz = map_nr * map_nc;
136 map = reshape (map, map_sz, 1);
141 map = round (255 * map);
144 && ((map(1,1) == 0 && map(2,1) == 255)
145 || (map(1,1) == 255 && map(2,1) == 0)));
148 [img_nr, img_nc] = size (img);
150 img_sz = img_nr * img_nc;
151 img = reshape (img, img_sz, 1);
153 img (img > map_nr) = map_nr;
156 if (strcmp (fmt, "ppm"))
158 ## Would be nice to make this consistent with the line used by the
159 ## load/save functions, but we need a good way to get username and
160 ## hostname information.
162 time_string = ctime (time ());
163 time_string = time_string (1:length (time_string)-1);
164 tagline = sprintf ("# Created by Octave %s, %s",
165 OCTAVE_VERSION, time_string);
175 n_long = rem (img_nc, 8);
176 tmp = zeros (ceil (img_nc/8), img_nr);
179 tmp = zeros (k, img_nc);
181 ## Append columns with zeros to original image so that
182 ## mod (cols, 8) = 0.
184 bwimg = postpad (reshape (map(img), img_nr, img_nc), k * 8, 0);
186 b = kron (pow2 (7:-1:0)', ones (1, img_nc));
189 tmp(i,:) = sum (bwimg(8*(i-1)+1:8*i,:) .* b);
192 fid = fopen (fname, "wb");
193 fprintf (fid, "P4\n%s\n%d %d\n", tagline, img_nr, img_nc);
194 fwrite (fid, tmp, "uchar");
200 fid = fopen (fname, "wb");
201 fprintf (fid, "P5\n%s\n%d %d\n255\n", tagline, img_nr, img_nc);
202 fwrite (fid, map(img), "uchar");
208 img_idx = ((1:3:3*img_sz)+2)';
209 map_idx = ((2*map_nr+1):map_sz)';
212 tmp(img_idx--) = tmap(img);
214 map_idx = map_idx - map_nr;
216 tmp(img_idx--) = tmap(img);
218 map_idx = map_idx - map_nr;
220 tmp(img_idx--) = tmap(img);
222 fid = fopen (fname, "wb");
223 fprintf (fid, "P6\n%s\n%d %d\n255\n", tagline, img_nr, img_nc);
224 fwrite (fid, tmp, "uchar");
230 elseif (strcmp (fmt, "ps") == 1)
233 error ("saveimage: must have a greyscale colormap for conversion to PostScript");
241 devpix = dpi / 72.0 + 0.5;
242 pixfac = 72.0 / dpi * devpix;
244 ## Compute padding to round cols * bps up to the nearest multiple of 8
245 ## (nr and nc are switched because we transposed the image above).
247 padright = (((img_nr * bps + 7) / 8) * 8 - img_nr * bps) / bps;
249 scols = img_nr * pixfac;
250 srows = img_nc * pixfac;
253 if (scols > pagewid * MARGIN || srows > pagehgt * MARGIN)
254 if (scols > pagewid * MARGIN)
255 scale = scale * (pagewid / scols * MARGIN);
256 scols = scale * img_nr * pixfac;
257 srows = scale * img_nc * pixfac;
259 if (srows > pagehgt * MARGIN)
260 scale = scale * (pagehgt / srows * MARGIN);
261 scols = scale * img_nr * pixfac;
262 srows = scale * img_nc * pixfac;
264 warning ("image too large for page, rescaling to %g", scale);
267 llx = (pagewid - scols) / 2;
268 lly = (pagehgt - srows) / 2;
269 urx = llx + fix (scols + 0.5);
270 ury = lly + fix (srows + 0.5);
272 fid = fopen (fname, "wb");
274 fprintf (fid, "%%!PS-Adobe-2.0 EPSF-2.0\n");
275 fprintf (fid, "%%%%Creator: Octave %s (saveimage.m)\n", OCTAVE_VERSION);
276 fprintf (fid, "%%%%Title: %s\n", fname);
277 fprintf (fid, "%%%%Pages: 1\n");
278 fprintf (fid, "%%%%BoundingBox: %d %d %d %d\n",
279 fix (llx), fix (lly), fix (urx), fix (ury));
280 fprintf (fid, "%%%%EndComments\n");
281 fprintf (fid, "/readstring {\n");
282 fprintf (fid, " currentfile exch readhexstring pop\n");
283 fprintf (fid, "} bind def\n");
284 fprintf (fid, "/picstr %d string def\n",
285 fix ((img_nr + padright) * bps / 8));
286 fprintf (fid, "%%%%EndProlog\n");
287 fprintf (fid, "%%%%Page: 1 1\n");
288 fprintf (fid, "gsave\n");
289 fprintf (fid, "%g %g translate\n", llx, lly);
290 fprintf (fid, "%g %g scale\n", scols, srows);
291 fprintf (fid, "%d %d %d\n", img_nr, img_nc, bps);
292 fprintf (fid, "[ %d 0 0 -%d 0 %d ]\n", img_nr, img_nc, img_nc);
293 fprintf (fid, "{ picstr readstring }\n");
294 fprintf (fid, "image\n");
298 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";
299 fprintf (fid, fmt, img);
301 if (rem (img_sz, 30) != 0)
305 fprintf (fid, "grestore\n");
306 fprintf (fid, "showpage\n");
307 fprintf (fid, "%%%%Trailer\n");
311 error ("saveimage: what happened to the image type?");