]> git.imager.perl.org - imager.git/commitdiff
Replaced i_readraw() and i_writeraw() with the equivalent _wiol functions.
authorArnar Mar Hrafnkelsson <addi@cpan.org>
Wed, 6 Jun 2001 17:28:30 +0000 (17:28 +0000)
committerArnar Mar Hrafnkelsson <addi@cpan.org>
Wed, 6 Jun 2001 17:28:30 +0000 (17:28 +0000)
Imager.pm
Imager.xs
STATUS
gif.c
image.h
raw.c
t/t103raw.t
t/t50basicoo.t
tiff.c

index 08366682813c724455685afacee795e454637fc3..58d31124db2e512496fb43058bc2bdd9440416ff 100644 (file)
--- 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;
-  }
 }
 
 
index 13bed7d4df90f45acaccad1859babf10cc1399df..fe09613e4bf43b7232f2c9f974603d0bc0a75a0f 100644 (file)
--- 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 5aeb061eb623ea53ce58b4b7a8eacfeebedd1b35..27cbc47569066a47b00d1c45e51bf9d81ae962eb 100644 (file)
--- 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 c26b7acc6a5ed4fd8ec5759e4f82fc0846e32d65..22ca044b3e67c815c557cb1f8d9e18981f431a75 100644 (file)
--- 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 1333fe191f67e6237f1348a413dee37d7df5ebb3..d0baa900cac08f2ec770983af29fb307dc8b5249 100644 (file)
--- 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 56977e7985f2f6e89e723b094e1183cc45cf3e11..b7d8e3d8930ee620fab293d5b3d80506d3b636f6 100644 (file)
--- a/raw.c
+++ b/raw.c
@@ -1,10 +1,12 @@
 #include "image.h"
 #include <stdio.h>
+#include "iolayer.h"
 #ifndef _MSC_VER
 #include <unistd.h>
 #endif
 #include <string.h>
 
+
 #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(k<im->ysize) {
-    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);
 }
 
index 1c20fa9e7891a13e0966c84966165770ed8201fa..bd056278423e1794d008cab02d7b118570c36bd9 100644 (file)
@@ -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)) {
index c9ab551f244806c3ae589fdd4f7394543651f81f..4da6d08c5d7c2e99c3da49201a617b83fcf8f9e3 100644 (file)
@@ -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 55ffafce846ebef0f3d6d94a1e03bcabc709f3ca..54955431547264abac0115554a914dbc55fd71a2 100644 (file)
--- 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",