X-Git-Url: http://git.imager.perl.org/imager.git/blobdiff_plain/b4e32feb638df3703144924750363931660d4fbc..f364e8839e5ed816dc3e212d02fa08505d5644bb:/Imager.pm diff --git a/Imager.pm b/Imager.pm index 9ebcffca..611f59c3 100644 --- a/Imager.pm +++ b/Imager.pm @@ -145,11 +145,17 @@ use Imager::Font; BEGIN { require Exporter; - require DynaLoader; - - $VERSION = '0.41'; - @ISA = qw(Exporter DynaLoader); - bootstrap Imager $VERSION; + @ISA = qw(Exporter); + $VERSION = '0.47'; + eval { + require XSLoader; + XSLoader::load(Imager => $VERSION); + 1; + } or do { + require DynaLoader; + push @ISA, 'DynaLoader'; + bootstrap Imager $VERSION; + } } BEGIN { @@ -221,11 +227,19 @@ BEGIN { callsub => sub { my %hsh=@_; i_conv($hsh{image},$hsh{coef}); } }; - $filters{gradgen} ={ - callseq => ['image', 'xo', 'yo', 'colors', 'dist'], - defaults => { }, - callsub => sub { my %hsh=@_; i_gradgen($hsh{image}, $hsh{xo}, $hsh{yo}, $hsh{colors}, $hsh{dist}); } - }; + $filters{gradgen} = + { + callseq => ['image', 'xo', 'yo', 'colors', 'dist'], + defaults => { dist => 0 }, + callsub => + sub { + my %hsh=@_; + my @colors = @{$hsh{colors}}; + $_ = _color($_) + for @colors; + i_gradgen($hsh{image}, $hsh{xo}, $hsh{yo}, \@colors, $hsh{dist}); + } + }; $filters{nearest_color} ={ callseq => ['image', 'xo', 'yo', 'colors', 'dist'], @@ -346,9 +360,19 @@ BEGIN { callsub => sub { my %hsh = @_; + + # make sure the segments are specified with colors + my @segments; + for my $segment (@{$hsh{segments}}) { + my @new_segment = @$segment; + + $_ = _color($_) or die $Imager::ERRSTR."\n" for @new_segment[3,4]; + push @segments, \@new_segment; + } + i_fountain($hsh{image}, $hsh{xa}, $hsh{ya}, $hsh{xb}, $hsh{yb}, $hsh{ftype}, $hsh{repeat}, $hsh{combine}, $hsh{super_sample}, - $hsh{ssample_param}, $hsh{segments}); + $hsh{ssample_param}, \@segments); }, }; $filters{unsharpmask} = @@ -526,7 +550,12 @@ sub new { $self->{ERRSTR}=undef; # $self->{DEBUG}=$DEBUG; $self->{DEBUG} && print "Initialized Imager\n"; - if ($hsh{xsize} && $hsh{ysize}) { $self->img_set(%hsh); } + if (defined $hsh{xsize} && defined $hsh{ysize}) { + unless ($self->img_set(%hsh)) { + $Imager::ERRSTR = $self->{ERRSTR}; + return; + } + } return $self; } @@ -537,9 +566,14 @@ sub copy { my $self = shift; unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } + unless (defined wantarray) { + my @caller = caller; + warn "copy() called in void context - copy() returns the copied image at $caller[1] line $caller[2]\n"; + return; + } + my $newcopy=Imager->new(); - $newcopy->{IMG}=i_img_new(); - i_copy($newcopy->{IMG},$self->{IMG}); + $newcopy->{IMG} = i_copy($self->{IMG}); return $newcopy; } @@ -547,19 +581,68 @@ sub copy { sub paste { my $self = shift; - unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } - my %input=(left=>0, top=>0, @_); - unless($input{img}) { - $self->{ERRSTR}="no source image"; + + unless ($self->{IMG}) { + $self->_set_error('empty input image'); + return; + } + my %input=(left=>0, top=>0, src_minx => 0, src_miny => 0, @_); + my $src = $input{img} || $input{src}; + unless($src) { + $self->_set_error("no source image"); return; } $input{left}=0 if $input{left} <= 0; $input{top}=0 if $input{top} <= 0; - my $src=$input{img}; + my($r,$b)=i_img_info($src->{IMG}); + my ($src_left, $src_top) = @input{qw/src_minx src_miny/}; + my ($src_right, $src_bottom); + if ($input{src_coords}) { + ($src_left, $src_top, $src_right, $src_bottom) = @{$input{src_coords}} + } + else { + if (defined $input{src_maxx}) { + $src_right = $input{src_maxx}; + } + elsif (defined $input{width}) { + if ($input{width} <= 0) { + $self->_set_error("paste: width must me positive"); + return; + } + $src_right = $src_left + $input{width}; + } + else { + $src_right = $r; + } + if (defined $input{src_maxx}) { + $src_bottom = $input{src_maxy}; + } + elsif (defined $input{height}) { + if ($input{height} < 0) { + $self->_set_error("paste: height must be positive"); + return; + } + $src_bottom = $src_top + $input{height}; + } + else { + $src_bottom = $b; + } + } + + $src_right > $r and $src_right = $r; + $src_bottom > $r and $src_bottom = $b; + + if ($src_right <= $src_left + || $src_bottom < $src_top) { + $self->_set_error("nothing to paste"); + return; + } i_copyto($self->{IMG}, $src->{IMG}, - 0,0, $r, $b, $input{left}, $input{top}); + $src_left, $src_top, $src_right, $src_bottom, + $input{left}, $input{top}); + return $self; # What should go here?? } @@ -568,46 +651,111 @@ sub paste { sub crop { my $self=shift; unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } - my %hsh=(left=>0,right=>$self->getwidth(),top=>0,bottom=>$self->getheight(),@_); + + unless (defined wantarray) { + my @caller = caller; + warn "crop() called in void context - crop() returns the cropped image at $caller[1] line $caller[2]\n"; + return; + } - my ($w,$h,$l,$r,$b,$t)=($self->getwidth(),$self->getheight(), - @hsh{qw(left right bottom top)}); - $l=0 if not defined $l; - $t=0 if not defined $t; + my %hsh=@_; - $r||=$l+delete $hsh{'width'} if defined $l and exists $hsh{'width'}; - $b||=$t+delete $hsh{'height'} if defined $t and exists $hsh{'height'}; - $l||=$r-delete $hsh{'width'} if defined $r and exists $hsh{'width'}; - $t||=$b-delete $hsh{'height'} if defined $b and exists $hsh{'height'}; + my ($w, $h, $l, $r, $b, $t) = + @hsh{qw(width height left right bottom top)}; - $r=$self->getwidth if not defined $r; - $b=$self->getheight if not defined $b; + # work through the various possibilities + if (defined $l) { + if (defined $w) { + $r = $l + $w; + } + elsif (!defined $r) { + $r = $self->getwidth; + } + } + elsif (defined $r) { + if (defined $w) { + $l = $r - $w; + } + else { + $l = 0; + } + } + elsif (defined $w) { + $l = int(0.5+($self->getwidth()-$w)/2); + $r = $l + $w; + } + else { + $l = 0; + $r = $self->getwidth; + } + if (defined $t) { + if (defined $h) { + $b = $t + $h; + } + elsif (!defined $b) { + $b = $self->getheight; + } + } + elsif (defined $b) { + if (defined $h) { + $t = $b - $h; + } + else { + $t = 0; + } + } + elsif (defined $h) { + $t=int(0.5+($self->getheight()-$h)/2); + $b=$t+$h; + } + else { + $t = 0; + $b = $self->getheight; + } ($l,$r)=($r,$l) if $l>$r; ($t,$b)=($b,$t) if $t>$b; - if ($hsh{'width'}) { - $l=int(0.5+($w-$hsh{'width'})/2); - $r=$l+$hsh{'width'}; - } else { - $hsh{'width'}=$r-$l; - } - if ($hsh{'height'}) { - $b=int(0.5+($h-$hsh{'height'})/2); - $t=$h+$hsh{'height'}; - } else { - $hsh{'height'}=$b-$t; - } + $l < 0 and $l = 0; + $r > $self->getwidth and $r = $self->getwidth; + $t < 0 and $t = 0; + $b > $self->getheight and $b = $self->getheight; -# print "l=$l, r=$r, h=$hsh{'width'}\n"; -# print "t=$t, b=$b, w=$hsh{'height'}\n"; + if ($l == $r || $t == $b) { + $self->_set_error("resulting image would have no content"); + return; + } - my $dst=Imager->new(xsize=>$hsh{'width'}, ysize=>$hsh{'height'}, channels=>$self->getchannels()); + my $dst = $self->_sametype(xsize=>$r-$l, ysize=>$b-$t); i_copyto($dst->{IMG},$self->{IMG},$l,$t,$r,$b,0,0); return $dst; } +sub _sametype { + my ($self, %opts) = @_; + + $self->{IMG} or return $self->_set_error("Not a valid image"); + + my $x = $opts{xsize} || $self->getwidth; + my $y = $opts{ysize} || $self->getheight; + my $channels = $opts{channels} || $self->getchannels; + + my $out = Imager->new; + if ($channels == $self->getchannels) { + $out->{IMG} = i_sametype($self->{IMG}, $x, $y); + } + else { + $out->{IMG} = i_sametype_chans($self->{IMG}, $x, $y, $channels); + } + unless ($out->{IMG}) { + $self->{ERRSTR} = $self->_error_as_msg; + return; + } + + return $out; +} + # Sets an image to a certain size and channel number # if there was previously data in the image it is discarded @@ -637,6 +785,13 @@ sub img_set { $self->{IMG}=Imager::ImgRaw::new($hsh{'xsize'}, $hsh{'ysize'}, $hsh{'channels'}); } + + unless ($self->{IMG}) { + $self->{ERRSTR} = Imager->_error_as_msg(); + return; + } + + $self; } # created a masked version of the current image @@ -673,14 +828,24 @@ sub to_paletted { $opts = shift; } + unless (defined wantarray) { + my @caller = caller; + warn "to_paletted() called in void context - to_paletted() returns the converted image at $caller[1] line $caller[2]\n"; + return; + } + my $result = Imager->new; $result->{IMG} = i_img_to_pal($self->{IMG}, $opts); #print "Type ", i_img_type($result->{IMG}), "\n"; - $result->{IMG} or undef $result; - - return $result; + if ($result->{IMG}) { + return $result; + } + else { + $self->{ERRSTR} = $self->_error_as_msg; + return; + } } # convert a paletted (or any image) to an 8-bit/channel RGB images @@ -688,6 +853,12 @@ sub to_rgb8 { my $self = shift; my $result; + 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"; + return; + } + if ($self->{IMG}) { $result = Imager->new; $result->{IMG} = i_img_to_rgb($self->{IMG}) @@ -890,14 +1061,14 @@ sub settag { } } -my @needseekcb = qw/tiff/; -my %needseekcb = map { $_, $_ } @needseekcb; - sub _get_reader_io { - my ($self, $input, $type) = @_; + my ($self, $input) = @_; - if ($input->{fd}) { + if ($input->{io}) { + return $input->{io}, undef; + } + elsif ($input->{fd}) { return io_new_fd($input->{fd}); } elsif ($input->{fh}) { @@ -921,8 +1092,8 @@ sub _get_reader_io { return io_new_buffer($input->{data}); } elsif ($input->{callback} || $input->{readcb}) { - if ($needseekcb{$type} && !$input->{seekcb}) { - $self->_set_error("Format $type needs a seekcb parameter"); + if (!$input->{seekcb}) { + $self->_set_error("Need a seekcb parameter"); } if ($input->{maxbuffer}) { return io_new_cb($input->{writecb}, @@ -1005,207 +1176,153 @@ sub read { undef($self->{IMG}); } - # FIXME: Find the format here if not specified - # yes the code isn't here yet - next week maybe? - # Next week? Are you high or something? That comment - # has been there for half a year dude. - # Look, i just work here, ok? + my ($IO, $fh) = $self->_get_reader_io(\%input) or return; - if (!$input{'type'} and $input{file}) { - $input{'type'}=$FORMATGUESS->($input{file}); + unless ($input{'type'}) { + $input{'type'} = i_test_format_probe($IO, -1); } + unless ($input{'type'}) { - $self->_set_error('type parameter missing and not possible to guess from extension'); + $self->_set_error('type parameter missing and not possible to guess from extension'); return undef; } - if (!$formats{$input{'type'}}) { - $self->{ERRSTR}='format not supported'; return undef; - } - - 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'}) - or return; + unless ($formats{$input{'type'}}) { + $self->_set_error("format '$input{'type'}' not supported"); + 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; + # Setup data source + if ( $input{'type'} eq 'jpeg' ) { + ($self->{IMG},$self->{IPTCRAW}) = i_readjpeg_wiol( $IO ); + if ( !defined($self->{IMG}) ) { + $self->{ERRSTR}=$self->_error_as_msg(); 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 '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 ); + 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 '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 '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 '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 'png' ) { + $self->{IMG}=i_readpng_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 png 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 '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 '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 '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{'gif_consolidate'}) { if ($input{colors}) { - my $colors; - ($self->{IMG}, $colors) =i_readgif_wiol( $IO ); - if ($colors) { - ${ $input{colors} } = [ map { NC(@$_) } @$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 '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->{IMG} =i_readgif_wiol( $IO ); } - $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); - - 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; + else { + my $page = $input{'page'}; + defined $page or $page = 0; + $self->{IMG} = i_readgif_single_wiol( $IO, $page ); + if ($input{colors}) { + ${ $input{colors} } = + [ i_getcolors($self->{IMG}, 0, i_colorcount($self->{IMG})) ]; } - $self->{DEBUG} && print "loading a raw file\n"; } - } 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}=$self->_error_as_msg(); + 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 +1411,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 +1424,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 +1439,102 @@ 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(); + 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; } - $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'; + } else { + if (!i_writetiff_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'; - } - $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} = $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 (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; + } + $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; } - return $self; } + 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; } @@ -1569,9 +1680,14 @@ sub filter { } } if (defined($filters{$input{'type'}}{defaults})) { - %hsh=('image',$self->{IMG},%{$filters{$input{'type'}}{defaults}},%input); + %hsh=( image => $self->{IMG}, + imager => $self, + %{$filters{$input{'type'}}{defaults}}, + %input ); } else { - %hsh=('image',$self->{IMG},%input); + %hsh=( image => $self->{IMG}, + imager => $self, + %input ); } my @cs=@{$filters{$input{'type'}}{callseq}}; @@ -1582,7 +1698,14 @@ sub filter { } } - &{$filters{$input{'type'}}{callsub}}(%hsh); + eval { + local $SIG{__DIE__}; # we don't want this processed by confess, etc + &{$filters{$input{'type'}}{callsub}}(%hsh); + }; + if ($@) { + chomp($self->{ERRSTR} = $@); + return; + } my @b=keys %hsh; @@ -1592,6 +1715,25 @@ sub filter { return $self; } +sub register_filter { + my $class = shift; + my %hsh = ( defaults => {}, @_ ); + + defined $hsh{type} + or die "register_filter() with no type\n"; + defined $hsh{callsub} + or die "register_filter() with no callsub\n"; + defined $hsh{callseq} + or die "register_filter() with no callseq\n"; + + exists $filters{$hsh{type}} + and return; + + $filters{$hsh{type}} = \%hsh; + + return 1; +} + # Scale an image to requested size and return the scaled version sub scale { @@ -1600,28 +1742,65 @@ sub scale { my $img = Imager->new(); my $tmp = Imager->new(); - unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } + unless (defined wantarray) { + my @caller = caller; + warn "scale() called in void context - scale() returns the scaled image at $caller[1] line $caller[2]\n"; + return; + } + + unless ($self->{IMG}) { + $self->_set_error('empty input image'); + return undef; + } + # 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') { $opts{scalefactor}=min($xpix,$ypix); } - if ($opts{'type'} eq 'max') { $opts{scalefactor}=max($xpix,$ypix); } - } elsif ($opts{xpixels}) { $opts{scalefactor}=$opts{xpixels}/$self->getwidth(); } - elsif ($opts{ypixels}) { $opts{scalefactor}=$opts{ypixels}/$self->getheight(); } + my ($xpix, $ypix)=( $opts{xpixels}/$self->getwidth() , + $opts{ypixels}/$self->getheight() ); + if ($opts{'type'} eq 'min') { + $opts{scalefactor}=min($xpix,$ypix); + } + elsif ($opts{'type'} eq 'max') { + $opts{scalefactor}=max($xpix,$ypix); + } + else { + $self->_set_error('invalid value for type parameter'); + return undef; + } + } elsif ($opts{xpixels}) { + $opts{scalefactor}=$opts{xpixels}/$self->getwidth(); + } + elsif ($opts{ypixels}) { + $opts{scalefactor}=$opts{ypixels}/$self->getheight(); + } if ($opts{qtype} eq 'normal') { $tmp->{IMG}=i_scaleaxis($self->{IMG},$opts{scalefactor},0); - if ( !defined($tmp->{IMG}) ) { $self->{ERRSTR}='unable to scale image'; return undef; } + if ( !defined($tmp->{IMG}) ) { + $self->{ERRSTR}='unable to scale image'; + return undef; + } $img->{IMG}=i_scaleaxis($tmp->{IMG},$opts{scalefactor},1); - if ( !defined($img->{IMG}) ) { $self->{ERRSTR}='unable to scale image'; return undef; } - return $img; + if ( !defined($img->{IMG}) ) { + $self->{ERRSTR}='unable to scale image'; + return undef; + } + + return $img; } - if ($opts{'qtype'} eq 'preview') { - $img->{IMG}=i_scale_nn($self->{IMG},$opts{'scalefactor'},$opts{'scalefactor'}); - if ( !defined($img->{IMG}) ) { $self->{ERRSTR}='unable to scale image'; return undef; } + elsif ($opts{'qtype'} eq 'preview') { + $img->{IMG} = i_scale_nn($self->{IMG}, $opts{'scalefactor'}, + $opts{'scalefactor'}); + if ( !defined($img->{IMG}) ) { + $self->{ERRSTR}='unable to scale image'; + return undef; + } return $img; } - $self->{ERRSTR}='scale: invalid value for qtype'; return undef; + else { + $self->_set_error('invalid value for qtype parameter'); + return undef; + } } # Scales only along the X axis @@ -1630,6 +1809,12 @@ sub scaleX { my $self=shift; my %opts=(scalefactor=>0.5,@_); + unless (defined wantarray) { + my @caller = caller; + warn "scaleX() called in void context - scaleX() returns the scaled image at $caller[1] line $caller[2]\n"; + return; + } + unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } my $img = Imager->new(); @@ -1649,6 +1834,12 @@ sub scaleY { my $self=shift; my %opts=(scalefactor=>0.5,@_); + unless (defined wantarray) { + my @caller = caller; + warn "scaleY() called in void context - scaleY() returns the scaled image at $caller[1] line $caller[2]\n"; + return; + } + unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } my $img = Imager->new(); @@ -1800,9 +1991,14 @@ sub transform2 { $Imager::ERRSTR = Imager::Expr::error(); return; } + my $channels = $opts->{channels} || 3; + unless ($channels >= 1 && $channels <= 4) { + return Imager->_set_error("channels must be an integer between 1 and 4"); + } my $img = Imager->new(); - $img->{IMG} = i_transform2($opts->{width}, $opts->{height}, $code->code(), + $img->{IMG} = i_transform2($opts->{width}, $opts->{height}, + $channels, $code->code(), $code->nregs(), $code->cregs(), [ map { $_->{IMG} } @imgs ]); if (!defined $img->{IMG}) { @@ -1815,12 +2011,19 @@ sub transform2 { sub rubthrough { my $self=shift; - my %opts=(tx=>0,ty=>0,@_); + my %opts=(tx => 0,ty => 0, @_); unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } unless ($opts{src} && $opts{src}->{IMG}) { $self->{ERRSTR}='empty input image for source'; return undef; } - unless (i_rubthru($self->{IMG}, $opts{src}->{IMG}, $opts{tx},$opts{ty})) { + %opts = (src_minx => 0, + src_miny => 0, + src_maxx => $opts{src}->getwidth(), + src_maxy => $opts{src}->getheight(), + %opts); + + unless (i_rubthru($self->{IMG}, $opts{src}->{IMG}, $opts{tx}, $opts{ty}, + $opts{src_minx}, $opts{src_miny}, $opts{src_maxx}, $opts{src_maxy})) { $self->{ERRSTR} = $self->_error_as_msg(); return undef; } @@ -1842,6 +2045,13 @@ sub flip { sub rotate { my $self = shift; my %opts = @_; + + unless (defined wantarray) { + my @caller = caller; + warn "rotate() called in void context - rotate() returns the rotated image at $caller[1] line $caller[2]\n"; + return; + } + if (defined $opts{right}) { my $degrees = $opts{right}; if ($degrees < 0) { @@ -1870,7 +2080,13 @@ sub rotate { my $amount = $opts{radians} || $opts{degrees} * 3.1415926535 / 180; my $result = Imager->new; - if ($result->{IMG} = i_rotate_exact($self->{IMG}, $amount)) { + if ($opts{back}) { + $result->{IMG} = i_rotate_exact($self->{IMG}, $amount, $opts{back}); + } + else { + $result->{IMG} = i_rotate_exact($self->{IMG}, $amount); + } + if ($result->{IMG}) { return $result; } else { @@ -1879,7 +2095,7 @@ sub rotate { } } else { - $self->{ERRSTR} = "Only the 'right' parameter is available"; + $self->{ERRSTR} = "Only the 'right', 'radians' and 'degrees' parameters are available"; return undef; } } @@ -1888,14 +2104,27 @@ sub matrix_transform { my $self = shift; my %opts = @_; + unless (defined wantarray) { + my @caller = caller; + warn "copy() called in void context - copy() returns the copied image at $caller[1] line $caller[2]\n"; + return; + } + if ($opts{matrix}) { my $xsize = $opts{xsize} || $self->getwidth; my $ysize = $opts{ysize} || $self->getheight; my $result = Imager->new; - $result->{IMG} = i_matrix_transform($self->{IMG}, $xsize, $ysize, - $opts{matrix}) - or return undef; + if ($opts{back}) { + $result->{IMG} = i_matrix_transform($self->{IMG}, $xsize, $ysize, + $opts{matrix}, $opts{back}) + or return undef; + } + else { + $result->{IMG} = i_matrix_transform($self->{IMG}, $xsize, $ysize, + $opts{matrix}) + or return undef; + } return $result; } @@ -1965,8 +2194,6 @@ sub box { return $self; } -# Draws an arc - this routine SUCKS and is buggy - it sometimes doesn't work when the arc is a convex polygon - sub arc { my $self=shift; unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } @@ -1976,39 +2203,56 @@ sub arc { 'x'=>$self->getwidth()/2, 'y'=>$self->getheight()/2, 'd1'=>0, 'd2'=>361, @_); - 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{aa}) { + 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; + } + } + i_arc_aa_cfill($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'},$opts{'d1'}, + $opts{'d2'}, $opts{fill}{fill}); + } + else { + my $color = _color($opts{'color'}); + unless ($color) { + $self->{ERRSTR} = $Imager::ERRSTR; + return; + } + if ($opts{d1} == 0 && $opts{d2} == 361 && $opts{aa}) { + i_circle_aa($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{'r'}, + $color); + } + else { + i_arc_aa($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'}, + $opts{'d1'}, $opts{'d2'}, $color); } } - i_arc_cfill($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'},$opts{'d1'}, - $opts{'d2'}, $opts{fill}{fill}); } else { - my $color = _color($opts{'color'}); - unless ($color) { - $self->{ERRSTR} = $Imager::ERRSTR; - return; - } - if ($opts{d1} == 0 && $opts{d2} == 361 && $opts{aa}) { - i_circle_aa($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{'r'}, - $color); + 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; + } + } + i_arc_cfill($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'},$opts{'d1'}, + $opts{'d2'}, $opts{fill}{fill}); } else { - if ($opts{'d1'} <= $opts{'d2'}) { - i_arc($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'}, - $opts{'d1'}, $opts{'d2'}, $color); - } - else { - i_arc($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'}, - $opts{'d1'}, 361, $color); - i_arc($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'}, - 0, $opts{'d2'}, $color); + my $color = _color($opts{'color'}); + unless ($color) { + $self->{ERRSTR} = $Imager::ERRSTR; + return; } + i_arc($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'}, + $opts{'d1'}, $opts{'d2'}, $color); } } @@ -2279,6 +2523,111 @@ sub getpixel { $self; } +sub getscanline { + my $self = shift; + my %opts = ( type => '8bit', x=>0, @_); + + defined $opts{width} or $opts{width} = $self->getwidth - $opts{x}; + + unless (defined $opts{'y'}) { + $self->_set_error("missing y parameter"); + return; + } + + if ($opts{type} eq '8bit') { + return i_glin($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, + $opts{y}); + } + elsif ($opts{type} eq 'float') { + return i_glinf($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, + $opts{y}); + } + else { + $self->_set_error("invalid type parameter - must be '8bit' or 'float'"); + return; + } +} + +sub setscanline { + my $self = shift; + my %opts = ( x=>0, @_); + + unless (defined $opts{'y'}) { + $self->_set_error("missing y parameter"); + return; + } + + if (!$opts{type}) { + if (ref $opts{pixels} && @{$opts{pixels}}) { + # try to guess the type + if ($opts{pixels}[0]->isa('Imager::Color')) { + $opts{type} = '8bit'; + } + elsif ($opts{pixels}[0]->isa('Imager::Color::Float')) { + $opts{type} = 'float'; + } + else { + $self->_set_error("missing type parameter and could not guess from pixels"); + return; + } + } + else { + # default + $opts{type} = '8bit'; + } + } + + if ($opts{type} eq '8bit') { + if (ref $opts{pixels}) { + return i_plin($self->{IMG}, $opts{x}, $opts{'y'}, @{$opts{pixels}}); + } + else { + return i_plin($self->{IMG}, $opts{x}, $opts{'y'}, $opts{pixels}); + } + } + elsif ($opts{type} eq 'float') { + if (ref $opts{pixels}) { + return i_plinf($self->{IMG}, $opts{x}, $opts{'y'}, @{$opts{pixels}}); + } + else { + return i_plinf($self->{IMG}, $opts{x}, $opts{'y'}, $opts{pixels}); + } + } + else { + $self->_set_error("invalid type parameter - must be '8bit' or 'float'"); + return; + } +} + +sub getsamples { + my $self = shift; + my %opts = ( type => '8bit', x=>0, @_); + + defined $opts{width} or $opts{width} = $self->getwidth - $opts{x}; + + unless (defined $opts{'y'}) { + $self->_set_error("missing y parameter"); + return; + } + + unless ($opts{channels}) { + $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}}); + } + else { + $self->_set_error("invalid type parameter - must be '8bit' or 'float'"); + return; + } +} + # make an identity matrix of the given size sub _identity { my ($size) = @_; @@ -2295,6 +2644,12 @@ sub convert { my ($self, %opts) = @_; my $matrix; + unless (defined wantarray) { + my @caller = caller; + warn "convert() called in void context - convert() returns the converted image at $caller[1] line $caller[2]\n"; + return; + } + # the user can either specify a matrix or preset # the matrix overrides the preset if (!exists($opts{matrix})) { @@ -2481,8 +2836,17 @@ sub getmask { sub setmask { my $self = shift; my %opts = @_; - if (!defined($self->{IMG})) { $self->{ERRSTR} = 'image is empty'; return undef; } + if (!defined($self->{IMG})) { + $self->{ERRSTR} = 'image is empty'; + return undef; + } + unless (defined $opts{mask}) { + $self->_set_error("mask parameter required"); + return; + } i_img_setmask( $self->{IMG} , $opts{mask} ); + + 1; } # Get number of colors in an image @@ -2504,7 +2868,7 @@ sub string { my %input=('x'=>0, 'y'=>0, @_); $input{string}||=$input{text}; - unless(exists $input{string}) { + unless(defined $input{string}) { $self->{ERRSTR}="missing required parameter 'string'"; return; } @@ -2515,13 +2879,74 @@ sub string { } unless ($input{font}->draw(image=>$self, %input)) { - $self->{ERRSTR} = $self->_error_as_msg(); return; } return $self; } +sub align_string { + my $self = shift; + + my $img; + if (ref $self) { + unless ($self->{IMG}) { + $self->{ERRSTR}='empty input image'; + return; + } + $img = $self; + } + else { + $img = undef; + } + + my %input=('x'=>0, 'y'=>0, @_); + $input{string}||=$input{text}; + + unless(exists $input{string}) { + $self->_set_error("missing required parameter 'string'"); + return; + } + + unless($input{font}) { + $self->_set_error("missing required parameter 'font'"); + return; + } + + my @result; + unless (@result = $input{font}->align(image=>$img, %input)) { + return; + } + + return wantarray ? @result : $result[0]; +} + +my @file_limit_names = qw/width height bytes/; + +sub set_file_limits { + shift; + + my %opts = @_; + my %values; + + if ($opts{reset}) { + @values{@file_limit_names} = (0) x @file_limit_names; + } + else { + @values{@file_limit_names} = i_get_image_file_limits(); + } + + for my $key (keys %values) { + defined $opts{$key} and $values{$key} = $opts{$key}; + } + + i_set_image_file_limits($values{width}, $values{height}, $values{bytes}); +} + +sub get_file_limits { + i_get_image_file_limits(); +} + # Shortcuts that can be exported sub newcolor { Imager::Color->new(@_); } @@ -2638,7 +3063,15 @@ sub parseiptc { return (caption=>$caption,photogr=>$photogr,headln=>$headln,credit=>$credit); } -# Autoload methods go after =cut, and are processed by the autosplit program. +sub Inline { + my ($lang) = @_; + + $lang eq 'C' + or die "Only C language supported"; + + require Imager::ExtUtils; + return Imager::ExtUtils->inline_config; +} 1; __END__ @@ -2662,11 +3095,13 @@ Imager - Perl extension for Generating 24 bit Images my $format; my $img = Imager->new(); - $img->open(file=>$file) or die $img->errstr(); + # see Imager::Files for information on the read() method + $img->read(file=>$file) or die $img->errstr(); $file =~ s/\.[^.]*$//; # Create smaller version + # documented in Imager::Transformations my $thumb = $img->scale(scalefactor=>.3); # Autostretch individual channels @@ -2680,15 +3115,13 @@ Imager - Perl extension for Generating 24 bit Images if ($Imager::formats{$format}) { $file.="_low.$format"; print "Storing image as: $file\n"; + # documented in Imager::Files $thumb->write(file=>$file) or die $thumb->errstr; last SAVE; } } - - - =head1 DESCRIPTION Imager is a module for creating and altering images. It can read and @@ -2700,66 +3133,94 @@ render text and more. =over -=item Imager +=item * + +Imager - This document - Synopsis Example, Table of Contents and +Overview. + +=item * + +L - a brief introduction to Imager. -This document - Synopsis Example, Table of Contents and Overview. +=item * -=item Imager::ImageTypes +L - how to do various things with Imager. -Basics of constructing image objects with C: -Direct type/virtual images, RGB(A)/paletted images, 8/16/double -bits/channel, color maps, channel masks, image tags, color +=item * + +L - Basics of constructing image objects with +C: Direct type/virtual images, RGB(A)/paletted images, +8/16/double bits/channel, color maps, channel masks, image tags, color quantization. Also discusses basic image information methods. -=item Imager::Files +=item * -IO interaction, reading/writing images, format specific tags. +L - IO interaction, reading/writing images, format +specific tags. -=item Imager::Draw +=item * -Drawing Primitives, lines, boxes, circles, arcs, flood fill. +L - Drawing Primitives, lines, boxes, circles, arcs, +flood fill. -=item Imager::Color +=item * -Color specification. +L - Color specification. -=item Imager::Fill +=item * -Fill pattern specification. +L - Fill pattern specification. -=item Imager::Font +=item * -General font rendering, bounding boxes and font metrics. +L - General font rendering, bounding boxes and font +metrics. -=item Imager::Transformations +=item * -Copying, scaling, cropping, flipping, blending, pasting, convert and -map. +L - Copying, scaling, cropping, flipping, +blending, pasting, convert and map. -=item Imager::Engines +=item * -Programmable transformations through C, C -and C. +L - Programmable transformations through +C, C and C. -=item Imager::Filters +=item * -Filters, sharpen, blur, noise, convolve etc. and filter plugins. +L - Filters, sharpen, blur, noise, convolve etc. and +filter plugins. -=item Imager::Expr +=item * -Expressions for evaluation engine used by transform2(). +L - Expressions for evaluation engine used by +transform2(). -=item Imager::Matrix2d +=item * -Helper class for affine transformations. +L - Helper class for affine transformations. -=item Imager::Fountain +=item * -Helper for making gradient profiles. +L - Helper for making gradient profiles. -=back +=item * + +L - using Imager's C API + +=item * + +L - API function reference +=item * +L - using Imager's C API from Inline::C + +=item * + +L - tools to get access to Imager's C API. + +=back =head2 Basic Overview @@ -2767,7 +3228,7 @@ An Image object is created with C<$img = Imager-Enew()>. Examples: $img=Imager->new(); # create empty image - $img->open(file=>'lena.png',type=>'png') or # read image from file + $img->read(file=>'lena.png',type=>'png') or # read image from file die $img->errstr(); # give an explanation # if something failed @@ -2784,10 +3245,288 @@ C<$img-Eerrstr()>. 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. +with an image object. You can also call Cerrstr> to get this +value. + +The Cnew> method is described in detail in +L. + +=head1 METHOD INDEX + +Where to find information on methods for Imager class objects. + +addcolors() - L + +addtag() - L - add image tags + +arc() - L + +align_string() - L + +bits() - L - number of bits per sample for the +image + +box() - L + +circle() - L + +colorcount() - L + +convert() - L - +transform the color space + +copy() - L + +crop() - L - extract part of an image + +deltag() - L - delete image tags + +difference() - L + +errstr() - L<"Basic Overview"> + +filter() - L + +findcolor() - L - search the image palette, if it +has one + +flip() - L + +flood_fill() - L + +getchannels() - L + +getcolorcount() - L + +getcolors() - L - get colors from the image +palette, if it has one + +get_file_limits() - L + +getheight() - L + +getpixel() - L + +getsamples() - L + +getscanline() - L + +getwidth() - L + +img_set() - L + +line() - L + +map() - L - remap color +channel values + +masked() - L - make a masked image + +matrix_transform() - L + +maxcolors() - L + +new() - L + +open() - L - an alias for read() + +paste() - L - draw an image onto an image + +polygon() - L + +polyline() - L + +read() - L - read a single image from an image file + +read_multi() - L - read multiple images from an image +file + +rotate() - L + +rubthrough() - L - draw an image onto an +image and use the alpha channel + +scale() - L + +setscanline() - L + +scaleX() - L + +scaleY() - L + +setcolors() - L - set palette colors in +a paletted image + +setpixel() - L + +set_file_limits() - L + +string() - L - draw text on an image + +tags() - L - fetch image tags + +to_paletted() - L + +to_rgb8() - L + +transform() - L + +transform2() - L + +type() - L - type of image (direct vs paletted) + +virtual() - L - whether the image has it's own +data + +write() - L - write an image to a file -The Cnew> method is described in detail in the -Imager::ImageTypes manpage. +write_multi() - L - write multiple image to an image +file. + +=head1 CONCEPT INDEX + +animated GIF - L + +aspect ratio - L, +L, L + +blend - alpha blending one image onto another +L + +blur - L, L + +boxes, drawing - L + +changes between image - L + +color - L + +color names - L, L + +combine modes - L + +compare images - L + +contrast - L, L + +convolution - L + +cropping - L + +C images - L + +dpi - L + +drawing boxes - L + +drawing lines - L + +drawing text - L, L + +error message - L<"Basic Overview"> + +files, font - L + +files, image - L + +filling, types of fill - L + +filling, boxes - L + +filling, flood fill - L + +flood fill - L + +fonts - L + +fonts, drawing with - L, L, +L + +fonts, metrics - L, L + +fonts, multiple master - L + +fountain fill - L, +L, L, +L + +GIF files - L + +GIF files, animated - L + +gradient fill - L, +L, L, +L + +guassian blur - L + +hatch fills - L + +invert image - L + +JPEG - L + +limiting image sizes - L + +lines, drawing - L + +matrix - L, +L, +L + +metadata, image - L + +mosaic - L + +noise, filter - L + +noise, rendered - L, +L + +paste - L, +L + +pseudo-color image - L, +L + +posterize - L + +png files - L, L + +pnm - L + +rectangles, drawing - L + +resizing an image - L, +L + +saving an image - L + +scaling - L + +sharpen - L, L + +size, image - L, +L + +size, text - L + +tags, image metadata - L + +text, drawing - L, L, +L + +text, wrapping text in an area - L + +text, measuring - L, L + +tiles, color - L + +unsharp mask - L + +watermark - L + +writing an image to a file - L =head1 SUPPORT @@ -2800,14 +3539,29 @@ To subscribe send a message with C in the body to: or use the form at: - http://www.molar.is/en/lists/imager-devel/ - (annonymous is temporarily off due to spam) +=over + +L + +=back where you can also find the mailing list archive. If you're into IRC, you can typically find the developers in #Imager -on irc.rhizomatic.net. As with any IRC channel, the participants -could be occupied or asleep, so please be patient. +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 + +L + +=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 @@ -2815,21 +3569,23 @@ Bugs are listed individually for relevant pod pages. =head1 AUTHOR -Arnar M. Hrafnkelsson (addi@imager.perl.org) and Tony Cook -(tony@imager.perl.org) See the README for a complete list. +Arnar M. Hrafnkelsson and Tony Cook (tony@imager.perl.org) among +others. See the README for a complete list. =head1 SEE ALSO -perl(1), Imager::ImageTypes(3), Imager::Files(3), Imager::Draw(3), -Imager::Color(3), Imager::Fill(3), Imager::Font(3), -Imager::Transformations(3), Imager::Engines(3), Imager::Filters(3), -Imager::Expr(3), Imager::Matrix2d(3), Imager::Fountain(3) - -Affix::Infix2Postfix(3), Parse::RecDescent(3) -http://www.eecs.umich.edu/~addi/perl/Imager/ +L(1), L(3), L(3), +L(3), L(3), L(3), +L(3), L(3), +L(3), L(3), L(3), +L(3), L(3) -=cut +L +L(3), L(3) +Other perl imaging modules include: +L(3), L(3), L(3). +=cut