-
Notifications
You must be signed in to change notification settings - Fork 0
/
getpgmraw.m
69 lines (53 loc) · 1014 Bytes
/
getpgmraw.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
% getpgmraw.m
% -----------
%
% function [pic,maxgray]=getpgmraw(filename)
%
%
% AUTHOR: A.S. Georghiades
% DATE: April 28, 1997
% PURPOSE:
% Reads gray scale PGM (raw) images.
%
% ARGUMENTS:
% filename: A string containing the name of the image file to be read.
%
% RETURNS:
% pic: The gray scale image in an array.
% maxgray: The maximum pixel value in pic. Usually 255.
%
% (No dependancies)
%
function [pic,maxgray]=getpgmraw(filename)
%% Open file.
%
fid=fopen(filename);
%% If not PGM then exit with an error message
%.
code=fscanf(fid,'%s',1);
if (code ~= 'P5')
error('Not a PGM (raw) image');
end
%% Get width.
%
width=[];
while (isempty(width))
[width,cnt]=fscanf(fid,'%d',1);
if (cnt==0)
fgetl(fid);
end
end
%% Get height.
%
height=fscanf(fid,'%d',1);
%% Get max gray value.
%
maxgray=fscanf(fid,'%d',1);
%% Read actual data.
%
cnt=fread(fid,1); % newline
pic=fread(fid,[width,height])';
%% Close file.
%
fclose(fid);
%%%%