]> git.imager.perl.org - imager.git/commitdiff
added tests for the tiff OO interface
authorTony Cook <tony@develop=help.com>
Wed, 16 May 2001 12:48:26 +0000 (12:48 +0000)
committerTony Cook <tony@develop=help.com>
Wed, 16 May 2001 12:48:26 +0000 (12:48 +0000)
added resolution setting for faxable tiffs (uncommenting and fixing
the original code)
added documentation for setting the resolution
declared i_writetiff_wiol_faxable in image.h

Changes
Imager.pm
Imager.xs
image.h
t/t106tiff.t
tiff.c

diff --git a/Changes b/Changes
index 02e4348ca136f899f8636196690ddee2d505a255..14be52487087d99db75a30ac241e72f3dc24a107 100644 (file)
--- a/Changes
+++ b/Changes
@@ -411,6 +411,9 @@ Revision history for Perl extension Imager.
         - several fixes to i_readgif_low()
        - Fixed crop() method so that it is consistent with documentation.
         - make clean now removes the testout directory
+       - added tests for the OO interface for tiff, added an option to
+         choose the resolution for fax tiffs, removed some unused code,
+         fixed minor problems with Imager::write() handling of image types
 
 ~~~~~~~~~~~~~^ ^ ^~~~~~~~~~~~~~
 
