# HG changeset patch # User Carnë Draug # Date 1373229917 -3600 # Node ID 59f575e504dc7b1673da7a4259b8b16a000b694a # Parent cb90f31a4d26ea74576d6869cd2b38c3bb0ac3e7 Connect imwrite with imformats. * private/core_imwrite.m: new function. Old code from imwrite() moved here just like what happened to imfinfo(). See message on cset bfad37d33435 * imfinfo.m: reduced to minimum input check, until finding filename. Passes all arguments to imageIO(). * imformats.m: change calls to imwrite() to the new core_imwrite(). diff -r cb90f31a4d26 -r 59f575e504dc scripts/image/imformats.m --- a/scripts/image/imformats.m Sun Jul 07 21:10:26 2013 +0100 +++ b/scripts/image/imformats.m Sun Jul 07 21:45:17 2013 +0100 @@ -243,7 +243,7 @@ ## the default info, read, and write functions [formats.info ] = deal (@core_imfinfo); [formats.read ] = deal (@core_imread); - [formats.write] = deal (@imread); + [formats.write] = deal (@core_imwrite); ## fills rest of format information by checking with GraphicsMagick formats = __magick_formats__ (formats); diff -r cb90f31a4d26 -r 59f575e504dc scripts/image/imwrite.m --- a/scripts/image/imwrite.m Sun Jul 07 21:10:26 2013 +0100 +++ b/scripts/image/imwrite.m Sun Jul 07 21:45:17 2013 +0100 @@ -63,126 +63,22 @@ ## @seealso{imread, imfinfo} ## @end deftypefn -function imwrite (img, varargin) - - persistent imwrite_possible_formats = { - "bmp"; "gif"; "jp2"; "jpg"; "jpx"; "jpeg"; "hdf"; "pbm"; "pcx"; - "pgm"; "png"; "pnm"; "ppm"; "ras"; "tif"; "tiff"; "xwd" }; - - persistent accepted_formats = __magick_format_list__ (imwrite_possible_formats); - - if (nargin < 2 || ! (isnumeric (img) || islogical (img))) +function imwrite (varargin) + if (nargin < 2) print_usage (); endif - map = []; - fmt = ""; - - offset = 1; - if (isnumeric (varargin{1})) - map = varargin{1}; - if (isempty (map)) - error ("imwrite: colormap must not be empty"); - endif - offset = 2; - endif - if (offset <= length (varargin) && ischar (varargin{offset})) - filename = varargin{offset}; - offset++; - if (rem (length (varargin) - offset, 2) == 0 && ischar (varargin{offset})) - fmt = varargin{offset}; - offset++; - endif + if (ischar (varargin{2})) + filename = varargin{2}; + elseif (nargin >= 3 && iscolormap (varargin{2}) && ! ischar (varargin{3})) + filename = varargin{3}' else - print_usage (); - endif - if (offset < length (varargin)) - has_param_list = 1; - for ii = offset:2:(length (varargin) - 1) - options.(varargin{ii}) = varargin{ii + 1}; - endfor - else - has_param_list = 0; - endif - - filename = tilde_expand (filename); - - if (isempty (fmt)) - [d, n, fmt] = fileparts (filename); - if (! isempty (fmt)) - fmt = fmt(2:end); - endif - endif - - if (isempty (img)) - error ("imwrite: invalid empty image"); - endif - - if (issparse (img) || issparse (map)) - error ("imwrite: sparse images not supported"); - endif - - if (! strcmp (fmt, accepted_formats)) - error ("imwrite: %s: unsupported or invalid image format", fmt); + error ("imwrite: no FILENAME specified"); endif - - img_class = class (img); - map_class = class (map); - nd = ndims (img); - - if (isempty (map)) - if (any (strcmp (img_class, {"logical", "uint8", "uint16", "double"}))) - if ((nd == 2 || nd == 3) && strcmp (img_class, "double")) - img = uint8 (img * 255); - endif - ## FIXME: should we handle color images with alpha channel here? - if (nd == 3 && size (img, 3) < 3) - error ("imwrite: invalid dimensions for truecolor image"); - endif - if (nd > 5) - error ("imwrite: invalid %d-dimensional image data", nd); - endif - else - error ("imwrite: %s: invalid class for truecolor image", img_class); - endif - if (has_param_list) - __magick_write__ (filename, fmt, img, options); - else - __magick_write__ (filename, fmt, img); - endif - else - if (any (strcmp (img_class, {"uint8", "uint16", "double"}))) - if (strcmp (img_class, "double")) - img = uint8 (img - 1); - endif - if (nd != 2 && nd != 4) - error ("imwrite: invalid size for indexed image"); - endif - else - error ("imwrite: %s: invalid class for indexed image data", img_class); - endif - if (! iscolormap (map)) - error ("imwrite: invalid indexed image colormap"); - endif - - ## FIXME: we should really be writing indexed images here but - ## __magick_write__ needs to be fixed to handle them. - - [r, g, b] = ind2rgb (img, map); - tmp = uint8 (cat (3, r, g, b) * 255); - - if (has_param_list) - __magick_write__ (filename, fmt, tmp, options); - ## __magick_write__ (filename, fmt, img, map, options); - else - __magick_write__ (filename, fmt, tmp); - ## __magick_write__ (filename, fmt, img, map); - endif - endif + varargout{1:nargout} = imageIO (@core_imwrite, "write", filename, varargin{:}); endfunction - %% Test input validation %!error imwrite () # Wrong # of args %!error imwrite (1) # Wrong # of args diff -r cb90f31a4d26 -r 59f575e504dc scripts/image/private/core_imwrite.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/image/private/core_imwrite.m Sun Jul 07 21:45:17 2013 +0100 @@ -0,0 +1,154 @@ +## Copyright (C) 2008-2012 John W. Eaton +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . + +## This function does all the work of imwrite. It exists here as private +## function so that imwrite can use other functions if imformats is +## configured to. It is also needed so that imformats can create a +## function handle for it. + +function core_imwrite (img, varargin) + + persistent imwrite_possible_formats = { + "bmp"; "gif"; "jp2"; "jpg"; "jpx"; "jpeg"; "hdf"; "pbm"; "pcx"; + "pgm"; "png"; "pnm"; "ppm"; "ras"; "tif"; "tiff"; "xwd" }; + + persistent accepted_formats = __magick_format_list__ (imwrite_possible_formats); + + if (nargin < 2 || ! (isnumeric (img) || islogical (img))) + print_usage ("imwrite"); + endif + + map = []; + fmt = ""; + + offset = 1; + if (isnumeric (varargin{1})) + map = varargin{1}; + if (isempty (map)) + error ("imwrite: colormap must not be empty"); + endif + offset = 2; + endif + if (offset <= length (varargin) && ischar (varargin{offset})) + filename = varargin{offset}; + offset++; + if (rem (length (varargin) - offset, 2) == 0 && ischar (varargin{offset})) + fmt = varargin{offset}; + offset++; + endif + else + print_usage ("imwrite"); + endif + if (offset < length (varargin)) + has_param_list = 1; + for ii = offset:2:(length (varargin) - 1) + options.(varargin{ii}) = varargin{ii + 1}; + endfor + else + has_param_list = 0; + endif + + filename = tilde_expand (filename); + + if (isempty (fmt)) + [d, n, fmt] = fileparts (filename); + if (! isempty (fmt)) + fmt = fmt(2:end); + endif + endif + + if (isempty (img)) + error ("imwrite: invalid empty image"); + endif + + if (issparse (img) || issparse (map)) + error ("imwrite: sparse images not supported"); + endif + + if (! strcmp (fmt, accepted_formats)) + error ("imwrite: %s: unsupported or invalid image format", fmt); + endif + + img_class = class (img); + map_class = class (map); + nd = ndims (img); + + if (isempty (map)) + if (any (strcmp (img_class, {"logical", "uint8", "uint16", "double"}))) + if ((nd == 2 || nd == 3) && strcmp (img_class, "double")) + img = uint8 (img * 255); + endif + ## FIXME: should we handle color images with alpha channel here? + if (nd == 3 && size (img, 3) < 3) + error ("imwrite: invalid dimensions for truecolor image"); + endif + if (nd > 5) + error ("imwrite: invalid %d-dimensional image data", nd); + endif + else + error ("imwrite: %s: invalid class for truecolor image", img_class); + endif + if (has_param_list) + __magick_write__ (filename, fmt, img, options); + else + __magick_write__ (filename, fmt, img); + endif + else + if (any (strcmp (img_class, {"uint8", "uint16", "double"}))) + if (strcmp (img_class, "double")) + img = uint8 (img - 1); + endif + if (nd != 2 && nd != 4) + error ("imwrite: invalid size for indexed image"); + endif + else + error ("imwrite: %s: invalid class for indexed image data", img_class); + endif + if (! iscolormap (map)) + error ("imwrite: invalid indexed image colormap"); + endif + + ## FIXME: we should really be writing indexed images here but + ## __magick_write__ needs to be fixed to handle them. + + [r, g, b] = ind2rgb (img, map); + tmp = uint8 (cat (3, r, g, b) * 255); + + if (has_param_list) + __magick_write__ (filename, fmt, tmp, options); + ## __magick_write__ (filename, fmt, img, map, options); + else + __magick_write__ (filename, fmt, tmp); + ## __magick_write__ (filename, fmt, img, map); + endif + endif + +endfunction + + +%% Test input validation +%!error imwrite () # Wrong # of args +%!error imwrite (1) # Wrong # of args +%!error imwrite ({"cell"}, "filename.jpg") # Wrong class for img +%!error imwrite (1, [], "filename.jpg") # Empty image map +%!error imwrite (1, 2, 3) # No filename specified +%!error imwrite (1, "filename") # No fmt specified +%!error imwrite (1, "filename", "junk") # Invalid fmt specified +%!error imwrite ([], "filename.jpg") # Empty img matrix +%!error imwrite (spones (2), "filename.jpg") # Invalid sparse img +