X-Git-Url: http://git.imager.perl.org/imager.git/blobdiff_plain/24ae6325e83017510d51aa4a938f70d6c1462845..5e2762ac320e5591435d6dd036881b004c7c0c37:/Imager.pm diff --git a/Imager.pm b/Imager.pm index 50dfe159..5b3dccd1 100644 --- a/Imager.pm +++ b/Imager.pm @@ -117,6 +117,7 @@ use Imager::Font; newcolour NC NF + NCF ); @EXPORT=qw( @@ -136,6 +137,7 @@ use Imager::Font; newcolor NF NC + NCF )], all => [@EXPORT_OK], default => [qw( @@ -143,10 +145,22 @@ use Imager::Font; unload_plugin )]); +# registered file readers +my %readers; + +# registered file writers +my %writers; + +# modules we attempted to autoload +my %attempted_to_load; + +# library keys that are image file formats +my %file_formats = map { $_ => 1 } qw/tiff pnm gif png jpeg raw bmp tga/; + BEGIN { require Exporter; @ISA = qw(Exporter); - $VERSION = '0.49'; + $VERSION = '0.61'; eval { require XSLoader; XSLoader::load(Imager => $VERSION); @@ -414,13 +428,19 @@ BEGIN { # initlize Imager # NOTE: this might be moved to an import override later on -#sub import { -# my $pack = shift; -# (look through @_ for special tags, process, and remove them); -# use Data::Dumper; -# print Dumper($pack); -# print Dumper(@_); -#} +sub import { + my $i = 1; + while ($i < @_) { + if ($_[$i] eq '-log-stderr') { + init_log(undef, 4); + splice(@_, $i, 1); + } + else { + ++$i; + } + } + goto &Exporter::import; +} sub init_log { i_init_log($_[0],$_[1]); @@ -528,12 +548,7 @@ sub _color { $result = Imager::Color->new(%$arg); } elsif ($copy =~ /^ARRAY\(/) { - if (grep $_ > 1, @$arg) { - $result = Imager::Color->new(@$arg); - } - else { - $result = Imager::Color::Float->new(@$arg); - } + $result = Imager::Color->new(@$arg); } else { $Imager::ERRSTR = "Not a color"; @@ -548,6 +563,15 @@ sub _color { return $result; } +sub _valid_image { + my ($self) = @_; + + $self->{IMG} and return 1; + + $self->_set_error('empty input image'); + + return; +} # # Methods to be called on objects. @@ -631,7 +655,7 @@ sub paste { else { $src_right = $r; } - if (defined $input{src_maxx}) { + if (defined $input{src_maxy}) { $src_bottom = $input{src_maxy}; } elsif (defined $input{height}) { @@ -647,7 +671,7 @@ sub paste { } $src_right > $r and $src_right = $r; - $src_bottom > $r and $src_bottom = $b; + $src_bottom > $b and $src_bottom = $b; if ($src_right <= $src_left || $src_bottom < $src_top) { @@ -741,7 +765,10 @@ sub crop { $self->_set_error("resulting image would have no content"); return; } - + if( $r < $l or $b < $t ) { + $self->_set_error("attempting to crop outside of the image"); + return; + } my $dst = $self->_sametype(xsize=>$r-$l, ysize=>$b-$t); i_copyto($dst->{IMG},$self->{IMG},$l,$t,$r,$b,0,0); @@ -871,7 +898,7 @@ sub to_rgb8 { unless (defined wantarray) { my @caller = caller; - warn "to_rgb8() called in void context - to_rgb8() returns the cropped image at $caller[1] line $caller[2]\n"; + warn "to_rgb8() called in void context - to_rgb8() returns the converted image at $caller[1] line $caller[2]\n"; return; } @@ -884,21 +911,70 @@ sub to_rgb8 { return $result; } +# convert a paletted (or any image) to an 8-bit/channel RGB images +sub to_rgb16 { + my $self = shift; + my $result; + + unless (defined wantarray) { + my @caller = caller; + warn "to_rgb16() called in void context - to_rgb8() returns the converted image at $caller[1] line $caller[2]\n"; + return; + } + + if ($self->{IMG}) { + $result = Imager->new; + $result->{IMG} = i_img_to_rgb16($self->{IMG}) + or undef $result; + } + + return $result; +} + sub addcolors { my $self = shift; my %opts = (colors=>[], @_); - @{$opts{colors}} or return undef; + unless ($self->{IMG}) { + $self->_set_error("empty input image"); + return; + } - $self->{IMG} and i_addcolors($self->{IMG}, @{$opts{colors}}); + my @colors = @{$opts{colors}} + or return undef; + + for my $color (@colors) { + $color = _color($color); + unless ($color) { + $self->_set_error($Imager::ERRSTR); + return; + } + } + + return i_addcolors($self->{IMG}, @colors); } sub setcolors { my $self = shift; my %opts = (start=>0, colors=>[], @_); - @{$opts{colors}} or return undef; - $self->{IMG} and i_setcolors($self->{IMG}, $opts{start}, @{$opts{colors}}); + unless ($self->{IMG}) { + $self->_set_error("empty input image"); + return; + } + + my @colors = @{$opts{colors}} + or return undef; + + for my $color (@colors) { + $color = _color($color); + unless ($color) { + $self->_set_error($Imager::ERRSTR); + return; + } + } + + return i_setcolors($self->{IMG}, $opts{start}, @colors); } sub getcolors { @@ -957,6 +1033,14 @@ sub virtual { $self->{IMG} and i_img_virtual($self->{IMG}); } +sub is_bilevel { + my ($self) = @_; + + $self->{IMG} or return; + + return i_img_is_monochrome($self->{IMG}); +} + sub tags { my ($self, %opts) = @_; @@ -1081,9 +1165,9 @@ sub settag { sub _get_reader_io { my ($self, $input) = @_; - if ($input->{io}) { - return $input->{io}, undef; - } + if ($input->{io}) { + return $input->{io}, undef; + } elsif ($input->{fd}) { return io_new_fd($input->{fd}); } @@ -1132,7 +1216,10 @@ sub _get_reader_io { sub _get_writer_io { my ($self, $input, $type) = @_; - if ($input->{fd}) { + if ($input->{io}) { + return $input->{io}; + } + elsif ($input->{fd}) { return io_new_fd($input->{fd}); } elsif ($input->{fh}) { @@ -1203,8 +1290,15 @@ sub read { return undef; } + _reader_autoload($input{type}); + + if ($readers{$input{type}} && $readers{$input{type}}{single}) { + return $readers{$input{type}}{single}->($self, $IO, %input); + } + unless ($formats{$input{'type'}}) { - $self->_set_error("format '$input{'type'}' not supported"); + my $read_types = join ', ', sort Imager->read_types(); + $self->_set_error("format '$input{'type'}' not supported - formats $read_types available for reading"); return; } @@ -1218,11 +1312,13 @@ sub read { return $self; } + my $allow_incomplete = $input{allow_incomplete}; + defined $allow_incomplete or $allow_incomplete = 0; + if ( $input{'type'} eq 'tiff' ) { my $page = $input{'page'}; defined $page or $page = 0; - # Fixme, check if that length parameter is ever needed - $self->{IMG}=i_readtiff_wiol( $IO, -1, $page ); + $self->{IMG}=i_readtiff_wiol( $IO, $allow_incomplete, $page ); if ( !defined($self->{IMG}) ) { $self->{ERRSTR}=$self->_error_as_msg(); return undef; } @@ -1231,7 +1327,7 @@ sub read { } if ( $input{'type'} eq 'pnm' ) { - $self->{IMG}=i_readpnm_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed + $self->{IMG}=i_readpnm_wiol( $IO, $allow_incomplete ); if ( !defined($self->{IMG}) ) { $self->{ERRSTR}='unable to read pnm image: '._error_as_msg(); return undef; @@ -1250,7 +1346,7 @@ sub read { } if ( $input{'type'} eq 'bmp' ) { - $self->{IMG}=i_readbmp_wiol( $IO ); + $self->{IMG}=i_readbmp_wiol( $IO, $allow_incomplete ); if ( !defined($self->{IMG}) ) { $self->{ERRSTR}=$self->_error_as_msg(); return undef; @@ -1280,7 +1376,7 @@ sub read { my $page = $input{'page'}; defined $page or $page = 0; $self->{IMG} = i_readgif_single_wiol( $IO, $page ); - if ($input{colors}) { + if ($self->{IMG} && $input{colors}) { ${ $input{colors} } = [ i_getcolors($self->{IMG}, 0, i_colorcount($self->{IMG})) ]; } @@ -1302,16 +1398,6 @@ sub read { $self->{DEBUG} && print "loading a tga file\n"; } - 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"; - } - - if ( $input{'type'} eq 'raw' ) { my %params=(datachannels=>3,storechannels=>3,interleave=>1,%input); @@ -1336,6 +1422,130 @@ sub read { return $self; } +sub register_reader { + my ($class, %opts) = @_; + + defined $opts{type} + or die "register_reader called with no type parameter\n"; + + my $type = $opts{type}; + + defined $opts{single} || defined $opts{multiple} + or die "register_reader called with no single or multiple parameter\n"; + + $readers{$type} = { }; + if ($opts{single}) { + $readers{$type}{single} = $opts{single}; + } + if ($opts{multiple}) { + $readers{$type}{multiple} = $opts{multiple}; + } + + return 1; +} + +sub register_writer { + my ($class, %opts) = @_; + + defined $opts{type} + or die "register_writer called with no type parameter\n"; + + my $type = $opts{type}; + + defined $opts{single} || defined $opts{multiple} + or die "register_writer called with no single or multiple parameter\n"; + + $writers{$type} = { }; + if ($opts{single}) { + $writers{$type}{single} = $opts{single}; + } + if ($opts{multiple}) { + $writers{$type}{multiple} = $opts{multiple}; + } + + return 1; +} + +sub read_types { + my %types = + ( + map { $_ => 1 } + keys %readers, + grep($file_formats{$_}, keys %formats), + qw(ico sgi), # formats not handled directly, but supplied with Imager + ); + + return keys %types; +} + +sub write_types { + my %types = + ( + map { $_ => 1 } + keys %writers, + grep($file_formats{$_}, keys %formats), + qw(ico sgi), # formats not handled directly, but supplied with Imager + ); + + return keys %types; +} + +# probes for an Imager::File::whatever module +sub _reader_autoload { + my $type = shift; + + return if $formats{$type} || $readers{$type}; + + return unless $type =~ /^\w+$/; + + my $file = "Imager/File/\U$type\E.pm"; + + unless ($attempted_to_load{$file}) { + eval { + ++$attempted_to_load{$file}; + require $file; + }; + if ($@) { + # try to get a reader specific module + my $file = "Imager/File/\U$type\EReader.pm"; + unless ($attempted_to_load{$file}) { + eval { + ++$attempted_to_load{$file}; + require $file; + }; + } + } + } +} + +# probes for an Imager::File::whatever module +sub _writer_autoload { + my $type = shift; + + return if $formats{$type} || $readers{$type}; + + return unless $type =~ /^\w+$/; + + my $file = "Imager/File/\U$type\E.pm"; + + unless ($attempted_to_load{$file}) { + eval { + ++$attempted_to_load{$file}; + require $file; + }; + if ($@) { + # try to get a writer specific module + my $file = "Imager/File/\U$type\EWriter.pm"; + unless ($attempted_to_load{$file}) { + eval { + ++$attempted_to_load{$file}; + require $file; + }; + } + } + } +} + sub _fix_gif_positions { my ($opts, $opt, $msg, @imgs) = @_; @@ -1450,96 +1660,112 @@ sub write { return undef; } - if (!$formats{$input{'type'}}) { $self->{ERRSTR}='format not supported'; return undef; } - - my ($IO, $fh) = $self->_get_writer_io(\%input, $input{'type'}) - or return undef; + _writer_autoload($input{type}); - if ($input{'type'} eq 'tiff') { - $self->_set_opts(\%input, "tiff_", $self) - or return undef; - $self->_set_opts(\%input, "exif_", $self) + my ($IO, $fh); + if ($writers{$input{type}} && $writers{$input{type}}{single}) { + ($IO, $fh) = $self->_get_writer_io(\%input, $input{'type'}) or return undef; - if (defined $input{class} && $input{class} eq 'fax') { - if (!i_writetiff_wiol_faxable($self->{IMG}, $IO, $input{fax_fine})) { - $self->{ERRSTR} = $self->_error_as_msg(); - return undef; - } - } else { - if (!i_writetiff_wiol($self->{IMG}, $IO)) { - $self->{ERRSTR} = $self->_error_as_msg(); - return undef; - } - } - } elsif ( $input{'type'} eq 'pnm' ) { - $self->_set_opts(\%input, "pnm_", $self) + $writers{$input{type}}{single}->($self, $IO, %input) or return undef; - if ( ! i_writeppm_wiol($self->{IMG},$IO) ) { - $self->{ERRSTR} = $self->_error_as_msg(); - 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} = $self->_error_as_msg(); - 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 ( !i_writetga_wiol($self->{IMG}, $IO, $input{wierdpack}, $input{compress}, $input{idstring}) ) { - $self->{ERRSTR}=$self->_error_as_msg(); + } + else { + if (!$formats{$input{'type'}}) { + my $write_types = join ', ', sort Imager->write_types(); + $self->_set_error("format '$input{'type'}' not supported - formats $write_types available for writing"); return undef; } - $self->{DEBUG} && print "writing a tga file\n"; - } elsif ( $input{'type'} eq 'gif' ) { - $self->_set_opts(\%input, "gif_", $self) + + ($IO, $fh) = $self->_get_writer_io(\%input, $input{'type'}) 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'; - } - if (!i_writegif_wiol($IO, \%input, $self->{IMG})) { - $self->{ERRSTR} = $self->_error_as_msg; - return; + + 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} = $self->_error_as_msg(); + return undef; + } + } else { + if (!i_writetiff_wiol($self->{IMG}, $IO)) { + $self->{ERRSTR} = $self->_error_as_msg(); + 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} = $self->_error_as_msg(); + 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} = $self->_error_as_msg(); + 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} = $self->_error_as_msg; + 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(); + 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'; + } + if (!i_writegif_wiol($IO, \%input, $self->{IMG})) { + $self->{ERRSTR} = $self->_error_as_msg; + return; + } } } @@ -1557,10 +1783,12 @@ sub write { sub write_multi { my ($class, $opts, @images) = @_; - if (!$opts->{'type'} && $opts->{'file'}) { - $opts->{'type'} = $FORMATGUESS->($opts->{'file'}); + my $type = $opts->{type}; + + if (!$type && $opts->{'file'}) { + $type = $FORMATGUESS->($opts->{'file'}); } - unless ($opts->{'type'}) { + unless ($type) { $class->_set_error('type parameter missing and not possible to guess from extension'); return; } @@ -1572,60 +1800,112 @@ sub write_multi { $class->_set_opts($opts, "i_", @images) or return; my @work = map $_->{IMG}, @images; - my ($IO, $file) = $class->_get_writer_io($opts, $opts->{'type'}) - or return undef; - if ($opts->{'type'} eq 'gif') { - $class->_set_opts($opts, "gif_", @images) - or return; - my $gif_delays = $opts->{gif_delays}; - local $opts->{gif_delays} = $gif_delays; - if ($opts->{gif_delays} && !ref $opts->{gif_delays}) { - # assume the caller wants the same delay for each frame - $opts->{gif_delays} = [ ($gif_delays) x @images ]; - } - my $res = i_writegif_wiol($IO, $opts, @work); - $res or $class->_set_error($class->_error_as_msg()); - return $res; - } - elsif ($opts->{'type'} eq 'tiff') { - $class->_set_opts($opts, "tiff_", @images) - or return; - $class->_set_opts($opts, "exif_", @images) - or return; - my $res; - $opts->{fax_fine} = 1 unless exists $opts->{fax_fine}; - if ($opts->{'class'} && $opts->{'class'} eq 'fax') { - $res = i_writetiff_multi_wiol_faxable($IO, $opts->{fax_fine}, @work); + + _writer_autoload($type); + + my ($IO, $file); + if ($writers{$type} && $writers{$type}{multiple}) { + ($IO, $file) = $class->_get_writer_io($opts, $type) + or return undef; + + $writers{$type}{multiple}->($class, $IO, $opts, @images) + or return undef; + } + else { + if (!$formats{$type}) { + my $write_types = join ', ', sort Imager->write_types(); + $class->_set_error("format '$type' not supported - formats $write_types available for writing"); + return undef; + } + + ($IO, $file) = $class->_get_writer_io($opts, $type) + or return undef; + + if ($type eq 'gif') { + $class->_set_opts($opts, "gif_", @images) + or return; + my $gif_delays = $opts->{gif_delays}; + local $opts->{gif_delays} = $gif_delays; + if ($opts->{gif_delays} && !ref $opts->{gif_delays}) { + # assume the caller wants the same delay for each frame + $opts->{gif_delays} = [ ($gif_delays) x @images ]; + } + unless (i_writegif_wiol($IO, $opts, @work)) { + $class->_set_error($class->_error_as_msg()); + return undef; + } + } + elsif ($type eq 'tiff') { + $class->_set_opts($opts, "tiff_", @images) + or return; + $class->_set_opts($opts, "exif_", @images) + or return; + my $res; + $opts->{fax_fine} = 1 unless exists $opts->{fax_fine}; + if ($opts->{'class'} && $opts->{'class'} eq 'fax') { + $res = i_writetiff_multi_wiol_faxable($IO, $opts->{fax_fine}, @work); + } + else { + $res = i_writetiff_multi_wiol($IO, @work); + } + unless ($res) { + $class->_set_error($class->_error_as_msg()); + return undef; + } } else { - $res = i_writetiff_multi_wiol($IO, @work); + if (@images == 1) { + unless ($images[0]->write(%$opts, io => $IO, type => $type)) { + return 1; + } + } + else { + $ERRSTR = "Sorry, write_multi doesn't support $type yet"; + return 0; + } } - $res or $class->_set_error($class->_error_as_msg()); - return $res; } - else { - $ERRSTR = "Sorry, write_multi doesn't support $opts->{'type'} yet"; - return 0; + + if (exists $opts->{'data'}) { + my $data = io_slurp($IO); + if (!$data) { + Imager->_set_error('Could not slurp from buffer'); + return undef; + } + ${$opts->{data}} = $data; } + return 1; } # read multiple images from a file sub read_multi { my ($class, %opts) = @_; - if ($opts{file} && !exists $opts{'type'}) { + my ($IO, $file) = $class->_get_reader_io(\%opts, $opts{'type'}) + or return; + + my $type = $opts{'type'}; + unless ($type) { + $type = i_test_format_probe($IO, -1); + } + + if ($opts{file} && !$type) { # guess the type - my $type = $FORMATGUESS->($opts{file}); - $opts{'type'} = $type; + $type = $FORMATGUESS->($opts{file}); } - unless ($opts{'type'}) { + + unless ($type) { $ERRSTR = "No type parameter supplied and it couldn't be guessed"; return; } - my ($IO, $file) = $class->_get_reader_io(\%opts, $opts{'type'}) - or return; - if ($opts{'type'} eq 'gif') { + _reader_autoload($type); + + if ($readers{$type} && $readers{$type}{multiple}) { + return $readers{$type}{multiple}->($IO, %opts); + } + + if ($type eq 'gif') { my @imgs; @imgs = i_readgif_multi_wiol($IO); if (@imgs) { @@ -1638,7 +1918,7 @@ sub read_multi { return; } } - elsif ($opts{'type'} eq 'tiff') { + elsif ($type eq 'tiff') { my @imgs = i_readtiff_multi_wiol($IO, -1); if (@imgs) { return map { @@ -1650,8 +1930,14 @@ sub read_multi { return; } } + else { + my $img = Imager->new; + if ($img->read(%opts, io => $IO, type => $type)) { + return ( $img ); + } + Imager->_set_error($img->errstr); + } - $ERRSTR = "Cannot read multiple images from $opts{'type'} files"; return; } @@ -1754,11 +2040,10 @@ sub register_filter { sub scale { my $self=shift; - my %opts=(scalefactor=>0.5,'type'=>'max',qtype=>'normal',@_); + my %opts=('type'=>'max',qtype=>'normal',@_); my $img = Imager->new(); my $tmp = Imager->new(); - - my $scalefactor = $opts{scalefactor}; + my ($x_scale, $y_scale); unless (defined wantarray) { my @caller = caller; @@ -1771,45 +2056,67 @@ sub scale { return undef; } + if ($opts{'xscalefactor'} && $opts{'yscalefactor'}) { + $x_scale = $opts{'xscalefactor'}; + $y_scale = $opts{'yscalefactor'}; + } + elsif ($opts{'xscalefactor'}) { + $x_scale = $opts{'xscalefactor'}; + $y_scale = $opts{'scalefactor'} || $x_scale; + } + elsif ($opts{'yscalefactor'}) { + $y_scale = $opts{'yscalefactor'}; + $x_scale = $opts{'scalefactor'} || $y_scale; + } + else { + $x_scale = $y_scale = $opts{'scalefactor'} || 0.5; + } + # work out the scaling if ($opts{xpixels} and $opts{ypixels} and $opts{'type'}) { my ($xpix, $ypix)=( $opts{xpixels} / $self->getwidth() , $opts{ypixels} / $self->getheight() ); if ($opts{'type'} eq 'min') { - $scalefactor = _min($xpix,$ypix); + $x_scale = $y_scale = _min($xpix,$ypix); } elsif ($opts{'type'} eq 'max') { - $scalefactor = _max($xpix,$ypix); + $x_scale = $y_scale = _max($xpix,$ypix); + } + elsif ($opts{'type'} eq 'nonprop' || $opts{'type'} eq 'non-proportional') { + $x_scale = $xpix; + $y_scale = $ypix; } else { $self->_set_error('invalid value for type parameter'); return undef; } } elsif ($opts{xpixels}) { - $scalefactor = $opts{xpixels} / $self->getwidth(); + $x_scale = $y_scale = $opts{xpixels} / $self->getwidth(); } elsif ($opts{ypixels}) { - $scalefactor = $opts{ypixels}/$self->getheight(); + $x_scale = $y_scale = $opts{ypixels}/$self->getheight(); } elsif ($opts{constrain} && ref $opts{constrain} && $opts{constrain}->can('constrain')) { # we've been passed an Image::Math::Constrain object or something # that looks like one + my $scalefactor; (undef, undef, $scalefactor) = $opts{constrain}->constrain($self->getwidth, $self->getheight); unless ($scalefactor) { $self->_set_error('constrain method failed on constrain parameter'); return undef; } + $x_scale = $y_scale = $scalefactor; } if ($opts{qtype} eq 'normal') { - $tmp->{IMG} = i_scaleaxis($self->{IMG}, $scalefactor, 0); + $tmp->{IMG} = i_scaleaxis($self->{IMG}, $x_scale, 0); if ( !defined($tmp->{IMG}) ) { $self->{ERRSTR} = 'unable to scale image'; return undef; } - $img->{IMG}=i_scaleaxis($tmp->{IMG}, $scalefactor, 1); + $img->{IMG}=i_scaleaxis($tmp->{IMG}, $y_scale, 1); if ( !defined($img->{IMG}) ) { $self->{ERRSTR}='unable to scale image'; return undef; @@ -1818,13 +2125,25 @@ sub scale { return $img; } elsif ($opts{'qtype'} eq 'preview') { - $img->{IMG} = i_scale_nn($self->{IMG}, $scalefactor, $scalefactor); + $img->{IMG} = i_scale_nn($self->{IMG}, $x_scale, $y_scale); if ( !defined($img->{IMG}) ) { $self->{ERRSTR}='unable to scale image'; return undef; } return $img; } + elsif ($opts{'qtype'} eq 'mixing') { + my $new_width = int(0.5 + $self->getwidth * $x_scale); + my $new_height = int(0.5 + $self->getheight * $y_scale); + $new_width >= 1 or $new_width = 1; + $new_height >= 1 or $new_height = 1; + $img->{IMG} = i_scale_mixing($self->{IMG}, $new_width, $new_height); + unless ($img->{IMG}) { + $self->_set_error(Imager->_error_as_meg); + return; + } + return $img; + } else { $self->_set_error('invalid value for qtype parameter'); return undef; @@ -2484,26 +2803,69 @@ sub flood_fill { return undef; } - if ($opts{fill}) { - unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) { - # assume it's a hash ref - require 'Imager/Fill.pm'; - unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) { - $self->{ERRSTR} = $Imager::ERRSTR; - return; + if ($opts{border}) { + my $border = _color($opts{border}); + unless ($border) { + $self->_set_error($Imager::ERRSTR); + return; + } + if ($opts{fill}) { + unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) { + # assume it's a hash ref + require Imager::Fill; + unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) { + $self->{ERRSTR} = $Imager::ERRSTR; + return; + } } + $rc = i_flood_cfill_border($self->{IMG}, $opts{'x'}, $opts{'y'}, + $opts{fill}{fill}, $border); + } + else { + my $color = _color($opts{'color'}); + unless ($color) { + $self->{ERRSTR} = $Imager::ERRSTR; + return; + } + $rc = i_flood_fill_border($self->{IMG}, $opts{'x'}, $opts{'y'}, + $color, $border); + } + if ($rc) { + return $self; + } + else { + $self->{ERRSTR} = $self->_error_as_msg(); + return; } - $rc = i_flood_cfill($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{fill}{fill}); } else { - my $color = _color($opts{'color'}); - unless ($color) { - $self->{ERRSTR} = $Imager::ERRSTR; + if ($opts{fill}) { + unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) { + # assume it's a hash ref + require 'Imager/Fill.pm'; + unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) { + $self->{ERRSTR} = $Imager::ERRSTR; + return; + } + } + $rc = i_flood_cfill($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{fill}{fill}); + } + else { + my $color = _color($opts{'color'}); + unless ($color) { + $self->{ERRSTR} = $Imager::ERRSTR; + return; + } + $rc = i_flood_fill($self->{IMG}, $opts{'x'}, $opts{'y'}, $color); + } + if ($rc) { + return $self; + } + else { + $self->{ERRSTR} = $self->_error_as_msg(); return; } - $rc = i_flood_fill($self->{IMG}, $opts{'x'}, $opts{'y'}, $color); - } - if ($rc) { $self; } else { $self->{ERRSTR} = $self->_error_as_msg(); return (); } + } } sub setpixel { @@ -2523,25 +2885,32 @@ sub setpixel { if (ref $x && ref $y) { unless (@$x == @$y) { $self->{ERRSTR} = 'length of x and y mismatch'; - return undef; + return; } + my $set = 0; if ($color->isa('Imager::Color')) { for my $i (0..$#{$opts{'x'}}) { - i_ppix($self->{IMG}, $x->[$i], $y->[$i], $color); + i_ppix($self->{IMG}, $x->[$i], $y->[$i], $color) + or ++$set; } } else { for my $i (0..$#{$opts{'x'}}) { - i_ppixf($self->{IMG}, $x->[$i], $y->[$i], $color); + i_ppixf($self->{IMG}, $x->[$i], $y->[$i], $color) + or ++$set; } } + $set or return; + return $set; } else { if ($color->isa('Imager::Color')) { - i_ppix($self->{IMG}, $x, $y, $color); + i_ppix($self->{IMG}, $x, $y, $color) + and return; } else { - i_ppixf($self->{IMG}, $x, $y, $color); + i_ppixf($self->{IMG}, $x, $y, $color) + and return; } } @@ -2594,6 +2963,8 @@ sub getscanline { my $self = shift; my %opts = ( type => '8bit', x=>0, @_); + $self->_valid_image or return; + defined $opts{width} or $opts{width} = $self->getwidth - $opts{x}; unless (defined $opts{'y'}) { @@ -2603,11 +2974,19 @@ sub getscanline { if ($opts{type} eq '8bit') { return i_glin($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, - $opts{y}); + $opts{'y'}); } elsif ($opts{type} eq 'float') { return i_glinf($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, - $opts{y}); + $opts{'y'}); + } + elsif ($opts{type} eq 'index') { + unless (i_img_type($self->{IMG})) { + $self->_set_error("type => index only valid on paletted images"); + return; + } + return i_gpal($self->{IMG}, $opts{x}, $opts{x} + $opts{width}, + $opts{'y'}); } else { $self->_set_error("invalid type parameter - must be '8bit' or 'float'"); @@ -2619,6 +2998,8 @@ sub setscanline { my $self = shift; my %opts = ( x=>0, @_); + $self->_valid_image or return; + unless (defined $opts{'y'}) { $self->_set_error("missing y parameter"); return; @@ -2660,6 +3041,14 @@ sub setscanline { return i_plinf($self->{IMG}, $opts{x}, $opts{'y'}, $opts{pixels}); } } + elsif ($opts{type} eq 'index') { + if (ref $opts{pixels}) { + return i_ppal($self->{IMG}, $opts{x}, $opts{'y'}, @{$opts{pixels}}); + } + else { + return i_ppal_p($self->{IMG}, $opts{x}, $opts{'y'}, $opts{pixels}); + } + } else { $self->_set_error("invalid type parameter - must be '8bit' or 'float'"); return; @@ -2668,7 +3057,7 @@ sub setscanline { sub getsamples { my $self = shift; - my %opts = ( type => '8bit', x=>0, @_); + my %opts = ( type => '8bit', x=>0, offset => 0, @_); defined $opts{width} or $opts{width} = $self->getwidth - $opts{x}; @@ -2681,18 +3070,103 @@ sub getsamples { $opts{channels} = [ 0 .. $self->getchannels()-1 ]; } - if ($opts{type} eq '8bit') { - return i_gsamp($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, - $opts{y}, @{$opts{channels}}); - } - elsif ($opts{type} eq 'float') { - return i_gsampf($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, - $opts{y}, @{$opts{channels}}); + if ($opts{target}) { + my $target = $opts{target}; + my $offset = $opts{offset}; + if ($opts{type} eq '8bit') { + my @samples = i_gsamp($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, + $opts{y}, @{$opts{channels}}) + or return; + @{$target}{$offset .. $offset + @samples - 1} = @samples; + return scalar(@samples); + } + elsif ($opts{type} eq 'float') { + my @samples = i_gsampf($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, + $opts{y}, @{$opts{channels}}); + @{$target}{$offset .. $offset + @samples - 1} = @samples; + return scalar(@samples); + } + elsif ($opts{type} =~ /^(\d+)bit$/) { + my $bits = $1; + + my @data; + my $count = i_gsamp_bits($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, + $opts{y}, $bits, $target, + $offset, @{$opts{channels}}); + unless (defined $count) { + $self->_set_error(Imager->_error_as_msg); + return; + } + + return $count; + } + else { + $self->_set_error("invalid type parameter - must be '8bit' or 'float'"); + return; + } } else { - $self->_set_error("invalid type parameter - must be '8bit' or 'float'"); + if ($opts{type} eq '8bit') { + return i_gsamp($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, + $opts{y}, @{$opts{channels}}); + } + elsif ($opts{type} eq 'float') { + return i_gsampf($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, + $opts{y}, @{$opts{channels}}); + } + elsif ($opts{type} =~ /^(\d+)bit$/) { + my $bits = $1; + + my @data; + i_gsamp_bits($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, + $opts{y}, $bits, \@data, 0, @{$opts{channels}}) + or return; + return @data; + } + else { + $self->_set_error("invalid type parameter - must be '8bit' or 'float'"); + return; + } + } +} + +sub setsamples { + my $self = shift; + my %opts = ( x => 0, offset => 0, @_ ); + + unless ($self->{IMG}) { + $self->_set_error('setsamples: empty input image'); + return; + } + + unless(defined $opts{data} && ref $opts{data}) { + $self->_set_error('setsamples: data parameter missing or invalid'); return; } + + unless ($opts{channels}) { + $opts{channels} = [ 0 .. $self->getchannels()-1 ]; + } + + unless ($opts{type} && $opts{type} =~ /^(\d+)bit$/) { + $self->_set_error('setsamples: type parameter missing or invalid'); + return; + } + my $bits = $1; + + unless (defined $opts{width}) { + $opts{width} = $self->getwidth() - $opts{x}; + } + + my $count = i_psamp_bits($self->{IMG}, $opts{x}, $opts{y}, $bits, + $opts{channels}, $opts{data}, $opts{offset}, + $opts{width}); + unless (defined $count) { + $self->_set_error(Imager->_error_as_msg); + return; + } + + return $count; } # make an identity matrix of the given size @@ -2802,9 +3276,9 @@ sub convert { $matrix = $opts{matrix}; } - my $new = Imager->new(); - $new->{IMG} = i_img_new(); - unless (i_convert($new->{IMG}, $self->{IMG}, $matrix)) { + my $new = Imager->new; + $new->{IMG} = i_convert($self->{IMG}, $matrix); + unless ($new->{IMG}) { # most likely a bad matrix $self->{ERRSTR} = _error_as_msg(); return undef; @@ -2926,6 +3400,61 @@ sub getcolorcount { return ($rc==-1? undef : $rc); } +# Returns a reference to a hash. The keys are colour named (packed) and the +# values are the number of pixels in this colour. +sub getcolorusagehash { + my $self = shift; + + my %opts = ( maxcolors => 2**30, @_ ); + my $max_colors = $opts{maxcolors}; + unless (defined $max_colors && $max_colors > 0) { + $self->_set_error('maxcolors must be a positive integer'); + return; + } + + unless (defined $self->{IMG}) { + $self->_set_error('empty input image'); + return; + } + + my $channels= $self->getchannels; + # We don't want to look at the alpha channel, because some gifs using it + # doesn't define it for every colour (but only for some) + $channels -= 1 if $channels == 2 or $channels == 4; + my %color_use; + my $height = $self->getheight; + for my $y (0 .. $height - 1) { + my $colors = $self->getsamples('y' => $y, channels => [ 0 .. $channels - 1 ]); + while (length $colors) { + $color_use{ substr($colors, 0, $channels, '') }++; + } + keys %color_use > $max_colors + and return; + } + return \%color_use; +} + +# This will return a ordered array of the colour usage. Kind of the sorted +# version of the values of the hash returned by getcolorusagehash. +# You might want to add safety checks and change the names, etc... +sub getcolorusage { + my $self = shift; + + my %opts = ( maxcolors => 2**30, @_ ); + my $max_colors = $opts{maxcolors}; + unless (defined $max_colors && $max_colors > 0) { + $self->_set_error('maxcolors must be a positive integer'); + return; + } + + unless (defined $self->{IMG}) { + $self->_set_error('empty input image'); + return undef; + } + + return i_get_anonymous_color_histo($self->{IMG}, $max_colors); +} + # draw string to an image sub string { @@ -2933,7 +3462,7 @@ sub string { unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } my %input=('x'=>0, 'y'=>0, @_); - $input{string}||=$input{text}; + defined($input{string}) or $input{string} = $input{text}; unless(defined $input{string}) { $self->{ERRSTR}="missing required parameter 'string'"; @@ -3018,6 +3547,7 @@ sub get_file_limits { sub newcolor { Imager::Color->new(@_); } sub newfont { Imager::Font->new(@_); } +sub NCF { Imager::Color::Float->new(@_) } *NC=*newcolour=*newcolor; *NF=*newfont; @@ -3056,9 +3586,10 @@ sub def_guess_type { return 'png' if ($ext eq "png"); return 'bmp' if ($ext eq "bmp" || $ext eq "dib"); return 'tga' if ($ext eq "tga"); - return 'rgb' if ($ext eq "rgb"); + return 'sgi' if ($ext eq "rgb" || $ext eq "bw" || $ext eq "sgi" || $ext eq "rgba"); return 'gif' if ($ext eq "gif"); return 'raw' if ($ext eq "raw"); + return lc $ext; # best guess return (); } @@ -3203,7 +3734,7 @@ render text and more. =item * -Imager - This document - Synopsis Example, Table of Contents and +Imager - This document - Synopsis, Example, Table of Contents and Overview. =item * @@ -3307,14 +3838,32 @@ or if you want to create an empty image: This example creates a completely black image of width 400 and height 300 and 4 channels. -When an operation fails which can be directly associated with an image -the error message is stored can be retrieved with -C<$img-Eerrstr()>. +=head1 ERROR HANDLING + +In general a method will return false when it fails, if it does use the errstr() method to find out why: + +=over + +=item errstr -In cases where no image object is associated with an operation -C<$Imager::ERRSTR> is used to report errors not directly associated -with an image object. You can also call Cerrstr> to get this -value. +Returns the last error message in that context. + +If the last error you received was from calling an object method, such +as read, call errstr() as an object method to find out why: + + my $image = Imager->new; + $image->read(file => 'somefile.gif') + or die $image->errstr; + +If it was a class method then call errstr() as a class method: + + my @imgs = Imager->read_multi(file => 'somefile.gif') + or die Imager->errstr; + +Note that in some cases object methods are implemented in terms of +class methods so a failing object method may set both. + +=back The Cnew> method is described in detail in L. @@ -3327,10 +3876,10 @@ addcolors() - L addtag() - L - add image tags -arc() - L - align_string() - L +arc() - L + bits() - L - number of bits per sample for the image @@ -3347,6 +3896,8 @@ copy() - L crop() - L - extract part of an image +def_guess_type() - L + deltag() - L - delete image tags difference() - L @@ -3369,10 +3920,16 @@ getcolorcount() - L getcolors() - L - get colors from the image palette, if it has one +getcolorusage() - L + +getcolorusagehash() - L + get_file_limits() - L getheight() - L +getmask() - L + getpixel() - L getsamples() - L @@ -3383,8 +3940,14 @@ getwidth() - L img_set() - L +init() - L + +is_bilevel() - L + line() - L +load_plugin() - L + map() - L - remap color channel values @@ -3394,10 +3957,25 @@ matrix_transform() - L maxcolors() - L +NC() - L + +NCF() - L + new() - L +newcolor() - L + +newcolour() - L + +newfont() - L + +NF() - L + open() - L - an alias for read() +parseiptc() - L - parse IPTC data from a JPEG +image + paste() - L - draw an image onto an image polygon() - L @@ -3409,6 +3987,15 @@ read() - L - read a single image from an image file read_multi() - L - read multiple images from an image file +read_types() - L - list image types Imager +can read. + +register_filter() - L + +register_reader() - L + +register_writer() - L + rotate() - L rubthrough() - L - draw an image onto an @@ -3423,20 +4010,26 @@ scaleY() - L setcolors() - L - set palette colors in a paletted image +set_file_limits() - L + +setmask() - L + setpixel() - L +setsamples() - L + setscanline() - L settag() - L -set_file_limits() - L - string() - L - draw text on an image tags() - L - fetch image tags to_paletted() - L +to_rgb16() - L + to_rgb8() - L transform() - L @@ -3445,6 +4038,8 @@ transform2() - L type() - L - type of image (direct vs paletted) +unload_plugin() - L + virtual() - L - whether the image has it's own data @@ -3453,9 +4048,12 @@ write() - L - write an image to a file write_multi() - L - write multiple image to an image file. +write_types() - L - list image types Imager +can write. + =head1 CONCEPT INDEX -animated GIF - L +animated GIF - L aspect ratio - L, L, L @@ -3483,15 +4081,18 @@ convolution - L cropping - L +CUR files - L + C images - L -dpi - L +dpi - L, +L drawing boxes - L drawing lines - L -drawing text - L, L +drawing text - L, L error message - L<"Basic Overview"> @@ -3509,8 +4110,8 @@ flood fill - L fonts - L -fonts, drawing with - L, L, -L +fonts, drawing with - L, +L, L fonts, metrics - L, L @@ -3528,10 +4129,14 @@ gradient fill - L, L, L, L +grayscale, convert image to - L + guassian blur - L hatch fills - L +ICO files - L + invert image - L JPEG - L @@ -3570,10 +4175,14 @@ rectangles, drawing - L resizing an image - L, L +RGB (SGI) files - L + saving an image - L scaling - L +SGI files - L + sharpen - L, L size, image - L, @@ -3600,8 +4209,7 @@ writing an image to a file - L =head1 SUPPORT -You can ask for help, report bugs or express your undying love for -Imager on the Imager-devel mailing list. +The best place to get help with Imager is the mailing list. To subscribe send a message with C in the body to: @@ -3617,10 +4225,6 @@ L where you can also find the mailing list archive. -If you're into IRC, you can typically find the developers in #Imager -on irc.perl.org. As with any IRC channel, the participants could be -occupied or asleep, so please be patient. - You can report bugs by pointing your browser at: =over @@ -3629,18 +4233,59 @@ L =back +or by sending an email to: + +=over + +bug-Imager@rt.cpan.org + +=back + Please remember to include the versions of Imager, perl, supporting libraries, and any relevant code. If you have specific images that cause the problems, please include those too. -=head1 BUGS +If you don't want to publish your email address on a mailing list you +can use CPAN::Forum: + + http://www.cpanforum.com/dist/Imager -Bugs are listed individually for relevant pod pages. +You will need to register to post. + +=head1 CONTRIBUTING TO IMAGER + +=head2 Feedback + +I like feedback. + +If you like or dislike Imager, you can add a public review of Imager +at CPAN Ratings: + + http://cpanratings.perl.org/dist/Imager + +This requires a Bitcard Account (http://www.bitcard.org). + +You can also send email to the maintainer below. + +If you send me a bug report via email, it will be copied to RT. + +=head2 Patches + +I accept patches, preferably against the main branch in subversion. +You should include an explanation of the reason for why the patch is +needed or useful. + +Your patch should include regression tests where possible, otherwise +it will be delayed until I get a chance to write them. =head1 AUTHOR -Arnar M. Hrafnkelsson and Tony Cook (tony@imager.perl.org) among -others. See the README for a complete list. +Tony Cook is the current maintainer for Imager. + +Arnar M. Hrafnkelsson is the original author of Imager. + +Many others have contributed to Imager, please see the README for a +complete list. =head1 SEE ALSO