From: Arnar Mar Hrafnkelsson <addi@cpan.org> Date: Mon, 19 Aug 2002 18:56:46 +0000 (+0000) Subject: Removed cruft since not all formats supported loading with iolayer. X-Git-Tag: Imager-0.48^2~355 X-Git-Url: http://git.imager.perl.org/imager.git/commitdiff_plain/2fe0b227b8a69eeaa276361234d2c8f72fe01130 Removed cruft since not all formats supported loading with iolayer. --- diff --git a/Imager.pm b/Imager.pm index 9ebcffca..2d8b90af 100644 --- a/Imager.pm +++ b/Imager.pm @@ -1024,188 +1024,124 @@ sub read { my %iolready=(jpeg=>1, png=>1, tiff=>1, pnm=>1, raw=>1, bmp=>1, tga=>1, rgb=>1, gif=>1); - if ($iolready{$input{'type'}}) { - # Setup data source - my ($IO, $fh) = $self->_get_reader_io(\%input, $input{'type'}) + # Setup data source + my ($IO, $fh) = $self->_get_reader_io(\%input, $input{'type'}) or return; - if ( $input{'type'} eq 'jpeg' ) { - ($self->{IMG},$self->{IPTCRAW})=i_readjpeg_wiol( $IO ); - if ( !defined($self->{IMG}) ) { - $self->{ERRSTR}='unable to read jpeg image'; return undef; - } - $self->{DEBUG} && print "loading a jpeg file\n"; - return $self; - } - - if ( $input{'type'} eq 'tiff' ) { - $self->{IMG}=i_readtiff_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed - if ( !defined($self->{IMG}) ) { - $self->{ERRSTR}=$self->_error_as_msg(); return undef; - } - $self->{DEBUG} && print "loading a tiff file\n"; - return $self; + if ( $input{'type'} eq 'jpeg' ) { + ($self->{IMG},$self->{IPTCRAW})=i_readjpeg_wiol( $IO ); + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}='unable to read jpeg image'; return undef; } + $self->{DEBUG} && print "loading a jpeg file\n"; + return $self; + } - if ( $input{'type'} eq 'pnm' ) { - $self->{IMG}=i_readpnm_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed - if ( !defined($self->{IMG}) ) { - $self->{ERRSTR}='unable to read pnm image: '._error_as_msg(); return undef; - } - $self->{DEBUG} && print "loading a pnm file\n"; - return $self; + if ( $input{'type'} eq 'tiff' ) { + $self->{IMG}=i_readtiff_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}=$self->_error_as_msg(); return undef; } + $self->{DEBUG} && print "loading a tiff file\n"; + 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 'pnm' ) { + $self->{IMG}=i_readpnm_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}='unable to read pnm image: '._error_as_msg(); return undef; } + $self->{DEBUG} && print "loading a pnm file\n"; + return $self; + } - if ( $input{'type'} eq 'bmp' ) { - $self->{IMG}=i_readbmp_wiol( $IO ); - if ( !defined($self->{IMG}) ) { - $self->{ERRSTR}=$self->_error_as_msg(); - return undef; - } - $self->{DEBUG} && print "loading a bmp file\n"; + 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 'gif' ) { - if ($input{colors} && !ref($input{colors})) { - # must be a reference to a scalar that accepts the colour map - $self->{ERRSTR} = "option 'colors' must be a scalar reference"; - return undef; - } - if ($input{colors}) { - my $colors; - ($self->{IMG}, $colors) =i_readgif_wiol( $IO ); - if ($colors) { - ${ $input{colors} } = [ map { NC(@$_) } @$colors ]; - } - } - else { - $self->{IMG} =i_readgif_wiol( $IO ); - } - if ( !defined($self->{IMG}) ) { - $self->{ERRSTR}=$self->_error_as_msg(); - return undef; - } - $self->{DEBUG} && print "loading a gif file\n"; + if ( $input{'type'} eq 'bmp' ) { + $self->{IMG}=i_readbmp_wiol( $IO ); + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}=$self->_error_as_msg(); + return undef; } + $self->{DEBUG} && print "loading a bmp file\n"; + } - if ( $input{'type'} eq 'tga' ) { - $self->{IMG}=i_readtga_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed - if ( !defined($self->{IMG}) ) { - $self->{ERRSTR}=$self->_error_as_msg(); - return undef; - } - $self->{DEBUG} && print "loading a tga file\n"; + if ( $input{'type'} eq 'gif' ) { + if ($input{colors} && !ref($input{colors})) { + # must be a reference to a scalar that accepts the colour map + $self->{ERRSTR} = "option 'colors' must be a scalar reference"; + return undef; } - - if ( $input{'type'} eq 'rgb' ) { - $self->{IMG}=i_readrgb_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed - if ( !defined($self->{IMG}) ) { - $self->{ERRSTR}=$self->_error_as_msg(); - return undef; + if ($input{colors}) { + my $colors; + ($self->{IMG}, $colors) =i_readgif_wiol( $IO ); + if ($colors) { + ${ $input{colors} } = [ map { NC(@$_) } @$colors ]; } - $self->{DEBUG} && print "loading a tga file\n"; } - - - if ( $input{'type'} eq 'raw' ) { - my %params=(datachannels=>3,storechannels=>3,interleave=>1,%input); - - if ( !($params{xsize} && $params{ysize}) ) { - $self->{ERRSTR}='missing xsize or ysize parameter for raw'; - return undef; - } - - $self->{IMG} = i_readraw_wiol( $IO, - $params{xsize}, - $params{ysize}, - $params{datachannels}, - $params{storechannels}, - $params{interleave}); - if ( !defined($self->{IMG}) ) { - $self->{ERRSTR}='unable to read raw image'; - return undef; - } - $self->{DEBUG} && print "loading a raw file\n"; + else { + $self->{IMG} =i_readgif_wiol( $IO ); } - - } else { - - # Old code for reference while changing the new stuff - - if (!$input{'type'} and $input{file}) { - $input{'type'}=$FORMATGUESS->($input{file}); + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}=$self->_error_as_msg(); + return undef; } + $self->{DEBUG} && print "loading a gif file\n"; + } - if (!$input{'type'}) { - $self->{ERRSTR}='type parameter missing and not possible to guess from extension'; return undef; + if ( $input{'type'} eq 'tga' ) { + $self->{IMG}=i_readtga_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}=$self->_error_as_msg(); + return undef; } + $self->{DEBUG} && print "loading a tga file\n"; + } - if (!$formats{$input{'type'}}) { - $self->{ERRSTR}='format not supported'; + if ( $input{'type'} eq 'rgb' ) { + $self->{IMG}=i_readrgb_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}=$self->_error_as_msg(); return undef; } + $self->{DEBUG} && print "loading a tga file\n"; + } - my ($fh, $fd); - if ($input{file}) { - $fh = new IO::File($input{file},"r"); - if (!defined $fh) { - $self->{ERRSTR}='Could not open file'; - return undef; - } - binmode($fh); - $fd = $fh->fileno(); - } - if ($input{fd}) { - $fd=$input{fd}; + if ( $input{'type'} eq 'raw' ) { + my %params=(datachannels=>3,storechannels=>3,interleave=>1,%input); + + if ( !($params{xsize} && $params{ysize}) ) { + $self->{ERRSTR}='missing xsize or ysize parameter for raw'; + return undef; } - if ( $input{'type'} eq 'gif' ) { - my $colors; - if ($input{colors} && !ref($input{colors})) { - # must be a reference to a scalar that accepts the colour map - $self->{ERRSTR} = "option 'colors' must be a scalar reference"; - return undef; - } - if (exists $input{data}) { - if ($input{colors}) { - ($self->{IMG}, $colors) = i_readgif_scalar($input{data}); - } else { - $self->{IMG}=i_readgif_scalar($input{data}); - } - } else { - if ($input{colors}) { - ($self->{IMG}, $colors) = i_readgif( $fd ); - } else { - $self->{IMG} = i_readgif( $fd ) - } - } - if ($colors) { - # we may or may not change i_readgif to return blessed objects... - ${ $input{colors} } = [ map { NC(@$_) } @$colors ]; - } - if ( !defined($self->{IMG}) ) { - $self->{ERRSTR}= 'reading GIF:'._error_as_msg(); - return undef; - } - $self->{DEBUG} && print "loading a gif file\n"; + $self->{IMG} = i_readraw_wiol( $IO, + $params{xsize}, + $params{ysize}, + $params{datachannels}, + $params{storechannels}, + $params{interleave}); + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}='unable to read raw image'; + return undef; } + $self->{DEBUG} && print "loading a raw file\n"; } + return $self; } sub _fix_gif_positions { my ($opts, $opt, $msg, @imgs) = @_; - + my $positions = $opts->{'gif_positions'}; my $index = 0; for my $pos (@$positions) { @@ -1294,9 +1230,9 @@ sub _set_opts { # Write an image to file sub write { my $self = shift; - my %input=(jpegquality=>75, - gifquant=>'mc', - lmdither=>6.0, + my %input=(jpegquality=>75, + gifquant=>'mc', + lmdither=>6.0, lmfixed=>[], idstring=>"", compress=>1, @@ -1307,9 +1243,6 @@ sub write { $self->_set_opts(\%input, "i_", $self) or return undef; - my %iolready=( tiff=>1, raw=>1, png=>1, pnm=>1, bmp=>1, jpeg=>1, tga=>1, - gif=>1 ); # this will be SO MUCH BETTER once they are all in there - unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } if (!$input{'type'} and $input{file}) { @@ -1325,105 +1258,99 @@ sub write { my ($IO, $fh) = $self->_get_writer_io(\%input, $input{'type'}) or return undef; - # this conditional is probably obsolete - if ($iolready{$input{'type'}}) { - - if ($input{'type'} eq 'tiff') { - $self->_set_opts(\%input, "tiff_", $self) - or return undef; - $self->_set_opts(\%input, "exif_", $self) - or return undef; + if ($input{'type'} eq 'tiff') { + $self->_set_opts(\%input, "tiff_", $self) + or return undef; + $self->_set_opts(\%input, "exif_", $self) + or return undef; - 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; - } - } else { - if (!i_writetiff_wiol($self->{IMG}, $IO)) { - $self->{ERRSTR}='Could not write to buffer'; - return undef; - } - } - } elsif ( $input{'type'} eq 'pnm' ) { - $self->_set_opts(\%input, "pnm_", $self) - or return undef; - if ( ! i_writeppm_wiol($self->{IMG},$IO) ) { - $self->{ERRSTR}='unable to write pnm image'; - return undef; - } - $self->{DEBUG} && print "writing a pnm file\n"; - } elsif ( $input{'type'} eq 'raw' ) { - $self->_set_opts(\%input, "raw_", $self) - or return undef; - if ( !i_writeraw_wiol($self->{IMG},$IO) ) { - $self->{ERRSTR}='unable to write raw image'; - return undef; - } - $self->{DEBUG} && print "writing a raw file\n"; - } elsif ( $input{'type'} eq 'png' ) { - $self->_set_opts(\%input, "png_", $self) - or return undef; - if ( !i_writepng_wiol($self->{IMG}, $IO) ) { - $self->{ERRSTR}='unable to write png image'; - return undef; - } - $self->{DEBUG} && print "writing a png file\n"; - } elsif ( $input{'type'} eq 'jpeg' ) { - $self->_set_opts(\%input, "jpeg_", $self) - or return undef; - $self->_set_opts(\%input, "exif_", $self) - or return undef; - if ( !i_writejpeg_wiol($self->{IMG}, $IO, $input{jpegquality})) { - $self->{ERRSTR} = $self->_error_as_msg(); - return undef; - } - $self->{DEBUG} && print "writing a jpeg file\n"; - } elsif ( $input{'type'} eq 'bmp' ) { - $self->_set_opts(\%input, "bmp_", $self) - or return undef; - if ( !i_writebmp_wiol($self->{IMG}, $IO) ) { - $self->{ERRSTR}='unable to write bmp image'; + 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; } - $self->{DEBUG} && print "writing a bmp file\n"; - } elsif ( $input{'type'} eq 'tga' ) { - $self->_set_opts(\%input, "tga_", $self) - or return undef; - - if ( !i_writetga_wiol($self->{IMG}, $IO, $input{wierdpack}, $input{compress}, $input{idstring}) ) { - $self->{ERRSTR}=$self->_error_as_msg(); + } else { + if (!i_writetiff_wiol($self->{IMG}, $IO)) { + $self->{ERRSTR}='Could not write to buffer'; return undef; } - $self->{DEBUG} && print "writing a tga file\n"; - } elsif ( $input{'type'} eq 'gif' ) { - $self->_set_opts(\%input, "gif_", $self) - or return undef; - # compatibility with the old interfaces - if ($input{gifquant} eq 'lm') { - $input{make_colors} = 'addi'; - $input{translate} = 'perturb'; - $input{perturb} = $input{lmdither}; - } elsif ($input{gifquant} eq 'gen') { - # just pass options through - } else { - $input{make_colors} = 'webmap'; # ignored - $input{translate} = 'giflib'; - } - $rc = i_writegif_wiol($IO, \%input, $self->{IMG}); } + } elsif ( $input{'type'} eq 'pnm' ) { + $self->_set_opts(\%input, "pnm_", $self) + or return undef; + if ( ! i_writeppm_wiol($self->{IMG},$IO) ) { + $self->{ERRSTR}='unable to write pnm image'; + return undef; + } + $self->{DEBUG} && print "writing a pnm file\n"; + } elsif ( $input{'type'} eq 'raw' ) { + $self->_set_opts(\%input, "raw_", $self) + or return undef; + if ( !i_writeraw_wiol($self->{IMG},$IO) ) { + $self->{ERRSTR}='unable to write raw image'; + return undef; + } + $self->{DEBUG} && print "writing a raw file\n"; + } elsif ( $input{'type'} eq 'png' ) { + $self->_set_opts(\%input, "png_", $self) + or return undef; + if ( !i_writepng_wiol($self->{IMG}, $IO) ) { + $self->{ERRSTR}='unable to write png image'; + return undef; + } + $self->{DEBUG} && print "writing a png file\n"; + } elsif ( $input{'type'} eq 'jpeg' ) { + $self->_set_opts(\%input, "jpeg_", $self) + or return undef; + $self->_set_opts(\%input, "exif_", $self) + or return undef; + if ( !i_writejpeg_wiol($self->{IMG}, $IO, $input{jpegquality})) { + $self->{ERRSTR} = $self->_error_as_msg(); + return undef; + } + $self->{DEBUG} && print "writing a jpeg file\n"; + } elsif ( $input{'type'} eq 'bmp' ) { + $self->_set_opts(\%input, "bmp_", $self) + or return undef; + if ( !i_writebmp_wiol($self->{IMG}, $IO) ) { + $self->{ERRSTR}='unable to write bmp image'; + return undef; + } + $self->{DEBUG} && print "writing a bmp file\n"; + } elsif ( $input{'type'} eq 'tga' ) { + $self->_set_opts(\%input, "tga_", $self) + or return undef; - if (exists $input{'data'}) { - my $data = io_slurp($IO); - if (!$data) { - $self->{ERRSTR}='Could not slurp from buffer'; - return undef; - } - ${$input{data}} = $data; + if ( !i_writetga_wiol($self->{IMG}, $IO, $input{wierdpack}, $input{compress}, $input{idstring}) ) { + $self->{ERRSTR}=$self->_error_as_msg(); + return undef; } - return $self; + $self->{DEBUG} && print "writing a tga file\n"; + } elsif ( $input{'type'} eq 'gif' ) { + $self->_set_opts(\%input, "gif_", $self) + or return undef; + # compatibility with the old interfaces + if ($input{gifquant} eq 'lm') { + $input{make_colors} = 'addi'; + $input{translate} = 'perturb'; + $input{perturb} = $input{lmdither}; + } elsif ($input{gifquant} eq 'gen') { + # just pass options through + } else { + $input{make_colors} = 'webmap'; # ignored + $input{translate} = 'giflib'; + } + $rc = i_writegif_wiol($IO, \%input, $self->{IMG}); + } + + if (exists $input{'data'}) { + my $data = io_slurp($IO); + if (!$data) { + $self->{ERRSTR}='Could not slurp from buffer'; + return undef; + } + ${$input{data}} = $data; } - return $self; }