Mercurial > hg > octave-nkf
annotate scripts/image/imwrite.m @ 9275:86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 27 May 2009 10:34:59 -0400 |
parents | 1bf0ce0930be |
children | be55736a0783 |
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 -*- | |
20 ## @deftypefn {Function File} {} imwrite (@var{img}, @var{filename}, @var{fmt}, @var{p1}, @var{v1}, @dots{}) | |
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 ## | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## The parameter-value pairs (@var{p1}, @var{v1}, @dots{}) are optional. Currently |
8148 | 28 ## the following options are supported for @t{JPEG} images |
29 ## | |
30 ## @table @samp | |
31 ## @item Quality | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
32 ## Sets the quality of the compression. The corresponding value should be an |
8148 | 33 ## integer between 0 and 100, with larger values meaning higher visual quality |
34 ## and less compression. | |
35 ## @end table | |
36 ## | |
37 ## @seealso{imread, imfinfo} | |
7987 | 38 ## @end deftypefn |
39 | |
40 function imwrite (varargin) | |
41 | |
42 persistent accepted_formats = { "bmp", "gif", "jpg", "jpeg", ... | |
8054 | 43 "ras", "pbm", "pgm", "png", "ppm", "svg", "tif", "tiff" }; |
7987 | 44 |
45 img = []; | |
46 map = []; | |
47 fmt = ""; | |
48 | |
49 if (nargin > 1 && isnumeric (varargin{1})) | |
50 img = varargin{1}; | |
51 offset = 2; | |
52 if (isnumeric (varargin{2})) | |
53 map = varargin{2}; | |
54 if (isempty (map)) | |
8054 | 55 error ("imwrite: colormap must not be empty"); |
7987 | 56 endif |
57 offset = 3; | |
58 endif | |
59 if (offset <= nargin && ischar (varargin{offset})) | |
60 filename = varargin{offset}; | |
61 offset++; | |
62 if (rem (nargin - offset, 2) == 0 && ischar (varargin{offset})) | |
8054 | 63 fmt = varargin{offset}; |
64 offset++; | |
7987 | 65 endif |
66 else | |
67 print_usage (); | |
68 endif | |
69 if (offset < nargin) | |
8054 | 70 has_param_list = 1; |
8507 | 71 for ii = offset:2:(nargin - 1) |
8054 | 72 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
|
73 endfor |
8054 | 74 else |
75 has_param_list = 0; | |
7987 | 76 endif |
77 else | |
78 print_usage (); | |
79 endif | |
80 | |
81 filename = tilde_expand (filename); | |
82 | |
83 if (isempty (fmt)) | |
84 [d, n, fmt] = fileparts (filename); | |
85 if (! isempty (fmt)) | |
86 fmt = fmt(2:end); | |
87 endif | |
88 endif | |
89 | |
90 if (issparse (img) || issparse (map)) | |
91 error ("imwrite: sparse images not supported"); | |
92 endif | |
93 | |
94 if (isempty (img)) | |
95 error ("imwrite: invalid empty image"); | |
96 endif | |
97 | |
98 if (! strcmp (fmt, accepted_formats)) | |
99 error ("imwrite: %s: unsupported or invalid image format", fmt); | |
100 endif | |
101 | |
102 img_class = class (img); | |
103 map_class = class (map); | |
8054 | 104 nd = ndims (img); |
7987 | 105 |
106 if (isempty (map)) | |
107 if (any (strcmp (img_class, {"logical", "uint8", "uint16", "double"}))) | |
108 if ((nd == 2 || nd == 3) && strcmp (img_class, "double")) | |
8054 | 109 img = uint8 (img * 255); |
7987 | 110 endif |
8054 | 111 ## FIXME -- should we handle color images w/ alpha channel here? |
112 if (nd == 3 && size (img, 3) < 3) | |
113 error ("imwrite: invalid dimensions for truecolor image"); | |
7987 | 114 endif |
115 if (nd > 5) | |
8054 | 116 error ("imwrite: invalid %d-dimensional image data", nd); |
7987 | 117 endif |
118 else | |
119 error ("imwrite: %s: invalid class for truecolor image", img_class); | |
120 endif | |
8054 | 121 if (has_param_list) |
122 __magick_write__ (filename, fmt, img, options); | |
123 else | |
124 __magick_write__ (filename, fmt, img); | |
125 endif | |
7987 | 126 else |
127 if (any (strcmp (img_class, {"uint8", "uint16", "double"}))) | |
128 if (strcmp (img_class, "double")) | |
8054 | 129 img = uint8 (img - 1); |
7987 | 130 endif |
8054 | 131 if (nd != 2 && nd != 4) |
132 error ("imwrite: invalid size for indexed image"); | |
7987 | 133 endif |
134 else | |
135 error ("imwrite: %s: invalid class for indexed image data", img_class); | |
136 endif | |
137 if (isa (map, "double")) | |
138 if (ndims (map) != 2 || size (map, 2) != 3) | |
8054 | 139 error ("imwrite: invalid size for colormap"); |
7987 | 140 endif |
141 else | |
142 error ("imwrite: %s invalid class for indexed image colormap", | |
8054 | 143 class (map)); |
7987 | 144 endif |
9275
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
145 |
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
146 ## 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
|
147 ## __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
|
148 |
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
149 [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
|
150 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
|
151 |
8054 | 152 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
|
153 __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
|
154 ## __magick_write__ (filename, fmt, img, map, options); |
8054 | 155 else |
9275
86f475d5e7d1
imwrite.m: convert indexed images to RGB before calling __magick_write__
John W. Eaton <jwe@octave.org>
parents:
9051
diff
changeset
|
156 __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
|
157 ## __magick_write__ (filename, fmt, img, map); |
8054 | 158 endif |
7987 | 159 endif |
160 | |
161 endfunction |