summaryrefslogtreecommitdiffstats
path: root/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadpgm.m
diff options
context:
space:
mode:
Diffstat (limited to 'SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadpgm.m')
-rwxr-xr-xSD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadpgm.m89
1 files changed, 89 insertions, 0 deletions
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadpgm.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadpgm.m
new file mode 100755
index 0000000..dfa8b61
--- /dev/null
+++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadpgm.m
@@ -0,0 +1,89 @@
1%LOADPGM Load a PGM image
2%
3% I = loadpgm(filename)
4%
5% Returns a matrix containing the image loaded from the PGM format
6% file filename. Handles ASCII (P2) and binary (P5) PGM file formats.
7%
8% If the filename has no extension, and open fails, a '.pgm' will
9% be appended.
10%
11%
12% Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab
13
14
15% Peter Corke 1994
16
17function I = loadpgm(file)
18 white = [' ' 9 10 13]; % space, tab, lf, cr
19 white = setstr(white);
20
21 fid = fopen(file, 'r');
22 if fid < 0,
23 fid = fopen([file '.pgm'], 'r');
24 end
25 if fid < 0,
26 error('Couldn''t open file');
27 end
28
29 magic = fread(fid, 2, 'char');
30 while 1
31 c = fread(fid,1,'char');
32 if c == '#',
33 fgetl(fid);
34 elseif ~any(c == white)
35 fseek(fid, -1, 'cof'); % unputc()
36 break;
37 end
38 end
39 cols = fscanf(fid, '%d', 1);
40 while 1
41 c = fread(fid,1,'char');
42 if c == '#',
43 fgetl(fid);
44 elseif ~any(c == white)
45 fseek(fid, -1, 'cof'); % unputc()
46 break;
47 end
48 end
49 rows = fscanf(fid, '%d', 1);
50 while 1
51 c = fread(fid,1,'char');
52 if c == '#',
53 fgetl(fid);
54 elseif ~any(c == white)
55 fseek(fid, -1, 'cof'); % unputc()
56 break;
57 end
58 end
59 maxval = fscanf(fid, '%d', 1);
60 while 1
61 c = fread(fid,1,'char');
62 if c == '#',
63 fgetl(fid);
64 elseif ~any(c == white)
65 fseek(fid, -1, 'cof'); % unputc()
66 break;
67 end
68 end
69 if magic(1) == 'P',
70 if magic(2) == '2',
71 %disp(['ASCII PGM file ' num2str(rows) ' x ' num2str(cols)])
72 I = fscanf(fid, '%d', [cols rows])';
73 elseif magic(2) == '5',
74 %disp(['Binary PGM file ' num2str(rows) ' x ' num2str(cols)])
75 if maxval == 1,
76 fmt = 'unint1';
77 elseif maxval == 15,
78 fmt = 'uint4';
79 elseif maxval == 255,
80 fmt = 'uint8';
81 elseif maxval == 2^32-1,
82 fmt = 'uint32';
83 end
84 I = fread(fid, [cols rows], fmt)';
85 else
86 disp('Not a PGM file');
87 end
88 end
89 fclose(fid);