changeset 7931:de26beacb20f

imread.m: simplify; loadimage.m: deprecate
author John W. Eaton <jwe@octave.org>
date Wed, 16 Jul 2008 12:55:48 -0400
parents 1f6eb3de1c4e
children d75586531aac
files scripts/ChangeLog scripts/deprecated/Makefile.in scripts/deprecated/loadimage.m scripts/image/Makefile.in scripts/image/imread.m scripts/image/imshow.m scripts/image/loadimage.m src/DLD-FUNCTIONS/__magick_read__.cc
diffstat 8 files changed, 97 insertions(+), 214 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Wed Jul 16 11:24:48 2008 -0400
+++ b/scripts/ChangeLog	Wed Jul 16 12:55:48 2008 -0400
@@ -1,5 +1,13 @@
 2008-07-16  John W. Eaton  <jwe@octave.org>
 
+	* deprecated/loadimage.m: New file.
+	* deprecated/Makefile.in (SOURCES): Add it to the list.
+	* image/loadimage.m: Delete.	
+	* image/Makefile.in (SOURCES): Remove it from the list.
+	* image/imread.m: Incorporate loadimage functionality here.
+	Simplify.
+	* image/imshow.m: Call imread, not loadimage.
+
 	* image/__img__.m: Set clim for true-color integer data.
 
 	* image/imshow.m: Don't convert integer true-color data to double.
--- a/scripts/deprecated/Makefile.in	Wed Jul 16 11:24:48 2008 -0400
+++ b/scripts/deprecated/Makefile.in	Wed Jul 16 12:55:48 2008 -0400
@@ -42,10 +42,10 @@
   hypergeometric_cdf.m hypergeometric_inv.m hypergeometric_pdf.m \
   hypergeometric_rnd.m intersection.m is_bool.m is_complex.m \
   is_list.m is_matrix.m is_scalar.m is_square.m is_stream.m \
-  is_struct.m is_symmetric.m is_vector.m isstr.m lchol.m lognormal_cdf.m \
-  lognormal_inv.m lognormal_pdf.m lognormal_rnd.m meshdom.m normal_cdf.m \
-  normal_inv.m normal_pdf.m normal_rnd.m pascal_cdf.m \
-  pascal_inv.m pascal_pdf.m pascal_rnd.m poisson_cdf.m \
+  is_struct.m is_symmetric.m is_vector.m isstr.m lchol.m loadimage.m \
+  lognormal_cdf.m lognormal_inv.m lognormal_pdf.m lognormal_rnd.m \
+  meshdom.m normal_cdf.m normal_inv.m normal_pdf.m normal_rnd.m \
+  pascal_cdf.m pascal_inv.m pascal_pdf.m pascal_rnd.m poisson_cdf.m \
   poisson_inv.m poisson_pdf.m poisson_rnd.m polyinteg.m setstr.m \
   spatan2.m spchol2inv.m spcholinv.m spcumprod.m spcumsum.m \
   spchol.m spdet.m spdiag.m spfind.m spinv.m spkron.m splchol.m \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/deprecated/loadimage.m	Wed Jul 16 12:55:48 2008 -0400
@@ -0,0 +1,42 @@
+## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2004, 2005,
+##               2006, 2007 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
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {[@var{x}, @var{map}] =} loadimage (@var{file})
+## Load an image file and it's associated color map from the specified
+## @var{file}.  The image must be stored in Octave's image format.
+## @seealso{saveimage, load, save}
+## @end deftypefn
+
+## Author: Tony Richardson <arichard@stark.cc.oh.us>
+## Created: July 1994
+## Adapted-By: jwe
+
+function [img_retval, map_retval] = loadimage (varargin)
+
+  persistent warned = false;
+  if (! warned)
+    warned = true;
+    warning ("Octave:deprecated-function",
+             "loadimage is obsolete and will be removed from a future version of Octave; please use imread instead");
+  endif
+
+  [img_retval, map_retval] = imread (varargin{:});
+
+endfunction
--- a/scripts/image/Makefile.in	Wed Jul 16 11:24:48 2008 -0400
+++ b/scripts/image/Makefile.in	Wed Jul 16 12:55:48 2008 -0400
@@ -36,7 +36,7 @@
 SOURCES = __img__.m __img_via_file__.m autumn.m bone.m brighten.m colormap.m \
   contrast.m cool.m copper.m flag.m gmap40.m gray.m gray2ind.m hot.m hsv.m \
   hsv2rgb.m image.m image_viewer.m imagesc.m imread.m imshow.m ind2gray.m \
