scripts/image/imread.m
author John W. Eaton <jwe@octave.org>
Thu Jul 16 11:56:44 2009 -0400 (2009-07-16)
changeset 9384 44300eb51817
parent 9268 a9c4aece8c2a
child 9414 edb25fa5e7d5
permissions -rw-r--r--
graphics.cc (get_array_limits): require min_pos value to be greater than zero
     1 ## Copyright (C) 2008, 2009 Thomas L. Scofield <scofield@calvin.edu>
     2 ## Copyright (C) 2008 Kristian Rumberg <kristianrumberg@gmail.com>
     3 ## Copyright (C) 2006 Thomas Weber <thomas.weber.mail@gmail.com>
     4 ## Copyright (C) 2005 Stefan van der Walt <stefan@sun.ac.za>
     5 ## Copyright (C) 2002 Andy Adler
     6 ##
     7 ## This file is part of Octave.
     8 ##
     9 ## Octave is free software; you can redistribute it and/or modify it
    10 ## under the terms of the GNU General Public License as published by
    11 ## the Free Software Foundation; either version 3 of the License, or (at
    12 ## your option) any later version.
    13 ##
    14 ## Octave is distributed in the hope that it will be useful, but
    15 ## WITHOUT ANY WARRANTY; without even the implied warranty of
    16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    17 ## General Public License for more details.
    18 ##
    19 ## You should have received a copy of the GNU General Public License
    20 ## along with Octave; see the file COPYING.  If not, see
    21 ## <http://www.gnu.org/licenses/>.
    22 
    23 ## -*- texinfo -*-
    24 ## @deftypefn {Function File} {[@var{img}, @var{map}, @var{alpha}] =} imread (@var{filename})
    25 ## Read images from various file formats.
    26 ##
    27 ## The size and numeric class of the output depends on the
    28 ## format of the image.  A color image is returned as an
    29 ## MxNx3 matrix.  Grey-level and black-and-white images are
    30 ## of size MxN.
    31 ## The color depth of the image determines the numeric
    32 ## class of the output: "uint8" or "uint16" for grey
    33 ## and color, and "logical" for black and white.
    34 ##
    35 ## @seealso{imwrite, imfinfo}
    36 ## @end deftypefn
    37 
    38 function varargout = imread (filename, varargin)
    39 
    40   if (nargin < 1)
    41     print_usage ();
    42   endif
    43 
    44   if (! ischar (filename))
    45     error ("imread: filename must be a string");
    46   endif
    47 
    48   filename = tilde_expand (filename);
    49 
    50   fn = file_in_path (IMAGE_PATH, filename);
    51 
    52   if (isempty (fn))
    53     error ("imread: cannot find %s", filename);
    54   endif
    55 
    56   try
    57     [varargout{1:nargout}] = magick_read_internal (fn, varargin{:});
    58   catch
    59 
    60     magick_error = lasterr ();
    61 
    62     img_field = false;
    63     x_field = false;
    64     map_field = false;
    65 
    66     try
    67       vars = load (fn);
    68       if (isstruct (vars))
    69 	img_field = isfield (vars, "img");
    70 	x_field = isfield (vars, "X");
    71 	map_field = isfield (vars, "map");
    72       endif
    73     catch
    74       error ("imread: invalid image file: %s", magick_error);
    75     end_try_catch
    76 
    77     if (map_field && (img_field || x_field))
    78       varargout{2} = vars.map;
    79       if (img_field)
    80 	varargout{1} = vars.img;
    81       else
    82 	varargout{1} = vars.X;
    83       endif
    84     else
    85       error ("imread: invalid Octave image file format");
    86     endif
    87 
    88   end_try_catch
    89 
    90 endfunction