annotate scripts/image/private/__imwrite__.m @ 17854:adb0ba0d0c13

imwrite: implement DelayTime option. * __magick_read__.cc (F__magick_write__): set animationDelay() when writing an image to implement the DelayTime option. * private/__imwrite__.m: input check for the new option DelayTime and set its default. Organize options in alphabetic order. * imwrite.m: document new option.
author Carnë Draug <carandraug@octave.org>
date Tue, 05 Nov 2013 05:48:56 +0000
parents aacb9da13df6
children bfbe5dcc9943
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,
17854
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
46 "delaytime", ones (1, size (img, 4)) *500, # 0.5 seconds
17853
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
47 "loopcount", 0, ## this is actually Inf
17332
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
48 "alpha", cast ([], class (img)));
16988
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 for idx = 1:2:numel (param_list)
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
51
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
52 switch (tolower (param_list{idx}))
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
53
17332
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
54 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
55 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
56 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
57 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
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 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
66 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
67 endif
eb7e9a991ffb Implement writing of CMYK and fix writing of image alpha channel (bug #32986).
Carnë Draug <carandraug@octave.org>
parents: 17266
diff changeset
68
17854
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
69 case "delaytime"
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
70 options.delaytime = param_list{idx+1};
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
71 if (! isnumeric (options.delaytime))
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
72 error ("imwrite: value for %s option must be numeric",
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
73 param_list{idx});
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
74 endif
17854
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
75 options.delaytime *= 100; # convert to 1/100ths of second
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
76 if (isscalar (options.delaytime))
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
77 options.delaytime(1:size (img, 4)) = options.delaytime;
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
78 elseif (size (img, 4) != numel (options.delaytime))
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
79 error ("imwrite: value for %s must either be a scalar or the number of frames",
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
80 param_list{idx});
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
81 endif
17854
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
82 if (any (options.delaytime(:) < 0) || any (options.delaytime(:) > 65535))
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
83 error ("imwrite: value for %s must be between 0 and 655.35 seconds",
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
84 param_list{idx});
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
85 endif
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
86
17853
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
87 case "loopcount"
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
88 options.loopcount = param_list{idx+1};
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
89 if (! isscalar (options.loopcount) || ! isnumeric (options.loopcount)
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
90 || (! isinf (options.loopcount) && (options.loopcount < 0 ||
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
91 options.loopcount > 65535)))
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
92 error ("imwrite: value for %s must be Inf or between 0 and 65535",
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
93 param_list{idx});
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
94 endif
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
95 ## Graphics Magick is a bit weird here. A value of 0 will be an
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
96 ## infinite loop, a value of 1, will really be no loop, while a
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
97 ## value of 2 or more will be that number of loops (checked
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
98 ## with GNOME image viewer). This means that there is no way
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
99 ## to make it loop only once. See
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
100 ## https://sourceforge.net/p/graphicsmagick/bugs/249/
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
101 ## There is also the problem of setting this when there is only
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
102 ## a single frame. See
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
103 ## https://sourceforge.net/p/graphicsmagick/bugs/248/
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
104 if (isinf (options.loopcount))
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
105 options.loopcount = 0;
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
106 elseif (options.loopcount == 0 || options.loopcount == 1)
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
107 options.loopcount++;
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
108 endif
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
109 options.loopcount = floor (options.loopcount);
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
110
17854
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
111 case "quality",
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
112 options.quality = param_list{idx+1};
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
113 if (! isnumeric (options.quality) || ! isscalar (options.quality)
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
114 || options.quality < 0 || options.quality > 100)
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
115 error ("imwrite: value for %s option must be a scalar between 0 and 100",
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
116 param_list{idx});
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
117 endif
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
118 options.quality = round (options.quality);
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
119
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
120 case "writemode",
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
121 options.writemode = param_list{idx+1};
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
122 if (! ischar (options.writemode)
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
123 || ! any (strcmpi (options.writemode, {"append", "overwrite"})))
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
124 error ('imwrite: value for %s option must be "append" or "overwrite"',
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
125 param_list{idx});
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
126 endif
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
127 options.writemode = tolower (options.writemode);
adb0ba0d0c13 imwrite: implement DelayTime option.
Carnë Draug <carandraug@octave.org>
parents: 17853
diff changeset
128
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
129 otherwise
17853
aacb9da13df6 imwrite: implement LoopCount option.
Carnë Draug <carandraug@octave.org>
parents: 17850
diff changeset
130 error ("imwrite: invalid PARAMETER `%s'", param_list{idx});
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
131
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
132 endswitch
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
133 endfor
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
134
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
135 if (! isempty (map))
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
136 if (! iscolormap (map))
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
137 error ("imwrite: invalid MAP for indexed image");
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
138 elseif (ndims (img) != 2 && ndims (img) != 4)
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
139 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
140 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
141
45e325002810 imwrite: fill colormap with black (RGB 0,0,0) for missing colors (bug #33615)
Carnë Draug <carandraug@octave.org>
parents: 17744
diff changeset
142 ## 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
143 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
144 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
145 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
146 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
147 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
148 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
149 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
150 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
151 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
152 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
153
17226
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
154 ## 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
155 ## 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
156 ## 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
157 if (isfloat (img))
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
158 if (rows (map) <= 256)
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
159 img = uint8 (img - 1);
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
160 else
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
161 img = uint16 (img - 1);
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
162 endif
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
163 endif
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
164 endif
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
165
16988
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
166 if (ndims (img) > 4)
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
167 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
168 elseif (all (size (img, 3) != [1 3 4]))
46805642048f Implement writing of indexed images.
Carnë Draug <carandraug@octave.org>
parents: 17176
diff changeset
169 ## 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
170 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
171 endif
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
172
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
173 __magick_write__ (filename, ext, img, map, options);
54b75bed4bc7 imwrite: implement WriteMode option.
Carnë Draug <carandraug@octave.org>
parents: 16965
diff changeset
174
16916
59f575e504dc Connect imwrite with imformats.
Carnë Draug <carandraug@octave.org>
parents:
diff changeset
175 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17332
diff changeset
176