-  ind2rgb.m jet.m loadimage.m ntsc2rgb.m ocean.m pink.m prism.m rainbow.m \
+  ind2rgb.m jet.m ntsc2rgb.m ocean.m pink.m prism.m rainbow.m \
   rgb2hsv.m rgb2ind.m rgb2ntsc.m saveimage.m spring.m summer.m white.m winter.m
 
 IMAGES = default.img
--- a/scripts/image/imread.m	Wed Jul 16 11:24:48 2008 -0400
+++ b/scripts/image/imread.m	Wed Jul 16 12:55:48 2008 -0400
@@ -21,7 +21,7 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {@var{I} =} imread(@var{filename})
+## @deftypefn {Function File} {[@var{img}, @var{map}, @var{alpha}] =} imread (@var{filename})
 ## Read images from various file formats.
 ##
 ## The size and numeric class of the output depends on the
@@ -29,164 +29,60 @@
 ## MxNx3 matrix.  Grey-level and black-and-white images are
 ## of size MxN.
 ## The colour depth of the image determines the numeric
-## class of the output: 'uint8' or 'uint16' for grey
-## and colour, and 'logical' for black and white.
-##
-## Note: For image formats other than jpeg and png, the
-## ImageMagick "convert" and "identify" utilities
-## are needed. ImageMagick can be found at www.imagemagick.org
+## class of the output: "uint8" or "uint16" for grey
+## and colour, and "logical" for black and white.
 ## @end deftypefn
 
-## Author: Andy Adler
-##
-## Modified: Stefan van der Walt <stefan@sun.ac.za>
-## Date: 24 January 2005
-##
-## Modified: Thomas Weber <thomas.weber.mail@gmail.com>
-## Date: 20 December 2006
-## Change parsing of imagemagick's output to get the 'color' depth for grayscale
-## images
-##
-## Modified Kristian Rumberg <kristianrumberg@gmail.com>
-## Date 2 April 2008
-## Imread now works with BMP's created with "convert inputimage out.bmp"
-## (tested with stable release Octave 3.0 in GNU/Linux and Windows XP),
-## modified the calling parameters to identify and convert
-##
-## Modified Thomas Scofield <scofield 'at' calvin.edu
-## Date 1 July 2008
-## Imread now uses Magick++ API to GraphicsMagick libraries instead
-## of ImageMagick libraries (tested with stable release Octave 3.0
-## in GNU/Linux).  All images are read by GraphicsMagick routines;
-## there is no longer exceptional handling of .png and .jpg files.
+function varargout = imread (filename, varargin)
 
-function varargout = imread ( filename, varargin )
   if (nargin < 1)
-    usage ("I = imread(filename)")
+    print_usage ();
   endif
 
-  if (!ischar (filename))
+  if (! ischar (filename))
     error ("imread: filename must be a string")
   endif
 
   filename = tilde_expand (filename);
-  fn = file_in_path ( IMAGE_PATH, filename );
-  if (isempty (fn))
-    error ( "imread: cannot find %s", filename );
-  endif
 
