Removed i_writeppm and put in i_writeppm_wiol instead.
authorArnar Mar Hrafnkelsson <addi@cpan.org>
Wed, 6 Jun 2001 05:03:37 +0000 (05:03 +0000)
committerArnar Mar Hrafnkelsson <addi@cpan.org>
Wed, 6 Jun 2001 05:03:37 +0000 (05:03 +0000)
Imager.pm
Imager.xs
image.h
pnm.c
t/t104ppm.t
t/t105gif.t
t/t30t1font.t
t/t35ttfont.t
t/t75polyaa.t

index e3ecd80b5d736a536ab6392486570775f5792a7f..08366682813c724455685afacee795e454637fc3 100644 (file)
--- a/Imager.pm
+++ b/Imager.pm
@@ -98,7 +98,7 @@ use Imager::Font;
                i_writegif_callback
 
                i_readpnm_wiol
-               i_writeppm
+               i_writeppm_wiol
 
                i_readraw
                i_writeraw
index b0411aed1be1eb9232d0e506fb1c627b6f9d03fe..13bed7d4df90f45acaccad1859babf10cc1399df 100644 (file)
--- a/Imager.xs
+++ b/Imager.xs
@@ -1212,7 +1212,7 @@ i_writegif(im,fd,colors,pixdev,fixed)
 
 undef_int
 i_writegifmc(im,fd,colors)
-    Imager::ImgRaw     im
+    Imager::ImgRaw    im
               int     fd
               int     colors
 
@@ -1500,6 +1500,12 @@ i_writeppm(im,fd)
     Imager::ImgRaw     im
               int     fd
 
+undef_int
+i_writeppm_wiol(im, ig)
+    Imager::ImgRaw     im
+        Imager::IO     ig
+
+
 Imager::ImgRaw
 i_readraw(fd,x,y,datachannels,storechannels,intrl)
               int     fd
diff --git a/image.h b/image.h
index 640b4ca4d9bd07848da176d7216bde956da7819f..1333fe191f67e6237f1348a413dee37d7df5ebb3 100644 (file)
--- a/image.h
+++ b/image.h
@@ -392,6 +392,7 @@ undef_int i_writeraw(i_img* im,int fd);
 
 i_img *i_readpnm_wiol(io_glue *ig, int length);
 undef_int i_writeppm(i_img *im,int fd);
+undef_int i_writeppm_wiol(i_img *im, io_glue *ig);
 
 
 i_img* i_scaleaxis(i_img *im, float Value, int Axis);
diff --git a/pnm.c b/pnm.c
index eec90997097752694ca59d496c6712ce116b5e5d..350d3537144f831f98818be5cec8d7aa1edcf296 100644 (file)
--- a/pnm.c
+++ b/pnm.c
@@ -1,6 +1,7 @@
 #include "image.h"
 #include "io.h"
 #include "log.h"
+#include "iolayer.h"
 
 #include <stdlib.h>
 #include <errno.h>
@@ -451,8 +452,57 @@ i_writeppm(i_img *im,int fd) {
 }
 
 
