Mercurial > octave-libtiff
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 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2008, 2009 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 -*- | |
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 | 21 ## @deftypefnx {Function File} {} imwrite (@var{img}, @var{map}, @var{filename}, @var{fmt}, @var{p1}, @var{v1}, @dots{}) |
22 ## Write images in various file formats. | |
23 ## | |
24 ## If @var{fmt} is missing, the file extension (if any) of | |
25 ## @var{filename} is used to determine the format. | |
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 | 29 ## the following options are supported for @t{JPEG} images |
30 ## | |
31 ## @table @samp | |
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 | 34 ## integer between 0 and 100, with larger values meaning higher visual quality |
35 ## and less compression. | |
36 ## @end table | |
37 ## | |
38 ## @seealso{imread, imfinfo} | |
7987 | 39 ## @end deftypefn |
40 | |
41 function imwrite (varargin) | |
42 | |
43 persistent accepted_formats = { "bmp", "gif", "jpg", "jpeg", ... | |
8054 | 44 "ras", "pbm", "pgm", "png", "ppm", "svg", "tif", "tiff" }; |
7987 | 45 |
46 img = []; | |
47 map = []; | |
48 fmt = ""; | |
49 | |
50 if (nargin > 1 && isnumeric (varargin{1})) | |
51 img = varargin{1}; | |
52 offset = 2; | |
53 if (isnumeric (varargin{2})) | |
54 map = varargin{2}; | |
55 if (isempty (map)) | |
8054 | 56 error ("imwrite: colormap must not be empty"); |
7987 | 57 endif |
58 offset = 3; | |
59 endif | |
60 if (offset <= nargin && ischar (varargin{offset})) | |
61 filename = varargin{offset}; | |
62 offset++; | |
63 if (rem (nargin - offset, 2) == 0 && ischar (varargin{offset})) | |
8054 | 64 fmt = varargin{offset}; |
65 offset++; | |
7987 | 66 endif |
67 else | |
68 print_usage (); | |
69 endif | |
70 if (offset < nargin) | |
8054 | 71 has_param_list = 1; |
8507 | 72 for ii = offset:2:(nargin - 1) |
8054 | 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 | 75 else |
76 has_param_list = 0; | |
7987 | 77 endif |
78 else | |
79 print_usage (); | |
80 endif | |
81 | |
82 filename = tilde_expand (filename); | |
83 | |
84 if (isempty (fmt)) | |
85 [d, n, fmt] = fileparts (filename); | |
86 if (! isempty (fmt)) | |
87 fmt = fmt(2:end); | |
88 endif | |
89 endif | |
90 | |
91 if (issparse (img) || issparse (map)) | |
92 error ("imwrite: sparse images not supported"); | |
93 endif | |
94 | |
95 if (isempty (img)) | |
96 error ("imwrite: invalid empty image"); | |
97 endif | |
98 | |
99 if (! strcmp (fmt, accepted_formats)) | |
100 error ("imwrite: %s: unsupported or invalid image format", fmt); | |
101 endif | |
102 | |
103 img_class = class (img); | |
104 map_class = class (map); | |
8054 | 105 nd = ndims (img); |
7987 | 106 |
107 if (isempty (map)) | |
108 if (any (strcmp (img_class, {"logical", "uint8", "uint16", "double"}))) | |
109 if ((nd == 2 || nd == 3) && strcmp (img_class, "double")) | |
8054 | 110 img = uint8 (img * 255); |
7987 | 111 endif |
8054 | 112 ## FIXME -- should we handle color images w/ alpha channel here? |
113 if (nd == 3 && size (img, 3) < 3) | |
114 error ("imwrite: invalid dimensions for truecolor image"); | |
7987 | 115 endif |
116 if (nd > 5) | |
8054 | 117 error ("imwrite: invalid %d-dimensional image data", nd); |
7987 | 118 endif |
119 else | |
120 error ("imwrite: %s: invalid class for truecolor image", img_class); | |
121 endif | |
8054 | 122 if (has_param_list) |
123 __magick_write__ (filename, fmt, img, options); | |
124 else | |
125 __magick_write__ (filename, fmt, img); | |
126 endif | |
7987 | 127 else |
128 if (any (strcmp (img_class, {"uint8", "uint16", "double"}))) | |
129 if (strcmp (img_class, "double")) | |
8054 | 130 img = uint8 (img - 1); |
7987 | 131 endif |
8054 | 132 if (nd != 2 && nd != 4) |
133 error ("imwrite: invalid size for indexed image"); | |
7987 | 134 endif |
135 else | |
136 error ("imwrite: %s: invalid class for indexed image data", img_class); | |
137 endif | |
138 if (isa (map, "double")) | |
139 if (ndims (map) != 2 || size (map, 2) != 3) | |
8054 | 140 error ("imwrite: invalid size for colormap"); |
7987 | 141 endif |
142 else | |
143 error ("imwrite: %s invalid class for indexed image colormap", | |
8054 | 144 class (map)); |
7987 | 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 | 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 | 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 | 159 endif |
7987 | 160 endif |
161 | |
162 endfunction |