index 6c2954c2682836ec96ddfbe09d22355cbf7139f1..37eb2861c1bf7d6484f7c01671e78570742246c0 100644 (file)
--- a/Imager.pm
+++ b/Imager.pm
@@ -561,7 +561,8 @@ sub read {
 
 sub write {
   my $self = shift;
-  my %input=(jpegquality=>75, gifquant=>'mc', lmdither=>6.0, lmfixed=>[], @_);
+  my %input=(jpegquality=>75, gifquant=>'mc', lmdither=>6.0, lmfixed=>[], 
+            fax_fine=>1, @_);
   my ($fh, $rc, $fd, $IO);
 
   my %iolready=( tiff=>1 ); # this will be SO MUCH BETTER once they are all in there
@@ -569,7 +570,7 @@ sub write {
   unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
 
   if (!$input{file} and !$input{'fd'} and !$input{'data'}) { $self->{ERRSTR}='file/fd/data parameter missing'; return undef; }
-  if (!$input{type}) { $input{type}=$FORMATGUESS->($input{file}); }
+  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; }
@@ -588,13 +589,13 @@ sub write {
 
 
   if ($iolready{$input{type}}) {
-    if ($fd) {
+    if (defined $fd) {
       $IO = io_new_fd($fd);
     }
 
     if ($input{type} eq 'tiff') {
-      if ($input{class} eq 'fax') {
-       if (!i_writetiff_wiol_faxable($self->{IMG}, $IO)) {
+      if (defined $input{class} && $input{class} eq 'fax') {
+       if (!i_writetiff_wiol_faxable($self->{IMG}, $IO, $input{fax_fine})) {
          $self->{ERRSTR}='Could not write to buffer';
          return undef;
        }
@@ -607,7 +608,6 @@ sub write {
       }
     }
 
-
     if (exists $input{'data'}) {
       my $data = io_slurp($IO);
       if (!$data) {
@@ -696,17 +696,6 @@ sub write {
        $self->{ERRSTR}='unable to write raw image'; return undef;
       }
       $self->{DEBUG} && print "writing a raw file\n";
-    } elsif ( $input{type} eq 'tiff' ) {
-      if ($input{class} eq 'fax') {
-       $rc=i_writetiff_wiol($self->{IMG},io_new_fd($fd) );
-      }
-      else {
-       $rc=i_writetiff_wiol_faxable($self->{IMG},io_new_fd($fd) );
-      }
-      if ( !defined($rc) ) {
-       $self->{ERRSTR}='unable to write tiff image'; return undef;
-      }
-      $self->{DEBUG} && print "writing a tiff file\n";
     }
 
   }
@@ -1654,6 +1643,8 @@ When writing to a tiff image file you can also specify the 'class'
 parameter, which can currently take a single value, "fax".  If class
 is set to fax then a tiff image which should be suitable for faxing
 will be written.  For the best results start with a grayscale image.
+By default the image is written at fine resolution you can override
+this by setting the "fax_fine" parameter to 0.
 
 If you are reading from a gif image file, you can supply a 'colors'
 parameter which must be a reference to a scalar.  The referenced
index e845d491ab3f0b113a870e6450250fbed904c46d..f94e97948bd6a35bd9bc3f33d4a9b26778200600 100644 (file)
--- a/Imager.xs
+++ b/Imager.xs
@@ -1072,9 +1072,10 @@ i_writetiff_wiol(im, ig)
         Imager::IO     ig
 
 undef_int
-i_writetiff_wiol_faxable(im, ig)
+i_writetiff_wiol_faxable(im, ig, fine)
     Imager::ImgRaw     im
         Imager::IO     ig
+              int     fine
 
 
 #endif /* HAVE_LIBTIFF */
diff --git a/image.h b/image.h
index 1a1d5522cb3a7a669cdae961908ba11477f42639..a7a78aa51a545149c1d397afb93219f4a92f5891 100644 (file)
--- a/image.h
+++ b/image.h
@@ -362,6 +362,7 @@ undef_int i_writejpeg(i_img *im,int fd,int qfactor);
 #ifdef HAVE_LIBTIFF
 i_img* i_readtiff_wiol(io_glue *ig, int length);
 undef_int i_writetiff_wiol(i_img *im, io_glue *ig);
+undef_int i_writetiff_wiol_faxable(i_img *im, io_glue *ig, int fine);
 
 #endif /* HAVE_LIBTIFF */
 
index 6cb23c42c24f15d1007a1b0de0046e5b9f5efac6..a4d1eb0784554204ea16282e0f8b961404c2ff6c 100644 (file)
@@ -1,6 +1,7 @@
-print "1..4\n";
+print "1..13\n";
 use Imager qw(:all);
-
+$^W=1; # warnings during command-line tests
+$|=1;  # give us some progress in the test harness
 init_log("testout/t106tiff.log",1);
 
 $green=i_color_new(0,255,0,255);
@@ -42,6 +43,9 @@ if (!i_has_format("tiff")) {
   print "# tiff average mean square pixel difference: ",sqrt(i_img_diff($img,$cmpimg))/150*150,"\n";
   print "ok 2\n";
 
+  i_img_diff($img, $cmpimg) and print "not ";
+  print "ok 3\n";
+
   $IO = Imager::io_new_bufchain();
   
   Imager::i_writetiff_wiol($img, $IO) or die "Cannot write to bufferchain\n";
@@ -54,9 +58,9 @@ if (!i_has_format("tiff")) {
   }
   
   if ($odata eq $tiffdata) {
-    print "ok 3\n";
+    print "ok 4\n";
   } else {
-    print "not ok 3\n";
+    print "not ok 4\n";
   }
 
   # test Micksa's tiff writer
@@ -78,9 +82,45 @@ if (!i_has_format("tiff")) {
     or die "Cannot create testout/t106tiff_fax.tiff: $!";
   binmode FH;
   $IO = Imager::io_new_fd(fileno(FH));
-  i_writetiff_wiol_faxable($faximg, $IO)
+  i_writetiff_wiol_faxable($faximg, $IO, 1)
     or print "not ";
-  print "ok 4\n";
+  print "ok 5\n";
   close FH;
+
+  # test the OO interface
+  my $ooim = Imager->new;
+  $ooim->read(file=>'testout/t106.tiff')
+    or print "not ";
+  print "ok 6\n";
+  $ooim->write(file=>'testout/t106_oo.tiff')
+    or print "not ";
+  print "ok 7\n";
+
+  # OO with the fax image
+  my $oofim = Imager->new;
+  $oofim->read(file=>'testout/t106tiff_fax.tiff')
+    or print "not ";
+  print "ok 8\n";
+  $oofim->write(file=>'testout/t106_oo_fax.tiff', class=>'fax')
+    or print "not ";
+  print "ok 9\n";
+
+  # the following should fail since there's no type and no filename
+  my $oodata;
+  $ooim->write(data=>\$oodata)
+    and print "not ";
+  print "ok 10\n";
+
+  # OO to data
+  $ooim->write(data=>\$oodata, type=>'tiff')
+    or print 'not ';
+  print "ok 11\n";
+  $oodata eq $tiffdata or print "not ";
+  print "ok 12\n";
+
+  # make sure we can write non-fine mode
+  $oofim->write(file=>'testout/t106_oo_faxlo.tiff', class=>'fax', fax_fine=>0)
+    or print "not ";
+  print "ok 13\n";
 }
 
diff --git a/tiff.c b/tiff.c
index 05a44edf18abe6207c012d60e0a41ef49f85e0e5..55ffafce846ebef0f3d6d94a1e03bcabc709f3ca 100644 (file)
--- a/tiff.c
+++ b/tiff.c
@@ -360,7 +360,7 @@ point.
 */
 
 undef_int
-i_writetiff_wiol_faxable(i_img *im, io_glue *ig) {
+i_writetiff_wiol_faxable(i_img *im, io_glue *ig, int fine) {
   uint32 width, height;
   unsigned char *linebuf = NULL;
   uint32 y;
@@ -369,6 +369,7 @@ i_writetiff_wiol_faxable(i_img *im, io_glue *ig) {
   TIFF* tif;
   int luma_channel;
   uint32 rowsperstrip;
+  float vres = fine ? 196 : 98;
 
   width    = im->xsize;
   height   = im->ysize;
@@ -441,15 +442,13 @@ i_writetiff_wiol_faxable(i_img *im, io_glue *ig) {
   mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField scanlinesize=%d\n", TIFFScanlineSize(tif) ));
   mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField planarconfig=%d == %d\n", rc, PLANARCONFIG_CONTIG));
 
-  /*
-  if (!TIFFSetField(tif, TIFFTAG_XRESOLUTION, 204))
+  if (!TIFFSetField(tif, TIFFTAG_XRESOLUTION, (float)204))
     { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Xresolution=204\n")); return 0; }
-  if (!TIFFSetField(tif, TIFFTAG_YRESOLUTION, 196))
+  if (!TIFFSetField(tif, TIFFTAG_YRESOLUTION, vres))
     { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Yresolution=196\n")); return 0; }
   if (!TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH)) {
     mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField ResolutionUnit=%d\n", RESUNIT_INCH)); return 0; 
   }
-  */
 
   for (y=0; y<height; y++) {
     int linebufpos=0;