Mercurial > hg > octave-lyh
annotate scripts/deprecated/saveimage.m @ 13977:08ae07e40d4f
Only run uimenu tests if FLTK toolkit is available (Bug #34908)
* graphics_toolkit.m: Correct @deftypefn to @deftypefnx for Texinfo to build
* allchild.m: Eliminate unnecessary for loop. Only run test if FLTK toolkit
is available.
* findall.m, uimenu.m: Only run test if FLTK toolkit is available.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Fri, 02 Dec 2011 14:48:45 -0800 |
parents | abb33ad310e6 |
children | 72c96de7a403 |
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" |
12909
abb33ad310e6
doc: Change references to removed loadimage->imread and deprecated saveimage->imwrite
Andreas Weber <andy.weber.aw@gmail.com>
parents:
12210
diff
changeset
|
33 ## PostScript format. |
3373 | 34 ## @end table |
3426 | 35 ## |
3373 | 36 ## If the fourth argument is supplied, the specified colormap will also be |
37 ## saved along with the image. | |
3426 | 38 ## |
3373 | 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. | |
12909
abb33ad310e6
doc: Change references to removed loadimage->imread and deprecated saveimage->imwrite
Andreas Weber <andy.weber.aw@gmail.com>
parents:
12210
diff
changeset
|
44 ## @seealso{imread, save, load, colormap} |
3373 | 45 ## @end deftypefn |
46 | |
2311 | 47 ## The conversion to PostScript is based on pbmtolps.c, which was |
2325 | 48 ## written by |
2311 | 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, | |
904 | 55 |
3202 | 56 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312 | 57 ## Created: July 1994 |
58 ## Adapted-By: jwe | |
1024 | 59 |
2312 | 60 ## Rewritten by jwe to avoid using octoppm and pbm routines so that |
6653 | 61 ## people who don't have the pbm stuff installed can still use this |
2325 | 62 ## function. |
2312 | 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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
76 function saveimage (fname, img, fmt, map) |
1507 | 77 |
12210 | 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 | |
1024 | 85 if (nargin < 2 || nargin > 4) |
6046 | 86 print_usage (); |
1499 | 87 endif |
88 | |
89 if (nargin < 4) | |
6967 | 90 if (size(img, 3) == 3) |
91 [img, map] = rgb2ind(img); | |
92 else | |
93 map = colormap (); | |
94 endif | |
1499 | 95 endif |
1507 | 96 |
97 [map_nr, map_nc] = size (map); | |
98 | |
99 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
|
100 error ("saveimage: MAP must be an N x 3 matrix"); |
1499 | 101 endif |
102 | |
103 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 || 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
|
109 || strcmp (fmt, "ps"))) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10433
diff
changeset
|
110 error ("saveimage: unsupported image format specification"); |
559 | 111 endif |
112 | |
4030 | 113 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
|
114 warning ("IMG variable is not a matrix"); |
1499 | 115 endif |
1024 | 116 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
117 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
|
118 error ("saveimage: FNAME must be a string"); |
559 | 119 endif |
120 | |
2303 | 121 ## If we just want Octave image format, save and return. |
1499 | 122 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
123 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
|
124 save ("-text", fname, "map", "img"); |
1499 | 125 return; |
126 endif | |
1024 | 127 |
2303 | 128 ## Convert to another format if requested. |
559 | 129 |
1887 | 130 grey = all (map(:,1) == map(:,2) && map(:,1) == map (:,3)); |
1507 | 131 |
132 pbm = pgm = ppm = 0; | |
133 | |
134 map_sz = map_nr * map_nc; | |
135 | |
136 map = reshape (map, map_sz, 1); | |
137 | |
5962 | 138 map (map > 1) = 1; |
139 map (map < 0) = 0; | |
1507 | 140 |
141 map = round (255 * map); | |
142 | |
3757 | 143 bw = (map_nr == 2 |
144 && ((map(1,1) == 0 && map(2,1) == 255) | |
145 || (map(1,1) == 255 && map(2,1) == 0))); | |
146 | |
1539 | 147 img = round (img'); |
1507 | 148 [img_nr, img_nc] = size (img); |
149 | |
150 img_sz = img_nr * img_nc; | |
151 img = reshape (img, img_sz, 1); | |
152 | |
5962 | 153 img (img > map_nr) = map_nr; |
154 img (img <= 0) = 1; | |
1507 | 155 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
156 if (strcmp (fmt, "ppm")) |
3618 | 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", | |
10433
2c01d24459fb
Detabify scripts in 'scripts/image/'
Soren Hauberg <hauberg@gmail.com>
parents:
8920
diff
changeset
|
165 OCTAVE_VERSION, time_string); |
3618 | 166 |
3757 | 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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
192 fid = fopen (fname, "wb"); |
3757 | 193 fprintf (fid, "P4\n%s\n%d %d\n", tagline, img_nr, img_nc); |
5446 | 194 fwrite (fid, tmp, "uchar"); |
3757 | 195 fprintf (fid, "\n"); |
196 fclose (fid); | |
197 | |
198 elseif (grey) | |
1507 | 199 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
200 fid = fopen (fname, "wb"); |
3618 | 201 fprintf (fid, "P5\n%s\n%d %d\n255\n", tagline, img_nr, img_nc); |
1507 | 202 fwrite (fid, map(img), "uchar"); |
203 fprintf (fid, "\n"); | |
204 fclose (fid); | |
205 | |
206 else | |
207 | |
1886 | 208 img_idx = ((1:3:3*img_sz)+2)'; |
209 map_idx = ((2*map_nr+1):map_sz)'; | |
1507 | 210 |
211 tmap = map(map_idx); | |
212 tmp(img_idx--) = tmap(img); | |
213 | |
214 map_idx = map_idx - map_nr; | |
1887 | 215 tmap = map(map_idx); |
1507 | 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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
222 fid = fopen (fname, "wb"); |
3618 | 223 fprintf (fid, "P6\n%s\n%d %d\n255\n", tagline, img_nr, img_nc); |
1507 | 224 fwrite (fid, tmp, "uchar"); |
225 fprintf (fid, "\n"); | |
226 fclose (fid); | |
227 | |
1499 | 228 endif |
229 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
230 elseif (strcmp (fmt, "ps") == 1) |
1507 | 231 |
232 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
|
233 error ("saveimage: must have a greyscale colormap for conversion to PostScript"); |
1507 | 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 | |
2303 | 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). | |
1507 | 246 |
247 padright = (((img_nr * bps + 7) / 8) * 8 - img_nr * bps) / bps; | |
248 | |
249 scols = img_nr * pixfac; | |
250 srows = img_nc * pixfac; | |
3268 | 251 scale = 1; |
1507 | 252 |
253 if (scols > pagewid * MARGIN || srows > pagehgt * MARGIN) | |
254 if (scols > pagewid * MARGIN) | |
3426 | 255 scale = scale * (pagewid / scols * MARGIN); |
256 scols = scale * img_nr * pixfac; | |
257 srows = scale * img_nc * pixfac; | |
1507 | 258 endif |
259 if (srows > pagehgt * MARGIN) | |
3426 | 260 scale = scale * (pagehgt / srows * MARGIN); |
261 scols = scale * img_nr * pixfac; | |
262 srows = scale * img_nc * pixfac; | |
1507 | 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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
272 fid = fopen (fname, "wb"); |
1499 | 273 |
1507 | 274 fprintf (fid, "%%!PS-Adobe-2.0 EPSF-2.0\n"); |
2485 | 275 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
|
276 fprintf (fid, "%%%%Title: %s\n", fname); |
1507 | 277 fprintf (fid, "%%%%Pages: 1\n"); |
278 fprintf (fid, "%%%%BoundingBox: %d %d %d %d\n", | |
279 fix (llx), fix (lly), fix (urx), fix (ury)); | |
3457 | 280 fprintf (fid, "%%%%EndComments\n"); |
1507 | 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); | |
3457 | 293 fprintf (fid, "{ picstr readstring }\n"); |
294 fprintf (fid, "image\n"); | |
1499 | 295 |
1507 | 296 img = map(img); |
297 | |
3836 | 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"; |
2485 | 299 fprintf (fid, fmt, img); |
1507 | 300 |
2485 | 301 if (rem (img_sz, 30) != 0) |
3457 | 302 fprintf (fid, "\n"); |
2485 | 303 endif |
1507 | 304 |
3457 | 305 fprintf (fid, "grestore\n"); |
306 fprintf (fid, "showpage\n"); | |
307 fprintf (fid, "%%%%Trailer\n"); | |
1507 | 308 fclose (fid); |
309 | |
310 else | |
311 error ("saveimage: what happened to the image type?"); | |
312 endif | |
559 | 313 |
314 endfunction |