Mercurial > hg > octave-lyh
annotate scripts/deprecated/saveimage.m @ 12210:cd6f7f4f53a3
Deprecate saveimage.m.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 29 Jan 2011 19:08:45 -0800 |
parents | scripts/image/saveimage.m@5ec6aa05638d |
children | abb33ad310e6 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1994-2011 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
1024 | 18 |
3379 | 19 ## -*- texinfo -*- |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
20 ## @deftypefn {Function File} {} saveimage (@var{fname}, @var{img}, @var{fmt}) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
21 ## @deftypefnx {Function File} {} saveimage (@var{fname}, @var{img}, @var{fmt}, @var{map}) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
22 ## Save the matrix @var{img} to file @var{fname} in image format @var{fmt}. |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
23 ## Valid values for @var{fmt} are |
3426 | 24 ## |
11595
5ec6aa05638d
Prevent doubled quotes around @table items in Info.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
25 ## @table @asis |
3373 | 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" |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
7061
diff
changeset
|
33 ## PostScript format. Note that images saved in PostScript format cannot |
3373 | 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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
77 function saveimage (fname, img, fmt, map) |
1507 | 78 |
12210 | 79 persistent warned = false; |
80 if (! warned) | |
81 warned = true; | |
82 warning ("Octave:deprecated-function", | |
83 "saveimage is obsolete and will be removed from a future version of Octave; please use imwrite instead"); | |
84 endif | |
85 | |
1024 | 86 if (nargin < 2 || nargin > 4) |
6046 | 87 print_usage (); |
1499 | 88 endif |
89 | |
90 if (nargin < 4) | |
6967 | 91 if (size(img, 3) == 3) |
92 [img, map] = rgb2ind(img); | |
93 else | |
94 map = colormap (); | |
95 endif | |
1499 | 96 endif |
1507 | 97 |
98 [map_nr, map_nc] = size (map); | |
99 | |
100 if (map_nc != 3) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
101 error ("saveimage: MAP must be an N x 3 matrix"); |
1499 | 102 endif |
103 | |
104 if (nargin < 3) | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
105 fmt = "img"; |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
106 elseif (! ischar (fmt)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
107 error ("saveimage: FMT specification must be a string"); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
108 elseif (! (strcmp (fmt, "img") |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
109 || strcmp (fmt, "ppm") |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
110 || strcmp (fmt, "ps"))) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10433
diff
changeset
|
111 error ("saveimage: unsupported image format specification"); |
559 | 112 endif |
113 | |
4030 | 114 if (! ismatrix (img)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
115 warning ("IMG variable is not a matrix"); |
1499 | 116 endif |
1024 | 117 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
118 if (! ischar (fname)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
119 error ("saveimage: FNAME must be a string"); |
559 | 120 endif |
121 | |
2303 | 122 ## If we just want Octave image format, save and return. |
1499 | 123 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
124 if (strcmp (fmt, "img")) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
125 save ("-text", fname, "map", "img"); |
1499 | 126 return; |
127 endif | |
1024 | 128 |
2303 | 129 ## Convert to another format if requested. |
559 | 130 |
1887 | 131 grey = all (map(:,1) == map(:,2) && map(:,1) == map (:,3)); |
1507 | 132 |
133 pbm = pgm = ppm = 0; | |
134 | |
135 map_sz = map_nr * map_nc; | |
136 | |
137 map = reshape (map, map_sz, 1); | |
138 | |
5962 | 139 map (map > 1) = 1; |
140 map (map < 0) = 0; | |
1507 | 141 |
142 map = round (255 * map); | |
143 | |
3757 | 144 bw = (map_nr == 2 |
145 && ((map(1,1) == 0 && map(2,1) == 255) | |
146 || (map(1,1) == 255 && map(2,1) == 0))); | |
147 | |
1539 | 148 img = round (img'); |
1507 | 149 [img_nr, img_nc] = size (img); |
150 | |
151 img_sz = img_nr * img_nc; | |
152 img = reshape (img, img_sz, 1); | |
153 | |
5962 | 154 img (img > map_nr) = map_nr; |
155 img (img <= 0) = 1; | |
1507 | 156 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
157 if (strcmp (fmt, "ppm")) |
3618 | 158 |
159 ## Would be nice to make this consistent with the line used by the | |
160 ## load/save functions, but we need a good way to get username and | |
161 ## hostname information. | |
162 | |
163 time_string = ctime (time ()); | |
164 time_string = time_string (1:length (time_string)-1); | |
165 tagline = sprintf ("# Created by Octave %s, %s", | |
10433
2c01d24459fb
Detabify scripts in 'scripts/image/'
Soren Hauberg <hauberg@gmail.com>
parents:
8920
diff
changeset
|
166 OCTAVE_VERSION, time_string); |
3618 | 167 |
3757 | 168 if (grey && bw) |
169 | |
170 if (map(1) != 0) | |
171 map = [0; 1]; | |
172 else | |
173 map = [1; 0]; | |
174 endif | |
175 | |
176 n_long = rem (img_nc, 8); | |
177 tmp = zeros (ceil (img_nc/8), img_nr); | |
178 | |
179 k = ceil (img_nr/8); | |
180 tmp = zeros (k, img_nc); | |
181 | |
182 ## Append columns with zeros to original image so that | |
183 ## mod (cols, 8) = 0. | |
184 | |
185 bwimg = postpad (reshape (map(img), img_nr, img_nc), k * 8, 0); | |
186 | |
187 b = kron (pow2 (7:-1:0)', ones (1, img_nc)); | |
188 | |
189 for i = 1:k | |
190 tmp(i,:) = sum (bwimg(8*(i-1)+1:8*i,:) .* b); | |
191 endfor | |
192 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
193 fid = fopen (fname, "wb"); |
3757 | 194 fprintf (fid, "P4\n%s\n%d %d\n", tagline, img_nr, img_nc); |
5446 | 195 fwrite (fid, tmp, "uchar"); |
3757 | 196 fprintf (fid, "\n"); |
197 fclose (fid); | |
198 | |
199 elseif (grey) | |
1507 | 200 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
201 fid = fopen (fname, "wb"); |
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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
223 fid = fopen (fname, "wb"); |
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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
231 elseif (strcmp (fmt, "ps") == 1) |
1507 | 232 |
233 if (! grey) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11469
diff
changeset
|
234 error ("saveimage: must have a greyscale colormap 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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
273 fid = fopen (fname, "wb"); |
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); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
277 fprintf (fid, "%%%%Title: %s\n", fname); |
1507 | 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 | |
3836 | 299 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 | 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 |