]> Creatis software - CreaPhase.git/blob - octave_packages/m/image/imread.m
update packages
[CreaPhase.git] / octave_packages / m / image / imread.m
1 ## Copyright (C) 2008-2012 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 ## @nospell{MxNx3} matrix.  Gray-level and black-and-white images are
30 ## of size @nospell{MxN}.
31 ## The color depth of the image determines the numeric
32 ## class of the output: "uint8" or "uint16" for gray
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__ (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
91
92 %!testif HAVE_MAGICK
93 %! vpng = [ ...
94 %!  137,  80,  78,  71,  13,  10,  26,  10,   0,   0, ...
95 %!    0,  13,  73,  72,  68,  82,   0,   0,   0,   3, ...
96 %!    0,   0,   0,   3,   8,   2,   0,   0,   0, 217, ...
97 %!   74,  34, 232,   0,   0,   0,   1, 115,  82,  71, ...
98 %!   66,   0, 174, 206,  28, 233,   0,   0,   0,   4, ...
99 %!  103,  65,  77,  65,   0,   0, 177, 143,  11, 252, ...
100 %!   97,   5,   0,   0,   0,  32,  99,  72,  82,  77, ...
101 %!    0,   0, 122,  38,   0,   0, 128, 132,   0,   0, ...
102 %!  250,   0,   0,   0, 128, 232,   0,   0, 117,  48, ...
103 %!    0,   0, 234,  96,   0,   0,  58, 152,   0,   0, ...
104 %!   23, 112, 156, 186,  81,  60,   0,   0,   0,  25, ...
105 %!   73,  68,  65,  84,  24,  87,  99,  96,  96,  96, ...
106 %!  248, 255, 255,  63, 144,   4,  81, 111, 101,  84, ...
107 %!   16,  28, 160,  16,   0, 197, 214,  13,  34,  74, ...
108 %!  117, 213,  17,   0,   0,   0,   0,  73,  69,  78, ...
109 %!   68, 174,  66,  96, 130];
110 %! fid = fopen('test.png', 'wb');
111 %! fwrite(fid, vpng);
112 %! fclose(fid);
113 %! A = imread('test.png');
114 %! delete('test.png');
115 %! assert(A(:,:,1), uint8 ([0, 255, 0; 255, 237, 255; 0, 255, 0]));
116 %! assert(A(:,:,2), uint8 ([0, 255, 0; 255,  28, 255; 0, 255, 0]));
117 %! assert(A(:,:,3), uint8 ([0, 255, 0; 255,  36, 255; 0, 255, 0]));