annotate scripts/image/imwrite.m @ 10793:be55736a0783

Grammarcheck the documentation from m-files.
author Rik <octave@nomad.inbox5.com>
date Sun, 18 Jul 2010 20:35:16 -0700
parents 86f475d5e7d1
children ff94219746bd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8610
diff changeset
1 ## Copyright (C) 2008, 2009 John W. Eaton
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2 ##
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3 ## This file is part of Octave.
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 ##
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 ## your option) any later version.
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 ##
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 ## General Public License for more details.
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 ##
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 ## -*- texinfo -*-
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 9275
diff changeset
20 ## @deftypefn {Function File} {} imwrite (@var{img}, @var{filename}, @var{fmt}, @var{p1}, @var{v1}, @dots{})
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 ## @deftypefnx {Function File} {} imwrite (@var{img}, @var{map}, @var{filename}, @var{fmt}, @var{p1}, @var{v1}, @dots{})
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 ## Write images in various file formats.
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 ##
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 ## If @var{fmt} is missing, the file extension (if any) of
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25 ## @var{filename} is used to determine the format.
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 ##
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 9275
diff changeset
27 ## The parameter-value pairs (@var{p1}, @var{v1}, @dots{}) are optional.
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 9275
diff changeset
28 ## Currently
8148
213dd524f96b Improve documentation for image I/O.
sh@sh-laptop
parents: 8054
diff changeset
29 ## the following options are supported for @t{JPEG} images
213dd524f96b Improve documentation for image I/O.
sh@sh-laptop
parents: 8054
diff changeset
30 ##
213dd524f96b Improve documentation for image I/O.
sh@sh-laptop
parents: 8054
diff changeset
31 ## @table @samp
213dd524f96b Improve documentation for image I/O.
sh@sh-laptop
parents: 8054
diff changeset
32 ## @item Quality
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
33 ## Sets the quality of the compression. The corresponding value should be an
8148
213dd524f96b Improve documentation for image I/O.
sh@sh-laptop
parents: 8054
diff changeset
34 ## integer between 0 and 100, with larger values meaning higher visual quality
213dd524f96b Improve documentation for image I/O.
sh@sh-laptop
parents: 8054
diff changeset
35 ## and less compression.
213dd524f96b Improve documentation for image I/O.
sh@sh-laptop
parents: 8054
diff changeset
36 ## @end table
213dd524f96b Improve documentation for image I/O.
sh@sh-laptop
parents: 8054
diff changeset
37 ##
213dd524f96b Improve documentation for image I/O.
sh@sh-laptop
parents: 8054
diff changeset
38 ## @seealso{imread, imfinfo}
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 ## @end deftypefn
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 function imwrite (varargin)
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 persistent accepted_formats = { "bmp", "gif", "jpg", "jpeg", ...
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
44 "ras", "pbm", "pgm", "png", "ppm", "svg", "tif", "tiff" };
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 img = [];
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 map = [];
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48 fmt = "";
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50 if (nargin > 1 && isnumeric (varargin{1}))
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 img = varargin{1};
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 offset = 2;
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53 if (isnumeric (varargin{2}))
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 map = varargin{2};
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55 if (isempty (map))
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
56 error ("imwrite: colormap must not be empty");
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58 offset = 3;
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60 if (offset <= nargin && ischar (varargin{offset}))
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 filename = varargin{offset};
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62 offset++;
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 if (rem (nargin - offset, 2) == 0 && ischar (varargin{offset}))
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
64 fmt = varargin{offset};
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
65 offset++;
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67 else
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 print_usage ();
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70 if (offset < nargin)
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
71 has_param_list = 1;
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8148
diff changeset
72 for ii = offset:2:(nargin - 1)
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
73 options.(varargin{ii}) = varargin{ii + 1};
8610
85c9906abfd1 use endif and endfor instead of end
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
74 endfor
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
75 else
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
76 has_param_list = 0;
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78 else
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79 print_usage ();
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82 filename = tilde_expand (filename);
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84 if (isempty (fmt))
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85 [d, n, fmt] = fileparts (filename);
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86 if (! isempty (fmt))
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87 fmt = fmt(2:end);
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91 if (issparse (img) || issparse (map))
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92 error ("imwrite: sparse images not supported");
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
95 if (isempty (img))
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96 error ("imwrite: invalid empty image");
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99 if (! strcmp (fmt, accepted_formats))
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
100 error ("imwrite: %s: unsupported or invalid image format", fmt);
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
102
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
103 img_class = class (img);
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104 map_class = class (map);
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
105 nd = ndims (img);
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
106
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
107 if (isempty (map))
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108 if (any (strcmp (img_class, {"logical", "uint8", "uint16", "double"})))
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
109 if ((nd == 2 || nd == 3) && strcmp (img_class, "double"))
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
110 img = uint8 (img * 255);
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
111 endif
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
112 ## FIXME -- should we handle color images w/ alpha channel here?
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
113 if (nd == 3 && size (img, 3) < 3)
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
114 error ("imwrite: invalid dimensions for truecolor image");
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
115 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
116 if (nd > 5)
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
117 error ("imwrite: invalid %d-dimensional image data", nd);
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
118 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
119 else
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
120 error ("imwrite: %s: invalid class for truecolor image", img_class);
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
121 endif
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
122 if (has_param_list)
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
123 __magick_write__ (filename, fmt, img, options);
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
124 else
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
125 __magick_write__ (filename, fmt, img);
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
126 endif
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
127 else
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
128 if (any (strcmp (img_class, {"uint8", "uint16", "double"})))
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
129 if (strcmp (img_class, "double"))
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
130 img = uint8 (img - 1);
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
131 endif
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
132 if (nd != 2 && nd != 4)
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
133 error ("imwrite: invalid size for indexed image");
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
134 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
135 else
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
136 error ("imwrite: %s: invalid class for indexed image data", img_class);
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
137 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
138 if (isa (map, "double"))
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
139 if (ndims (map) != 2 || size (map, 2) != 3)
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
140 error ("imwrite: invalid size for colormap");
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
141 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
142 else
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
143 error ("imwrite: %s invalid class for indexed image colormap",
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
144 class (map));
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
145 endif
9275
86f475d5e7d1 imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents: 9051
diff changeset
146
86f475d5e7d1 imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents: 9051
diff changeset
147 ## 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
148 ## __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
149
86f475d5e7d1 imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents: 9051
diff changeset
150 [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
151 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
152
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
153 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
154 __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
155 ## __magick_write__ (filename, fmt, img, map, options);
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
156 else
9275
86f475d5e7d1 imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents: 9051
diff changeset
157 __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
158 ## __magick_write__ (filename, fmt, img, map);
8054
272eaebbb6ba Various features added to imwrite
scofield@scofield
parents: 7987
diff changeset
159 endif
7987
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
160 endif
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
161
4a423a042971 add imwrite.m
John W. Eaton <jwe@octave.org>
parents:
diff changeset
162 endfunction