From: Tony Cook Date: Wed, 16 May 2001 12:48:26 +0000 (+0000) Subject: added tests for the tiff OO interface X-Git-Tag: Imager-0.48^2~628 X-Git-Url: http://git.imager.perl.org/imager.git/commitdiff_plain/4c2d69708047d96031ba38e83e2cc9c160c2977f added tests for the tiff OO interface 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 --- diff --git a/Changes b/Changes index 02e4348c..14be5248 100644 --- 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 ~~~~~~~~~~~~~^ ^ ^~~~~~~~~~~~~~ diff --git a/Imager.pm b/Imager.pm index 6c2954c2..37eb2861 100644 --- 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 diff --git a/Imager.xs b/Imager.xs index e845d491..f94e9794 100644 --- 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 1a1d5522..a7a78aa5 100644 --- 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 */ diff --git a/t/t106tiff.t b/t/t106tiff.t index 6cb23c42..a4d1eb07 100644 --- a/t/t106tiff.t +++ b/t/t106tiff.t @@ -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 05a44edf..55ffafce 100644 --- 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