Mercurial > hg > octave-lyh
annotate scripts/image/saveimage.m @ 11595:5ec6aa05638d
Prevent doubled quotes around @table items in Info.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Thu, 20 Jan 2011 21:29:14 -0800 |
parents | fd0a3ac60b0e |
children |
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 |
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) | |
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
|
94 error ("saveimage: MAP must be an N x 3 matrix"); |
1499 | 95 endif |
96 | |
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 || 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
|
103 || strcmp (fmt, "ps"))) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10433
diff
changeset
|
104 error ("saveimage: unsupported image format specification"); |
559 | 105 endif |
106 | |
4030 | 107 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
|
108 warning ("IMG variable is not a matrix"); |
1499 | 109 endif |
1024 | 110 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
111 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
|
112 error ("saveimage: FNAME must be a string"); |
559 | 113 endif |
114 | |
2303 | 115 ## If we just want Octave image format, save and return. |
1499 | 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 (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
|
118 save ("-text", fname, "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 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
150 if (strcmp (fmt, "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", | |
10433
2c01d24459fb
Detabify scripts in 'scripts/image/'
Soren Hauberg <hauberg@gmail.com>
parents:
8920
diff
changeset
|
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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
186 fid = fopen (fname, "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 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
194 fid = fopen (fname, "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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
216 fid = fopen (fname, "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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
224 elseif (strcmp (fmt, "ps") == 1) |
1507 | 225 |
226 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
|
227 error ("saveimage: must have a greyscale colormap 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 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
266 fid = fopen (fname, "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); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
270 fprintf (fid, "%%%%Title: %s\n", fname); |
1507 | 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 |