support saving to pgm file
authorTony Cook <tony@develop=help.com>
Thu, 10 May 2001 02:49:30 +0000 (02:49 +0000)
committerTony Cook <tony@develop=help.com>
Thu, 10 May 2001 02:49:30 +0000 (02:49 +0000)
Changes
pnm.c
t/t104ppm.t

diff --git a/Changes b/Changes
index 6720bf3..60a2126 100644 (file)
--- a/Changes
+++ b/Changes
@@ -404,6 +404,7 @@ Revision history for Perl extension Imager.
         - Added support for transparent to alpha conversion during loading
           of png files.  Note that libpng 1.0.1 is broken in handling this,
          we know that 1.0.5 works.
+       - support for writing PGM (portable gray map) files
 
 ~~~~~~~~~~~~~^ ^ ^~~~~~~~~~~~~~
 
diff --git a/pnm.c b/pnm.c
index ff738da..eec9099 100644 (file)
--- a/pnm.c
+++ b/pnm.c
@@ -406,23 +406,47 @@ i_writeppm(i_img *im,int fd) {
   int rc;
 
   mm_log((1,"i_writeppm(im* 0x%x,fd %d)\n",im,fd));
-  if (im->channels!=3) {
-    mm_log((1,"i_writeppm: ppm is 3 channel only (current image is %d)\n",im->channels));
-    return(0);
-  }
   
-  sprintf(header,"P6\n#CREATOR: Imager\n%d %d\n255\n",im->xsize,im->ysize);
-  
-  if (mywrite(fd,header,strlen(header))<0) {
-    mm_log((1,"i_writeppm: unable to write ppm header.\n"));
-    return(0);
+  i_clear_error();
+
+  if (im->channels==3) {
+    sprintf(header,"P6\n#CREATOR: Imager\n%d %d\n255\n",im->xsize,im->ysize);
+    
+    if (mywrite(fd,header,strlen(header))<0) {
+      i_push_error(errno, "could not write ppm header");
+      mm_log((1,"i_writeppm: unable to write ppm header.\n"));
+      return(0);
+    }
+    
+    rc=mywrite(fd,im->data,im->bytes);
+    if (rc<0) {
+      i_push_error(errno, "could not write ppm data");
+      mm_log((1,"i_writeppm: unable to write ppm data.\n"));
+      return(0);
+    }
   }
-  
-  rc=mywrite(fd,im->data,im->bytes);
-  if (rc<0) {
-    mm_log((1,"i_writeppm: unable to write ppm data.\n"));
+  else if (im->channels == 1) {
+    sprintf(header, "P5\n#CREATOR: Imager\n%d %d\n255\n",
+           im->xsize, im->ysize);
+    if (mywrite(fd,header, strlen(header)) < 0) {
+      i_push_error(errno, "could not write pgm header");
+      mm_log((1,"i_writeppm: unable to write pgm header.\n"));
+      return(0);
+    }
+    
+    rc=mywrite(fd,im->data,im->bytes);
+    if (rc<0) {
+      i_push_error(errno, "could not write pgm data");
+      mm_log((1,"i_writeppm: unable to write pgm data.\n"));
+      return(0);
+    }
+  }
+  else {
+    i_push_error(0, "can only save 1 or 3 channel images to pnm");
+    mm_log((1,"i_writeppm: ppm/pgm is 1 or 3 channel only (current image is %d)\n",im->channels));
     return(0);
   }
+  
   return(1);
 }
 
index b91525a..b14d7a9 100644 (file)
@@ -1,6 +1,6 @@
 use Imager ':all';
 
-print "1..3\n";
+print "1..6\n";
 
 init_log("testout/t104ppm.log",1);
 
@@ -40,5 +40,28 @@ print "ok 2\n";
 print i_img_diff($img, $cmpimg) 
   ? "not ok 3 # saved image different\n" : "ok 3\n";
 
+# build a grayscale image
+my $gimg = Imager::ImgRaw::new(150, 150, 1);
+my $gray = i_color_new(128, 0, 0);
+my $dgray = i_color_new(64, 0, 0);
+my $white = i_color_new(255, 0, 0);
+i_box_filled($gimg, 20, 20, 130, 130, $gray);
+i_box_filled($gimg, 40, 40, 110, 110, $dgray);
+i_arc($gimg, 75, 75, 30, 0, 361, $white);
+open FH, "> testout/t104_gray.pgm"
+  or die "Cannot create testout/t104_gray.pgm: $!\n";
+binmode FH;
+i_writeppm($gimg, fileno(FH))
+  or print "not ";
+print "ok 4\n";
+close FH;
+open FH, "< testout/t104_gray.pgm"
+  or die "Cannot open testout/t104_gray.pgm: $!\n";
+binmode FH;
+$IO = Imager::io_new_fd(fileno(FH));
+my $gcmpimg = i_readpnm_wiol($IO, -1) 
+  or print "not ";
+print "ok 5\n";
+i_img_diff($gimg, $gcmpimg) == 0 or print "not ";
+print "ok 6\n";
 
-# FIXME: may need tests for 1,2,4 channel images?