-  [ig, ig, ext] = fileparts (fn);
-  ext = upper (ext);    
-    
-  ## real work uses GraphicsMagick
-  if ( file_in_loadpath ("__magick_read__.oct") )
-    [varargout{1:nargout}] = __magick_read__ ( fn, varargin{:} );
-    ## color .PNG formats (any others?) have 4 levels in the 3rd dimension
-    if ( length (size (varargout{1}))==3 && length (varargout{1}(1,1,:)) > 3 )
-      varargout{1} = varargout{1}(:,:,1:3);
-    endif
-    break
-  endif
-    
-  ## The next line could be altered from "identify ..." to "gm identify ..."
-  ## If we continue to carry this out with a system call, my guess is that
-  ## more people will have ImageMagick than have GraphicsMagick.  But if
-  ## we want this to work for people who have neither, then the system call
-  ## needs to be scrapped and replaced with a call to a dynamically-linked
-  ## routine that again employs the C++ API to GraphicsMagick.
-  ## Note also the system call to ImageMagick's "convert" farther down.
-  cmd = sprintf ('identify -verbose \"%s\" | grep -e "bit" -e Type', fn);
-  [sys, ident] = system (cmd);
-  if (sys != 0)
-    error ( "imread: error running ImageMagick's 'identify' on %s", fn );
-  endif
-  depth = re_grab ( "([[:digit:]]{1,2})-bit", ident );
-  imtype = re_grab ( "Type: ([[:alpha:]]*)", ident );
+  fn = file_in_path (IMAGE_PATH, filename);
 
-  depth = str2num (depth);
-  if ( isempty (depth) || ( pow2 (nextpow2 (depth)) != depth ) )
-    error ( "imread: invalid image depth %s", depth );
-  endif
-
-  if !( strcmp ( imtype, "Bilevel" )   || strcmp ( imtype, "Grayscale" )
-        || strcmp ( imtype, "TrueColor" ) || strcmp ( imtype, "TrueColorMatte" )
-        || strcmp ( imtype, "Palette" ) || strcmp ( imtype, "PaletteMatte" ) )
-    # The 'PaletteMatte' option added by TLS to accomodate ImageMagick
-    # on .png images.  It appears GraphicsMagick returns a different
-    # string, so this will likely be only a temporary change.
-    error ( "imread: unknown image type '%s'", imtype );
-  endif
-
-  switch (imtype)
-    case {"Bilevel"} fmt = "pgm";
-    case {"Grayscale"} fmt = "pgm";
-    case {"TrueColor", "TrueColorMatte", "Palette", "PaletteMatte"}
-      fmt = "ppm";
-  endswitch
-    
-  ## Why are pipes so slow?
-  ##    cmd = sprintf ( "convert -flatten -strip %s %s:-", fn, fmt );
-    
-  tmpf = [tmpnam(), ".", fmt];
-  ##cmd = sprintf ( "convert -flatten -strip +compress '%s' '%s' 2>/dev/null",
-  ##                fn, tmpf );
-  cmd = sprintf ( "convert -strip \"%s\" \"%s\"", fn, tmpf );
-
-  sys = system (cmd);    
-  if (sys != 0)
-    error ("imread: error running ImageMagick's 'convert'");
-    unlink (tmpf);
+  if (isempty (fn))
+    error ( "imread: cannot find %s", filename);
   endif
 
   try
-    fid = fopen ( tmpf, "rb" );
+    [varargout{1:nargout}] = __magick_read__ (fn, varargin{:});
   catch
-    unlink (tmpf);
-    error ( "imread: could not open temporary file %s", tmpf )
+
+    magick_error = lasterr ();
+
+    img_field = false;
+    x_field = false;
+    map_field = false;
+
+    try
+      vars = load (fn);
+      if (isstruct (vars))
+	img_field = isfield (vars, "img");
+	x_field = isfield (vars, "X");
+	map_field = isfield (vars, "map");
+      endif
+    catch
+      error ("imread: invalid image file: %s", magick_error)
+    end_try_catch
+
+    if (map_field && (img_field || x_field))
+      varargout{2} = vars.map;
+      if (img_field)
+	varargout{1} = vars.img;
+      else
+	varargout{1} = vars.X;
+      endif
+    else
+      error ("imread: invalid Octave image file format");
+    endif
+
   end_try_catch
 
