Updated png to use _wlio functions, and fixed a filename error for ppm test.
authorArnar Mar Hrafnkelsson <addi@cpan.org>
Thu, 7 Jun 2001 07:50:00 +0000 (07:50 +0000)
committerArnar Mar Hrafnkelsson <addi@cpan.org>
Thu, 7 Jun 2001 07:50:00 +0000 (07:50 +0000)
Imager.pm
Imager.xs
STATUS
image.h
png.c
t/t102png.t
t/t104ppm.t

index 58d31124db2e512496fb43058bc2bdd9440416ff..dd24e30274e2a3dd0954fa6282f31ebc4c411cea 100644 (file)
--- a/Imager.pm
+++ b/Imager.pm
@@ -87,8 +87,8 @@ use Imager::Font;
                i_writetiff_wiol
                i_writetiff_wiol_faxable
 
-               i_readpng
-               i_writepng
+               i_readpng_wiol
+               i_writepng_wiol
 
                i_readgif
                i_readgif_callback
@@ -468,7 +468,7 @@ sub read {
     $self->{ERRSTR}='format not supported'; return undef;
   }
 
-  my %iolready=(jpeg=>1, tiff=>1, pnm=>1, raw=>1);
+  my %iolready=(jpeg=>1, png=>1, tiff=>1, pnm=>1, raw=>1);
 
   if ($iolready{$input{type}}) {
     # Setup data source
@@ -501,6 +501,15 @@ sub read {
       return $self;
     }
 
+    if ( $input{type} eq 'png' ) {
+      $self->{IMG}=i_readpng_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed
+      if ( !defined($self->{IMG}) ) {
+       $self->{ERRSTR}='unable to read png image';
+       return undef;
+      }
+      $self->{DEBUG} && print "loading a png file\n";
+    }
+
     if ( $input{type} eq 'raw' ) {
       my %params=(datachannels=>3,storechannels=>3,interleave=>1,%input);
 
@@ -521,6 +530,7 @@ sub read {
       }
       $self->{DEBUG} && print "loading a raw file\n";
     }
+
   } else {
 
     # Old code for reference while changing the new stuff
@@ -595,17 +605,6 @@ sub read {
        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";
     }
   }
   return $self;
index fe09613e4bf43b7232f2c9f974603d0bc0a75a0f..f0cf8a7f1057f09d4c3860f81a3be8783a6deaae 100644 (file)
--- a/Imager.xs
+++ b/Imager.xs
@@ -1139,28 +1139,15 @@ i_writetiff_wiol_faxable(im, ig, fine)
 #ifdef HAVE_LIBPNG
 
 Imager::ImgRaw
-i_readpng(fd)
-              int     fd
+i_readpng_wiol(ig, length)
+        Imager::IO     ig
+              int     length
 
 
 undef_int
-i_writepng(im,fd)
+i_writepng_wiol(im, ig)
     Imager::ImgRaw     im
-              int     fd
-
-
-Imager::ImgRaw
-i_readpng_scalar(...)
-          PROTOTYPE: $
-            PREINIT:
-             char*    data;
-      unsigned int     length;
-              CODE:
-              data = (char *)SvPV(ST(0), length);
-             RETVAL=i_readpng_scalar(data,length);
-             OUTPUT:
-              RETVAL
-       
+        Imager::IO     ig
 
 
 #endif
diff --git a/STATUS b/STATUS
index 27cbc47569066a47b00d1c45e51bf9d81ae962eb..bc531887d006fddedb749ed3bcb471c1f6a03fe5 100644 (file)
--- a/STATUS
+++ b/STATUS
@@ -29,10 +29,10 @@ quant.c               # comments, lots of changing code
 
 Iolayer todo list:
 Format Read/Write
-Gif:    O    O
-png:    O    O 
-raw:    O    O
+gif:    O    O
 jpeg:   X    O
+png:    X    X
+raw:    X    X
 tiff:   X    X
 pnm:    X    X
 
