help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

PGM function


From: Dave Jansing
Subject: PGM function
Date: Wed, 20 May 1998 08:35:34 -0400 (EDT)

Here is the function to read PGM files.  It is crude and simple, but works
great.  It can easily be modified to read in PPM and PBM.

-- 
                               Dr. David Jansing
       University of Louisville - Department of Electrical Engineering
http://archangel.spd.louisville.edu/~dave    address@hidden
------------------------------------------------------------------------------


%---------------------------------------------------
%
% The following function reads in PGM (8-bit
% unsigned) data and places it into a matrix of
% NxM size.
%
% Author:  David Jansing
% Date:    April 14, 1998
% For use with Octave
%
%---------------------------------------------------

function [image_data]=read_pgm(filename);

[fid,msg]=fopen(filename,"r","native");

if (~isempty(msg))              % Messaging problem
        error(msg);
end

if (fid==-1)                    % Can't find file
        error(sprintf('Can''t open "%s" for reading.',filename));
end

str=fgetl(fid);                 % Get the 'P5'
str=fgetl(fid);                 % Get next field (is either column/row or 
comment)

while (str(1:1)=='#')
        str=fgetl(fid);         % Eliminate the comments from the image file 
(if they exist)
end

[S_A]=split(str," ");
M=str2num(S_A(1,:));
N=str2num(S_A(2,:));
% disp("The columns are:"),disp(M)
% disp("The rows are:"),disp(N)

str=fgetl(fid);                 % Get number of intensity values (typically 255)

[raw_data,num_pixels]=fread(fid,M*N,"uchar",0,"native");
% disp("The number of pixels read in:"),disp(num_pixels)

if (num_pixels!=(M*N))
        error(sprintf('Did not read in enough pixels.'));
end

for i = 1:N
        for j = 1:M
                image_data(i,j)=raw_data((i-1)*M+j);
        end
end



reply via email to

[Prev in Thread] Current Thread [Next in Thread]