-  fgetl (fid); # P5 or P6 (pgm or ppm)
-  [width, height] = sscanf ( fgetl (fid), "%d %d", "C" );
-  fgetl (fid); # ignore max components
-
-  if (depth == 16)
-    ## PGM format has MSB first, i.e. big endian
-    [data, count] = fread ( fid, "uint16", 0, "ieee-be" );
-  else
-    [data, count] = fread ( fid, "uint8" );
-  endif
-    
-  fclose (fid);
-  unlink (tmpf);
-
-  if (any (strcmp ( imtype, {"TrueColor", "TrueColorMatte", ...
-                             "Palette", "PaletteMatte"} ) ) )
-    channels = 3;
-  else
-    channels = 1;
-  endif
-  if (count != width*height*channels)
-    error ( "imread: image data chunk has invalid size %i != %i*%i*%i == %i",
-            count, width, height, channels, width*height*channels );
-  endif
-
-  varargout = {};
-  switch (imtype)
-    case {"Bilevel"} varargout{1} = logical ( reshape (data, width, height)' );
-    case {"Grayscale"} varargout{1} = uint8 ( reshape (data, width, height)' );
-    case {"TrueColor", "TrueColorMatte", "Palette", "PaletteMatte"}
-      varargout{1} = cat(3, reshape ( data(1:3:end), width, height )',
-                         reshape ( data(2:3:end), width, height )',
-                         reshape ( data(3:3:end), width, height )');
-      eval( sprintf( "varargout{1} = uint%d(varargout{1});", depth ) );
-  endswitch
 endfunction
-
-function value = re_grab ( re, str )
-  T = regexp ( str, re, 'tokens' );
-  if ( isempty (T) )
-    value = "";
-  else
-    value = T{1}{1};
-  endif
-endfunction
--- a/scripts/image/imshow.m	Wed Jul 16 11:24:48 2008 -0400
+++ b/scripts/image/imshow.m	Wed Jul 16 12:55:48 2008 -0400
@@ -65,7 +65,7 @@
   ## Get the image.
   if (ischar (im))
     ## Eventually, this should be imread.
-    [im, map] = loadimage (im);
+    [im, map] = imread (im);
     indexed = true;
     colormap (map);
   endif
--- a/scripts/image/loadimage.m	Wed Jul 16 11:24:48 2008 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,63 +0,0 @@
-## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2004, 2005,
-##               2006, 2007 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
-## <http://www.gnu.org/licenses/>.
-
-## -*- texinfo -*-
-## @deftypefn {Function File} {[@var{x}, @var{map}] =} loadimage (@var{file})
-## Load an image file and it's associated color map from the specified
-## @var{file}.  The image must be stored in Octave's image format.
-## @seealso{saveimage, load, save}
-## @end deftypefn
-
-## Author: Tony Richardson <arichard@stark.cc.oh.us>
-## Created: July 1994
-## Adapted-By: jwe
-
-function [img_retval, map_retval] = loadimage (filename)
-
-  if (nargin != 1)
-    print_usage ();
-  elseif (! ischar (filename))
-    error ("loadimage: expecting filename as a string");
-  endif
-
-  file = file_in_path (IMAGE_PATH, filename);
-
-  if (isempty (file))
-    error ("loadimage: unable to find image file");
-  endif
-
-  ## The file is assumed to have variables img and map, or X and map.
-
-  vars = load (file);
-
-  if (isfield (vars, "img"))
-    img_retval = vars.img;
-  elseif (isfield (vars, "X"))
-    img_retval = vars.X;
-  else
-    error ("loadimage: invalid image file found");
-  endif
-
-  if (isfield (vars, "map"))
-    map_retval = vars.map;
-  else
-    error ("loadimage: invalid image file found");
-  endif
-
-endfunction
--- a/src/DLD-FUNCTIONS/__magick_read__.cc	Wed Jul 16 11:24:48 2008 -0400
+++ b/src/DLD-FUNCTIONS/__magick_read__.cc	Wed Jul 16 12:55:48 2008 -0400
@@ -309,7 +309,7 @@
 {
   octave_value_list output;
 
-  if (args.length() > 2 || args.length() < 1 || ! args(0).is_string ()
+  if (args.length () > 2 || args.length () < 1 || ! args(0).is_string ()
       || nargout > 3)
     {
       print_usage ();
@@ -318,7 +318,7 @@
 
   Array<int> frameidx;
 
-  if (args.length() == 2 && args(1).is_real_type())
+  if (args.length () == 2 && args(1).is_real_type ())
     frameidx = args(1).int_vector_value();
   else
     {