annotate scripts/image/private/__imwrite__.m @ 17234:0b8f78cdc5cd

imwrite: give error if image is an empty or sparse matrix.
author Carnë Draug <carandraug@octave.org>
date Tue, 13 Aug 2013 06:20:02 +0100
parents 46805642048f
children b1a09f72a520
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
2 ## Copyright (C) 2013 Carnë Draug
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
3 ##
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
4 ## This file is part of Octave.
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
5 ##
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
6 ## 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
7 ## 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
8 ## 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
9 ## your option) any later version.
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
10 ##
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
11 ## 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
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
14 ## General Public License for more details.
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
15 ##
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
16 ## 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
17 ## along with Octave; see the file COPYING. If not, see
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
18 ## <http://www.gnu.org/licenses/>.
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
19
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
20 ## 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
21 ## function so that imwrite can use other functions if imformats is
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
22 ## 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
23 ## function handle for it.
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
24
17086
3db796f89695 Rename private core_imfnc functions to follow the __fnc__ convention style.
Carnë Draug <carandraug@octave.org>
parents: 16988
diff changeset
25 function __imwrite__ (img, varargin)
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
26
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
27 if (nargin < 2 || ! (isnumeric (img) || islogical (img)))
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
28 print_usage ("imwrite");
17234
0b8f78cdc5cd imwrite: give error if image is an empty or sparse matrix.
Carnë Draug <carandraug@octave.org>
parents: 17226
diff changeset
29 elseif (isempty (img))
0b8f78cdc5cd imwrite: give error if image is an empty or sparse matrix.
Carnë Draug <carandraug@octave.org>
parents: 17226
diff changeset
30 error ("imwrite: invalid empty image");
0b8f78cdc5cd imwrite: give error if image is an empty or sparse matrix.
Carnë Draug <carandraug@octave.org>
parents: 17226
diff changeset
31 elseif (issparse (img) || issparse (map))
0b8f78cdc5cd imwrite: give error if image is an empty or sparse matrix.
Carnë Draug <carandraug@octave.org>
parents: 17226
diff changeset
32 error ("imwrite: sparse images are not supported");
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
33 endif
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
34
16965
786a50507bbd imwrite: fix bug from wrong indexing of varargin.
Carnë Draug <carandraug@octave.org>
parents: 16964
diff changeset
35 [filename, ext, map, param_list] = imwrite_filename (varargin{:});
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
36
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
37 if (rem (numel (param_list), 2) != 0)
17176
abf384f5d243 maint: Remove unneeded input validation from internal fcns in private/ directories.
Rik <rik@octave.org>
parents: 17086
diff changeset
38 error ("imwrite: no pair for all arguments (odd number left)");
16916
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
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
41 ## set default for options
17226
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
42 options = struct ("writemode", "overwrite",
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
43 "quality", 75);
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
44
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
45 for idx = 1:2:numel (param_list)
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
46
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
47 switch (tolower (param_list{idx}))
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
48
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
49 case "writemode",
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
50 options.writemode = param_list{idx+1};
17176
abf384f5d243 maint: Remove unneeded input validation from internal fcns in private/ directories.
Rik <rik@octave.org>
parents: 17086
diff changeset
51 if (! ischar (options.writemode)
abf384f5d243 maint: Remove unneeded input validation from internal fcns in private/ directories.
Rik <rik@octave.org>
parents: 17086
diff changeset
52 || ! any (strcmpi (options.writemode, {"append", "overwrite"})))
abf384f5d243 maint: Remove unneeded input validation from internal fcns in private/ directories.
Rik <rik@octave.org>
parents: 17086
diff changeset
53 error ('imwrite: value for %s option must be "append" or "overwrite"',
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
54 param_list{idx});
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
55 endif
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
56 options.writemode = tolower (options.writemode);
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
57
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
58 case "quality",
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
59 options.quality = param_list{idx+1};
17176
abf384f5d243 maint: Remove unneeded input validation from internal fcns in private/ directories.
Rik <rik@octave.org>
parents: 17086
diff changeset
60 if (! isnumeric (options.quality) || ! isscalar (options.quality)
abf384f5d243 maint: Remove unneeded input validation from internal fcns in private/ directories.
Rik <rik@octave.org>
parents: 17086
diff changeset
61 || options.quality < 0 || options.quality > 100)
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
62 error ("imwrite: value for %s option must be a scalar between 0 and 100",
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
63 param_list{idx});
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
64 endif
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
65 options.quality = round (options.quality);
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
66
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
67 otherwise
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
68 error ("imwrite: invalid PARAMETER `%s'", varargin{idx});
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
69
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
70 endswitch
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
71 endfor
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
72
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
73 if (! isempty (map))
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
74 if (! iscolormap (map))
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
75 error ("imwrite: invalid MAP for indexed image");
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
76 elseif (ndims (img) != 2 && ndims (img) != 4)
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
77 error ("imwrite: indexed image must have 2 or 4 dimensions (found %i)", ndims (img));
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
78 endif
17226
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
79 ## If the image is floating point, then we convert it to integer (makes
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
80 ## it easier in __magick_write__ since it only handles integers. Also,
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
81 ## if it's floating point, it has an offset of 1
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
82 if (isfloat (img))
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
83 if (rows (map) <= 256)
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
84 img = uint8 (img - 1);
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
85 else
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
86 img = uint16 (img - 1);
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
87 endif
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
88 endif
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
89 endif
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
90
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
91 if (ndims (img) > 4)
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
92 error ("imwrite: invalid %d-dimensional image data", ndims (img));
17226
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
93 elseif (all (size (img, 3) != [1 3 4]))
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
94 ## 1, 3, or 4 for grayscle, RGB, and CMYK respectively
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
95 error ("imwrite: IMG 3rd dimension must be 1, 3, or 4");
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
96 endif
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
97
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
98 __magick_write__ (filename, ext, img, map, options);
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
99
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
100 endfunction