From: Arnar Mar Hrafnkelsson Date: Wed, 6 Jun 2001 17:28:30 +0000 (+0000) Subject: Replaced i_readraw() and i_writeraw() with the equivalent _wiol functions. X-Git-Tag: Imager-0.48^2~609 X-Git-Url: http://git.imager.perl.org/imager.git/commitdiff_plain/895dbd343d71e643a738e72444dac3ce2506463b Replaced i_readraw() and i_writeraw() with the equivalent _wiol functions. --- diff --git a/Imager.pm b/Imager.pm index 08366682..58d31124 100644 --- a/Imager.pm +++ b/Imager.pm @@ -100,8 +100,8 @@ use Imager::Font; i_readpnm_wiol i_writeppm_wiol - i_readraw - i_writeraw + i_readraw_wiol + i_writeraw_wiol i_contrast i_hardinvert @@ -443,119 +443,172 @@ sub read { undef($self->{IMG}); } - if (!$input{fd} and !$input{file} and !$input{data}) { $self->{ERRSTR}='no file, fd or data parameter'; return undef; } + if (!$input{fd} and !$input{file} and !$input{data}) { + $self->{ERRSTR}='no file, fd or data parameter'; return undef; + } if ($input{file}) { $fh = new IO::File($input{file},"r"); - if (!defined $fh) { $self->{ERRSTR}='Could not open file'; return undef; } + if (!defined $fh) { + $self->{ERRSTR}='Could not open file'; return undef; + } binmode($fh); $fd = $fh->fileno(); } - if ($input{fd}) { $fd=$input{fd} }; + if ($input{fd}) { + $fd=$input{fd}; + } # FIXME: Find the format here if not specified # yes the code isn't here yet - next week maybe? - if (!$input{type} and $input{file}) { $input{type}=$FORMATGUESS->($input{file}); } - if (!$formats{$input{type}}) { $self->{ERRSTR}='format not supported'; return undef; } + if (!$input{type} and $input{file}) { + $input{type}=$FORMATGUESS->($input{file}); + } + if (!$formats{$input{type}}) { + $self->{ERRSTR}='format not supported'; return undef; + } - my %iolready=(jpeg=>1, tiff=>1, pnm=>1); + my %iolready=(jpeg=>1, tiff=>1, pnm=>1, raw=>1); if ($iolready{$input{type}}) { # Setup data source - $IO = io_new_fd($fd); # sort of simple for now eh? + $IO = io_new_fd($fd); # sort of simple for now eh? if ( $input{type} eq 'jpeg' ) { ($self->{IMG},$self->{IPTCRAW})=i_readjpeg_wiol( $IO ); - if ( !defined($self->{IMG}) ) { $self->{ERRSTR}='unable to read jpeg image'; return undef; } + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}='unable to read jpeg image'; return undef; + } $self->{DEBUG} && print "loading a jpeg file\n"; return $self; } if ( $input{type} eq 'tiff' ) { $self->{IMG}=i_readtiff_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed - if ( !defined($self->{IMG}) ) { $self->{ERRSTR}='unable to read tiff image'; return undef; } + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}='unable to read tiff image'; return undef; + } $self->{DEBUG} && print "loading a tiff file\n"; return $self; } if ( $input{type} eq 'pnm' ) { $self->{IMG}=i_readpnm_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed - if ( !defined($self->{IMG}) ) { $self->{ERRSTR}='unable to read pnm image: '._error_as_msg(); return undef; } + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}='unable to read pnm image: '._error_as_msg(); return undef; + } $self->{DEBUG} && print "loading a pnm file\n"; return $self; } - } else { + if ( $input{type} eq 'raw' ) { + my %params=(datachannels=>3,storechannels=>3,interleave=>1,%input); - # Old code for reference while changing the new stuff + if ( !($params{xsize} && $params{ysize}) ) { + $self->{ERRSTR}='missing xsize or ysize parameter for raw'; + return undef; + } + + $self->{IMG} = i_readraw_wiol( $IO, + $params{xsize}, + $params{ysize}, + $params{datachannels}, + $params{storechannels}, + $params{interleave}); + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}='unable to read raw image'; + return undef; + } + $self->{DEBUG} && print "loading a raw file\n"; + } + } else { + # Old code for reference while changing the new stuff - if (!$input{type} and $input{file}) { $input{type}=$FORMATGUESS->($input{file}); } - if (!$input{type}) { $self->{ERRSTR}='type parameter missing and not possible to guess from extension'; return undef; } - if (!$formats{$input{type}}) { $self->{ERRSTR}='format not supported'; return undef; } + if (!$input{type} and $input{file}) { + $input{type}=$FORMATGUESS->($input{file}); + } - if ($input{file}) { - $fh = new IO::File($input{file},"r"); - if (!defined $fh) { $self->{ERRSTR}='Could not open file'; return undef; } - binmode($fh); - $fd = $fh->fileno(); - } - if ($input{fd}) { $fd=$input{fd} }; + if (!$input{type}) { + $self->{ERRSTR}='type parameter missing and not possible to guess from extension'; return undef; + } - if ( $input{type} eq 'gif' ) { - my $colors; - if ($input{colors} && !ref($input{colors})) { - # must be a reference to a scalar that accepts the colour map - $self->{ERRSTR} = "option 'colors' must be a scalar reference"; + if (!$formats{$input{type}}) { + $self->{ERRSTR}='format not supported'; return undef; } - if (exists $input{data}) { - if ($input{colors}) { - ($self->{IMG}, $colors) = i_readgif_scalar($input{data}); - } - else { - $self->{IMG}=i_readgif_scalar($input{data}); + + if ($input{file}) { + $fh = new IO::File($input{file},"r"); + if (!defined $fh) { + $self->{ERRSTR}='Could not open file'; + return undef; } + binmode($fh); + $fd = $fh->fileno(); + } + + if ($input{fd}) { + $fd=$input{fd}; } - else { - if ($input{colors}) { - ($self->{IMG}, $colors) = i_readgif( $fd ); + + if ( $input{type} eq 'gif' ) { + my $colors; + if ($input{colors} && !ref($input{colors})) { + # must be a reference to a scalar that accepts the colour map + $self->{ERRSTR} = "option 'colors' must be a scalar reference"; + return undef; } - else { - $self->{IMG} = i_readgif( $fd ) + if (exists $input{data}) { + if ($input{colors}) { + ($self->{IMG}, $colors) = i_readgif_scalar($input{data}); + } else { + $self->{IMG}=i_readgif_scalar($input{data}); + } + } else { + if ($input{colors}) { + ($self->{IMG}, $colors) = i_readgif( $fd ); + } else { + $self->{IMG} = i_readgif( $fd ) + } } + if ($colors) { + # we may or may not change i_readgif to return blessed objects... + ${ $input{colors} } = [ map { NC(@$_) } @$colors ]; + } + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}= 'reading GIF:'._error_as_msg(); + return undef; + } + $self->{DEBUG} && print "loading a gif file\n"; + + + } elsif ( $input{type} eq 'jpeg' ) { + if (exists $input{data}) { + ($self->{IMG},$self->{IPTCRAW})=i_readjpeg_scalar($input{data}); + } else { + ($self->{IMG},$self->{IPTCRAW})=i_readjpeg( $fd ); + } + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}='unable to read jpeg image'; + return undef; + } + $self->{DEBUG} && print "loading a jpeg file\n"; + } elsif ( $input{type} eq 'png' ) { + if (exists $input{data}) { + $self->{IMG}=i_readpng_scalar($input{data}); + } else { + $self->{IMG}=i_readpng( $fd ); + } + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}='unable to read png image'; + return undef; + } + $self->{DEBUG} && print "loading a png file\n"; } - if ($colors) { - # we may or may not change i_readgif to return blessed objects... - ${$input{colors}} = [ map { NC(@$_) } @$colors ]; - } - if ( !defined($self->{IMG}) ) { - $self->{ERRSTR}= 'reading GIF:'._error_as_msg(); return undef; - } - $self->{DEBUG} && print "loading a gif file\n"; - } elsif ( $input{type} eq 'jpeg' ) { - if (exists $input{data}) { ($self->{IMG},$self->{IPTCRAW})=i_readjpeg_scalar($input{data}); } - else { ($self->{IMG},$self->{IPTCRAW})=i_readjpeg( $fd ); } - if ( !defined($self->{IMG}) ) { $self->{ERRSTR}='unable to read jpeg image'; return undef; } - $self->{DEBUG} && print "loading a jpeg file\n"; - } elsif ( $input{type} eq 'png' ) { - if (exists $input{data}) { $self->{IMG}=i_readpng_scalar($input{data}); } - else { $self->{IMG}=i_readpng( $fd ); } - if ( !defined($self->{IMG}) ) { $self->{ERRSTR}='unable to read png image'; return undef; } - $self->{DEBUG} && print "loading a png file\n"; - } elsif ( $input{type} eq 'raw' ) { - my %params=(datachannels=>3,storechannels=>3,interleave=>1); - for(keys(%input)) { $params{$_}=$input{$_}; } - - if ( !($params{xsize} && $params{ysize}) ) { $self->{ERRSTR}='missing xsize or ysize parameter for raw'; return undef; } - $self->{IMG}=i_readraw( $fd, $params{xsize}, $params{ysize}, - $params{datachannels}, $params{storechannels}, $params{interleave}); - if ( !defined($self->{IMG}) ) { $self->{ERRSTR}='unable to read raw image'; return undef; } - $self->{DEBUG} && print "loading a raw file\n"; } return $self; - } } diff --git a/Imager.xs b/Imager.xs index 13bed7d4..fe09613e 100644 --- a/Imager.xs +++ b/Imager.xs @@ -1342,13 +1342,13 @@ i_readgif(fd) colour_table = NULL; colours = 0; - if(GIMME_V == G_ARRAY) { + if(GIMME_V == G_ARRAY) { rimg = i_readgif(fd,&colour_table,&colours); } else { /* don't waste time with colours if they aren't wanted */ rimg = i_readgif(fd,NULL,NULL); } - + if (colour_table == NULL) { EXTEND(SP,1); r=sv_newmortal(); @@ -1368,9 +1368,9 @@ i_readgif(fd) av_store(ct, q, (SV*)newRV_noinc((SV*)av_make(3, temp))); } myfree(colour_table); - + EXTEND(SP,2); - r=sv_newmortal(); + r = sv_newmortal(); sv_setref_pv(r, "Imager::ImgRaw", (void*)rimg); PUSHs(r); PUSHs(newRV_noinc((SV*)ct)); @@ -1507,8 +1507,8 @@ i_writeppm_wiol(im, ig) Imager::ImgRaw -i_readraw(fd,x,y,datachannels,storechannels,intrl) - int fd +i_readraw_wiol(ig,x,y,datachannels,storechannels,intrl) + Imager::IO ig int x int y int datachannels @@ -1516,9 +1516,10 @@ i_readraw(fd,x,y,datachannels,storechannels,intrl) int intrl undef_int -i_writeraw(im,fd) +i_writeraw_wiol(im,ig) Imager::ImgRaw im - int fd + Imager::IO ig + Imager::ImgRaw diff --git a/STATUS b/STATUS index 5aeb061e..27cbc475 100644 --- a/STATUS +++ b/STATUS @@ -25,6 +25,15 @@ stackmach.c # no comments quant.c # comments, lots of changing code - +===================== + +Iolayer todo list: +Format Read/Write +Gif: O O +png: O O +raw: O O +jpeg: X O +tiff: X X +pnm: X X diff --git a/gif.c b/gif.c index c26b7acc..22ca044b 100644 --- a/gif.c +++ b/gif.c @@ -165,8 +165,7 @@ i_readgif_low(GifFileType *GifFile, int **colour_table, int *colours) { non-NULL, but we check that to see if we need to free an allocated colour table on error. */ - if (colour_table) - *colour_table = NULL; + if (colour_table) *colour_table = NULL; BackGround = GifFile->SBackGroundColor; ColorMap = (GifFile->Image.ColorMap ? GifFile->Image.ColorMap : GifFile->SColorMap); @@ -178,7 +177,7 @@ i_readgif_low(GifFileType *GifFile, int **colour_table, int *colours) { } - im = i_img_empty_ch(NULL,GifFile->SWidth,GifFile->SHeight,3); + im = i_img_empty_ch(NULL, GifFile->SWidth, GifFile->SHeight, 3); Size = GifFile->SWidth * sizeof(GifPixelType); @@ -237,7 +236,7 @@ i_readgif_low(GifFileType *GifFile, int **colour_table, int *colours) { Width = GifFile->Image.Width; Height = GifFile->Image.Height; ImageNum++; - mm_log((1,"i_readgif: Image %d at (%d, %d) [%dx%d]: \n",ImageNum, Col, Row, Width, Height)); + mm_log((1,"i_readgif_low: Image %d at (%d, %d) [%dx%d]: \n",ImageNum, Col, Row, Width, Height)); if (GifFile->Image.Left + GifFile->Image.Width > GifFile->SWidth || GifFile->Image.Top + GifFile->Image.Height > GifFile->SHeight) { diff --git a/image.h b/image.h index 1333fe19..d0baa900 100644 --- a/image.h +++ b/image.h @@ -387,8 +387,8 @@ void i_qdist(i_img *im); #endif /* HAVE_LIBGIF */ -i_img *i_readraw(int fd,int x,int y,int datachannels,int storechannels,int intrl); -undef_int i_writeraw(i_img* im,int fd); +i_img *i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, int intrl); +undef_int i_writeraw_wiol(i_img* im, io_glue *ig); i_img *i_readpnm_wiol(io_glue *ig, int length); undef_int i_writeppm(i_img *im,int fd); diff --git a/raw.c b/raw.c index 56977e79..b7d8e3d8 100644 --- a/raw.c +++ b/raw.c @@ -1,10 +1,12 @@ #include "image.h" #include +#include "iolayer.h" #ifndef _MSC_VER #include #endif #include + #define TRUE 1 #define FALSE 0 @@ -36,7 +38,7 @@ expandchannels(unsigned char *inbuffer,unsigned char *outbuffer,int chunks,int d } i_img * -i_readraw(int fd,int x,int y,int datachannels,int storechannels,int intrl) { +i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, int intrl) { i_img* im; int rc,k; @@ -46,14 +48,16 @@ i_readraw(int fd,int x,int y,int datachannels,int storechannels,int intrl) { int inbuflen,ilbuflen,exbuflen; - mm_log((1,"readraw(fd %d,x %d,y %d,datachannels %d,storechannels %d,intrl %d)\n",fd,x,y,datachannels,storechannels,intrl)); + io_glue_commit_types(ig); + mm_log((1, "i_readraw(ig %p,x %d,y %d,datachannels %d,storechannels %d,intrl %d)\n", + ig, x, y, datachannels, storechannels, intrl)); - im=i_img_empty_ch(NULL,x,y,storechannels); + im = i_img_empty_ch(NULL,x,y,storechannels); - inbuflen=im->xsize*datachannels; - ilbuflen=inbuflen; - exbuflen=im->xsize*storechannels; - inbuffer=(unsigned char*)mymalloc(inbuflen); + inbuflen = im->xsize*datachannels; + ilbuflen = inbuflen; + exbuflen = im->xsize*storechannels; + inbuffer = (unsigned char*)mymalloc(inbuflen); mm_log((1,"inbuflen: %d, ilbuflen: %d, exbuflen: %d.\n",inbuflen,ilbuflen,exbuflen)); if (intrl==0) ilbuffer=inbuffer; else ilbuffer=(unsigned char*)mymalloc(inbuflen); @@ -61,7 +65,7 @@ i_readraw(int fd,int x,int y,int datachannels,int storechannels,int intrl) { k=0; while(kysize) { - rc=myread(fd,inbuffer,inbuflen); + rc = ig->readcb(ig, inbuffer, inbuflen); if (rc!=inbuflen) { fprintf(stderr,"Premature end of file.\n"); exit(2); } interleave(inbuffer,ilbuffer,im->xsize,datachannels); expandchannels(ilbuffer,exbuffer,im->xsize,datachannels,storechannels); @@ -78,13 +82,17 @@ i_readraw(int fd,int x,int y,int datachannels,int storechannels,int intrl) { undef_int -i_writeraw(i_img* im,int fd) { +i_writeraw_wiol(i_img* im, io_glue *ig) { int rc; - mm_log((1,"writeraw(im 0x%x,fd %d)\n",im,fd)); + io_glue_commit_types(ig); + mm_log((1,"writeraw(im %p,ig %p)\n", im, ig)); if (im == NULL) { mm_log((1,"Image is empty\n")); return(0); } - rc=mywrite(fd,im->data,im->bytes); - if (rc!=im->bytes) { mm_log((1,"i_writeraw: Couldn't write to file\n")); return(0); } + rc = ig->writecb(ig, im->data, im->bytes); + if (rc != im->bytes) { + mm_log((1,"i_writeraw: Couldn't write to file\n")); + return(0); + } return(1); } diff --git a/t/t103raw.t b/t/t103raw.t index 1c20fa9e..bd056278 100644 --- a/t/t103raw.t +++ b/t/t103raw.t @@ -22,14 +22,16 @@ i_box_filled($timg, 2, 2, 18, 18, $trans); open(FH,">testout/t103.raw") || die "Cannot open testout/t103.raw for writing\n"; binmode(FH); -i_writeraw($img,fileno(FH)) || die "Cannot write testout/t103.raw\n"; +$IO = Imager::io_new_fd( fileno(FH) ); +i_writeraw_wiol($img, $IO) || die "Cannot write testout/t103.raw\n"; close(FH); print "ok 1\n"; open(FH,"testout/t103.raw") || die "Cannot open testout/t103.raw\n"; binmode(FH); -$cmpimg=i_readraw(fileno(FH),150,150,3,3,0) || die "Cannot read testout/t103.raw\n"; +$IO = Imager::io_new_fd( fileno(FH) ); +$cmpimg = i_readraw_wiol($IO, 150, 150, 3, 3, 0) || die "Cannot read testout/t103.raw\n"; close(FH); print "# raw average mean square pixel difference: ",sqrt(i_img_diff($img,$cmpimg))/150*150,"\n"; @@ -48,7 +50,9 @@ save_data('testout/t103_img_int.raw'); open FH, "testout/t103_base.raw" or die "Cannot open testout/t103_base.raw: $!"; binmode FH; -my $baseimg = i_readraw(fileno(FH), 4, 4, 3, 3, 0) +$IO = Imager::io_new_fd( fileno(FH) ); + +my $baseimg = i_readraw_wiol( $IO, 4, 4, 3, 3, 0) or die "Cannot read base raw image"; close FH; @@ -65,7 +69,9 @@ sub read_test { my ($in, $xsize, $ysize, $data, $store, $intrl, $base, $test) = @_; open FH, $in or die "Cannot open $in: $!"; binmode FH; - my $img = i_readraw(fileno(FH), $xsize, $ysize, $data, $store, $intrl); + my $IO = Imager::io_new_fd( fileno(FH) ); + + my $img = i_readraw_wiol($IO, $xsize, $ysize, $data, $store, $intrl); if ($img) { print "ok $test\n"; if (i_img_diff($img, $baseimg)) { diff --git a/t/t50basicoo.t b/t/t50basicoo.t index c9ab551f..4da6d08c 100644 --- a/t/t50basicoo.t +++ b/t/t50basicoo.t @@ -42,8 +42,8 @@ for $type (@types) { $img->read( %opts ) or die "failed: ",$img->errstr,"\n"; } -$img2=$img->crop(width=>50, height=>50); -$img2->write(file=> 'testout/t50.ppm', type=>'pnm'); +$img2 = $img->crop(width=>50, height=>50); +$img2 -> write(file=> 'testout/t50.ppm', type=>'pnm'); undef($img); diff --git a/tiff.c b/tiff.c index 55ffafce..54955431 100644 --- a/tiff.c +++ b/tiff.c @@ -82,7 +82,7 @@ i_readtiff_wiol(io_glue *ig, int length) { /* Also add code to check for mmapped code */ io_glue_commit_types(ig); - mm_log((1, "i_readtiff_wiol(ig 0x%p, length %d)\n", ig, length)); + mm_log((1, "i_readtiff_wiol(ig %p, length %d)\n", ig, length)); tif = TIFFClientOpen("Iolayer: FIXME", "rm",