diff --git a/image.h b/image.h
index d0baa900cac08f2ec770983af29fb307dc8b5249..e5fd555ca0971a0e344b017104ef0cd72bca8ff3 100644 (file)
--- a/image.h
+++ b/image.h
@@ -355,7 +355,6 @@ extern void quant_transparent(i_quantize *quant, i_palidx *indices, i_img *img,
 i_img* i_readjpeg(int fd,char** iptc_itext,int *tlength);
 i_img* i_readjpeg_scalar(char *data, int length,char** iptc_itext,int *itlength);
 i_img* i_readjpeg_wiol(io_glue *ig, int length, char** iptc_itext, int *itlength);
-
 i_img* i_readjpeg_extra2(int fd,char** iptc_itext);
 undef_int i_writejpeg(i_img *im,int fd,int qfactor);
 #endif /* HAVE_LIBJPEG */
@@ -368,8 +367,7 @@ undef_int i_writetiff_wiol_faxable(i_img *im, io_glue *ig, int fine);
 #endif /* HAVE_LIBTIFF */
 
 #ifdef HAVE_LIBPNG
-i_img *i_readpng(int fd);
-i_img *i_readpng_scalar(char *data, int length);
+i_img *i_readpng_wiol(io_glue *ig, int length);
 undef_int i_writepng(i_img *im,int fd);
 #endif /* HAVE_LIBPNG */
 
diff --git a/png.c b/png.c
index 41b5f9792528afccddb75bb159c03ee7cecb58d4..ffad73e845667addec752a1d198ece65b8f8852d 100644 (file)
--- a/png.c
+++ b/png.c
@@ -1,3 +1,4 @@
+#include "iolayer.h"
 #include "image.h"
 #include "png.h"
 
@@ -45,243 +46,157 @@ int CC2C[PNG_COLOR_MASK_PALETTE|PNG_COLOR_MASK_COLOR|PNG_COLOR_MASK_ALPHA];
   voidp write_io_ptr = png_get_io_ptr(write_ptr);
 */
 
-struct png_scalar_info {
-  char *data;
-  int length;
-  int cpos;
-};
 
 
-struct png_wiol_info {
-  io_glue *cp;
-  int length;
-  int cpos;
-};
 
 static void
-user_read_data(png_structp png_ptr,png_bytep data, png_size_t length) {
-  struct png_scalar_info *sci=(struct png_scalar_info *)png_ptr->io_ptr;
-  
-  /*   fprintf(stderr,"user_read_data: cpos %d/%d (%d)\n",sci->cpos,sci->length,length); */
-  
-  if (sci->cpos+(ssize_t)length > sci->length ) { png_error(png_ptr, "Read overflow error on a scalar."); }
-  
-  memcpy(data, sci->data+sci->cpos , length);
-  sci->cpos+=length;
+wiol_read_data(png_structp png_ptr, png_bytep data, png_size_t length) {
+  io_glue *ig = (io_glue *)png_ptr->io_ptr;
+  int rc = ig->readcb(ig, data, length);
+  if (rc != length) png_error(png_ptr, "Read overflow error on an iolayer source.");
 }
 
-/*
 static void
-user_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length) {
-FIXME: implement these 
+wiol_write_data(png_structp png_ptr, png_bytep data, png_size_t length) {
+  int rc;
+  io_glue *ig = (io_glue *)png_ptr->io_ptr;
+  rc = ig->writecb(ig, data, length);
+  if (rc != length) png_error(png_ptr, "Write error on an iolayer source.");
 }
 
 static void
-user_flush_data(png_structp png_ptr) {
-  FIXME: implement these 
+wiol_flush_data(png_structp png_ptr) {
+  /* XXX : This needs to be added to the io layer */
 }
-*/
-
-
-void
-scalar_png_init_io(png_structp png_ptr,struct png_scalar_info *sci) {
-  png_ptr->io_ptr = (png_voidp)sci;
-}
-
-
-
-
 
 
 int
 check_if_png(char *file_name, FILE **fp) {
-   char buf[PNG_BYTES_TO_CHECK];
+  char buf[PNG_BYTES_TO_CHECK];
    
-   /* Open the prospective PNG file. */
-   if ((*fp = fopen(file_name, "rb")) != NULL) return 0;
+  /* Open the prospective PNG file. */
+  if ((*fp = fopen(file_name, "rb")) != NULL) return 0;
    
-   /* Read in some of the signature bytes */
-   if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK) return 0;
+  /* Read in some of the signature bytes */
+  if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK) return 0;
 
-   /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature.
-      Return nonzero (true) if they match */
+  /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature.
+     Return nonzero (true) if they match */
 
