## Copyright (C) 1996 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 2, 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, write to the ## Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA ## 02111-1307, USA. ## Load an image file. ## ## [img, map] = loadimage (img_file) ## loads an image and its associated color map from file img_file, ## which must be in octave's image format. ## ## [img, map] = loadimage (img_file, "img") ## is the same as loadimage (img_file). ## ## [r, g, b] = loadimage (img_file, "ppm") ## loads an image from file img_file, ## which must be in color ppm format (ascii or binary). ## ## [img, map] = loadimage (img_file, "ppm") ## loads an image from file img_file, ## which may be in any ppm format (ascii or binary). ## If the image is in color (ppm, not pgm or pbm), ## rgb2ind() is used to convert it to an indexed image. ## ## if no output arguments are specified, the image is displayed ## ## SEE ALSO: saveimage, load, save ## Author: Tony Richardson ## Created: July 1994 ## Adapted-By: jwe ## Updated Nov 1999 to include ppm,pgm.pbm image formats by ## Clif Kussmaul ## and Tricia Salomonsen . ## Updated April 2005 fix some obsolete syntax for Octave 2.1 by ## En-ran Zhou function [varargout] = loadimage2 (filename, img_form) ## process arguments if (nargin < 1 || nargin > 2) usage ("[img, map] = loadimage (filename, [format])"); endif if (! isstr (filename)) error ("loadimage: file name must be a string"); endif if (nargin < 2) img_form = "img"; endif ######################################## ## load image in octave's image format if (strcmp (img_form, "img")) ## file is in octave's image format, ## and is assumed to have variables img and map, or X and map. file = file_in_path (IMAGEPATH, filename); if (isempty (file)) error ("loadimage: unable to find image file"); endif eval (['load ', file]); if (exist ("img")) img_retval = img; elseif (exist ("X")) img_retval = X; else error ("loadimage: invalid image file found"); endif if (exist ("map")) map_retval = map; else error ("loadimage: invalid image file found"); endif if (nargout == 0) imshow(img_retval, map_retval); else varargout{1} = img_retval; varargout{2} = map_retval; endif ## end of img format ######################################## ## load image in PBM/PGM/PPM image format elseif (strcmp (img_form, "ppm") || strcmp (img_form, "pgm") || strcmp (img_form, "pbm") ) ## check magic number fp = fopen(filename,"rt"); [magic, count] = fscanf(fp,"%s", "C") ; if (magic(1) != "P") error ("loadimage: invalid image file (bad magic number)"); endif ## read 2 values (W,H) for pbm, 3 (W,H,M) for pgm or ppm if (magic(2) == "1" || magic(2) == "4") dneed = 2; else dneed = 3; endif while (dneed > 0) ## skip blank lines and comment lines while (1) data = fgetl(fp); if (length(data) > 0 && ! strcmp(data(1),"#")) break; endif endwhile ## try to read values [dvals, dcnt] = sscanf(data, "%d", dneed); if (dneed >= 3 && dneed - dcnt < 3) dsize(3) = dvals(dneed - 2); endif if (dneed >= 2 && dneed - dcnt < 2) dsize(2) = dvals(dneed - 1); endif if (dneed >= 1 && dneed - dcnt < 1) dsize(1) = dvals(dneed - 0); endif dneed = dneed - dcnt; endwhile if (magic(2) == "1" || magic(2) == "4") # read binary PBM in ascii ("1") or binary ("4") and transpose if (magic(2) == "1") data = fscanf(fp, "%d", [ dsize(2) dsize(1)] )'; else idata = fread(fp, [ceil(dsize(2)/8) dsize(1)], "uchar")'; ## unpack bits from bytes data = zeros(dsize(1),dsize(2)); for i=8:-1:1 data(:,i:8:size(data,2)) = rem(idata, 2); idata = floor(idata / 2); endfor endif # would like to make 2-entry colormap, but imshow crashes in saveimage.m data = (!data) * 255; map = gray(256); if (nargout == 0) imshow(data, map); else varargout{1} = data; varargout{2} = map; endif elseif (magic(2) == "2" || magic(2) == "5") ## read greyscale PGM in ascii ("2") or binary ("5") and transpose if (magic(2) == "2") data = fscanf(fp, "%d", [dsize(3) dsize(2)] )'; else data = fread( fp, [dsize(3) dsize(2)], "uchar")'; endif map = gray(dsize(1)); if (nargout == 0) imshow(data, map); else varargout{1} = data; varargout{2} = map; endif elseif (magic(2) == "3" || magic(2) == "6") ## read color PPM in ascii ("3") or binary ("6") if (magic(2) == "3") data = fscanf(fp, "%d", [dsize(3)*3 dsize(2)] ); else data = fread( fp, [dsize(3)*3 dsize(2)], "uchar"); endif # extract R,G,B values between 0 and 1 data = data / dsize(1); r = data(1:3:size(data,1),:)'; g = data(2:3:size(data,1),:)'; b = data(3:3:size(data,1),:)'; if (nargout == 0) imshow(r,g,b); elseif (nargout < 3) [img_retval, map_retval] = rgb2ind(r, g, b); varargout{1} = img_retval; varargout{2} = map_retval; elseif (nargout == 3) varargout{1} = r; varargout{2} = g; varargout{3} = b; endif else error("loadimage: invalid image file (bad magic number)"); endif fclose(fp); ## end of ppm/pgm/pbm format else error ("loadimage: image format not supported."); endif endfunction