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