help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: load image in different format


From: Joao Cardoso
Subject: Re: load image in different format
Date: Tue, 06 Jun 2000 17:24:59 +0100

Stephen Eglen wrote:
> 
> Hi, a similar request was made a couple of months ago.  I suggested:
> 
> ----------------------------------------------------------------------
> > Does it exists m files to read BMP, TIFF or GIF files into Matlab ?
> > Thanks for help
> 
> I'm not aware of any octave/matlab implementations at the moment.  If

Some year ago (1996!) I have done it, using giftoppm etc to convert the
images to ppm and scanning the resulting files in octave. Without any
warranty (as usual) I enclose then.


Joao
function gray = gif2gray(file)

o_file = tmpnam;
system(sprintf("giftoppm %s > %s",file, o_file));

gray = ppm2gray(o_file);

system(sprintf("rm -f %s", o_file));
endfunction
function gray = ppm2gray(file)

# ppm2gray: raw/ascii ppm picture to gray image format
#
# xv-3.10a stores each header line on a separate line, \n separated
# giftoppm is identical to xv
# in xloadimage-4.0 there is only one header line, parameters separated by space
# xwpick-2.10 also
# octave... should really use 'octtopnm', its much faster for _real_ pictures...
# all use P6/P3 as ppm format.

fp=fopen(file,"r");
ver=fscanf(fp,"%s%*c","C");     # get version

a='#';                                          # get rid of xv comments
while (a(1,1) == '#')
        pos = ftell(fp);
        a=fgetl(fp);
endwhile

fseek(fp,pos);
[xx yy max_c] = fscanf(fp,"%d%d%d","C");        # get number of x and y pixels

if ( ver(1,2) == '6' )                                  # raw format
        a=fread(fp,[xx*3 yy],"uchar");          # read data
elseif (ver(1,2) == '3' )                               # ascii format
        a = fscanf(fp,"%d", [xx*3 yy]);
#       a = fscanf(fp,"%d");                            # octave hangup!
else
        fclose(fp);
        error("ppm2gray: only process ascii/rawbit, P3/P6, format files")
        return
endif
        
        r=a(1:3:xx*3,:);
        g=a(2:3:xx*3,:);
        b=a(3:3:xx*3,:);
        
        gray = (r.+g.+b)'./(3*max_c);

fclose(fp);

endfunction
function [r g b] = ppm2rgb(file)

# ppm2oct: raw ppm picture to gray image format
#
# xv-3.10a stores each header line on a separate line, \n separated
# giftoppm is identical to xv
# in xloadimage-4.0 there is only one header line, parameters separated by space
# xwpick-2.10 also
# octave ...
# all use P6 as ppm format.

fp=fopen(file,"r");
ver=fscanf(fp,"%s%*c","C");     # get version
if ( ver(1,2) != '6' )
        error("ppm2oct: only process rawbit, P6, format files")
        return
endif

a='#';                                  # get rid of xv comments
while (a(1,1) == '#')
        pos = ftell(fp);
        a=fgetl(fp);
endwhile

fseek(fp,pos);
[xx yy max_c] = fscanf(fp,"%d%d%d","C");        # get number of x and y pixels

a=fread(fp,[xx*3 yy],"uchar");          # read data

r=a(1:3:xx*3,:)'./max_c;
g=a(2:3:xx*3,:)'./max_c;
b=a(3:3:xx*3,:)'./max_c;

fclose(fp);

endfunction

reply via email to

[Prev in Thread] Current Thread [Next in Thread]