annotate scripts/image/private/__imwrite__.m @ 17850:45e325002810

imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
author Carnë Draug <carandraug@octave.org>
date Mon, 04 Nov 2013 21:48:45 +0000
parents d63878346099
children aacb9da13df6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17338
diff changeset
1 ## Copyright (C) 2008-2013 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");
17266
b1a09f72a520 imwrite: fix input checking for colormap (bug #39791)
Carnë Draug <carandraug@octave.org>
parents: 17234
diff changeset
29 endif
b1a09f72a520 imwrite: fix input checking for colormap (bug #39791)
Carnë Draug <carandraug@octave.org>
parents: 17234
diff changeset
30
b1a09f72a520 imwrite: fix input checking for colormap (bug #39791)
Carnë Draug <carandraug@octave.org>
parents: 17234
diff changeset
31 [filename, ext, map, param_list] = imwrite_filename (varargin{:});
b1a09f72a520 imwrite: fix input checking for colormap (bug #39791)
Carnë Draug <carandraug@octave.org>
parents: 17234
diff changeset
32
b1a09f72a520 imwrite: fix input checking for colormap (bug #39791)
Carnë Draug <carandraug@octave.org>
parents: 17234
diff changeset
33 if (isempty (img))
17234
0b8f78cdc5cd imwrite: give error if image is an empty or sparse matrix.
Carnë Draug <carandraug@octave.org>
parents: 17226
diff changeset
34 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
35 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
36 error ("imwrite: sparse images are not supported");
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
37 endif
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
38
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
39 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
40 error ("imwrite: no pair for all arguments (odd number left)");
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
41 endif
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
42
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
43 ## set default for options
17226
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
44 options = struct ("writemode", "overwrite",
17332
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
45 "quality", 75,
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
46 "alpha", cast ([], class (img)));
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
47
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
48 for idx = 1:2:numel (param_list)
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
49
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
50 switch (tolower (param_list{idx}))
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
51
17332
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
52 case "alpha"
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
53 options.alpha = param_list{idx+1};
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
54 if (! isnumeric (options.alpha))
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
55 error ("imwrite: value for %s option must be a numeric matrix",
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
56 param_list{idx});
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
57 elseif (size (options.alpha, 3) != 1)
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
58 error ("imwrite: 3rd dimension of matrix for %s must be singleton",
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
59 param_list{idx});
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
60 elseif (ndims (options.alpha) > 4 ||
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
61 any (size (options.alpha)([1 2]) != size (img)([1 2])) ||
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
62 size (options.alpha, 4) != size (img, 4))
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
63 error ("imwrite: matrix for %s must have same dimension as image",
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
64 param_list{idx});
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
65 endif
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
66
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
67 case "writemode",
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
68 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
69 if (! ischar (options.writemode)
abf384f5d243 maint: Remove unneeded input validation from internal fcns in private/ directories.
Rik <rik@octave.org>
parents: 17086
diff changeset
70 || ! 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
71 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
72 param_list{idx});
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
73 endif
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
74 options.writemode = tolower (options.writemode);
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
75
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
76 case "quality",
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
77 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
78 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
79 || options.quality < 0 || options.quality > 100)
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
80 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
81 param_list{idx});
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
82 endif
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
83 options.quality = round (options.quality);
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
84
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
85 otherwise
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
86 error ("imwrite: invalid PARAMETER `%s'", varargin{idx});
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
87
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
88 endswitch
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
89 endfor
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
90
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
91 if (! isempty (map))
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
92 if (! iscolormap (map))
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
93 error ("imwrite: invalid MAP for indexed image");
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
94 elseif (ndims (img) != 2 && ndims (img) != 4)
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
95 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
96 endif
17850
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
97
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
98 ## Fill in the colormap as required with rgb (0, 0, 0) (bug #33615)
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
99 nColors = rows (map);
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
100 if (any (strcmp (class (img), {"uint8", "uint16", "logical"})))
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
101 required_colors = max (img(:)) +1;
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
102 else
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
103 required_colors = max (img(:));
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
104 endif
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
105 if (nColors < required_colors)
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
106 warning ("imwrite: MAP has not enough colors. Filling with black");
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
107 map(nColors+1:required_colors,:) = 0;
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
108 endif
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
109
17226
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
110 ## 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
111 ## 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
112 ## 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
113 if (isfloat (img))
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
114 if (rows (map) <= 256)
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
115 img = uint8 (img - 1);
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
116 else
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
117 img = uint16 (img - 1);
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
118 endif
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
119 endif
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
120 endif
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
121
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
122 if (ndims (img) > 4)
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
123 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
124 elseif (all (size (img, 3) != [1 3 4]))
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
125 ## 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
126 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
127 endif
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
128
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
129 __magick_write__ (filename, ext, img, map, options);
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
130
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
131 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17332
diff changeset
132