Mercurial > hg > octave-nkf
annotate scripts/image/imwrite.m @ 14570:d07d96e53612 stable
seconds after the minute can be 0-60, not 0-61
* system.txi (Timing Utilities): Correct possible values for number of
seconds in time structures. From Rafael Arndt <rafaelarndt@gmail.com>.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 17 Apr 2012 14:42:49 -0400 |
parents | 72c96de7a403 |
children | f3d52523cde1 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11591
diff
changeset
|
1 ## Copyright (C) 2008-2012 John W. Eaton |
7987 | 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 -*- | |
10891
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
20 ## @deftypefn {Function File} {} imwrite (@var{img}, @var{filename}) |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
21 ## @deftypefnx {Function File} {} imwrite (@var{img}, @var{filename}, @var{fmt}) |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
22 ## @deftypefnx {Function File} {} imwrite (@var{img}, @var{filename}, @var{fmt}, @var{p1}, @var{v1}, @dots{}) |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
23 ## @deftypefnx {Function File} {} imwrite (@var{img}, @var{map}, @var{filename}, @dots{}) |
7987 | 24 ## Write images in various file formats. |
25 ## | |
10891
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
26 ## If @var{fmt} is not supplied, the file extension of @var{filename} is used |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
27 ## to determine the format. |
7987 | 28 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
29 ## The parameter-value pairs (@var{p1}, @var{v1}, @dots{}) are optional. |
10891
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
30 ## Currently the following options are supported for @t{JPEG} images: |
8148 | 31 ## |
32 ## @table @samp | |
33 ## @item Quality | |
10891
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
34 ## Set the quality of the compression. The value should be an |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
35 ## integer between 0 and 100, with larger values indicating higher visual |
10891
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
36 ## quality and lower compression. |
8148 | 37 ## @end table |
38 ## | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
39 ## @strong{Supported Formats} |
10985
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
40 ## @multitable @columnfractions .33 .66 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
41 ## @headitem Extension @tab Format |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
42 ## @item bmp @tab Windows Bitmap |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
43 ## @item gif @tab Graphics Interchange Format |
10985
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
44 ## @item jpg and jpeg @tab Joint Photographic Experts Group |
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
45 ## @item pbm @tab Portable Bitmap |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
46 ## @item pcx @tab |
10985
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
47 ## @item pgm @tab Portable Graymap |
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
48 ## @item png @tab Portable Network Graphics |
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
49 ## @item pnm @tab Portable Anymap |
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
50 ## @item ppm @tab Portable Pixmap |
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
51 ## @item ras @tab Sun Raster |
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
52 ## @item tif and tiff @tab Tagged Image File Format |
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
53 ## @item xwd @tab X11 Dump |
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
54 ## @end multitable |
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
55 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
56 ## @strong{Unsupported Formats} |
10985
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
57 ## @multitable @columnfractions .33 .66 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
58 ## @headitem Extension @tab Format |
10985
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
59 ## @item hdf @tab Hierarchical Data Format V4 |
11591
1d13679b587e
Use @nospell macro on certain words in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
60 ## @item @nospell{jp2} and jpx @tab Joint Photographic Experts Group 2000 |
10985
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
61 ## @end multitable |
165e7e79b82c
Updated for writing image files via imwrite.
John Swensen <jpswensen@gmail.com>
parents:
10891
diff
changeset
|
62 ## |
8148 | 63 ## @seealso{imread, imfinfo} |
7987 | 64 ## @end deftypefn |
65 | |
10891
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
66 function imwrite (img, varargin) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
67 |
11035
8a6b7947f618
Added support for checking supported image types at runtime.
John Swensen <jpswensen@gmail.com>
parents:
10985
diff
changeset
|
68 persistent imwrite_possible_formats = { |
11038
35416c9baf76
missing semicolon in last imwrite changeset
John W. Eaton <jwe@octave.org>
parents:
11035
diff
changeset
|
69 "bmp"; "gif"; "jp2"; "jpg"; "jpx"; "jpeg"; "hdf"; "pbm"; "pcx"; |
11035
8a6b7947f618
Added support for checking supported image types at runtime.
John Swensen <jpswensen@gmail.com>
parents:
10985
diff
changeset
|
70 "pgm"; "png"; "pnm"; "ppm"; "ras"; "tif"; "tiff"; "xwd" }; |
7987 | 71 |
11035
8a6b7947f618
Added support for checking supported image types at runtime.
John Swensen <jpswensen@gmail.com>
parents:
10985
diff
changeset
|
72 persistent accepted_formats = __magick_format_list__ (imwrite_possible_formats); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
73 |
10891
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
74 if (nargin < 2 || ! (isnumeric (img) || islogical (img))) |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
75 print_usage (); |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
76 endif |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
77 |
7987 | 78 map = []; |
79 fmt = ""; | |
80 | |
10891
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
81 offset = 1; |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
82 if (isnumeric (varargin{1})) |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
83 map = varargin{1}; |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
84 if (isempty (map)) |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
85 error ("imwrite: colormap must not be empty"); |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
86 endif |
7987 | 87 offset = 2; |
10891
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
88 endif |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
89 if (offset <= length (varargin) && ischar (varargin{offset})) |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
90 filename = varargin{offset}; |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
91 offset++; |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
92 if (rem (length (varargin) - offset, 2) == 0 && ischar (varargin{offset})) |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
93 fmt = varargin{offset}; |
7987 | 94 offset++; |
95 endif | |
96 else | |
97 print_usage (); | |
98 endif | |
10891
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
99 if (offset < length (varargin)) |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
100 has_param_list = 1; |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
101 for ii = offset:2:(length (varargin) - 1) |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
102 options.(varargin{ii}) = varargin{ii + 1}; |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
103 endfor |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
104 else |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
105 has_param_list = 0; |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
106 endif |
7987 | 107 |
108 filename = tilde_expand (filename); | |
109 | |
110 if (isempty (fmt)) | |
111 [d, n, fmt] = fileparts (filename); | |
112 if (! isempty (fmt)) | |
113 fmt = fmt(2:end); | |
114 endif | |
115 endif | |
116 | |
10891
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
117 if (isempty (img)) |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
118 error ("imwrite: invalid empty image"); |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
119 endif |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
120 |
7987 | 121 if (issparse (img) || issparse (map)) |
122 error ("imwrite: sparse images not supported"); | |
123 endif | |
124 | |
125 if (! strcmp (fmt, accepted_formats)) | |
126 error ("imwrite: %s: unsupported or invalid image format", fmt); | |
127 endif | |
128 | |
129 img_class = class (img); | |
130 map_class = class (map); | |
8054 | 131 nd = ndims (img); |
7987 | 132 |
133 if (isempty (map)) | |
134 if (any (strcmp (img_class, {"logical", "uint8", "uint16", "double"}))) | |
135 if ((nd == 2 || nd == 3) && strcmp (img_class, "double")) | |
8054 | 136 img = uint8 (img * 255); |
7987 | 137 endif |
8054 | 138 ## FIXME -- should we handle color images w/ alpha channel here? |
139 if (nd == 3 && size (img, 3) < 3) | |
140 error ("imwrite: invalid dimensions for truecolor image"); | |
7987 | 141 endif |
142 if (nd > 5) | |
8054 | 143 error ("imwrite: invalid %d-dimensional image data", nd); |
7987 | 144 endif |
145 else | |
146 error ("imwrite: %s: invalid class for truecolor image", img_class); | |
147 endif | |
8054 | 148 if (has_param_list) |
149 __magick_write__ (filename, fmt, img, options); | |
150 else | |
151 __magick_write__ (filename, fmt, img); | |
152 endif | |
7987 | 153 else |
154 if (any (strcmp (img_class, {"uint8", "uint16", "double"}))) | |
155 if (strcmp (img_class, "double")) | |
8054 | 156 img = uint8 (img - 1); |
7987 | 157 endif |
8054 | 158 if (nd != 2 && nd != 4) |
159 error ("imwrite: invalid size for indexed image"); | |
7987 | 160 endif |
161 else | |
162 error ("imwrite: %s: invalid class for indexed image data", img_class); | |
163 endif | |
164 if (isa (map, "double")) | |
165 if (ndims (map) != 2 || size (map, 2) != 3) | |
8054 | 166 error ("imwrite: invalid size for colormap"); |
7987 | 167 endif |
168 else | |
169 error ("imwrite: %s invalid class for indexed image colormap", | |
8054 | 170 class (map)); |
7987 | 171 endif |
9275
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
172 |
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
173 ## FIXME -- we should really be writing indexed images here but |
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
174 ## __magick_write__ needs to be fixed to handle them. |
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
175 |
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
176 [r, g, b] = ind2rgb (img, map); |
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
177 tmp = uint8 (cat (3, r, g, b) * 255); |
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
178 |
8054 | 179 if (has_param_list) |
9275
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
180 __magick_write__ (filename, fmt, tmp, options); |
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
181 ## __magick_write__ (filename, fmt, img, map, options); |
8054 | 182 else |
9275
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
183 __magick_write__ (filename, fmt, tmp); |
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
184 ## __magick_write__ (filename, fmt, img, map); |
8054 | 185 endif |
7987 | 186 endif |
187 | |
188 endfunction | |
10891
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
189 |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
190 %% Test input validation |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
191 %!error imwrite () # Wrong # of args |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
192 %!error imwrite (1) # Wrong # of args |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
193 %!error imwrite ({"cell"}, "filename.jpg") # Wrong class for img |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
194 %!error imwrite (1, [], "filename.jpg") # Empty image map |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
195 %!error imwrite (1, 2, 3) # No filename specified |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
196 %!error imwrite (1, "filename") # No fmt specified |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
197 %!error imwrite (1, "filename", "junk") # Invalid fmt specified |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
198 %!error imwrite ([], "filename.jpg") # Empty img matrix |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
199 %!error imwrite (spones(2), "filename.jpg") # Invalid sparse img |
ff94219746bd
imwrite.m: Allow writing of 1-bit (B&W) images.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
200 |