diff options
Diffstat (limited to 'SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadppm.m')
| -rwxr-xr-x | SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadppm.m | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadppm.m b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadppm.m new file mode 100755 index 0000000..0c004fc --- /dev/null +++ b/SD-VBS/common/toolbox/toolbox_basic/TOOLBOX_calib/loadppm.m | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | %LOADPPM Load a PPM image | ||
| 2 | % | ||
| 3 | % I = loadppm(filename) | ||
| 4 | % | ||
| 5 | % Returns a matrix containing the image loaded from the PPM format | ||
| 6 | % file filename. Handles ASCII (P3) and binary (P6) PPM file formats. | ||
| 7 | % | ||
| 8 | % If the filename has no extension, and open fails, a '.ppm' and | ||
| 9 | % '.pnm' extension will be tried. | ||
| 10 | % | ||
| 11 | % SEE ALSO: saveppm loadpgm | ||
| 12 | % | ||
| 13 | % Copyright (c) Peter Corke, 1999 Machine Vision Toolbox for Matlab | ||
| 14 | |||
| 15 | |||
| 16 | % Peter Corke 1994 | ||
| 17 | |||
| 18 | function I = loadppm(file) | ||
| 19 | white = [' ' 9 10 13]; % space, tab, lf, cr | ||
| 20 | white = setstr(white); | ||
| 21 | |||
| 22 | fid = fopen(file, 'r'); | ||
| 23 | if fid < 0, | ||
| 24 | fid = fopen([file '.ppm'], 'r'); | ||
| 25 | end | ||
| 26 | if fid < 0, | ||
| 27 | fid = fopen([file '.pnm'], 'r'); | ||
| 28 | end | ||
| 29 | if fid < 0, | ||
| 30 | error('Couldn''t open file'); | ||
| 31 | end | ||
| 32 | |||
| 33 | magic = fread(fid, 2, 'char'); | ||
| 34 | while 1 | ||
| 35 | c = fread(fid,1,'char'); | ||
| 36 | if c == '#', | ||
| 37 | fgetl(fid); | ||
| 38 | elseif ~any(c == white) | ||
| 39 | fseek(fid, -1, 'cof'); % unputc() | ||
| 40 | break; | ||
| 41 | end | ||
| 42 | end | ||
| 43 | cols = fscanf(fid, '%d', 1); | ||
| 44 | while 1 | ||
| 45 | c = fread(fid,1,'char'); | ||
| 46 | if c == '#', | ||
| 47 | fgetl(fid); | ||
| 48 | elseif ~any(c == white) | ||
| 49 | fseek(fid, -1, 'cof'); % unputc() | ||
| 50 | break; | ||
| 51 | end | ||
| 52 | end | ||
| 53 | rows = fscanf(fid, '%d', 1); | ||
| 54 | while 1 | ||
| 55 | c = fread(fid,1,'char'); | ||
| 56 | if c == '#', | ||
| 57 | fgetl(fid); | ||
| 58 | elseif ~any(c == white) | ||
| 59 | fseek(fid, -1, 'cof'); % unputc() | ||
| 60 | break; | ||
| 61 | end | ||
| 62 | end | ||
| 63 | maxval = fscanf(fid, '%d', 1); | ||
| 64 | while 1 | ||
| 65 | c = fread(fid,1,'char'); | ||
| 66 | if c == '#', | ||
| 67 | fgetl(fid); | ||
| 68 | elseif ~any(c == white) | ||
| 69 | fseek(fid, -1, 'cof'); % unputc() | ||
| 70 | break; | ||
| 71 | end | ||
| 72 | end | ||
| 73 | if magic(1) == 'P', | ||
| 74 | if magic(2) == '3', | ||
| 75 | %disp(['ASCII PPM file ' num2str(rows) ' x ' num2str(cols)]) | ||
| 76 | I = fscanf(fid, '%d', [cols*3 rows]); | ||
| 77 | elseif magic(2) == '6', | ||
| 78 | %disp(['Binary PPM file ' num2str(rows) ' x ' num2str(cols)]) | ||
| 79 | if maxval == 1, | ||
| 80 | fmt = 'unint1'; | ||
| 81 | elseif maxval == 15, | ||
| 82 | fmt = 'uint4'; | ||
| 83 | elseif maxval == 255, | ||
| 84 | fmt = 'uint8'; | ||
| 85 | elseif maxval == 2^32-1, | ||
| 86 | fmt = 'uint32'; | ||
| 87 | end | ||
| 88 | I = fread(fid, [cols*3 rows], fmt); | ||
| 89 | else | ||
| 90 | disp('Not a PPM file'); | ||
| 91 | end | ||
| 92 | end | ||
| 93 | % | ||
| 94 | % now the matrix has interleaved columns of R, G, B | ||
| 95 | % | ||
| 96 | I = I'; | ||
| 97 | size(I); | ||
| 98 | R = I(:,1:3:(cols*3)); | ||
| 99 | G = I(:,2:3:(cols*3)); | ||
| 100 | B = I(:,3:3:(cols*3)); | ||
| 101 | fclose(fid); | ||
| 102 | |||
| 103 | |||
| 104 | I = zeros(rows,cols,3); | ||
| 105 | I(:,:,1) = R; | ||
| 106 | I(:,:,2) = G; | ||
| 107 | I(:,:,3) = B; | ||
| 108 | I = uint8(I); | ||
| 109 | \ No newline at end of file | ||
