comparison scripts/image/private/core_imwrite.m @ 16944:1b3b3ee88284

Add optional extension argument to image IO functions. * imfinfo.m, private/core_imfinfo.m, imread.m, private/core_imread.m, imwrite.m: added support for optional EXT argument. * imwrite.m: in addition to support the extra EXT argument, removed list of supported image formats from help text, and suggested to use imformats() instead. * private/core_imwrite.m: remove usage of __magick_format_list__() which is being replaced by imformats(). Check validity of colormap earlier on. Removed tests that are repeated in imwrite.m (leftovers from copying the file). * private/imageIO.m: deal with separate filename and extension and ignore file existence checking if we are writing. * __magic_read__.cc: remove private function __magick_format_list__() that was only being used by core_imwrite.m and is being replaced by imformats().
author Carnë Draug <carandraug@octave.org>
date Wed, 10 Jul 2013 18:18:21 +0100
parents 59f575e504dc
children ee2166121a28
comparison
equal deleted inserted replaced
16943:0dab17e69a55 16944:1b3b3ee88284
21 ## configured to. It is also needed so that imformats can create a 21 ## configured to. It is also needed so that imformats can create a
22 ## function handle for it. 22 ## function handle for it.
23 23
24 function core_imwrite (img, varargin) 24 function core_imwrite (img, varargin)
25 25
26 persistent imwrite_possible_formats = {
27 "bmp"; "gif"; "jp2"; "jpg"; "jpx"; "jpeg"; "hdf"; "pbm"; "pcx";
28 "pgm"; "png"; "pnm"; "ppm"; "ras"; "tif"; "tiff"; "xwd" };
29
30 persistent accepted_formats = __magick_format_list__ (imwrite_possible_formats);
31
32 if (nargin < 2 || ! (isnumeric (img) || islogical (img))) 26 if (nargin < 2 || ! (isnumeric (img) || islogical (img)))
33 print_usage ("imwrite"); 27 print_usage ("imwrite");
34 endif 28 endif
35 29
36 map = []; 30 map = [];
37 fmt = ""; 31 fmt = "";
38 32
39 offset = 1; 33 offset = 1;
40 if (isnumeric (varargin{1})) 34 if (isnumeric (varargin{1}))
41 map = varargin{1}; 35 map = varargin{1};
42 if (isempty (map)) 36 if (! iscolormap (map))
43 error ("imwrite: colormap must not be empty"); 37 error ("imwrite: invalid COLORMAP");
44 endif 38 endif
45 offset = 2; 39 offset = 2;
46 endif 40 endif
47 if (offset <= length (varargin) && ischar (varargin{offset})) 41 if (offset <= length (varargin) && ischar (varargin{offset}))
48 filename = varargin{offset}; 42 filename = varargin{offset};
53 endif 47 endif
54 else 48 else
55 print_usage ("imwrite"); 49 print_usage ("imwrite");
56 endif 50 endif
57 if (offset < length (varargin)) 51 if (offset < length (varargin))
58 has_param_list = 1; 52 has_param_list = true;
59 for ii = offset:2:(length (varargin) - 1) 53 for ii = offset:2:(length (varargin) - 1)
60 options.(varargin{ii}) = varargin{ii + 1}; 54 options.(varargin{ii}) = varargin{ii + 1};
61 endfor 55 endfor
62 else 56 else
63 has_param_list = 0; 57 has_param_list = false;
64 endif 58 endif
65 59
66 filename = tilde_expand (filename); 60 filename = tilde_expand (filename);
67 61
68 if (isempty (fmt)) 62 if (isempty (fmt))
69 [d, n, fmt] = fileparts (filename); 63 [~, ~, fmt] = fileparts (filename);
70 if (! isempty (fmt)) 64 if (! isempty (fmt))
71 fmt = fmt(2:end); 65 fmt = fmt(2:end);
72 endif 66 endif
73 endif 67 endif
74 68
76 error ("imwrite: invalid empty image"); 70 error ("imwrite: invalid empty image");
77 endif 71 endif
78 72
79 if (issparse (img) || issparse (map)) 73 if (issparse (img) || issparse (map))
80 error ("imwrite: sparse images not supported"); 74 error ("imwrite: sparse images not supported");
81 endif
82
83 if (! strcmp (fmt, accepted_formats))
84 error ("imwrite: %s: unsupported or invalid image format", fmt);
85 endif 75 endif
86 76
87 img_class = class (img); 77 img_class = class (img);
88 map_class = class (map); 78 map_class = class (map);
89 nd = ndims (img); 79 nd = ndims (img);
95 endif 85 endif
96 ## FIXME: should we handle color images with alpha channel here? 86 ## FIXME: should we handle color images with alpha channel here?
97 if (nd == 3 && size (img, 3) < 3) 87 if (nd == 3 && size (img, 3) < 3)
98 error ("imwrite: invalid dimensions for truecolor image"); 88 error ("imwrite: invalid dimensions for truecolor image");
99 endif 89 endif
90 ## FIXME: why nd>5? Shouldn't it be nd>4? What's the 5th dimension for?
100 if (nd > 5) 91 if (nd > 5)
101 error ("imwrite: invalid %d-dimensional image data", nd); 92 error ("imwrite: invalid %d-dimensional image data", nd);
102 endif 93 endif
103 else 94 else
104 error ("imwrite: %s: invalid class for truecolor image", img_class); 95 error ("imwrite: %s: invalid class for truecolor image", img_class);
137 ## __magick_write__ (filename, fmt, img, map); 128 ## __magick_write__ (filename, fmt, img, map);
138 endif 129 endif
139 endif 130 endif
140 131
141 endfunction 132 endfunction
142
143
144 %% Test input validation
145 %!error imwrite () # Wrong # of args
146 %!error imwrite (1) # Wrong # of args
147 %!error imwrite ({"cell"}, "filename.jpg") # Wrong class for img
148 %!error imwrite (1, [], "filename.jpg") # Empty image map
149 %!error imwrite (1, 2, 3) # No filename specified
150 %!error imwrite (1, "filename") # No fmt specified
151 %!error imwrite (1, "filename", "junk") # Invalid fmt specified
152 %!error imwrite ([], "filename.jpg") # Empty img matrix
153 %!error imwrite (spones (2), "filename.jpg") # Invalid sparse img
154