From octave-sources-request at bevo dot che dot wisc dot edu Sun Dec 1 20:29:15 2002 Subject: Contributes ppmread.m From: matuda <0131277601 at jcom dot home dot ne dot jp> To: bug-octave at bevo dot che dot wisc dot edu Date: Sun, 1 Dec 2002 19:37:16 -0600 This is a multi-part message in MIME format. --------------94EF939452D200EF52555AAB Content-Type: text/plain; charset=iso-2022-jp Content-Transfer-Encoding: 7bit Hello dear! The "ppmread.m" function is for reading ppm foramt image files to have Octave's RGB matrixes. I tested ppm and pgm files created with ImageMagick and Netpbm. I wish this function is helpfull for all Octave users. Namio MATUDA --------------94EF939452D200EF52555AAB Content-Type: text/plain; charset=iso-2022-jp; name="ppmread.m" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="ppmread.m" ## -*- texinfo -*- ## at deftypefn {Function File} {[@var{r}, @var{g}, @var{b}] =} ppmread (@var{file}) ## Load an image file stored in ppm and pgm format from the specified ## at var{file} dot ## at end deftypefn ## at seealso{loadimage, saveimage, load, and save} ## Author: Namio Matuda ## Created: December 2002 function [R_ret, G_ret, B_ret] = ppmread (filename) if (nargin != 1) usage ("[r, g, b] = ppmread (filename)"); elseif (! isstr (filename)) error ("ppmread: expecting filename as a string"); endif file = file_in_path (IMAGEPATH, filename); if (isempty (file)) error ("ppmread: unable to find image file"); endif ## The file is assumed to have variables img and map, or X and map. ip = fopen(file, 'rb'); mg = fscanf(ip, "%s",1); iscom=fscanf(ip, "%s",1); if iscom(1) == '#'; c = 0; while c != 10 ; c=fread(ip, 1, 'uchar'); endwhile; %fread(ip, 1, 'uchar'); wsize=str2num(fscanf(ip, "%s",1)); else wsize=str2num(iscom); endif hsize=str2num(fscanf(ip, "%s",1)); maxc = str2num(fscanf(ip, "%s",1)); R=zeros(hsize,wsize); G=zeros(hsize,wsize); B=zeros(hsize,wsize); switch mg; case "P2" switch maxc; case 65535; for jh = 1:hsize RGB=fscanf(ip, "%d", wsize)'/maxc; R(jh,:)=RGB(1:wsize); G(jh,:)=R(jh,:); B(jh,:)=R(jh,:); endfor case 255; fread(ip, 1, 'uchar'); for jh = 1:hsize RGB=fscanf(ip, "%u", wsize)'/maxc; R(jh,:)=RGB(1:wsize); G(jh,:)=R(jh,:); B(jh,:)=R(jh,:); endfor endswitch case "P3" fread(ip, 1, 'uchar'); switch maxc; case 65535; for jh = 1:hsize RGB = fscanf(ip, "%d", 3*wsize)'/maxc; R(jh,:)=RGB(1:3:3*wsize-2); G(jh,:)=RGB(2:3:3*wsize-1); B(jh,:)=RGB(3:3:3*wsize); endfor case 255; for jh = 1:hsize RGB = fscanf(ip, "%u", 3*wsize)'/maxc; R(jh,:)=RGB(1:3:3*wsize-2); G(jh,:)=RGB(2:3:3*wsize-1); B(jh,:)=RGB(3:3:3*wsize); endfor endswitch case "P5" switch maxc; case 65535; for jh = 1:hsize RGB=fread(ip, wsize, 'uint')'/maxc; R(jh,:)=RGB(1:wsize); G(jh,:)=R(jh,:); B(jh,:)=R(jh,:); endfor case 255; fread(ip, 1, 'uchar'); for jh = 1:hsize RGB=fread(ip, wsize, 'uchar')'/maxc; R(jh,:)=RGB(1:wsize); G(jh,:)=R(jh,:); B(jh,:)=R(jh,:); endfor endswitch case "P6" switch maxc; case 65535; for jh = 1:hsize RGB = fread(ip, 3*wsize, 'ushort')'/maxc; R(jh,:)=RGB(1:3:3*wsize-2); G(jh,:)=RGB(2:3:3*wsize-1); B(jh,:)=RGB(3:3:3*wsize); endfor case 255; fread(ip, 1, 'uchar'); for jh = 1:hsize RGB = fread(ip, 3*wsize, 'uchar')'/maxc; R(jh,:)=RGB(1:3:3*wsize-2); G(jh,:)=RGB(2:3:3*wsize-1); B(jh,:)=RGB(3:3:3*wsize); endfor endswitch endswitch fclose(ip); R_ret=R; G_ret=G; B_ret=B; endfunction --------------94EF939452D200EF52555AAB--