]> Creatis software - CreaPhase.git/blob - octave_packages/m/deprecated/saveimage.m
update packages
[CreaPhase.git] / octave_packages / m / deprecated / saveimage.m
1 ## Copyright (C) 1994-2012 John W. Eaton
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 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
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
24 ##
25 ## @table @asis
26 ## @item "img"
27 ## Octave's image format.  The current colormap is also saved in the file.
28 ##
29 ## @item "ppm"
30 ## Portable pixmap format.
31 ##
32 ## @item "ps"
33 ## PostScript format.
34 ## @end table
35 ##
36 ## If the fourth argument is supplied, the specified colormap will also be
37 ## saved along with the image.
38 ##
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}
45 ## @end deftypefn
46
47 ## The conversion to PostScript is based on pbmtolps.c, which was
48 ## written by
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,
55
56 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
57 ## Created: July 1994
58 ## Adapted-By: jwe
59
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
62 ## function.
63 ##
64 ## The conversion to PostScript is based on pnmtops.c, which is part of
65 ## the portable bitmap utilties and bears this copyright notice:
66 ##
67 ## Copyright (C) 1989 by Jef Poskanzer.
68 ##
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
74 ## implied warranty.
75
76 function saveimage (fname, img, fmt, map)
77
78   persistent warned = false;
79   if (! warned)
80     warned = true;
81     warning ("Octave:deprecated-function",
82              "saveimage is obsolete and will be removed from a future version of Octave; please use imwrite instead");
83   endif
84
85   if (nargin < 2 || nargin > 4)
86     print_usage ();
87   endif
88
89   if (nargin < 4)
90     if (size(img, 3) == 3)
91       [img, map] = rgb2ind(img);
92     else
93       map = colormap ();
94     endif
95   endif
96
97   [map_nr, map_nc] = size (map);
98
99   if (map_nc != 3)
100     error ("saveimage: MAP must be an N x 3 matrix");
101   endif
102
103   if (nargin < 3)
104     fmt = "img";
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");
111   endif
112
113   if (! ismatrix (img))
114     warning ("IMG variable is not a matrix");
115   endif
116
117   if (! ischar (fname))
118     error ("saveimage: FNAME must be a string");
119   endif
120
121   ## If we just want Octave image format, save and return.
122
123   if (strcmp (fmt, "img"))
124     save ("-text", fname, "map", "img");
125     return;
126   endif
127
128   ## Convert to another format if requested.
129
130   grey = all (map(:,1) == map(:,2) && map(:,1) == map (:,3));
131
132   pbm = pgm = ppm = 0;
133
134   map_sz = map_nr * map_nc;
135
136   map = reshape (map, map_sz, 1);
137
138   map (map > 1) = 1;
139   map (map < 0) = 0;
140
141   map = round (255 * map);
142
143   bw = (map_nr == 2
144         && ((map(1,1) == 0 && map(2,1) == 255)
145             || (map(1,1) == 255 && map(2,1) == 0)));
146
147   img = round (img');
148   [img_nr, img_nc] = size (img);
149
150   img_sz = img_nr * img_nc;
151   img = reshape (img, img_sz, 1);
152
153   img (img > map_nr) = map_nr;
154   img (img <= 0) = 1;
155
156   if (strcmp (fmt, "ppm"))
157
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.
161
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);
166
167     if (grey && bw)
168
169       if (map(1) != 0)
170         map = [0; 1];
171       else
172         map = [1; 0];
173       endif
174
175       n_long = rem (img_nc, 8);
176       tmp = zeros (ceil (img_nc/8), img_nr);
177
178       k = ceil (img_nr/8);
179       tmp = zeros (k, img_nc);
180
181       ## Append columns with zeros to original image so that
182       ## mod (cols, 8) = 0.
183
184       bwimg = postpad (reshape (map(img), img_nr, img_nc), k * 8, 0);
185
186       b = kron (pow2 (7:-1:0)', ones (1, img_nc));
187
188       for i = 1:k
189         tmp(i,:) = sum (bwimg(8*(i-1)+1:8*i,:) .* b);
190       endfor
191
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");
195       fprintf (fid, "\n");
196       fclose (fid);
197
198     elseif (grey)
199
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");
203       fprintf (fid, "\n");
204       fclose (fid);
205
206     else
207
208       img_idx = ((1:3:3*img_sz)+2)';
209       map_idx = ((2*map_nr+1):map_sz)';
210
211       tmap = map(map_idx);
212       tmp(img_idx--) = tmap(img);
213
214       map_idx = map_idx - map_nr;
215       tmap = map(map_idx);
216       tmp(img_idx--) = tmap(img);
217
218       map_idx = map_idx - map_nr;
219       tmap = map(map_idx);
220       tmp(img_idx--) = tmap(img);
221
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");
225       fprintf (fid, "\n");
226       fclose (fid);
227
228     endif
229
230   elseif (strcmp (fmt, "ps") == 1)
231
232     if (! grey)
233       error ("saveimage: must have a greyscale colormap for conversion to PostScript");
234     endif
235
236     bps = 8;
237     dpi = 300;
238     pagewid = 612;
239     pagehgt = 762;
240     MARGIN = 0.95;
241     devpix = dpi / 72.0 + 0.5;
242     pixfac = 72.0 / dpi * devpix;
243
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).
246
247     padright = (((img_nr * bps + 7) / 8) * 8 - img_nr * bps) / bps;
248
249     scols = img_nr * pixfac;
250     srows = img_nc * pixfac;
251     scale = 1;
252
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;
258       endif
259       if (srows > pagehgt * MARGIN)
260         scale = scale * (pagehgt / srows * MARGIN);
261         scols = scale * img_nr * pixfac;
262         srows = scale * img_nc * pixfac;
263       endif
264       warning ("image too large for page, rescaling to %g", scale);
265     endif
266
267     llx = (pagewid - scols) / 2;
268     lly = (pagehgt - srows) / 2;
269     urx = llx + fix (scols + 0.5);
270     ury = lly + fix (srows + 0.5);
271
272     fid = fopen (fname, "wb");
273
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");
295
296     img = map(img);
297
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);
300
301     if (rem (img_sz, 30) != 0)
302       fprintf (fid, "\n");
303     endif
304
305     fprintf (fid, "grestore\n");
306     fprintf (fid, "showpage\n");
307     fprintf (fid, "%%%%Trailer\n");
308     fclose (fid);
309
310   else
311     error ("saveimage: what happened to the image type?");
312   endif
313
314 endfunction