+undef_int
+i_writeppm_wiol(i_img *im, io_glue *ig) {
+  char header[255];
+  int rc;
+  writep write_func;
 
+  mm_log((1,"i_writeppm(im %p, ig %p)\n", im, ig));
+  i_clear_error();
 
+  /* Add code to get the filename info from the iolayer */
+  /* Also add code to check for mmapped code */
 
+  io_glue_commit_types(ig);
 
-
+  if (im->channels==3) {
+    sprintf(header,"P6\n#CREATOR: Imager\n%d %d\n255\n",im->xsize,im->ysize);
+    
+    if (ig->writecb(ig, 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 = ig->writecb(ig, 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);
+    }
+  }
+  else if (im->channels == 1) {
+    sprintf(header, "P5\n#CREATOR: Imager\n%d %d\n255\n",
+           im->xsize, im->ysize);
+    if (ig->writecb(ig, 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 = ig->writecb(ig, 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 fce99d1feb3ca5b7a020d8a397cf5b0416a836e6..99d96c7982def67ea7dfb83afad4980df2b1e548 100644 (file)
@@ -1,44 +1,46 @@
 use Imager ':all';
 
-print "1..6\n";
+print "1..8\n";
 
 init_log("testout/t104ppm.log",1);
 
-$green=i_color_new(0,255,0,255);
-$blue=i_color_new(0,0,255,255);
-$red=i_color_new(255,0,0,255);
+my $green = i_color_new(0,255,0,255);
+my $blue  = i_color_new(0,0,255,255);
+my $red   = i_color_new(255,0,0,255);
 
-$img=Imager::ImgRaw::new(150,150,3);
-$cmpimg=Imager::ImgRaw::new(150,150,3);
+my $img    = Imager::ImgRaw::new(150,150,3);
 
 i_box_filled($img,70,25,130,125,$green);
 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 $timg = Imager::ImgRaw::new(20, 20, 4);
-my $trans = i_color_new(255, 0, 0, 127);
-i_box_filled($timg, 0, 0, 20, 20, $green);
-i_box_filled($timg, 2, 2, 18, 18, $trans);
-
-open(FH,">testout/t104.ppm") || die "Cannot open testout/t104.ppm\n";
-binmode(FH);
-i_writeppm($img,fileno(FH)) || die "Cannot write testout/t104.ppm\n";
-close(FH);
+my $fh = openimage(">testout/104.ppm");
+$IO = Imager::io_new_fd(fileno($fh));
+i_writeppm_wiol($img, $IO) 
+  or die "Cannot write testout/t104.ppm\n";
+close($fh);
 
 print "ok 1\n";
 
-open(FH,"testout/t104.ppm") || die "Cannot open testout/t104.ppm\n";
-binmode(FH);
+$IO = Imager::io_new_bufchain();
+i_writeppm_wiol($img, $IO)
+  or die "Cannot write to bufchain";
+$data = Imager::io_slurp($IO);
+print "ok 2\n";
 
-my $IO = Imager::io_new_fd(fileno(FH));
-$cmpimg=i_readpnm_wiol($IO,-1) || die "Cannot read testout/t104.ppm\n";
-close(FH);
+$fh = openimage("testout/t104.ppm");
+$IO = Imager::io_new_fd( fileno($fh) );
+$cmpimg = i_readpnm_wiol($IO,-1) || die "Cannot read testout/t104.ppm\n";
+close($fh);
+print "ok 3\n";
 
-print "ok 2\n";
+print i_img_diff($img, $cmpimg) ? "not ok 4 # saved image different\n" : "ok 4\n";
+
+my $rdata = slurp("testout/t104.ppm");
+print "not " if $rdata ne $data;
+print "ok 5\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);
@@ -48,20 +50,34 @@ my $white = i_color_new(255, 0, 0, 255);
 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";
+
+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";
+my $IO = Imager::io_new_fd(fileno(FH));
+i_writeppm_wiol($gimg, $IO) or print "not ";
+print "ok 6\n";
 close FH;
-open FH, "< testout/t104_gray.pgm"
-  or die "Cannot open testout/t104_gray.pgm: $!\n";
+
+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";
+my $gcmpimg = i_readpnm_wiol($IO, -1) or print "not ";
+print "ok 7\n";
 i_img_diff($gimg, $gcmpimg) == 0 or print "not ";
-print "ok 6\n";
+print "ok 8\n";
+
+sub openimage {
+  my $fname = shift;
+  local(*FH);
+  open(FH, $fname) or die "Cannot open $fname: $!\n";
+  binmode(FH);
+  return *FH;
+}
 
+sub slurp {
+  my $fh = openimage(shift);
+  local $/;
+  my $data = <$fh>;
+  close($fh);
+  return $data;
+}
index f3fe342dd9733f53f70e520b791f1e89cb21ab33..65e3636e60a4e74bdc8042d59ec5a5c60c93af51 100644 (file)
@@ -46,7 +46,7 @@ if (!i_has_format("gif")) {
     $palette=''; # just to skip a warning.
 
     print "ok 3\n";
-    
+
     # check that reading interlaced/non-interlaced versions of 
     # the same GIF produce the same image
     # I could replace this with code that used Imager's built-in
@@ -66,18 +66,22 @@ if (!i_has_format("gif")) {
 
     open FH, ">testout/t105i.ppm" or die "Cannot create testout/t105i.ppm";
     binmode FH;
-    i_writeppm($imgi, fileno(FH)) or die "Cannot write testout/t105i.ppm";
+    $IO = Imager::io_new_fd( fileno(FH) );
+    i_writeppm_wiol($imgi, $IO) or die "Cannot write testout/t105i.ppm";
     close FH;
 
+
     open FH, ">testout/t105ni.ppm" or die "Cannot create testout/t105ni.ppm";
     binmode FH;
-    i_writeppm($imgni, fileno(FH)) or die "Cannot write testout/t105ni.ppm";
+    $IO = Imager::io_new_fd( fileno(FH) );
+    i_writeppm_wiol($imgni, $IO) or die "Cannot write testout/t105ni.ppm";
     close FH;
 
     # compare them
     open FH, "<testout/t105i.ppm" or die "Cannot open testout/t105i.ppm";
     $datai = do { local $/; <FH> };
     close FH;
+
     open FH, "<testout/t105ni.ppm" or die "Cannot open testout/t105ni.ppm";
     $datani = do { local $/; <FH> };
     close FH;
@@ -99,7 +103,7 @@ if (!i_has_format("gif")) {
       my $img2 = i_readgif_callback(sub { my $tmp; read(FH, $tmp, $_[0]) and $tmp });
       close FH; 
       print $img ? "ok 7\n" : "not ok 7\n";
-      
+
       print test_readgif_cb(1) ? "ok 8\n" : "not ok 8\n";
       print test_readgif_cb(512) ? "ok 9\n" : "not ok 9\n";
       print test_readgif_cb(1024) ? "ok 10\n" : "not ok 10\n";
index b96abca7ca04ea4881df4d46e49711c5a17d1ad3..49e7ca65433975b978697a2417fb271df53b8cdb 100644 (file)
@@ -25,7 +25,7 @@ if (!(i_has_format("t1")) ) {
 } else {
 
   print "# has t1\n";
-  
+
   $fontname_pfb=$ENV{'T1FONTTESTPFB'}||'./fontfiles/dcr10.pfb';
   $fontname_afm=$ENV{'T1FONTTESTAFM'}||'./fontfiles/dcr10.afm';
 
@@ -41,39 +41,41 @@ if (!(i_has_format("t1")) ) {
 
   i_init_fonts();
   i_t1_set_aa(1);
-  
+
   $fnum=Imager::i_t1_new($fontname_pfb,$fontname_afm); # this will load the pfb font
   if ($fnum<0) { die "Couldn't load font $fontname_pfb"; }
+
   $bgcolor=Imager::Color->new(255,0,0,0);
   $overlay=Imager::ImgRaw::new(200,70,3);
-  
+
   i_t1_cp($overlay,5,50,1,$fnum,50.0,'XMCLH',5,1);
   i_draw($overlay,0,50,100,50,$bgcolor);
-  
+
   @bbox=i_t1_bbox(0,50.0,'XMCLH',5);
   print "bbox: ($bbox[0], $bbox[1]) - ($bbox[2], $bbox[3])\n";
-  
+
   open(FH,">testout/t30t1font.ppm") || die "cannot open testout/t35t1font.ppm\n";
   binmode(FH); # for os2
-  i_writeppm($overlay,fileno(FH));
+  $IO = Imager::io_new_fd( fileno(FH) );
+  i_writeppm_wiol($overlay,$IO);
   close(FH);
 
   print "ok 2\n";
-  
+
   $bgcolor=Imager::Color::set($bgcolor,200,200,200,0);
   $backgr=Imager::ImgRaw::new(280,150,3);
 
   i_t1_set_aa(2);
   i_t1_text($backgr,10,100,$bgcolor,$fnum,150.0,'test',4,1);
-  
+
   open(FH,">testout/t30t1font2.ppm") || die "cannot open testout/t35t1font.ppm\n";
   binmode(FH);
-  i_writeppm($backgr,fileno(FH));
+  $IO = Imager::io_new_fd( fileno(FH) );
+  i_writeppm_wiol($backgr, $IO);
   close(FH);
-  
+
   print "ok 3\n";
-  
+
   $rc=i_t1_destroy($fnum);
   if ($fnum <0) { die "i_t1_destroy failed: rc=$rc\n"; }
 
index 44acbd408e1f5d74ad00ff4bc13f432906a24afd..a82bba589e473454f8d2f2486a399c4aae4de6af 100644 (file)
@@ -50,7 +50,8 @@ i_draw($overlay,0,50,100,50,$bgcolor);
 
 open(FH,">testout/t35ttfont.ppm") || die "cannot open testout/t35ttfont.ppm\n";
 binmode(FH);
-i_writeppm($overlay,fileno(FH));
+$IO = Imager::io_new_fd( fileno(FH) );
+i_writeppm_wiol($overlay, $IO);
 close(FH);
 
 print "ok 2\n";
@@ -64,7 +65,8 @@ i_tt_text($ttraw,$backgr,100,100,$bgcolor,50.0,'test',4,1);
 
 open(FH,">testout/t35ttfont2.ppm") || die "cannot open testout/t35ttfont.ppm\n";
 binmode(FH);
-i_writeppm($backgr,fileno(FH));
+$IO = Imager::io_new_fd( fileno(FH) );
+i_writeppm_wiol($backgr, $IO);
 close(FH);
 
 print "ok 3\n";
index 29c97933cbcb61bac915c6978df75a0ecf0c6205..c0888674bf9aa681c6a2f6868d97d8698da860bf 100644 (file)
@@ -52,7 +52,9 @@ push(@y,$y[0]);
 print "ok 2\n";
 
 open(FH,">testout/t75.ppm") || die "Cannot open testout/t75.ppm\n";
-i_writeppm($img->{IMG},fileno(FH)) || die "Cannot write testout/t75.ppm\n";
+binmode(FH);
+$IO = Imager::io_new_fd( fileno(FH) );
+i_writeppm_wiol($img->{IMG}, $IO) || die "Cannot write testout/t75.ppm\n";
 close(FH);
 
 print "ok 3\n";