-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadPGM.m
109 lines (97 loc) · 2.95 KB
/
loadPGM.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
%=================================================================================
% Advanced Multimedia Applications
% Title : Load PGM Image (loadPGM.m)
% Description: Returns a matrix containing the image loaded from the PGM format
% file filename. Handles ASCII (P2) and binary (P5) PGM file formats.
%
% If the filename has no extension, and open fails, a '.pgm' will
% be appended.
%
% Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab
% Adapted from - Peter Corke 1994
% Input : filename (image path)
% Output : I - data samples from image
% rows - number of rows
% cols - number of columns
% maxlum - maximum luminance value
%=================================================================================
function [I,rows,cols,maxlum] = loadPGM(filename)
% define whitespaces
white = [' ' 9 10 13]; % space, tab, lf, cr
white = char(white);
fid = fopen(filename, 'r');
% check if file exists
if fid < 0,
fid = fopen([filename '.pgm'], 'r');
end
if fid < 0,
error('Couldn''t open file');
end
% -- start the header extraction --
% <magic number>
magic = fread(fid, 2, 'char'); % read in magic number (assume 2 chars)
while 1
c = fread(fid,1,'char');
if c == '#',
fgetl(fid); % ignore comment line
elseif ~any(c == white)
fseek(fid, -1, 'cof'); % go back one char
break;
end
end
% <number of columns>
cols = fscanf(fid, '%d', 1); % read in columns (integer)
while 1
c = fread(fid,1,'char');
if c == '#',
fgetl(fid); % ignore comment line
elseif ~any(c == white)
fseek(fid, -1, 'cof'); % go back one char
break;
end
end
% <number of rows>
rows = fscanf(fid, '%d', 1); % read in rows (integer)
while 1
c = fread(fid,1,'char');
if c == '#',
fgetl(fid); % ignore comment line
elseif ~any(c == white)
fseek(fid, -1, 'cof'); % go back one char
break;
end
end
% <max luminance value>
maxlum = fscanf(fid, '%d', 1); % read in max grey (integer)
while 1
c = fread(fid,1,'char');
if c == '#',
fgetl(fid); % ignore comment line
elseif ~any(c == white)
fseek(fid, -1, 'cof'); % go back one char
break;
end
end
% check magic number to see which pgm version this is
% throw error if different format
if magic(1) == 'P',
if magic(2) == '2',
%disp(['ASCII PGM file ' num2str(rows) ' x ' num2str(cols)])
I = fscanf(fid, '%d', [cols rows])';
elseif magic(2) == '5',
%disp(['Binary PGM file ' num2str(rows) ' x ' num2str(cols)])
if maxlum == 1,
fmt = 'unint1';
elseif maxlum == 15,
fmt = 'uint4';
elseif maxlum == 255,
fmt = 'uint8';
elseif maxlum == 2^32-1,
fmt = 'uint32';
end
I = fread(fid, [cols rows], fmt)';
else
disp('Not a PGM file');
end
end
fclose(fid); % close file