-   return(!png_sig_cmp((png_bytep)buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
+  return(!png_sig_cmp((png_bytep)buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
 }
 
-/* Read a PNG file.  You may want to return an error code if the read
- * fails (depending upon the failure).  There are two "prototypes" given
- * here - one where we are given the filename, and we need to open the
- * file, and the other where we are given an open file (possibly with
- * some or all of the magic bytes read - see comments above).
- */
 
-i_img *
-i_readpng(int fd) {
-  i_img *im;
+
+undef_int
+i_writepng(i_img *im, int fd) {
+  FILE *fp;
   png_structp png_ptr;
   png_infop info_ptr;
-  png_uint_32 width, height, y;
-  int bit_depth, color_type, interlace_type;
-  int number_passes;
-  int channels, pass;
-  
-  FILE *fp;
-  unsigned int sig_read;
-
-  sig_read = 0;
+  int width,height,y;
+  volatile int cspace,channels;
 
-  if ((fp = fdopen(fd,"r")) == NULL) {
+  mm_log((1,"i_writepng(0x%x,fd %d)\n",im,fd));
+  
+  if ((fp = fdopen(fd,"w")) == NULL) {
     mm_log((1,"can't fdopen.\n"));
     exit(1);
   }
 
-  mm_log((1,"i_readpng(fd %d)\n",fd));
+  height=im->ysize;
+  width=im->xsize;
 
-  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
+  channels=im->channels;
+
+  if (channels>2) { cspace=PNG_COLOR_TYPE_RGB; channels-=3; }
+  else { cspace=PNG_COLOR_TYPE_GRAY; channels--; }
+  
+  if (channels) cspace|=PNG_COLOR_MASK_ALPHA;
+  mm_log((1,"cspace=%d\n",cspace));
+
+  channels=im->channels;
+
+  /* Create and initialize the png_struct with the desired error handler
+   * functions.  If you want to use the default stderr and longjump method,
+   * you can supply NULL for the last three parameters.  We also check that
+   * the library version is compatible with the one used at compile time,
+   * in case we are using dynamically linked libraries.  REQUIRED.
+   */
+  
+  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
   
   if (png_ptr == NULL) {
     fclose(fp);
     return 0;
   }
   
-  /* Allocate/initialize the memory for image information.  REQUIRED. */
+  /* Allocate/initialize the image information data.  REQUIRED */
   info_ptr = png_create_info_struct(png_ptr);
+
   if (info_ptr == NULL) {
     fclose(fp);
-    png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
-    return 0;
+    png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
+    return(0);
   }
   
-  /* Set error handling if you are using the setjmp/longjmp method (this is
-   * the normal method of doing things with libpng).  REQUIRED unless you
-   * set up your own error handlers in the png_create_read_struct() earlier.
+  /* Set error handling.  REQUIRED if you aren't supplying your own
+   * error hadnling functions in the png_create_write_struct() call.
    */
-  
   if (setjmp(png_ptr->jmpbuf)) {
-    /* Free all of the memory associated with the png_ptr and info_ptr */
-    png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
-    fclose(fp);
     /* If we get here, we had a problem reading the file */
-    return NULL;
+    fclose(fp);
+    png_destroy_write_struct(&png_ptr,  (png_infopp)NULL);
+    return(0);
   }
   
-  /* Set up the input control if you are using standard C streams */
   png_init_io(png_ptr, fp);
-  /* If we have already read some of the signature */
-  png_set_sig_bytes(png_ptr, sig_read);
-  
-  /* The call to png_read_info() gives us all of the information from the
-   * PNG file before the first IDAT (image data chunk).  REQUIRED
-   */
-  
-  png_read_info(png_ptr, info_ptr);
-  png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
-              &interlace_type, NULL, NULL);
-  
-  mm_log((1,
-         "png_get_IHDR results: width %d, height %d, bit_depth %d,color_type %d,interlace_type %d\n",
-         width, height, bit_depth, color_type, interlace_type));
-
-  CC2C[PNG_COLOR_TYPE_GRAY]=1;
-  CC2C[PNG_COLOR_TYPE_PALETTE]=3;
-  CC2C[PNG_COLOR_TYPE_RGB]=3;
-  CC2C[PNG_COLOR_TYPE_RGB_ALPHA]=4;
-  CC2C[PNG_COLOR_TYPE_GRAY_ALPHA]=2;
 
-  channels=CC2C[color_type];
-  mm_log((1,"channels %d\n", channels));
-  
-  /**** Set up the data transformations you want.  Note that these are all
-   **** optional.  Only call them if you want/need them.  Many of the
-   **** transformations only work on specific types of images, and many
-   **** are mutually exclusive.
-   ****/
-
-  /* tell libpng to strip 16 bit/color files down to 8 bits/color */
-  png_set_strip_16(png_ptr);
-  
-  /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
-   * byte into separate bytes (useful for paletted and grayscale images).
-   */
-
-  png_set_packing(png_ptr);
-
-  /* Expand paletted colors into true RGB triplets */
-  if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
-  
-  /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
-  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand(png_ptr);
-
-  /* Expand paletted or RGB images with transparency to full alpha channels
-   * so the data will be available as RGBA quartets.
-   */
-
-  if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
-    channels++;
-    mm_log((1, "image has transparency, adding alpha: channels = %d\n", channels));
-    png_set_expand(png_ptr);
-  }
-  
-  /* Strip alpha bytes from the input data without combining with the
-   * background (not recommended).
+  /* Set the image information here.  Width and height are up to 2^31,
+   * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
+   * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
+   * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
+   * or PNG_COLOR_TYPE_RGB_ALPHA.  interlace is either PNG_INTERLACE_NONE or
+   * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
+   * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
    */
-  /*
-    if ( (color_type != PNG_COLOR_TYPE_RGB_ALPHA) && (color_type != PNG_COLOR_TYPE_GRAY_ALPHA) )
-    png_set_strip_alpha(png_ptr);
-  */
-  number_passes = png_set_interlace_handling(png_ptr);
 
-  mm_log((1,"number of passes=%d\n",number_passes));
-  
-  png_read_update_info(png_ptr, info_ptr);
+  png_set_IHDR(png_ptr, info_ptr, width, height, 8, cspace,
+              PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
 
-  im = i_img_empty_ch(NULL, width, height, channels);
-  for (pass = 0; pass < number_passes; pass++)
-    for (y = 0; y < height; y++) {
-      png_read_row(png_ptr,(png_bytep) &(im->data[channels*width*y]), NULL);
-    }
-  /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
-  
-  png_read_end(png_ptr, info_ptr); 
-  /* clean up after the read, and free any memory allocated - REQUIRED */
-  png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
+  png_write_info(png_ptr, info_ptr);
+  for (y = 0; y < height; y++) png_write_row(png_ptr, (png_bytep) &(im->data[channels*width*y]));
+  png_write_end(png_ptr, info_ptr);
+  png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
 
   fclose(fp);
-  return im;
+  return(1);
 }
 
-  
 
 
 undef_int
-i_writepng(i_img *im,int fd) {
+i_writepng_wiol(i_img *im, io_glue *ig) {
   FILE *fp;
   png_structp png_ptr;
   png_infop info_ptr;
   int width,height,y;
   volatile int cspace,channels;
 
-  mm_log((1,"i_writepng(0x%x,fd %d)\n",im,fd));
+  io_glue_commit_types(ig);
+  mm_log((1,"i_writepng(im %p ,ig %p)\n", im, ig));
   
-  if ((fp = fdopen(fd,"w")) == NULL) {
-    mm_log((1,"can't fdopen.\n"));
-    exit(1);
-  }
-
-  height=im->ysize;
-  width=im->xsize;
+  height = im->ysize;
+  width  = im->xsize;
 
   channels=im->channels;
 
-  if (channels>2) { cspace=PNG_COLOR_TYPE_RGB; channels-=3; }
+  if (channels > 2) { cspace = PNG_COLOR_TYPE_RGB; channels-=3; }
   else { cspace=PNG_COLOR_TYPE_GRAY; channels--; }
   
   if (channels) cspace|=PNG_COLOR_MASK_ALPHA;
   mm_log((1,"cspace=%d\n",cspace));
 
-  channels=im->channels;
+  channels = im->channels;
 
   /* Create and initialize the png_struct with the desired error handler
    * functions.  If you want to use the default stderr and longjump method,
@@ -292,31 +207,27 @@ i_writepng(i_img *im,int fd) {
   
   png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
   
-  if (png_ptr == NULL) {
-    fclose(fp);
-    return 0;
-  }
+  if (png_ptr == NULL) return 0;
+
   
   /* Allocate/initialize the image information data.  REQUIRED */
   info_ptr = png_create_info_struct(png_ptr);
 
   if (info_ptr == NULL) {
-    fclose(fp);
     png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
-    return(0);
+    return 0;
   }
   
   /* Set error handling.  REQUIRED if you aren't supplying your own
    * error hadnling functions in the png_create_write_struct() call.
    */
   if (setjmp(png_ptr->jmpbuf)) {
-    /* If we get here, we had a problem reading the file */
-    fclose(fp);
     png_destroy_write_struct(&png_ptr,  (png_infopp)NULL);
     return(0);
   }
   
-  png_init_io(png_ptr, fp);
+  png_set_write_fn(png_ptr, (png_voidp) (ig), wiol_write_data, wiol_flush_data);
+  png_ptr->io_ptr = (png_voidp) ig;
 
   /* Set the image information here.  Width and height are up to 2^31,
    * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
@@ -331,11 +242,13 @@ i_writepng(i_img *im,int fd) {
               PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
 
   png_write_info(png_ptr, info_ptr);
+
   for (y = 0; y < height; y++) png_write_row(png_ptr, (png_bytep) &(im->data[channels*width*y]));
+
   png_write_end(png_ptr, info_ptr);
+
   png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
 
-  fclose(fp);
   return(1);
 }
 
@@ -343,27 +256,23 @@ i_writepng(i_img *im,int fd) {
 
 
 i_img*
-i_readpng_scalar(char *data, int length) {
+i_readpng_wiol(io_glue *ig, int length) {
   i_img *im;
   png_structp png_ptr;
   png_infop info_ptr;
-  png_uint_32 width, height, y;
+  png_uint_32 width, height;
   int bit_depth, color_type, interlace_type;
-  int number_passes;
-  int channels, pass;
+  int number_passes,y;
+  int channels,pass;
   unsigned int sig_read;
 
-  struct png_scalar_info sci;
-
-  sci.data=data;
-  sci.length=length;
-  sci.cpos=0;
+  sig_read  = 0;
 
-  sig_read=0;
-  mm_log((1,"i_readpng_scalar(char 0x%08X, length %d)\n",data,length));
+  io_glue_commit_types(ig);
+  mm_log((1,"i_readpng_wiol(ig %p, length %d)\n", ig, length));
 
   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
-  png_set_read_fn(png_ptr, (void*) (&sci), user_read_data);
+  png_set_read_fn(png_ptr, (png_voidp) (ig), wiol_read_data);
   
   info_ptr = png_create_info_struct(png_ptr);
   if (info_ptr == NULL) {
@@ -372,18 +281,18 @@ i_readpng_scalar(char *data, int length) {
   }
   
   if (setjmp(png_ptr->jmpbuf)) {
-    mm_log((1,"i_readpng_scalar: error.\n"));
+    mm_log((1,"i_readpng_wiol: error.\n"));
     png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
     return NULL;
   }
-  
-  scalar_png_init_io(png_ptr, &sci);
+
+  png_ptr->io_ptr = (png_voidp) ig;
   png_set_sig_bytes(png_ptr, sig_read);
   png_read_info(png_ptr, info_ptr);
   png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
   
   mm_log((1,
-         "png_get_IHDR results: width %d, height %d, bit_depth %d,color_type %d,interlace_type %d\n",
+         "png_get_IHDR results: width %d, height %d, bit_depth %d, color_type %d, interlace_type %d\n",
          width,height,bit_depth,color_type,interlace_type));
   
   CC2C[PNG_COLOR_TYPE_GRAY]=1;
@@ -391,9 +300,10 @@ i_readpng_scalar(char *data, int length) {
   CC2C[PNG_COLOR_TYPE_RGB]=3;
   CC2C[PNG_COLOR_TYPE_RGB_ALPHA]=4;
   CC2C[PNG_COLOR_TYPE_GRAY_ALPHA]=2;
-  channels=CC2C[color_type];
-  mm_log((1,"channels %d\n",channels));
-  im=i_img_empty_ch(NULL,width,height,channels);
+  channels = CC2C[color_type];
+
+  mm_log((1,"i_readpng_wiol: channels %d\n",channels));
+
   png_set_strip_16(png_ptr);
   png_set_packing(png_ptr);
   if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
@@ -404,96 +314,21 @@ i_readpng_scalar(char *data, int length) {
     mm_log((1, "image has transparency, adding alpha: channels = %d\n", channels));
     png_set_expand(png_ptr);
   }
-
+  
   number_passes = png_set_interlace_handling(png_ptr);
   mm_log((1,"number of passes=%d\n",number_passes));
   png_read_update_info(png_ptr, info_ptr);
-  mm_log((1,"made it to here 1\n"));
+  
+  im = i_img_empty_ch(NULL,width,height,channels);
+
   for (pass = 0; pass < number_passes; pass++)
     for (y = 0; y < height; y++) { png_read_row(png_ptr,(png_bytep) &(im->data[channels*width*y]), NULL); }
-  mm_log((1,"made it to here 2\n"));
-  png_read_end(png_ptr, info_ptr); 
-  mm_log((1,"made it to here 3\n"));
-  png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
-  mm_log((1,"made it to here 4\n"));
-  mm_log((1,"(0x%08X) <- i_readpng_scalar\n",im));  
-  return im;
-}
-
-
-
-
-/* i_img* */
-/* i_readpng_wiol(io_glue *ig, int length) { */
-/*   i_img *im; */
-/*   png_structp png_ptr; */
-/*   png_infop info_ptr; */
-/*   png_uint_32 width, height; */
-/*   int bit_depth, color_type, interlace_type; */
-/*   int number_passes,y; */
-/*   int channels,pass; */
-/*   unsigned int sig_read; */
-
-/*   struct png_wiol_info wi; */
-
-/*   wi.data   = ig; */
-/*   wi.length = length; */
-/*   wi.cpos   = 0; */
-    
-/*   sig_read=0; */
-/*   mm_log((1,"i_readpng_wiol(char 0x%p, length %d)\n", data, length)); */
-
-/*   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL); */
-/*   png_set_read_fn(png_ptr, (void*) (&wi), user_read_data); */
   
-/*   info_ptr = png_create_info_struct(png_ptr); */
-/*   if (info_ptr == NULL) { */
-/*     png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); */
-/*     return NULL; */
-/*   } */
-  
-/*   if (setjmp(png_ptr->jmpbuf)) { */
-/*     mm_log((1,"i_readpng_wiol: error.\n")); */
-/*     png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); */
-/*     return NULL; */
-/*   } */
+  png_read_end(png_ptr, info_ptr); 
   
-/*   scalar_png_init_io(png_ptr, &sci); */
-/*   png_set_sig_bytes(png_ptr, sig_read); */
-/*   png_read_info(png_ptr, info_ptr); */
-/*   png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL); */
+  png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
   
-/*   mm_log((1, */
-/*       "png_get_IHDR results: width %d, height %d, bit_depth %d,color_type %d,interlace_type %d\n", */
-/*       width,height,bit_depth,color_type,interlace_type)); */
+  mm_log((1,"(0x%08X) <- i_readpng_scalar\n", im));  
   
-/*   CC2C[PNG_COLOR_TYPE_GRAY]=1; */
-/*   CC2C[PNG_COLOR_TYPE_PALETTE]=3; */
-/*   CC2C[PNG_COLOR_TYPE_RGB]=3; */
-/*   CC2C[PNG_COLOR_TYPE_RGB_ALPHA]=4; */
-/*   CC2C[PNG_COLOR_TYPE_GRAY_ALPHA]=2; */
-/*   channels = CC2C[color_type]; */
-
-/*   mm_log((1,"i_readpng_wiol: channels %d\n",channels)); */
-
-/*   im = i_img_empty_ch(NULL,width,height,channels); */
-/*   png_set_strip_16(png_ptr); */
-/*   png_set_packing(png_ptr); */
-/*   if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr); */
-/*   if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand(png_ptr); */
-/*   if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr); */
-/*   number_passes = png_set_interlace_handling(png_ptr); */
-/*   mm_log((1,"number of passes=%d\n",number_passes)); */
-/*   png_read_update_info(png_ptr, info_ptr); */
-/*   mm_log((1,"made it to here 1\n")); */
-/*   for (pass = 0; pass < number_passes; pass++) */
-/*     for (y = 0; y < height; y++) { png_read_row(png_ptr,(png_bytep) &(im->data[channels*width*y]), NULL); } */
-/*   mm_log((1,"made it to here 2\n")); */
-/*   png_read_end(png_ptr, info_ptr);  */
-/*   mm_log((1,"made it to here 3\n")); */
-/*   png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); */
-/*   mm_log((1,"made it to here 4\n")); */
-/*   mm_log((1,"(0x%08X) <- i_readpng_scalar\n",im));   */
-
-/*   return im; */
-/* } */
+  return im;
+}
index a8a0774a13ca2178ab30f6a742ad0ad2b5cb66be..d938de9a617cd258ba6131cc4856a00190f92c92 100644 (file)
@@ -42,14 +42,16 @@ if (!i_has_format("png")) {
 } else {
   open(FH,">testout/t102.png") || die "cannot open testout/t102.png for writing\n";
   binmode(FH);
-  i_writepng($img,fileno(FH)) || print "not ";
+  $IO = Imager::io_new_fd(fileno(FH));
+  i_writepng_wiol($img, $IO) || print "not ";
   close(FH);
 
   print "ok 2\n";
 
   open(FH,"testout/t102.png") || die "cannot open testout/t102.png\n";
   binmode(FH);
-  $cmpimg=i_readpng(fileno(FH)) || print "not ";
+  $IO = Imager::io_new_fd(fileno(FH));
+  $cmpimg = i_readpng_wiol($IO, -1) || print "not ";
   close(FH);
 
   print "ok 3\n";
@@ -60,7 +62,8 @@ if (!i_has_format("png")) {
   open FH, "> testout/t102_trans.png"
     or die "Cannot open testout/t102_trans.png: $!";
   binmode FH;
-  if (i_writepng($timg, fileno(FH))) {
+  $IO = Imager::io_new_fd(fileno(FH));
+  if (i_writepng_wiol($timg, $IO)) {
     print "ok 5\n";
   }
   else {
@@ -71,7 +74,8 @@ if (!i_has_format("png")) {
   open FH,"testout/t102_trans.png" 
     or die "cannot open testout/t102_trans.png\n";
   binmode(FH);
-  $cmpimg=i_readpng(fileno(FH)) || print "not ";
+  $IO = Imager::io_new_fd(fileno(FH));
+  $cmpimg = i_readpng_wiol($IO, -1) || print "not ";
   close(FH);
 
   print "ok 6\n";
@@ -85,15 +89,18 @@ if (!i_has_format("png")) {
   open FH, "< testimg/palette.png"
     or die "cannot open testimg/palette.png: $!\n";
   binmode FH;
+  $IO = Imager::io_new_fd(fileno(FH));
   # 1.1 may segfault here (it does with libefence)
-  my $pimg = i_readpng(fileno(FH))
+  my $pimg = i_readpng_wiol($IO,-1)
     or print "not ";
   print "ok 8\n";
   close FH;
+
   open FH, "< testimg/palette_out.png"
     or die "cannot open testimg/palette_out.png: $!\n";
   binmode FH;
-  my $poimg = i_readpng(fileno(FH))
+  $IO = Imager::io_new_fd(fileno(FH));
+  my $poimg = i_readpng_wiol($IO, -1)
     or print "not ";
   print "ok 9\n";
   close FH;
index 99d96c7982def67ea7dfb83afad4980df2b1e548..d4f3af84b57e5700f148585cdf8931b9d75d76cb 100644 (file)
@@ -15,7 +15,7 @@ i_box_filled($img,20,25,80,125,$blue);
 i_arc($img,75,75,30,0,361,$red);
 i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);
 
-my $fh = openimage(">testout/104.ppm");
+my $fh = openimage(">testout/t104.ppm");
 $IO = Imager::io_new_fd(fileno($fh));
 i_writeppm_wiol($img, $IO) 
   or die "Cannot write testout/t104.ppm\n";
@@ -24,8 +24,7 @@ close($fh);
 print "ok 1\n";
 
 $IO = Imager::io_new_bufchain();
-i_writeppm_wiol($img, $IO)
-  or die "Cannot write to bufchain";
+i_writeppm_wiol($img, $IO) or die "Cannot write to bufchain";
 $data = Imager::io_slurp($IO);
 print "ok 2\n";