Mercurial > octave-nkf
annotate scripts/image/private/core_imwrite.m @ 16965:786a50507bbd
imwrite: fix bug from wrong indexing of varargin.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Fri, 12 Jul 2013 16:39:10 +0100 |
parents | eb7c0c2dc6d2 |
children | 54b75bed4bc7 |
rev | line source |
---|---|
16916
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
1 ## Copyright (C) 2008-2012 John W. Eaton |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
2 ## |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
3 ## This file is part of Octave. |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
4 ## |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
5 ## Octave is free software; you can redistribute it and/or modify it |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
6 ## under the terms of the GNU General Public License as published by |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
8 ## your option) any later version. |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
9 ## |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
10 ## Octave is distributed in the hope that it will be useful, but |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
13 ## General Public License for more details. |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
14 ## |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
15 ## You should have received a copy of the GNU General Public License |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
16 ## along with Octave; see the file COPYING. If not, see |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
18 |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
19 ## This function does all the work of imwrite. It exists here as private |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
20 ## function so that imwrite can use other functions if imformats is |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
21 ## configured to. It is also needed so that imformats can create a |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
22 ## function handle for it. |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
23 |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
24 function core_imwrite (img, varargin) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
25 |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
26 if (nargin < 2 || ! (isnumeric (img) || islogical (img))) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
27 print_usage ("imwrite"); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
28 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
29 |
16965
786a50507bbd
imwrite: fix bug from wrong indexing of varargin.
Carnë Draug <carandraug@octave.org>
parents:
16964
diff
changeset
|
30 [filename, ext, map, param_list] = imwrite_filename (varargin{:}); |
16916
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
31 |
16964
eb7c0c2dc6d2
imwrite: fix bug due to uninitialized variable with no options (bug #39472)
Carnë Draug <carandraug@octave.org>
parents:
16948
diff
changeset
|
32 options = struct (); |
eb7c0c2dc6d2
imwrite: fix bug due to uninitialized variable with no options (bug #39472)
Carnë Draug <carandraug@octave.org>
parents:
16948
diff
changeset
|
33 has_param_list = false; |
eb7c0c2dc6d2
imwrite: fix bug due to uninitialized variable with no options (bug #39472)
Carnë Draug <carandraug@octave.org>
parents:
16948
diff
changeset
|
34 if (! isempty (param_list)) |
16944
1b3b3ee88284
Add optional extension argument to image IO functions.
Carnë Draug <carandraug@octave.org>
parents:
16916
diff
changeset
|
35 has_param_list = true; |
16948
ee2166121a28
imwrite: clean up finding of filename and its connection to imformats.
Carnë Draug <carandraug@octave.org>
parents:
16944
diff
changeset
|
36 for ii = 1:2:(length (param_list)) |
ee2166121a28
imwrite: clean up finding of filename and its connection to imformats.
Carnë Draug <carandraug@octave.org>
parents:
16944
diff
changeset
|
37 options.(param_list{ii}) = param_list{ii + 1}; |
16916
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
38 endfor |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
39 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
40 |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
41 if (isempty (img)) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
42 error ("imwrite: invalid empty image"); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
43 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
44 |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
45 if (issparse (img) || issparse (map)) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
46 error ("imwrite: sparse images not supported"); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
47 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
48 |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
49 img_class = class (img); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
50 map_class = class (map); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
51 nd = ndims (img); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
52 |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
53 if (isempty (map)) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
54 if (any (strcmp (img_class, {"logical", "uint8", "uint16", "double"}))) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
55 if ((nd == 2 || nd == 3) && strcmp (img_class, "double")) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
56 img = uint8 (img * 255); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
57 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
58 ## FIXME: should we handle color images with alpha channel here? |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
59 if (nd == 3 && size (img, 3) < 3) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
60 error ("imwrite: invalid dimensions for truecolor image"); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
61 endif |
16944
1b3b3ee88284
Add optional extension argument to image IO functions.
Carnë Draug <carandraug@octave.org>
parents:
16916
diff
changeset
|
62 ## FIXME: why nd>5? Shouldn't it be nd>4? What's the 5th dimension for? |
16916
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
63 if (nd > 5) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
64 error ("imwrite: invalid %d-dimensional image data", nd); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
65 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
66 else |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
67 error ("imwrite: %s: invalid class for truecolor image", img_class); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
68 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
69 if (has_param_list) |
16948
ee2166121a28
imwrite: clean up finding of filename and its connection to imformats.
Carnë Draug <carandraug@octave.org>
parents:
16944
diff
changeset
|
70 __magick_write__ (filename, ext, img, options); |
16916
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
71 else |
16948
ee2166121a28
imwrite: clean up finding of filename and its connection to imformats.
Carnë Draug <carandraug@octave.org>
parents:
16944
diff
changeset
|
72 __magick_write__ (filename, ext, img); |
16916
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
73 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
74 else |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
75 if (any (strcmp (img_class, {"uint8", "uint16", "double"}))) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
76 if (strcmp (img_class, "double")) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
77 img = uint8 (img - 1); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
78 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
79 if (nd != 2 && nd != 4) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
80 error ("imwrite: invalid size for indexed image"); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
81 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
82 else |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
83 error ("imwrite: %s: invalid class for indexed image data", img_class); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
84 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
85 if (! iscolormap (map)) |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
86 error ("imwrite: invalid indexed image colormap"); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
87 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
88 |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
89 ## FIXME: we should really be writing indexed images here but |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
90 ## __magick_write__ needs to be fixed to handle them. |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
91 |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
92 [r, g, b] = ind2rgb (img, map); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
93 tmp = uint8 (cat (3, r, g, b) * 255); |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
94 |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
95 if (has_param_list) |
16948
ee2166121a28
imwrite: clean up finding of filename and its connection to imformats.
Carnë Draug <carandraug@octave.org>
parents:
16944
diff
changeset
|
96 __magick_write__ (filename, ext, tmp, options); |
ee2166121a28
imwrite: clean up finding of filename and its connection to imformats.
Carnë Draug <carandraug@octave.org>
parents:
16944
diff
changeset
|
97 ## __magick_write__ (filename, ext, img, map, options); |
16916
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
98 else |
16948
ee2166121a28
imwrite: clean up finding of filename and its connection to imformats.
Carnë Draug <carandraug@octave.org>
parents:
16944
diff
changeset
|
99 __magick_write__ (filename, ext, tmp); |
ee2166121a28
imwrite: clean up finding of filename and its connection to imformats.
Carnë Draug <carandraug@octave.org>
parents:
16944
diff
changeset
|
100 ## __magick_write__ (filename, ext, img, map); |
16916
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
101 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
102 endif |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
103 |
59f575e504dc
Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff
changeset
|
104 endfunction |