]> git.imager.perl.org - imager.git/blobdiff - Imager.pm
avoid a possible sign-extension for offsets/sizes in SGI
[imager.git] / Imager.pm
index 16d1352f1742875a73a1f9015a8121fa1538977e..52957805ddea92ad073729c236598786421f032f 100644 (file)
--- a/Imager.pm
+++ b/Imager.pm
@@ -1,11 +1,12 @@
 package Imager;
 
 use strict;
 package Imager;
 
 use strict;
-use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS %formats $DEBUG %filters %DSOs $ERRSTR $fontstate %OPCODES $I2P $FORMATGUESS $warn_obsolete);
+use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS %formats $DEBUG %filters %DSOs $ERRSTR %OPCODES $I2P $FORMATGUESS $warn_obsolete);
 use IO::File;
 use IO::File;
-
+use Scalar::Util;
 use Imager::Color;
 use Imager::Font;
 use Imager::Color;
 use Imager::Font;
+use Config;
 
 @EXPORT_OK = qw(
                init
 
 @EXPORT_OK = qw(
                init
@@ -19,17 +20,11 @@ use Imager::Font;
                unload_plugin
 
                i_list_formats
                unload_plugin
 
                i_list_formats
-               i_has_format
 
                i_color_new
                i_color_set
                i_color_info
 
 
                i_color_new
                i_color_set
                i_color_info
 
-               i_img_empty
-               i_img_empty_ch
-               i_img_exorcise
-               i_img_destroy
-
                i_img_info
 
                i_img_setmask
                i_img_info
 
                i_img_setmask
@@ -61,37 +56,11 @@ use Imager::Font;
 
                i_img_diff
 
 
                i_img_diff
 
-               i_init_fonts
-               i_t1_new
-               i_t1_destroy
-               i_t1_set_aa
-               i_t1_cp
-               i_t1_text
-               i_t1_bbox
-
                i_tt_set_aa
                i_tt_cp
                i_tt_text
                i_tt_bbox
 
                i_tt_set_aa
                i_tt_cp
                i_tt_text
                i_tt_bbox
 
-               i_readjpeg_wiol
-               i_writejpeg_wiol
-
-               i_readtiff_wiol
-               i_writetiff_wiol
-               i_writetiff_wiol_faxable
-
-               i_readpng_wiol
-               i_writepng_wiol
-
-               i_readgif
-               i_readgif_wiol
-               i_readgif_callback
-               i_writegif
-               i_writegifmc
-               i_writegif_gen
-               i_writegif_callback
-
                i_readpnm_wiol
                i_writeppm_wiol
 
                i_readpnm_wiol
                i_writeppm_wiol
 
@@ -121,14 +90,6 @@ use Imager::Font;
 );
 
 @EXPORT=qw(
 );
 
 @EXPORT=qw(
-          init_log
-          i_list_formats
-          i_has_format
-          malloc_state
-          i_color_new
-
-          i_img_empty
-          i_img_empty_ch
          );
 
 %EXPORT_TAGS=
          );
 
 %EXPORT_TAGS=
@@ -154,6 +115,13 @@ my %writers;
 # modules we attempted to autoload
 my %attempted_to_load;
 
 # modules we attempted to autoload
 my %attempted_to_load;
 
+# errors from loading files
+my %file_load_errors;
+
+# what happened when we tried to load
+my %reader_load_errors;
+my %writer_load_errors;
+
 # library keys that are image file formats
 my %file_formats = map { $_ => 1 } qw/tiff pnm gif png jpeg raw bmp tga/;
 
 # library keys that are image file formats
 my %file_formats = map { $_ => 1 } qw/tiff pnm gif png jpeg raw bmp tga/;
 
@@ -172,27 +140,31 @@ my %defaults;
 
 BEGIN {
   require Exporter;
 
 BEGIN {
   require Exporter;
-  @ISA = qw(Exporter);
-  $VERSION = '0.74';
-  eval {
-    require XSLoader;
-    XSLoader::load(Imager => $VERSION);
-    1;
-  } or do {
-    require DynaLoader;
-    push @ISA, 'DynaLoader';
-    bootstrap Imager $VERSION;
+  my $ex_version = eval $Exporter::VERSION;
+  if ($ex_version < 5.57) {
+    @ISA = qw(Exporter);
   }
   }
+  $VERSION = '1.009';
+  require XSLoader;
+  XSLoader::load(Imager => $VERSION);
 }
 
 }
 
-BEGIN {
-  Imager::Font::__init();
-  for(i_list_formats()) { $formats{$_}++; }
+my %formats_low;
+my %format_classes =
+  (
+   png => "Imager::File::PNG",
+   gif => "Imager::File::GIF",
+   tiff => "Imager::File::TIFF",
+   jpeg => "Imager::File::JPEG",
+   w32 => "Imager::Font::W32",
+   ft2 => "Imager::Font::FT2",
+   t1 => "Imager::Font::T1",
+  );
 
 
-  if (!$formats{'t1'} and !$formats{'tt'} 
-      && !$formats{'ft2'} && !$formats{'w32'}) {
-    $fontstate='no font support';
-  }
+tie %formats, "Imager::FORMATS", \%formats_low, \%format_classes;
+
+BEGIN {
+  for(i_list_formats()) { $formats_low{$_}++; }
 
   %OPCODES=(Add=>[0],Sub=>[1],Mult=>[2],Div=>[3],Parm=>[4],'sin'=>[5],'cos'=>[6],'x'=>[4,0],'y'=>[4,1]);
 
 
   %OPCODES=(Add=>[0],Sub=>[1],Mult=>[2],Div=>[3],Parm=>[4],'sin'=>[5],'cos'=>[6],'x'=>[4,0],'y'=>[4,1]);
 
@@ -225,12 +197,25 @@ BEGIN {
                         callsub => sub { my %hsh=@_; i_hardinvert($hsh{image}); }
                        };
 
                         callsub => sub { my %hsh=@_; i_hardinvert($hsh{image}); }
                        };
 
-  $filters{autolevels} ={
+  $filters{hardinvertall} =
+    {
+     callseq => ['image'],
+     defaults => { },
+     callsub => sub { my %hsh=@_; i_hardinvertall($hsh{image}); }
+    };
+
+  $filters{autolevels_skew} ={
                         callseq => ['image','lsat','usat','skew'],
                         defaults => { lsat=>0.1,usat=>0.1,skew=>0.0 },
                         callsub => sub { my %hsh=@_; i_autolevels($hsh{image},$hsh{lsat},$hsh{usat},$hsh{skew}); }
                        };
 
                         callseq => ['image','lsat','usat','skew'],
                         defaults => { lsat=>0.1,usat=>0.1,skew=>0.0 },
                         callsub => sub { my %hsh=@_; i_autolevels($hsh{image},$hsh{lsat},$hsh{usat},$hsh{skew}); }
                        };
 
+  $filters{autolevels} ={
+                        callseq => ['image','lsat','usat'],
+                        defaults => { lsat=>0.1,usat=>0.1 },
+                        callsub => sub { my %hsh=@_; i_autolevels_mono($hsh{image},$hsh{lsat},$hsh{usat}); }
+                       };
+
   $filters{turbnoise} ={
                        callseq => ['image'],
                        defaults => { xo=>0.0,yo=>0.0,scale=>10.0 },
   $filters{turbnoise} ={
                        callseq => ['image'],
                        defaults => { xo=>0.0,yo=>0.0,scale=>10.0 },
@@ -445,7 +430,7 @@ BEGIN {
 # Non methods
 #
 
 # Non methods
 #
 
-# initlize Imager
+# initialize Imager
 # NOTE: this might be moved to an import override later on
 
 sub import {
 # NOTE: this might be moved to an import override later on
 
 sub import {
@@ -463,28 +448,67 @@ sub import {
 }
 
 sub init_log {
 }
 
 sub init_log {
-  i_init_log($_[0],$_[1]);
-  i_log_entry("Imager $VERSION starting\n", 1);
+  Imager->open_log(log => $_[0], level => $_[1]);
 }
 
 
 sub init {
   my %parms=(loglevel=>1,@_);
 }
 
 
 sub init {
   my %parms=(loglevel=>1,@_);
-  if ($parms{'log'}) {
-    init_log($parms{'log'},$parms{'loglevel'});
-  }
 
   if (exists $parms{'warn_obsolete'}) {
     $warn_obsolete = $parms{'warn_obsolete'};
   }
 
 
   if (exists $parms{'warn_obsolete'}) {
     $warn_obsolete = $parms{'warn_obsolete'};
   }
 
-#    if ($parms{T1LIB_CONFIG}) { $ENV{T1LIB_CONFIG}=$parms{T1LIB_CONFIG}; }
-#    if ( $ENV{T1LIB_CONFIG} and ( $fontstate eq 'missing conf' )) {
-#      i_init_fonts();
-#      $fontstate='ok';
-#    }
+  if ($parms{'log'}) {
+    Imager->open_log(log => $parms{log}, level => $parms{loglevel})
+      or return;
+  }
+
   if (exists $parms{'t1log'}) {
   if (exists $parms{'t1log'}) {
-    i_init_fonts($parms{'t1log'});
+    if ($formats{t1}) {
+      if (Imager::Font::T1::i_init_t1($parms{'t1log'})) {
+       Imager->_set_error(Imager->_error_as_msg);
+       return;
+      }
+    }
+  }
+
+  return 1;
+}
+
+{
+  my $is_logging = 0;
+
+  sub open_log {
+    my $class = shift;
+    my (%opts) = ( loglevel => 1, @_ );
+
+    $is_logging = i_init_log($opts{log}, $opts{loglevel});
+    unless ($is_logging) {
+      Imager->_set_error(Imager->_error_as_msg());
+      return;
+    }
+
+    Imager->log("Imager $VERSION starting\n", 1);
+
+    return $is_logging;
+  }
+
+  sub close_log {
+    i_init_log(undef, -1);
+    $is_logging = 0;
+  }
+
+  sub log {
+    my ($class, $message, $level) = @_;
+
+    defined $level or $level = 1;
+
+    i_log_entry($message, $level);
+  }
+
+  sub is_logging {
+    return $is_logging;
   }
 }
 
   }
 }
 
@@ -502,6 +526,12 @@ END {
 sub load_plugin {
   my ($filename)=@_;
   my $i;
 sub load_plugin {
   my ($filename)=@_;
   my $i;
+
+  if ($^O eq 'android') {
+    require File::Spec;
+    $filename = File::Spec->rel2abs($filename);
+  }
+
   my ($DSO_handle,$str)=DSO_open($filename);
   if (!defined($DSO_handle)) { $Imager::ERRSTR="Couldn't load plugin '$filename'\n"; return undef; }
   my %funcs=DSO_funclist($DSO_handle);
   my ($DSO_handle,$str)=DSO_open($filename);
   if (!defined($DSO_handle)) { $Imager::ERRSTR="Couldn't load plugin '$filename'\n"; return undef; }
   my %funcs=DSO_funclist($DSO_handle);
@@ -525,6 +555,11 @@ sub load_plugin {
 sub unload_plugin {
   my ($filename)=@_;
 
 sub unload_plugin {
   my ($filename)=@_;
 
+  if ($^O eq 'android') {
+    require File::Spec;
+    $filename = File::Spec->rel2abs($filename);
+  }
+
   if (!$DSOs{$filename}) { $ERRSTR="plugin '$filename' not loaded."; return undef; }
   my ($DSO_handle,$funcref)=@{$DSOs{$filename}};
   for(keys %{$funcref}) {
   if (!$DSOs{$filename}) { $ERRSTR="plugin '$filename' not loaded."; return undef; }
   my ($DSO_handle,$funcref)=@{$DSOs{$filename}};
   for(keys %{$funcref}) {
@@ -600,11 +635,16 @@ sub _combine {
 }
 
 sub _valid_image {
 }
 
 sub _valid_image {
-  my ($self) = @_;
+  my ($self, $method) = @_;
 
 
-  $self->{IMG} and return 1;
+  ref $self
+    or return Imager->_set_error("$method needs an image object");
 
 
-  $self->_set_error('empty input image');
+  $self->{IMG} && Scalar::Util::blessed($self->{IMG}) and return 1;
+
+  my $msg = $self->{IMG} ? "images do not cross threads" : "empty input image";
+  $msg = "$method: $msg" if $method;
+  $self->_set_error($msg);
 
   return;
 }
 
   return;
 }
@@ -634,17 +674,13 @@ sub new {
   $self->{ERRSTR}=undef; #
   $self->{DEBUG}=$DEBUG;
   $self->{DEBUG} and print "Initialized Imager\n";
   $self->{ERRSTR}=undef; #
   $self->{DEBUG}=$DEBUG;
   $self->{DEBUG} and print "Initialized Imager\n";
-  if (defined $hsh{xsize} || defined $hsh{ysize}) { 
-    unless ($self->img_set(%hsh)) {
-      $Imager::ERRSTR = $self->{ERRSTR};
-      return;
-    }
-  }
-  elsif (defined $hsh{file} || 
-        defined $hsh{fh} ||
-        defined $hsh{fd} ||
-        defined $hsh{callback} ||
-        defined $hsh{readcb}) {
+  if (defined $hsh{file} ||
+      defined $hsh{fh} ||
+      defined $hsh{fd} ||
+      defined $hsh{callback} ||
+      defined $hsh{readcb} ||
+      defined $hsh{data} ||
+      defined $hsh{io}) {
     # allow $img = Imager->new(file => $filename)
     my %extras;
     
     # allow $img = Imager->new(file => $filename)
     my %extras;
     
@@ -658,6 +694,16 @@ sub new {
       return;
     }
   }
       return;
     }
   }
+  elsif (defined $hsh{xsize} || defined $hsh{ysize}) {
+    unless ($self->img_set(%hsh)) {
+      $Imager::ERRSTR = $self->{ERRSTR};
+      return;
+    }
+  }
+  elsif (%hsh) {
+    Imager->_set_error("new: supply xsize and ysize or a file access parameter or no parameters");
+    return;
+  }
 
   return $self;
 }
 
   return $self;
 }
@@ -667,7 +713,9 @@ sub new {
 
 sub copy {
   my $self = shift;
 
 sub copy {
   my $self = shift;
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
+
+  $self->_valid_image("copy")
+    or return;
 
   unless (defined wantarray) {
     my @caller = caller;
 
   unless (defined wantarray) {
     my @caller = caller;
@@ -685,16 +733,19 @@ sub copy {
 sub paste {
   my $self = shift;
 
 sub paste {
   my $self = shift;
 
-  unless ($self->{IMG}) { 
-    $self->_set_error('empty input image');
-    return;
-  }
+  $self->_valid_image("paste")
+    or 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;
   }
   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;
   }
+  unless ($src->_valid_image("paste")) {
+    $self->{ERRSTR} = $src->{ERRSTR} . " (for src)";
+    return;
+  }
   $input{left}=0 if $input{left} <= 0;
   $input{top}=0 if $input{top} <= 0;
 
   $input{left}=0 if $input{left} <= 0;
   $input{top}=0 if $input{top} <= 0;
 
@@ -753,7 +804,9 @@ sub paste {
 
 sub crop {
   my $self=shift;
 
 sub crop {
   my $self=shift;
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
+
+  $self->_valid_image("crop")
+    or return;
   
   unless (defined wantarray) {
     my @caller = caller;
   
   unless (defined wantarray) {
     my @caller = caller;
@@ -841,7 +894,8 @@ sub crop {
 sub _sametype {
   my ($self, %opts) = @_;
 
 sub _sametype {
   my ($self, %opts) = @_;
 
-  $self->{IMG} or return $self->_set_error("Not a valid image");
+  $self->_valid_image
+    or return;
 
   my $x = $opts{xsize} || $self->getwidth;
   my $y = $opts{ysize} || $self->getheight;
 
   my $x = $opts{xsize} || $self->getwidth;
   my $y = $opts{ysize} || $self->getheight;
@@ -865,16 +919,29 @@ sub _sametype {
 # Sets an image to a certain size and channel number
 # if there was previously data in the image it is discarded
 
 # Sets an image to a certain size and channel number
 # if there was previously data in the image it is discarded
 
+my %model_channels =
+  (
+   gray => 1,
+   graya => 2,
+   rgb => 3,
+   rgba => 4,
+  );
+
 sub img_set {
   my $self=shift;
 
   my %hsh=(xsize=>100, ysize=>100, channels=>3, bits=>8, type=>'direct', @_);
 
 sub img_set {
   my $self=shift;
 
   my %hsh=(xsize=>100, ysize=>100, channels=>3, bits=>8, type=>'direct', @_);
 
-  if (defined($self->{IMG})) {
-    # let IIM_DESTROY destroy it, it's possible this image is
-    # referenced from a virtual image (like masked)
-    #i_img_destroy($self->{IMG});
-    undef($self->{IMG});
+  undef($self->{IMG});
+
+  if ($hsh{model}) {
+    if (my $channels = $model_channels{$hsh{model}}) {
+      $hsh{channels} = $channels;
+    }
+    else {
+      $self->_set_error("new: unknown value for model '$hsh{model}'");
+      return;
+    }
   }
 
   if ($hsh{type} eq 'paletted' || $hsh{type} eq 'pseudo') {
   }
 
   if ($hsh{type} eq 'paletted' || $hsh{type} eq 'pseudo') {
@@ -888,12 +955,12 @@ sub img_set {
     $self->{IMG} = i_img_16_new($hsh{xsize}, $hsh{ysize}, $hsh{channels});
   }
   else {
     $self->{IMG} = i_img_16_new($hsh{xsize}, $hsh{ysize}, $hsh{channels});
   }
   else {
-    $self->{IMG}=Imager::ImgRaw::new($hsh{'xsize'}, $hsh{'ysize'},
-                                     $hsh{'channels'});
+    $self->{IMG}= i_img_8_new($hsh{'xsize'}, $hsh{'ysize'},
+                             $hsh{'channels'});
   }
 
   unless ($self->{IMG}) {
   }
 
   unless ($self->{IMG}) {
-    $self->{ERRSTR} = Imager->_error_as_msg();
+    $self->_set_error(Imager->_error_as_msg());
     return;
   }
 
     return;
   }
 
@@ -904,7 +971,9 @@ sub img_set {
 sub masked {
   my $self = shift;
 
 sub masked {
   my $self = shift;
 
-  $self or return undef;
+  $self->_valid_image("masked")
+    or return;
+
   my %opts = (left    => 0, 
               top     => 0, 
               right   => $self->getwidth, 
   my %opts = (left    => 0, 
               top     => 0, 
               right   => $self->getwidth, 
@@ -916,11 +985,16 @@ sub masked {
   $result->{IMG} = i_img_masked_new($self->{IMG}, $mask, $opts{left}, 
                                     $opts{top}, $opts{right} - $opts{left},
                                     $opts{bottom} - $opts{top});
   $result->{IMG} = i_img_masked_new($self->{IMG}, $mask, $opts{left}, 
                                     $opts{top}, $opts{right} - $opts{left},
                                     $opts{bottom} - $opts{top});
+  unless ($result->{IMG}) {
+    $self->_set_error(Imager->_error_as_msg);
+    return;
+  }
+
   # keep references to the mask and base images so they don't
   # disappear on us
   $result->{DEPENDS} = [ $self->{IMG}, $mask ];
 
   # keep references to the mask and base images so they don't
   # disappear on us
   $result->{DEPENDS} = [ $self->{IMG}, $mask ];
 
-  $result;
+  return $result;
 }
 
 # convert an RGB image into a paletted image
 }
 
 # convert an RGB image into a paletted image
@@ -940,24 +1014,45 @@ sub to_paletted {
     return;
   }
 
     return;
   }
 
+  $self->_valid_image("to_paletted")
+    or return;
+
   my $result = Imager->new;
   my $result = Imager->new;
-  $result->{IMG} = i_img_to_pal($self->{IMG}, $opts);
+  unless ($result->{IMG} = i_img_to_pal($self->{IMG}, $opts)) {
+    $self->_set_error(Imager->_error_as_msg);
+    return;
+  }
 
 
-  #print "Type ", i_img_type($result->{IMG}), "\n";
+  return $result;
+}
 
 
-  if ($result->{IMG}) {
-    return $result;
-  }
-  else {
-    $self->{ERRSTR} = $self->_error_as_msg;
+sub make_palette {
+  my ($class, $quant, @images) = @_;
+
+  unless (@images) {
+    Imager->_set_error("make_palette: supply at least one image");
     return;
   }
     return;
   }
+  my $index = 1;
+  for my $img (@images) {
+    unless ($img->{IMG}) {
+      Imager->_set_error("make_palette: image $index is empty");
+      return;
+    }
+    ++$index;
+  }
+
+  my @cols = i_img_make_palette($quant, map $_->{IMG}, @images);
+  unless (@cols) {
+      Imager->_set_error(Imager->_error_as_msg);
+      return;
+  }
+  return @cols;
 }
 
 }
 
-# convert a paletted (or any image) to an 8-bit/channel RGB images
+# convert a paletted (or any image) to an 8-bit/channel RGB image
 sub to_rgb8 {
   my $self = shift;
 sub to_rgb8 {
   my $self = shift;
-  my $result;
 
   unless (defined wantarray) {
     my @caller = caller;
 
   unless (defined wantarray) {
     my @caller = caller;
@@ -965,30 +1060,57 @@ sub to_rgb8 {
     return;
   }
 
     return;
   }
 
-  if ($self->{IMG}) {
-    $result = Imager->new;
-    $result->{IMG} = i_img_to_rgb($self->{IMG})
-      or undef $result;
+  $self->_valid_image("to_rgb8")
+    or return;
+
+  my $result = Imager->new;
+  unless ($result->{IMG} = i_img_to_rgb($self->{IMG})) {
+    $self->_set_error(Imager->_error_as_msg());
+    return;
   }
 
   return $result;
 }
 
   }
 
   return $result;
 }
 
-# convert a paletted (or any image) to an 8-bit/channel RGB images
+# convert a paletted (or any image) to a 16-bit/channel RGB image
 sub to_rgb16 {
   my $self = shift;
 sub to_rgb16 {
   my $self = shift;
-  my $result;
 
   unless (defined wantarray) {
     my @caller = caller;
 
   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";
+    warn "to_rgb16() called in void context - to_rgb16() returns the converted image at $caller[1] line $caller[2]\n";
+    return;
+  }
+
+  $self->_valid_image("to_rgb16")
+    or return;
+
+  my $result = Imager->new;
+  unless ($result->{IMG} = i_img_to_rgb16($self->{IMG})) {
+    $self->_set_error(Imager->_error_as_msg());
+    return;
+  }
+
+  return $result;
+}
+
+# convert a paletted (or any image) to an double/channel RGB image
+sub to_rgb_double {
+  my $self = shift;
+
+  unless (defined wantarray) {
+    my @caller = caller;
+    warn "to_rgb16() called in void context - to_rgb_double() returns the converted image at $caller[1] line $caller[2]\n";
     return;
   }
 
     return;
   }
 
-  if ($self->{IMG}) {
-    $result = Imager->new;
-    $result->{IMG} = i_img_to_rgb16($self->{IMG})
-      or undef $result;
+  $self->_valid_image("to_rgb_double")
+    or return;
+
+  my $result = Imager->new;
+  unless ($result->{IMG} = i_img_to_drgb($self->{IMG})) {
+    $self->_set_error(Imager->_error_as_msg());
+    return;
   }
 
   return $result;
   }
 
   return $result;
@@ -998,10 +1120,8 @@ sub addcolors {
   my $self = shift;
   my %opts = (colors=>[], @_);
 
   my $self = shift;
   my %opts = (colors=>[], @_);
 
-  unless ($self->{IMG}) {
-    $self->_set_error("empty input image");
-    return;
-  }
+  $self->_valid_image("addcolors")
+    or return -1;
 
   my @colors = @{$opts{colors}}
     or return undef;
 
   my @colors = @{$opts{colors}}
     or return undef;
@@ -1021,10 +1141,8 @@ sub setcolors {
   my $self = shift;
   my %opts = (start=>0, colors=>[], @_);
 
   my $self = shift;
   my %opts = (start=>0, colors=>[], @_);
 
-  unless ($self->{IMG}) {
-    $self->_set_error("empty input image");
-    return;
-  }
+  $self->_valid_image("setcolors")
+    or return;
 
   my @colors = @{$opts{colors}}
     or return undef;
 
   my @colors = @{$opts{colors}}
     or return undef;
@@ -1043,6 +1161,10 @@ sub setcolors {
 sub getcolors {
   my $self = shift;
   my %opts = @_;
 sub getcolors {
   my $self = shift;
   my %opts = @_;
+
+  $self->_valid_image("getcolors")
+    or return;
+
   if (!exists $opts{start} && !exists $opts{count}) {
     # get them all
     $opts{start} = 0;
   if (!exists $opts{start} && !exists $opts{count}) {
     # get them all
     $opts{start} = 0;
@@ -1054,52 +1176,82 @@ sub getcolors {
   elsif (!exists $opts{start}) {
     $opts{start} = 0;
   }
   elsif (!exists $opts{start}) {
     $opts{start} = 0;
   }
-  
-  $self->{IMG} and 
-    return i_getcolors($self->{IMG}, $opts{start}, $opts{count});
+
+  return i_getcolors($self->{IMG}, $opts{start}, $opts{count});
 }
 
 sub colorcount {
 }
 
 sub colorcount {
-  i_colorcount($_[0]{IMG});
+  my ($self) = @_;
+
+  $self->_valid_image("colorcount")
+    or return -1;
+
+  return i_colorcount($self->{IMG});
 }
 
 sub maxcolors {
 }
 
 sub maxcolors {
-  i_maxcolors($_[0]{IMG});
+  my $self = shift;
+
+  $self->_valid_image("maxcolors")
+    or return -1;
+
+  i_maxcolors($self->{IMG});
 }
 
 sub findcolor {
   my $self = shift;
   my %opts = @_;
 }
 
 sub findcolor {
   my $self = shift;
   my %opts = @_;
-  $opts{color} or return undef;
 
 
-  $self->{IMG} and i_findcolor($self->{IMG}, $opts{color});
+  $self->_valid_image("findcolor")
+    or return;
+
+  unless ($opts{color}) {
+    $self->_set_error("findcolor: no color parameter");
+    return;
+  }
+
+  my $color = _color($opts{color})
+    or return;
+
+  return i_findcolor($self->{IMG}, $color);
 }
 
 sub bits {
   my $self = shift;
 }
 
 sub bits {
   my $self = shift;
-  my $bits = $self->{IMG} && i_img_bits($self->{IMG});
+
+  $self->_valid_image("bits")
+    or return;
+
+  my $bits = i_img_bits($self->{IMG});
   if ($bits && $bits == length(pack("d", 1)) * 8) {
     $bits = 'double';
   }
   if ($bits && $bits == length(pack("d", 1)) * 8) {
     $bits = 'double';
   }
-  $bits;
+  return $bits;
 }
 
 sub type {
   my $self = shift;
 }
 
 sub type {
   my $self = shift;
-  if ($self->{IMG}) {
-    return i_img_type($self->{IMG}) ? "paletted" : "direct";
-  }
+
+  $self->_valid_image("type")
+    or return;
+
+  return i_img_type($self->{IMG}) ? "paletted" : "direct";
 }
 
 sub virtual {
   my $self = shift;
 }
 
 sub virtual {
   my $self = shift;
-  $self->{IMG} and i_img_virtual($self->{IMG});
+
+  $self->_valid_image("virtual")
+    or return;
+
+  return i_img_virtual($self->{IMG});
 }
 
 sub is_bilevel {
   my ($self) = @_;
 
 }
 
 sub is_bilevel {
   my ($self) = @_;
 
-  $self->{IMG} or return;
+  $self->_valid_image("is_bilevel")
+    or return;
 
   return i_img_is_monochrome($self->{IMG});
 }
 
   return i_img_is_monochrome($self->{IMG});
 }
@@ -1107,7 +1259,8 @@ sub is_bilevel {
 sub tags {
   my ($self, %opts) = @_;
 
 sub tags {
   my ($self, %opts) = @_;
 
-  $self->{IMG} or return;
+  $self->_valid_image("tags")
+    or return;
 
   if (defined $opts{name}) {
     my @result;
 
   if (defined $opts{name}) {
     my @result;
@@ -1143,7 +1296,9 @@ sub addtag {
   my $self = shift;
   my %opts = @_;
 
   my $self = shift;
   my %opts = @_;
 
-  return -1 unless $self->{IMG};
+  $self->_valid_image("addtag")
+    or return;
+
   if ($opts{name}) {
     if (defined $opts{value}) {
       if ($opts{value} =~ /^\d+$/) {
   if ($opts{name}) {
     if (defined $opts{value}) {
       if ($opts{value} =~ /^\d+$/) {
@@ -1191,7 +1346,8 @@ sub deltag {
   my $self = shift;
   my %opts = @_;
 
   my $self = shift;
   my %opts = @_;
 
-  return 0 unless $self->{IMG};
+  $self->_valid_image("deltag")
+    or return 0;
 
   if (defined $opts{'index'}) {
     return i_tags_delete($self->{IMG}, $opts{'index'});
 
   if (defined $opts{'index'}) {
     return i_tags_delete($self->{IMG}, $opts{'index'});
@@ -1211,6 +1367,9 @@ sub deltag {
 sub settag {
   my ($self, %opts) = @_;
 
 sub settag {
   my ($self, %opts) = @_;
 
+  $self->_valid_image("settag")
+    or return;
+
   if ($opts{name}) {
     $self->deltag(name=>$opts{name});
     return $self->addtag(name=>$opts{name}, value=>$opts{value});
   if ($opts{name}) {
     $self->deltag(name=>$opts{name});
     return $self->addtag(name=>$opts{name}, value=>$opts{value});
@@ -1235,12 +1394,11 @@ sub _get_reader_io {
     return io_new_fd($input->{fd});
   }
   elsif ($input->{fh}) {
     return io_new_fd($input->{fd});
   }
   elsif ($input->{fh}) {
-    my $fd = fileno($input->{fh});
-    unless (defined $fd) {
+    unless (Scalar::Util::openhandle($input->{fh})) {
       $self->_set_error("Handle in fh option not opened");
       return;
     }
       $self->_set_error("Handle in fh option not opened");
       return;
     }
-    return io_new_fd($fd);
+    return Imager::IO->new_fh($input->{fh});
   }
   elsif ($input->{file}) {
     my $file = IO::File->new($input->{file}, "r");
   }
   elsif ($input->{file}) {
     my $file = IO::File->new($input->{file}, "r");
@@ -1277,26 +1435,24 @@ sub _get_reader_io {
 }
 
 sub _get_writer_io {
 }
 
 sub _get_writer_io {
-  my ($self, $input, $type) = @_;
+  my ($self, $input) = @_;
+
+  my $buffered = exists $input->{buffered} ? $input->{buffered} : 1;
 
 
+  my $io;
+  my @extras;
   if ($input->{io}) {
   if ($input->{io}) {
-    return $input->{io};
+    $io = $input->{io};
   }
   elsif ($input->{fd}) {
   }
   elsif ($input->{fd}) {
-    return io_new_fd($input->{fd});
+    $io = io_new_fd($input->{fd});
   }
   elsif ($input->{fh}) {
   }
   elsif ($input->{fh}) {
-    my $fd = fileno($input->{fh});
-    unless (defined $fd) {
+    unless (Scalar::Util::openhandle($input->{fh})) {
       $self->_set_error("Handle in fh option not opened");
       return;
     }
       $self->_set_error("Handle in fh option not opened");
       return;
     }
-    # flush it
-    my $oldfh = select($input->{fh});
-    # flush anything that's buffered, and make sure anything else is flushed
-    $| = 1;
-    select($oldfh);
-    return io_new_fd($fd);
+    $io = Imager::IO->new_fh($input->{fh});
   }
   elsif ($input->{file}) {
     my $fh = new IO::File($input->{file},"w+");
   }
   elsif ($input->{file}) {
     my $fh = new IO::File($input->{file},"w+");
@@ -1305,28 +1461,30 @@ sub _get_writer_io {
       return;
     }
     binmode($fh) or die;
       return;
     }
     binmode($fh) or die;
-    return (io_new_fd(fileno($fh)), $fh);
+    $io = io_new_fd(fileno($fh));
+    push @extras, $fh;
   }
   elsif ($input->{data}) {
   }
   elsif ($input->{data}) {
-    return io_new_bufchain();
+    $io = io_new_bufchain();
   }
   elsif ($input->{callback} || $input->{writecb}) {
   }
   elsif ($input->{callback} || $input->{writecb}) {
-    if ($input->{maxbuffer}) {
-      return io_new_cb($input->{callback} || $input->{writecb},
-                       $input->{readcb},
-                       $input->{seekcb}, $input->{closecb},
-                       $input->{maxbuffer});
-    }
-    else {
-      return io_new_cb($input->{callback} || $input->{writecb},
-                       $input->{readcb},
-                       $input->{seekcb}, $input->{closecb});
+    if ($input->{maxbuffer} && $input->{maxbuffer} == 1) {
+      $buffered = 0;
     }
     }
+    $io = io_new_cb($input->{callback} || $input->{writecb},
+                   $input->{readcb},
+                   $input->{seekcb}, $input->{closecb});
   }
   else {
     $self->_set_error("file/fd/fh/data/callback parameter missing");
     return;
   }
   }
   else {
     $self->_set_error("file/fd/fh/data/callback parameter missing");
     return;
   }
+
+  unless ($buffered) {
+    $io->set_buffered(0);
+  }
+
+  return ($io, @extras);
 }
 
 # Read an image from file
 }
 
 # Read an image from file
@@ -1344,52 +1502,39 @@ sub read {
 
   my ($IO, $fh) = $self->_get_reader_io(\%input) or return;
 
 
   my ($IO, $fh) = $self->_get_reader_io(\%input) or return;
 
-  unless ($input{'type'}) {
-    $input{'type'} = i_test_format_probe($IO, -1);
+  my $type = $input{'type'};
+  unless ($type) {
+    $type = i_test_format_probe($IO, -1);
+  }
+
+  if ($input{file} && !$type) {
+    # guess the type 
+    $type = $FORMATGUESS->($input{file});
   }
 
   }
 
-  unless ($input{'type'}) {
-         $self->_set_error('type parameter missing and not possible to guess from extension'); 
+  unless ($type) {
+    my $msg = "type parameter missing and it couldn't be determined from the file contents";
+    $input{file} and $msg .= " or file name";
+    $self->_set_error($msg);
     return undef;
   }
 
     return undef;
   }
 
-  _reader_autoload($input{type});
+  _reader_autoload($type);
 
 
-  if ($readers{$input{type}} && $readers{$input{type}}{single}) {
-    return $readers{$input{type}}{single}->($self, $IO, %input);
+  if ($readers{$type} && $readers{$type}{single}) {
+    return $readers{$type}{single}->($self, $IO, %input);
   }
 
   }
 
-  unless ($formats{$input{'type'}}) {
+  unless ($formats_low{$type}) {
     my $read_types = join ', ', sort Imager->read_types();
     my $read_types = join ', ', sort Imager->read_types();
-    $self->_set_error("format '$input{'type'}' not supported - formats $read_types available for reading");
+    $self->_set_error("format '$type' not supported - formats $read_types available for reading - $reader_load_errors{$type}");
     return;
   }
 
     return;
   }
 
-  # 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;
-  }
-
   my $allow_incomplete = $input{allow_incomplete};
   defined $allow_incomplete or $allow_incomplete = 0;
 
   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;
-    $self->{IMG}=i_readtiff_wiol( $IO, $allow_incomplete, $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' ) {
+  if ( $type eq 'pnm' ) {
     $self->{IMG}=i_readpnm_wiol( $IO, $allow_incomplete );
     if ( !defined($self->{IMG}) ) {
       $self->{ERRSTR}='unable to read pnm image: '._error_as_msg(); 
     $self->{IMG}=i_readpnm_wiol( $IO, $allow_incomplete );
     if ( !defined($self->{IMG}) ) {
       $self->{ERRSTR}='unable to read pnm image: '._error_as_msg(); 
@@ -1399,16 +1544,7 @@ sub read {
     return $self;
   }
 
     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} = $self->_error_as_msg();
-      return undef;
-    }
-    $self->{DEBUG} && print "loading a png file\n";
-  }
-
-  if ( $input{'type'} eq 'bmp' ) {
+  if ( $type eq 'bmp' ) {
     $self->{IMG}=i_readbmp_wiol( $IO, $allow_incomplete );
     if ( !defined($self->{IMG}) ) {
       $self->{ERRSTR}=$self->_error_as_msg();
     $self->{IMG}=i_readbmp_wiol( $IO, $allow_incomplete );
     if ( !defined($self->{IMG}) ) {
       $self->{ERRSTR}=$self->_error_as_msg();
@@ -1417,42 +1553,7 @@ sub read {
     $self->{DEBUG} && print "loading a bmp file\n";
   }
 
     $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{'gif_consolidate'}) {
-      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 );
-      }
-    }
-    else {
-      my $page = $input{'page'};
-      defined $page or $page = 0;
-      $self->{IMG} = i_readgif_single_wiol( $IO, $page );
-      if ($self->{IMG} && $input{colors}) {
-       ${ $input{colors} } =
-         [ i_getcolors($self->{IMG}, 0, i_colorcount($self->{IMG})) ];
-      }
-    }
-
-    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' ) {
+  if ( $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();
     $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();
@@ -1461,7 +1562,7 @@ sub read {
     $self->{DEBUG} && print "loading a tga file\n";
   }
 
     $self->{DEBUG} && print "loading a tga file\n";
   }
 
-  if ( $input{'type'} eq 'raw' ) {
+  if ( $type eq 'raw' ) {
     unless ( $input{xsize} && $input{ysize} ) {
       $self->_set_error('missing xsize or ysize parameter for raw');
       return undef;
     unless ( $input{xsize} && $input{ysize} ) {
       $self->_set_error('missing xsize or ysize parameter for raw');
       return undef;
@@ -1560,60 +1661,89 @@ sub write_types {
   return keys %types;
 }
 
   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";
+sub _load_file {
+  my ($file, $error) = @_;
 
 
-  unless ($attempted_to_load{$file}) {
-    eval {
+  if ($attempted_to_load{$file}) {
+    if ($file_load_errors{$file}) {
+      $$error = $file_load_errors{$file};
+      return 0;
+    }
+    else {
+      return 1;
+    }
+  }
+  else {
+    local $SIG{__DIE__};
+    my $loaded = eval {
+      local @INC = @INC;
+      pop @INC if $INC[-1] eq '.';
       ++$attempted_to_load{$file};
       require $file;
       ++$attempted_to_load{$file};
       require $file;
+      return 1;
     };
     };
-    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;
-       };
-      }
+    if ($loaded) {
+      return 1;
+    }
+    else {
+      my $work = $@ || "Unknown error";
+      chomp $work;
+      $work =~ s/\n?Compilation failed in require at .*Imager\.pm line .*\z//m;
+      $work =~ s/\n/\\n/g;
+      $work =~ s/\s*\.?\z/ loading $file/;
+      $file_load_errors{$file} = $work;
+      $$error = $work;
+      return 0;
     }
   }
 }
 
     }
   }
 }
 
+# probes for an Imager::File::whatever module
+sub _reader_autoload {
+  my $type = shift;
+
+  return if $formats_low{$type} || $readers{$type};
+
+  return unless $type =~ /^\w+$/;
+
+  my $file = "Imager/File/\U$type\E.pm";
+
+  my $error;
+  my $loaded = _load_file($file, \$error);
+  if (!$loaded && $error =~ /^Can't locate /) {
+    my $filer = "Imager/File/\U$type\EReader.pm";
+    $loaded = _load_file($filer, \$error);
+    if ($error =~ /^Can't locate /) {
+      $error = "Can't locate $file or $filer";
+    }
+  }
+  unless ($loaded) {
+    $reader_load_errors{$type} = $error;
+  }
+}
+
 # probes for an Imager::File::whatever module
 sub _writer_autoload {
   my $type = shift;
 
 # probes for an Imager::File::whatever module
 sub _writer_autoload {
   my $type = shift;
 
-  return if $formats{$type} || $readers{$type};
+  return if $formats_low{$type} || $writers{$type};
 
   return unless $type =~ /^\w+$/;
 
   my $file = "Imager/File/\U$type\E.pm";
 
 
   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;
-       };
-      }
+  my $error;
+  my $loaded = _load_file($file, \$error);
+  if (!$loaded && $error =~ /^Can't locate /) {
+    my $filew = "Imager/File/\U$type\EWriter.pm";
+    $loaded = _load_file($filew, \$error);
+    if ($error =~ /^Can't locate /) {
+      $error = "Can't locate $file or $filew";
     }
   }
     }
   }
+  unless ($loaded) {
+    $writer_load_errors{$type} = $error;
+  }
 }
 
 sub _fix_gif_positions {
 }
 
 sub _fix_gif_positions {
@@ -1727,57 +1857,42 @@ sub write {
             fax_fine=>1, @_);
   my $rc;
 
             fax_fine=>1, @_);
   my $rc;
 
+  $self->_valid_image("write")
+    or return;
+
   $self->_set_opts(\%input, "i_", $self)
     or return undef;
 
   $self->_set_opts(\%input, "i_", $self)
     or return undef;
 
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
-
-  if (!$input{'type'} and $input{file}) { 
-    $input{'type'}=$FORMATGUESS->($input{file});
+  my $type = $input{'type'};
+  if (!$type and $input{file}) { 
+    $type = $FORMATGUESS->($input{file});
   }
   }
-  if (!$input{'type'}) { 
+  unless ($type) { 
     $self->{ERRSTR}='type parameter missing and not possible to guess from extension';
     return undef;
   }
 
     $self->{ERRSTR}='type parameter missing and not possible to guess from extension';
     return undef;
   }
 
-  _writer_autoload($input{type});
+  _writer_autoload($type);
 
   my ($IO, $fh);
 
   my ($IO, $fh);
-  if ($writers{$input{type}} && $writers{$input{type}}{single}) {
-    ($IO, $fh) = $self->_get_writer_io(\%input, $input{'type'})
+  if ($writers{$type} && $writers{$type}{single}) {
+    ($IO, $fh) = $self->_get_writer_io(\%input)
       or return undef;
 
       or return undef;
 
-    $writers{$input{type}}{single}->($self, $IO, %input)
+    $writers{$type}{single}->($self, $IO, %input, type => $type)
       or return undef;
   }
   else {
       or return undef;
   }
   else {
-    if (!$formats{$input{'type'}}) { 
+    if (!$formats_low{$type}) { 
       my $write_types = join ', ', sort Imager->write_types();
       my $write_types = join ', ', sort Imager->write_types();
-      $self->_set_error("format '$input{'type'}' not supported - formats $write_types available for writing");
+      $self->_set_error("format '$type' not supported - formats $write_types available for writing - $writer_load_errors{$type}");
       return undef;
     }
     
       return undef;
     }
     
-    ($IO, $fh) = $self->_get_writer_io(\%input, $input{'type'})
+    ($IO, $fh) = $self->_get_writer_io(\%input, $type)
       or return undef;
       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} = $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' ) {
+  
+    if ( $type eq 'pnm' ) {
       $self->_set_opts(\%input, "pnm_", $self)
         or return undef;
       if ( ! i_writeppm_wiol($self->{IMG},$IO) ) {
       $self->_set_opts(\%input, "pnm_", $self)
         or return undef;
       if ( ! i_writeppm_wiol($self->{IMG},$IO) ) {
@@ -1785,7 +1900,8 @@ sub write {
         return undef;
       }
       $self->{DEBUG} && print "writing a pnm file\n";
         return undef;
       }
       $self->{DEBUG} && print "writing a pnm file\n";
-    } elsif ( $input{'type'} eq 'raw' ) {
+    }
+    elsif ( $type eq 'raw' ) {
       $self->_set_opts(\%input, "raw_", $self)
         or return undef;
       if ( !i_writeraw_wiol($self->{IMG},$IO) ) {
       $self->_set_opts(\%input, "raw_", $self)
         or return undef;
       if ( !i_writeraw_wiol($self->{IMG},$IO) ) {
@@ -1793,25 +1909,8 @@ sub write {
         return undef;
       }
       $self->{DEBUG} && print "writing a raw file\n";
         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' ) {
+    }
+    elsif ( $type eq 'bmp' ) {
       $self->_set_opts(\%input, "bmp_", $self)
         or return undef;
       if ( !i_writebmp_wiol($self->{IMG}, $IO) ) {
       $self->_set_opts(\%input, "bmp_", $self)
         or return undef;
       if ( !i_writebmp_wiol($self->{IMG}, $IO) ) {
@@ -1819,7 +1918,8 @@ sub write {
         return undef;
       }
       $self->{DEBUG} && print "writing a bmp file\n";
         return undef;
       }
       $self->{DEBUG} && print "writing a bmp file\n";
-    } elsif ( $input{'type'} eq 'tga' ) {
+    }
+    elsif ( $type eq 'tga' ) {
       $self->_set_opts(\%input, "tga_", $self)
         or return undef;
       
       $self->_set_opts(\%input, "tga_", $self)
         or return undef;
       
@@ -1828,24 +1928,6 @@ sub write {
         return undef;
       }
       $self->{DEBUG} && print "writing a tga file\n";
         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;
-      }
     }
   }
 
     }
   }
 
@@ -1873,9 +1955,17 @@ sub write_multi {
     return;
   }
   # translate to ImgRaw
     return;
   }
   # translate to ImgRaw
-  if (grep !UNIVERSAL::isa($_, 'Imager') || !$_->{IMG}, @images) {
-    $class->_set_error('Usage: Imager->write_multi({ options }, @images)');
-    return 0;
+  my $index = 1;
+  for my $img (@images) {
+    unless (ref $img && Scalar::Util::blessed($img) && $img->isa("Imager")) {
+      $class->_set_error("write_multi: image $index is not an Imager image object");
+      return;
+    }
+    unless ($img->_valid_image("write_multi")) {
+      $class->_set_error($img->errstr . " (image $index)");
+      return;
+    }
+    ++$index;
   }
   $class->_set_opts($opts, "i_", @images)
     or return;
   }
   $class->_set_opts($opts, "i_", @images)
     or return;
@@ -1901,37 +1991,7 @@ sub write_multi {
     ($IO, $file) = $class->_get_writer_io($opts, $type)
       or 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;
-      }
+    if (0) { # eventually PNM in here, now that TIFF/GIF are elsewhere
     }
     else {
       if (@images == 1) {
     }
     else {
       if (@images == 1) {
@@ -1975,7 +2035,9 @@ sub read_multi {
   }
 
   unless ($type) {
   }
 
   unless ($type) {
-    $ERRSTR = "No type parameter supplied and it couldn't be guessed";
+    my $msg = "type parameter missing and it couldn't be determined from the file contents";
+    $opts{file} and $msg .= " or file name";
+    Imager->_set_error($msg);
     return;
   }
 
     return;
   }
 
@@ -1991,30 +2053,9 @@ sub read_multi {
     return;
   }
 
     return;
   }
 
-  if ($type eq 'gif') {
-    my @imgs;
-    @imgs = i_readgif_multi_wiol($IO);
-    if (@imgs) {
-      return map { 
-        bless { IMG=>$_, DEBUG=>$DEBUG, ERRSTR=>undef }, 'Imager' 
-      } @imgs;
-    }
-    else {
-      $ERRSTR = _error_as_msg();
-      return;
-    }
-  }
-  elsif ($type eq 'tiff') {
-    my @imgs = i_readtiff_multi_wiol($IO, -1);
-    if (@imgs) {
-      return map { 
-        bless { IMG=>$_, DEBUG=>$DEBUG, ERRSTR=>undef }, 'Imager' 
-      } @imgs;
-    }
-    else {
-      $ERRSTR = _error_as_msg();
-      return;
-    }
+  my @imgs;
+  if ($type eq 'pnm') {
+    @imgs = i_readpnm_multi_wiol($IO, $opts{allow_incomplete}||0);
   }
   else {
     my $img = Imager->new;
   }
   else {
     my $img = Imager->new;
@@ -2022,9 +2063,16 @@ sub read_multi {
       return ( $img );
     }
     Imager->_set_error($img->errstr);
       return ( $img );
     }
     Imager->_set_error($img->errstr);
+    return;
   }
 
   }
 
+  if (!@imgs) {
+    $ERRSTR = _error_as_msg();
   return;
   return;
+  }
+  return map { 
+        bless { IMG=>$_, DEBUG=>$DEBUG, ERRSTR=>undef }, 'Imager' 
+      } @imgs;
 }
 
 # Destroy an Imager object
 }
 
 # Destroy an Imager object
@@ -2051,7 +2099,9 @@ sub filter {
   my $self=shift;
   my %input=@_;
   my %hsh;
   my $self=shift;
   my %input=@_;
   my %hsh;
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
+
+  $self->_valid_image("filter")
+    or return;
 
   if (!$input{'type'}) { $self->{ERRSTR}='type parameter missing'; return undef; }
 
 
   if (!$input{'type'}) { $self->{ERRSTR}='type parameter missing'; return undef; }
 
@@ -2226,10 +2276,8 @@ sub scale {
     return;
   }
 
     return;
   }
 
-  unless ($self->{IMG}) { 
-    $self->_set_error('empty input image'); 
-    return undef;
-  }
+  $self->_valid_image("scale")
+    or return;
 
   my ($x_scale, $y_scale, $new_width, $new_height) = 
     $self->scale_calculate(%opts)
 
   my ($x_scale, $y_scale, $new_width, $new_height) = 
     $self->scale_calculate(%opts)
@@ -2283,10 +2331,8 @@ sub scaleX {
     return;
   }
 
     return;
   }
 
-  unless ($self->{IMG}) { 
-    $self->{ERRSTR} = 'empty input image';
-    return undef;
-  }
+  $self->_valid_image("scaleX")
+    or return;
 
   my $img = Imager->new();
 
 
   my $img = Imager->new();
 
@@ -2323,7 +2369,8 @@ sub scaleY {
     return;
   }
 
     return;
   }
 
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
+  $self->_valid_image("scaleY")
+    or return;
 
   my $img = Imager->new();
 
 
   my $img = Imager->new();
 
@@ -2354,17 +2401,23 @@ sub scaleY {
 
 sub transform {
   my $self=shift;
 
 sub transform {
   my $self=shift;
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
   my %opts=@_;
   my (@op,@ropx,@ropy,$iop,$or,@parm,$expr,@xt,@yt,@pt,$numre);
 
 #  print Dumper(\%opts);
 #  xopcopdes
 
   my %opts=@_;
   my (@op,@ropx,@ropy,$iop,$or,@parm,$expr,@xt,@yt,@pt,$numre);
 
 #  print Dumper(\%opts);
 #  xopcopdes
 
+  $self->_valid_image("transform")
+    or return;
+
   if ( $opts{'xexpr'} and $opts{'yexpr'} ) {
     if (!$I2P) {
   if ( $opts{'xexpr'} and $opts{'yexpr'} ) {
     if (!$I2P) {
-      eval ("use Affix::Infix2Postfix;");
-      print $@;
+      {
+       local @INC = @INC;
+       pop @INC if $INC[-1] eq '.';
+       eval ("use Affix::Infix2Postfix;");
+      }
+
       if ( $@ ) {
        $self->{ERRSTR}='transform: expr given and Affix::Infix2Postfix is not avaliable.'; 
        return undef;
       if ( $@ ) {
        $self->{ERRSTR}='transform: expr given and Affix::Infix2Postfix is not avaliable.'; 
        return undef;
@@ -2452,6 +2505,15 @@ sub transform2 {
   $opts->{variables} = [ qw(x y) ];
   my ($width, $height) = @{$opts}{qw(width height)};
   if (@imgs) {
   $opts->{variables} = [ qw(x y) ];
   my ($width, $height) = @{$opts}{qw(width height)};
   if (@imgs) {
+    my $index = 1;
+    for my $img (@imgs) {
+      unless ($img->_valid_image("transform2")) {
+       Imager->_set_error($img->errstr . " (input image $index)");
+       return;
+      }
+      ++$index;
+    }
+
     $width ||= $imgs[0]->getwidth();
     $height ||= $imgs[0]->getheight();
     my $img_num = 1;
     $width ||= $imgs[0]->getwidth();
     $height ||= $imgs[0]->getheight();
     my $img_num = 1;
@@ -2506,13 +2568,12 @@ sub rubthrough {
   my $self=shift;
   my %opts= @_;
 
   my $self=shift;
   my %opts= @_;
 
-  unless ($self->{IMG}) { 
-    $self->{ERRSTR}='empty input image'; 
-    return undef;
-  }
-  unless ($opts{src} && $opts{src}->{IMG}) {
-    $self->{ERRSTR}='empty input image for src'; 
-    return undef;
+  $self->_valid_image("rubthrough")
+    or return;
+
+  unless ($opts{src} && $opts{src}->_valid_image("rubthrough")) {
+    $self->{ERRSTR} = $opts{src}{ERRSTR} . ' (for src)';
+    return;
   }
 
   %opts = (src_minx => 0,
   }
 
   %opts = (src_minx => 0,
@@ -2549,18 +2610,16 @@ sub compose {
      @_
     );
 
      @_
     );
 
-  unless ($self->{IMG}) {
-    $self->_set_error("compose: empty input image");
-    return;
-  }
+  $self->_valid_image("compose")
+    or return;
 
   unless ($opts{src}) {
     $self->_set_error("compose: src parameter missing");
     return;
   }
   
 
   unless ($opts{src}) {
     $self->_set_error("compose: src parameter missing");
     return;
   }
   
-  unless ($opts{src}{IMG}) {
-    $self->_set_error("compose: src parameter empty image");
+  unless ($opts{src}->_valid_image("compose")) {
+    $self->_set_error($opts{src}->errstr . " (for src)");
     return;
   }
   my $src = $opts{src};
     return;
   }
   my $src = $opts{src};
@@ -2596,8 +2655,8 @@ sub compose {
   my $combine = $self->_combine($opts{combine}, 'normal');
 
   if ($opts{mask}) {
   my $combine = $self->_combine($opts{combine}, 'normal');
 
   if ($opts{mask}) {
-    unless ($opts{mask}{IMG}) {
-      $self->_set_error("compose: mask parameter empty image");
+    unless ($opts{mask}->_valid_image("compose")) {
+      $self->_set_error($opts{mask}->errstr . " (for mask)");
       return;
     }
 
       return;
     }
 
@@ -2609,16 +2668,20 @@ sub compose {
     defined $mask_top or $mask_top = $opts{mask_miny};
     defined $mask_top or $mask_top = 0;
 
     defined $mask_top or $mask_top = $opts{mask_miny};
     defined $mask_top or $mask_top = 0;
 
-    i_compose_mask($self->{IMG}, $src->{IMG}, $opts{mask}{IMG}, 
+    unless (i_compose_mask($self->{IMG}, $src->{IMG}, $opts{mask}{IMG}, 
                   $left, $top, $src_left, $src_top,
                   $mask_left, $mask_top, $width, $height, 
                   $left, $top, $src_left, $src_top,
                   $mask_left, $mask_top, $width, $height, 
-                  $combine, $opts{opacity})
-      or return;
+                          $combine, $opts{opacity})) {
+      $self->_set_error(Imager->_error_as_msg);
+      return;
+    }
   }
   else {
   }
   else {
-    i_compose($self->{IMG}, $src->{IMG}, $left, $top, $src_left, $src_top,
-             $width, $height, $combine, $opts{opacity})
-      or return;
+    unless (i_compose($self->{IMG}, $src->{IMG}, $left, $top, $src_left, $src_top,
+                     $width, $height, $combine, $opts{opacity})) {
+      $self->_set_error(Imager->_error_as_msg);
+      return;
+    }
   }
 
   return $self;
   }
 
   return $self;
@@ -2627,6 +2690,10 @@ sub compose {
 sub flip {
   my $self  = shift;
   my %opts  = @_;
 sub flip {
   my $self  = shift;
   my %opts  = @_;
+
+  $self->_valid_image("flip")
+    or return;
+
   my %xlate = (h=>0, v=>1, hv=>2, vh=>2);
   my $dir;
   return () unless defined $opts{'dir'} and defined $xlate{$opts{'dir'}};
   my %xlate = (h=>0, v=>1, hv=>2, vh=>2);
   my $dir;
   return () unless defined $opts{'dir'} and defined $xlate{$opts{'dir'}};
@@ -2645,6 +2712,9 @@ sub rotate {
     return;
   }
 
     return;
   }
 
+  $self->_valid_image("rotate")
+    or return;
+
   if (defined $opts{right}) {
     my $degrees = $opts{right};
     if ($degrees < 0) {
   if (defined $opts{right}) {
     my $degrees = $opts{right};
     if ($degrees < 0) {
@@ -2670,7 +2740,7 @@ sub rotate {
     }
   }
   elsif (defined $opts{radians} || defined $opts{degrees}) {
     }
   }
   elsif (defined $opts{radians} || defined $opts{degrees}) {
-    my $amount = $opts{radians} || $opts{degrees} * 3.1415926535 / 180;
+    my $amount = $opts{radians} || $opts{degrees} * 3.14159265358979 / 180;
 
     my $back = $opts{back};
     my $result = Imager->new;
 
     my $back = $opts{back};
     my $result = Imager->new;
@@ -2704,6 +2774,9 @@ sub matrix_transform {
   my $self = shift;
   my %opts = @_;
 
   my $self = shift;
   my %opts = @_;
 
+  $self->_valid_image("matrix_transform")
+    or return;
+
   unless (defined wantarray) {
     my @caller = caller;
     warn "copy() called in void context - copy() returns the copied image at $caller[1] line $caller[2]\n";
   unless (defined wantarray) {
     my @caller = caller;
     warn "copy() called in void context - copy() returns the copied image at $caller[1] line $caller[2]\n";
@@ -2750,25 +2823,49 @@ sub i_color_set {
 # Draws a box between the specified corner points.
 sub box {
   my $self=shift;
 # Draws a box between the specified corner points.
 sub box {
   my $self=shift;
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
-  my $dflcl=i_color_new(255,255,255,255);
-  my %opts=(color=>$dflcl,xmin=>0,ymin=>0,xmax=>$self->getwidth()-1,ymax=>$self->getheight()-1,@_);
+  my $raw = $self->{IMG};
 
 
+  $self->_valid_image("box")
+    or return;
+
+  my %opts = @_;
+
+  my ($xmin, $ymin, $xmax, $ymax);
   if (exists $opts{'box'}) { 
   if (exists $opts{'box'}) { 
-    $opts{'xmin'} = _min($opts{'box'}->[0],$opts{'box'}->[2]);
-    $opts{'xmax'} = _max($opts{'box'}->[0],$opts{'box'}->[2]);
-    $opts{'ymin'} = _min($opts{'box'}->[1],$opts{'box'}->[3]);
-    $opts{'ymax'} = _max($opts{'box'}->[1],$opts{'box'}->[3]);
+    $xmin = _min($opts{'box'}->[0],$opts{'box'}->[2]);
+    $xmax = _max($opts{'box'}->[0],$opts{'box'}->[2]);
+    $ymin = _min($opts{'box'}->[1],$opts{'box'}->[3]);
+    $ymax = _max($opts{'box'}->[1],$opts{'box'}->[3]);
+  }
+  else {
+    defined($xmin = $opts{xmin}) or $xmin = 0;
+    defined($xmax = $opts{xmax}) or $xmax = $self->getwidth()-1;
+    defined($ymin = $opts{ymin}) or $ymin = 0;
+    defined($ymax = $opts{ymax}) or $ymax = $self->getheight()-1;
   }
 
   if ($opts{filled}) { 
   }
 
   if ($opts{filled}) { 
-    my $color = _color($opts{'color'});
-    unless ($color) { 
-      $self->{ERRSTR} = $Imager::ERRSTR; 
-      return; 
+    my $color = $opts{'color'};
+
+    if (defined $color) {
+      unless (_is_color_object($color)) {
+       $color = _color($color);
+       unless ($color) { 
+         $self->{ERRSTR} = $Imager::ERRSTR; 
+         return;
+       }
+      }
+    }
+    else {
+      $color = i_color_new(255,255,255,255);
+    }
+
+    if ($color->isa("Imager::Color")) {
+      i_box_filled($raw, $xmin, $ymin,$xmax, $ymax, $color);
+    }
+    else {
+      i_box_filledf($raw, $xmin, $ymin,$xmax, $ymax, $color);
     }
     }
-    i_box_filled($self->{IMG},$opts{xmin},$opts{ymin},$opts{xmax},
-                 $opts{ymax}, $color); 
   }
   elsif ($opts{fill}) {
     unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) {
   }
   elsif ($opts{fill}) {
     unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) {
@@ -2779,24 +2876,38 @@ sub box {
         return undef;
       }
     }
         return undef;
       }
     }
-    i_box_cfill($self->{IMG},$opts{xmin},$opts{ymin},$opts{xmax},
-                $opts{ymax},$opts{fill}{fill});
+    i_box_cfill($raw, $xmin, $ymin, $xmax, $ymax, $opts{fill}{fill});
   }
   else {
   }
   else {
-    my $color = _color($opts{'color'});
+    my $color = $opts{'color'};
+    if (defined $color) {
+      unless (_is_color_object($color)) {
+       $color = _color($color);
+       unless ($color) { 
+         $self->{ERRSTR} = $Imager::ERRSTR;
+         return;
+       }
+      }
+    }
+    else {
+      $color = i_color_new(255, 255, 255, 255);
+    }
     unless ($color) { 
       $self->{ERRSTR} = $Imager::ERRSTR;
       return;
     }
     unless ($color) { 
       $self->{ERRSTR} = $Imager::ERRSTR;
       return;
     }
-    i_box($self->{IMG},$opts{xmin},$opts{ymin},$opts{xmax},$opts{ymax},
-          $color);
+    i_box($raw, $xmin, $ymin, $xmax, $ymax, $color);
   }
   }
+
   return $self;
 }
 
 sub arc {
   my $self=shift;
   return $self;
 }
 
 sub arc {
   my $self=shift;
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
+
+  $self->_valid_image("arc")
+    or return;
+
   my $dflcl= [ 255, 255, 255, 255];
   my $good = 1;
   my %opts=
   my $dflcl= [ 255, 255, 255, 255];
   my $good = 1;
   my %opts=
@@ -2819,8 +2930,14 @@ sub arc {
          return;
        }
       }
          return;
        }
       }
-      i_arc_aa_cfill($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'},$opts{'d1'},
-                    $opts{'d2'}, $opts{fill}{fill});
+      if ($opts{d1} == 0 && $opts{d2} == 361) {
+       i_circle_aa_fill($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{'r'},
+                        $opts{fill}{fill});
+      }
+      else {
+       i_arc_aa_cfill($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'},$opts{'d1'},
+                      $opts{'d2'}, $opts{fill}{fill});
+      }
     }
     elsif ($opts{filled}) {
       my $color = _color($opts{'color'});
     }
     elsif ($opts{filled}) {
       my $color = _color($opts{'color'});
@@ -2898,7 +3015,9 @@ sub line {
   my %opts=(color=>$dflcl,
            endp => 1,
            @_);
   my %opts=(color=>$dflcl,
            endp => 1,
            @_);
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
+
+  $self->_valid_image("line")
+    or return;
 
   unless (exists $opts{x1} and exists $opts{y1}) { $self->{ERRSTR}='missing begining coord'; return undef; }
   unless (exists $opts{x2} and exists $opts{y2}) { $self->{ERRSTR}='missing ending coord'; return undef; }
 
   unless (exists $opts{x1} and exists $opts{y1}) { $self->{ERRSTR}='missing begining coord'; return undef; }
   unless (exists $opts{x2} and exists $opts{y2}) { $self->{ERRSTR}='missing ending coord'; return undef; }
@@ -2929,7 +3048,8 @@ sub polyline {
   my $dflcl=i_color_new(0,0,0,0);
   my %opts=(color=>$dflcl,@_);
 
   my $dflcl=i_color_new(0,0,0,0);
   my %opts=(color=>$dflcl,@_);
 
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
+  $self->_valid_image("polyline")
+    or return;
 
   if (exists($opts{points})) { @points=@{$opts{points}}; }
   if (!exists($opts{points}) and exists($opts{'x'}) and exists($opts{'y'}) ) {
 
   if (exists($opts{points})) { @points=@{$opts{points}}; }
   if (!exists($opts{points}) and exists($opts{'x'}) and exists($opts{'y'}) ) {
@@ -2968,7 +3088,8 @@ sub polygon {
   my $dflcl = i_color_new(0,0,0,0);
   my %opts = (color=>$dflcl, @_);
 
   my $dflcl = i_color_new(0,0,0,0);
   my %opts = (color=>$dflcl, @_);
 
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
+  $self->_valid_image("polygon")
+    or return;
 
   if (exists($opts{points})) {
     $opts{'x'} = [ map { $_->[0] } @{$opts{points}} ];
 
   if (exists($opts{points})) {
     $opts{'x'} = [ map { $_->[0] } @{$opts{points}} ];
@@ -2979,6 +3100,8 @@ sub polygon {
     $self->{ERRSTR} = 'no points array, or x and y arrays.'; return undef;
   }
 
     $self->{ERRSTR} = 'no points array, or x and y arrays.'; return undef;
   }
 
+  my $mode = _first($opts{mode}, 0);
+
   if ($opts{'fill'}) {
     unless (UNIVERSAL::isa($opts{'fill'}, 'Imager::Fill')) {
       # assume it's a hash ref
   if ($opts{'fill'}) {
     unless (UNIVERSAL::isa($opts{'fill'}, 'Imager::Fill')) {
       # assume it's a hash ref
@@ -2988,21 +3111,86 @@ sub polygon {
         return undef;
       }
     }
         return undef;
       }
     }
-    i_poly_aa_cfill($self->{IMG}, $opts{'x'}, $opts{'y'}, 
-                    $opts{'fill'}{'fill'});
+    unless (i_poly_aa_cfill_m($self->{IMG}, $opts{'x'}, $opts{'y'},
+                             $mode, $opts{'fill'}{'fill'})) {
+      return $self->_set_error($self->_error_as_msg);
+    }
   }
   else {
     my $color = _color($opts{'color'});
     unless ($color) { 
       $self->{ERRSTR} = $Imager::ERRSTR; 
   }
   else {
     my $color = _color($opts{'color'});
     unless ($color) { 
       $self->{ERRSTR} = $Imager::ERRSTR; 
-      return; 
+      return;
+    }
+    unless (i_poly_aa_m($self->{IMG}, $opts{'x'}, $opts{'y'}, $mode, $color)) {
+      return $self->_set_error($self->_error_as_msg);
     }
     }
-    i_poly_aa($self->{IMG}, $opts{'x'}, $opts{'y'}, $color);
   }
 
   return $self;
 }
 
   }
 
   return $self;
 }
 
+sub polypolygon {
+  my ($self, %opts) = @_;
+
+  $self->_valid_image("polypolygon")
+    or return;
+
+  my $points = $opts{points};
+  $points
+    or return $self->_set_error("polypolygon: missing required points");
+
+  my $mode = _first($opts{mode}, "evenodd");
+
+  if ($opts{filled}) {
+    my $color = _color(_first($opts{color}, [ 0, 0, 0, 0 ]))
+      or return $self->_set_error($Imager::ERRSTR);
+
+    i_poly_poly_aa($self->{IMG}, $points, $mode, $color)
+      or return $self->_set_error($self->_error_as_msg);
+  }
+  elsif ($opts{fill}) {
+    my $fill = $opts{fill};
+    $self->_valid_fill($fill, "polypolygon")
+      or return;
+
+    i_poly_poly_aa_cfill($self->{IMG}, $points, $mode, $fill->{fill})
+      or return $self->_set_error($self->_error_as_msg);
+  }
+  else {
+    my $color = _color(_first($opts{color}, [ 0, 0, 0, 255 ]))
+      or return $self->_set_error($Imager::ERRSTR);
+
+    my $rimg = $self->{IMG};
+
+    if (_first($opts{aa}, 1)) {
+      for my $poly (@$points) {
+       my $xp = $poly->[0];
+       my $yp = $poly->[1];
+       for my $i (0 .. $#$xp - 1) {
+         i_line_aa($rimg, $xp->[$i], $yp->[$i], $xp->[$i+1], $yp->[$i+1],
+                   $color, 0);
+       }
+       i_line_aa($rimg, $xp->[$#$xp], $yp->[$#$yp], $xp->[0], $yp->[0],
+                 $color, 0);
+      }
+    }
+    else {
+      for my $poly (@$points) {
+       my $xp = $poly->[0];
+       my $yp = $poly->[1];
+       for my $i (0 .. $#$xp - 1) {
+         i_line($rimg, $xp->[$i], $yp->[$i], $xp->[$i+1], $yp->[$i+1],
+                $color, 0);
+       }
+       i_line($rimg, $xp->[$#$xp], $yp->[$#$yp], $xp->[0], $yp->[0],
+              $color, 0);
+      }
+    }
+  }
+
+  return $self;
+}
 
 # this the multipoint bezier curve
 # this is here more for testing that actual usage since
 
 # this the multipoint bezier curve
 # this is here more for testing that actual usage since
@@ -3015,7 +3203,8 @@ sub polybezier {
   my $dflcl=i_color_new(0,0,0,0);
   my %opts=(color=>$dflcl,@_);
 
   my $dflcl=i_color_new(0,0,0,0);
   my %opts=(color=>$dflcl,@_);
 
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
+  $self->_valid_image("polybezier")
+    or return;
 
   if (exists $opts{points}) {
     $opts{'x'}=map { $_->[0]; } @{$opts{'points'}};
 
   if (exists $opts{points}) {
     $opts{'x'}=map { $_->[0]; } @{$opts{'points'}};
@@ -3041,6 +3230,9 @@ sub flood_fill {
   my %opts = ( color=>Imager::Color->new(255, 255, 255), @_ );
   my $rc;
 
   my %opts = ( color=>Imager::Color->new(255, 255, 255), @_ );
   my $rc;
 
+  $self->_valid_image("flood_fill")
+    or return;
+
   unless (exists $opts{'x'} && exists $opts{'y'}) {
     $self->{ERRSTR} = "missing seed x and y parameters";
     return undef;
   unless (exists $opts{'x'} && exists $opts{'y'}) {
     $self->{ERRSTR} = "missing seed x and y parameters";
     return undef;
@@ -3112,52 +3304,79 @@ sub flood_fill {
 }
 
 sub setpixel {
 }
 
 sub setpixel {
-  my $self = shift;
+  my ($self, %opts) = @_;
 
 
-  my %opts = ( color=>$self->{fg} || NC(255, 255, 255), @_);
+  $self->_valid_image("setpixel")
+    or return;
+
+  my $color = $opts{color};
+  unless (defined $color) {
+    $color = $self->{fg};
+    defined $color or $color = NC(255, 255, 255);
+  }
+
+  unless (ref $color && UNIVERSAL::isa($color, "Imager::Color")) {
+    unless ($color = _color($color, 'setpixel')) {
+      $self->_set_error("setpixel: " . Imager->errstr);
+      return;
+    }
+  }
 
   unless (exists $opts{'x'} && exists $opts{'y'}) {
 
   unless (exists $opts{'x'} && exists $opts{'y'}) {
-    $self->{ERRSTR} = 'missing x and y parameters';
-    return undef;
+    $self->_set_error('setpixel: missing x or y parameter');
+    return;
   }
 
   my $x = $opts{'x'};
   my $y = $opts{'y'};
   }
 
   my $x = $opts{'x'};
   my $y = $opts{'y'};
-  my $color = _color($opts{color})
-    or return undef;
-  if (ref $x && ref $y) {
-    unless (@$x == @$y) {
-      $self->{ERRSTR} = 'length of x and y mismatch';
+  if (ref $x || ref $y) {
+    $x = ref $x ? $x : [ $x ];
+    $y = ref $y ? $y : [ $y ];
+    unless (@$x) {
+      $self->_set_error("setpixel: x is a reference to an empty array");
+      return;
+    }
+    unless (@$y) {
+      $self->_set_error("setpixel: y is a reference to an empty array");
       return;
     }
       return;
     }
+
+    # make both the same length, replicating the last element
+    if (@$x < @$y) {
+      $x = [ @$x, ($x->[-1]) x (@$y - @$x) ];
+    }
+    elsif (@$y < @$x) {
+      $y = [ @$y, ($y->[-1]) x (@$x - @$y) ];
+    }
+
     my $set = 0;
     if ($color->isa('Imager::Color')) {
     my $set = 0;
     if ($color->isa('Imager::Color')) {
-      for my $i (0..$#{$opts{'x'}}) {
+      for my $i (0..$#$x) {
         i_ppix($self->{IMG}, $x->[$i], $y->[$i], $color)
          or ++$set;
       }
     }
     else {
         i_ppix($self->{IMG}, $x->[$i], $y->[$i], $color)
          or ++$set;
       }
     }
     else {
-      for my $i (0..$#{$opts{'x'}}) {
+      for my $i (0..$#$x) {
         i_ppixf($self->{IMG}, $x->[$i], $y->[$i], $color)
          or ++$set;
       }
     }
         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)
     return $set;
   }
   else {
     if ($color->isa('Imager::Color')) {
       i_ppix($self->{IMG}, $x, $y, $color)
-       and return;
+       and return "0 but true";
     }
     else {
       i_ppixf($self->{IMG}, $x, $y, $color)
     }
     else {
       i_ppixf($self->{IMG}, $x, $y, $color)
-       and return;
+       and return "0 but true";
     }
     }
-  }
 
 
-  $self;
+    return 1;
+  }
 }
 
 sub getpixel {
 }
 
 sub getpixel {
@@ -3165,48 +3384,74 @@ sub getpixel {
 
   my %opts = ( "type"=>'8bit', @_);
 
 
   my %opts = ( "type"=>'8bit', @_);
 
+  $self->_valid_image("getpixel")
+    or return;
+
   unless (exists $opts{'x'} && exists $opts{'y'}) {
   unless (exists $opts{'x'} && exists $opts{'y'}) {
-    $self->{ERRSTR} = 'missing x and y parameters';
-    return undef;
+    $self->_set_error('getpixel: missing x or y parameter');
+    return;
   }
 
   my $x = $opts{'x'};
   my $y = $opts{'y'};
   }
 
   my $x = $opts{'x'};
   my $y = $opts{'y'};
-  if (ref $x && ref $y) {
-    unless (@$x == @$y) {
-      $self->{ERRSTR} = 'length of x and y mismatch';
-      return undef;
+  my $type = $opts{'type'};
+  if (ref $x || ref $y) {
+    $x = ref $x ? $x : [ $x ];
+    $y = ref $y ? $y : [ $y ];
+    unless (@$x) {
+      $self->_set_error("getpixel: x is a reference to an empty array");
+      return;
+    }
+    unless (@$y) {
+      $self->_set_error("getpixel: y is a reference to an empty array");
+      return;
+    }
+
+    # make both the same length, replicating the last element
+    if (@$x < @$y) {
+      $x = [ @$x, ($x->[-1]) x (@$y - @$x) ];
+    }
+    elsif (@$y < @$x) {
+      $y = [ @$y, ($y->[-1]) x (@$x - @$y) ];
     }
     }
+
     my @result;
     my @result;
-    if ($opts{"type"} eq '8bit') {
-      for my $i (0..$#{$opts{'x'}}) {
+    if ($type eq '8bit') {
+      for my $i (0..$#$x) {
         push(@result, i_get_pixel($self->{IMG}, $x->[$i], $y->[$i]));
       }
     }
         push(@result, i_get_pixel($self->{IMG}, $x->[$i], $y->[$i]));
       }
     }
-    else {
-      for my $i (0..$#{$opts{'x'}}) {
+    elsif ($type eq 'float' || $type eq 'double') {
+      for my $i (0..$#$x) {
         push(@result, i_gpixf($self->{IMG}, $x->[$i], $y->[$i]));
       }
     }
         push(@result, i_gpixf($self->{IMG}, $x->[$i], $y->[$i]));
       }
     }
+    else {
+      $self->_set_error("getpixel: type must be '8bit' or 'float'");
+      return;
+    }
     return wantarray ? @result : \@result;
   }
   else {
     return wantarray ? @result : \@result;
   }
   else {
-    if ($opts{"type"} eq '8bit') {
+    if ($type eq '8bit') {
       return i_get_pixel($self->{IMG}, $x, $y);
     }
       return i_get_pixel($self->{IMG}, $x, $y);
     }
-    else {
+    elsif ($type eq 'float' || $type eq 'double') {
       return i_gpixf($self->{IMG}, $x, $y);
     }
       return i_gpixf($self->{IMG}, $x, $y);
     }
+    else {
+      $self->_set_error("getpixel: type must be '8bit' or 'float'");
+      return;
+    }
   }
   }
-
-  $self;
 }
 
 sub getscanline {
   my $self = shift;
   my %opts = ( type => '8bit', x=>0, @_);
 
 }
 
 sub getscanline {
   my $self = shift;
   my %opts = ( type => '8bit', x=>0, @_);
 
-  $self->_valid_image or return;
+  $self->_valid_image("getscanline")
+    or return;
 
   defined $opts{width} or $opts{width} = $self->getwidth - $opts{x};
 
 
   defined $opts{width} or $opts{width} = $self->getwidth - $opts{x};
 
@@ -3241,7 +3486,8 @@ sub setscanline {
   my $self = shift;
   my %opts = ( x=>0, @_);
 
   my $self = shift;
   my %opts = ( x=>0, @_);
 
-  $self->_valid_image or return;
+  $self->_valid_image("setscanline")
+    or return;
 
   unless (defined $opts{'y'}) {
     $self->_set_error("missing y parameter");
 
   unless (defined $opts{'y'}) {
     $self->_set_error("missing y parameter");
@@ -3302,6 +3548,9 @@ sub getsamples {
   my $self = shift;
   my %opts = ( type => '8bit', x=>0, offset => 0, @_);
 
   my $self = shift;
   my %opts = ( type => '8bit', x=>0, offset => 0, @_);
 
+  $self->_valid_image("getsamples")
+    or return;
+
   defined $opts{width} or $opts{width} = $self->getwidth - $opts{x};
 
   unless (defined $opts{'y'}) {
   defined $opts{width} or $opts{width} = $self->getwidth - $opts{x};
 
   unless (defined $opts{'y'}) {
@@ -3309,24 +3558,20 @@ sub getsamples {
     return;
   }
   
     return;
   }
   
-  unless ($opts{channels}) {
-    $opts{channels} = [ 0 .. $self->getchannels()-1 ];
-  }
-
   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},
   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}})
+                           $opts{y}, $opts{channels})
        or return;
        or return;
-      @{$target}{$offset .. $offset + @samples - 1} = @samples;
+      @{$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},
       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;
+                            $opts{y}, $opts{channels});
+      @{$target}[$offset .. $offset + @samples - 1] = @samples;
       return scalar(@samples);
     }
     elsif ($opts{type} =~ /^(\d+)bit$/) {
       return scalar(@samples);
     }
     elsif ($opts{type} =~ /^(\d+)bit$/) {
@@ -3335,7 +3580,7 @@ sub getsamples {
       my @data;
       my $count = i_gsamp_bits($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, 
                               $opts{y}, $bits, $target, 
       my @data;
       my $count = i_gsamp_bits($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, 
                               $opts{y}, $bits, $target, 
-                              $offset, @{$opts{channels}});
+                              $offset, $opts{channels});
       unless (defined $count) {
        $self->_set_error(Imager->_error_as_msg);
        return;
       unless (defined $count) {
        $self->_set_error(Imager->_error_as_msg);
        return;
@@ -3351,18 +3596,18 @@ sub getsamples {
   else {
     if ($opts{type} eq '8bit') {
       return i_gsamp($self->{IMG}, $opts{x}, $opts{x}+$opts{width},
   else {
     if ($opts{type} eq '8bit') {
       return i_gsamp($self->{IMG}, $opts{x}, $opts{x}+$opts{width},
-                    $opts{y}, @{$opts{channels}});
+                    $opts{y}, $opts{channels});
     }
     elsif ($opts{type} eq 'float') {
       return i_gsampf($self->{IMG}, $opts{x}, $opts{x}+$opts{width},
     }
     elsif ($opts{type} eq 'float') {
       return i_gsampf($self->{IMG}, $opts{x}, $opts{x}+$opts{width},
-                     $opts{y}, @{$opts{channels}});
+                     $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}, 
     }
     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}})
+                  $opts{y}, $bits, \@data, 0, $opts{channels})
        or return;
       return @data;
     }
        or return;
       return @data;
     }
@@ -3375,35 +3620,66 @@ sub getsamples {
 
 sub setsamples {
   my $self = shift;
 
 sub setsamples {
   my $self = shift;
-  my %opts = ( x => 0, offset => 0, @_ );
 
 
-  unless ($self->{IMG}) {
-    $self->_set_error('setsamples: empty input image');
-    return;
+  $self->_valid_image("setsamples")
+    or return;
+
+  my %opts = ( x => 0, offset => 0 );
+  my $data_index;
+  # avoid duplicating the data parameter, it may be a large scalar
+  my $i = 0;
+  while ($i < @_ -1) {
+    if ($_[$i] eq 'data') {
+      $data_index = $i+1;
+    }
+    else {
+      $opts{$_[$i]} = $_[$i+1];
+    }
+
+    $i += 2;
   }
 
   }
 
-  unless(defined $opts{data} && ref $opts{data}) {
-    $self->_set_error('setsamples: data parameter missing or invalid');
+  unless(defined $data_index) {
+    $self->_set_error('setsamples: data parameter missing');
     return;
   }
     return;
   }
-
-  unless ($opts{channels}) {
-    $opts{channels} = [ 0 .. $self->getchannels()-1 ];
+  unless (defined $_[$data_index]) {
+    $self->_set_error('setsamples: data parameter not defined');
+    return;
   }
 
   }
 
-  unless ($opts{type} && $opts{type} =~ /^(\d+)bit$/) {
-    $self->_set_error('setsamples: type parameter missing or invalid');
-    return;
+  my $type = $opts{type};
+  defined $type or $type = '8bit';
+
+  my $width = defined $opts{width} ? $opts{width}
+    : $self->getwidth() - $opts{x};
+
+  my $count;
+  if ($type eq '8bit') {
+    $count = i_psamp($self->{IMG}, $opts{x}, $opts{y}, $opts{channels},
+                    $_[$data_index], $opts{offset}, $width);
   }
   }
-  my $bits = $1;
+  elsif ($type eq 'float') {
+    $count = i_psampf($self->{IMG}, $opts{x}, $opts{y}, $opts{channels},
+                     $_[$data_index], $opts{offset}, $width);
+  }
+  elsif ($type =~ /^([0-9]+)bit$/) {
+    my $bits = $1;
+
+    unless (ref $_[$data_index]) {
+      $self->_set_error("setsamples: data must be an array ref for type not 8bit or float");
+      return;
+    }
 
 
-  unless (defined $opts{width}) {
-    $opts{width} = $self->getwidth() - $opts{x};
+    $count = i_psamp_bits($self->{IMG}, $opts{x}, $opts{y}, $bits,
+                         $opts{channels}, $_[$data_index], $opts{offset}, 
+                         $width);
+  }
+  else {
+    $self->_set_error('setsamples: type parameter invalid');
+    return;
   }
 
   }
 
-  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;
   unless (defined $count) {
     $self->_set_error(Imager->_error_as_msg);
     return;
@@ -3428,6 +3704,9 @@ sub convert {
   my ($self, %opts) = @_;
   my $matrix;
 
   my ($self, %opts) = @_;
   my $matrix;
 
+  $self->_valid_image("convert")
+    or return;
+
   unless (defined wantarray) {
     my @caller = caller;
     warn "convert() called in void context - convert() returns the converted image at $caller[1] line $caller[2]\n";
   unless (defined wantarray) {
     my @caller = caller;
     warn "convert() called in void context - convert() returns the converted image at $caller[1] line $caller[2]\n";
@@ -3523,12 +3802,53 @@ sub convert {
   $new->{IMG} = i_convert($self->{IMG}, $matrix);
   unless ($new->{IMG}) {
     # most likely a bad matrix
   $new->{IMG} = i_convert($self->{IMG}, $matrix);
   unless ($new->{IMG}) {
     # most likely a bad matrix
+    i_push_error(0, "convert");
     $self->{ERRSTR} = _error_as_msg();
     return undef;
   }
   return $new;
 }
 
     $self->{ERRSTR} = _error_as_msg();
     return undef;
   }
   return $new;
 }
 
+# combine channels from multiple input images, a class method
+sub combine {
+  my ($class, %opts) = @_;
+
+  my $src = delete $opts{src};
+  unless ($src) {
+    $class->_set_error("src parameter missing");
+    return;
+  }
+  my @imgs;
+  my $index = 0;
+  for my $img (@$src) {
+    unless (eval { $img->isa("Imager") }) {
+      $class->_set_error("src must contain image objects");
+      return;
+    }
+    unless ($img->_valid_image("combine")) {
+      $Imager::ERRSTR = $img->{ERRSTR} . " (src->[$index])";
+      return;
+    }
+    push @imgs, $img->{IMG};
+  }
+  my $result;
+  if (my $channels = delete $opts{channels}) {
+    $result = i_combine(\@imgs, $channels);
+  }
+  else {
+    $result = i_combine(\@imgs);
+  }
+  unless ($result) {
+    $class->_set_error($class->_error_as_msg);
+    return;
+  }
+
+  my $img = $class->new;
+  $img->{IMG} = $result;
+
+  return $img;
+}
+
 
 # general function to map an image through lookup tables
 
 
 # general function to map an image through lookup tables
 
@@ -3536,6 +3856,9 @@ sub map {
   my ($self, %opts) = @_;
   my @chlist = qw( red green blue alpha );
 
   my ($self, %opts) = @_;
   my @chlist = qw( red green blue alpha );
 
+  $self->_valid_image("map")
+    or return;
+
   if (!exists($opts{'maps'})) {
     # make maps from channel maps
     my $chnum;
   if (!exists($opts{'maps'})) {
     # make maps from channel maps
     my $chnum;
@@ -3556,15 +3879,17 @@ sub map {
 sub difference {
   my ($self, %opts) = @_;
 
 sub difference {
   my ($self, %opts) = @_;
 
+  $self->_valid_image("difference")
+    or return;
+
   defined $opts{mindist} or $opts{mindist} = 0;
 
   defined $opts{other}
     or return $self->_set_error("No 'other' parameter supplied");
   defined $opts{mindist} or $opts{mindist} = 0;
 
   defined $opts{other}
     or return $self->_set_error("No 'other' parameter supplied");
-  defined $opts{other}{IMG}
-    or return $self->_set_error("No image data in 'other' image");
-
-  $self->{IMG}
-    or return $self->_set_error("No image data");
+  unless ($opts{other}->_valid_image("difference")) {
+    $self->_set_error($opts{other}->errstr . " (other image)");
+    return;
+  }
 
   my $result = Imager->new;
   $result->{IMG} = i_diff_image($self->{IMG}, $opts{other}{IMG}, 
 
   my $result = Imager->new;
   $result->{IMG} = i_diff_image($self->{IMG}, $opts{other}{IMG}, 
@@ -3587,31 +3912,74 @@ sub border {
 
 sub getwidth {
   my $self = shift;
 
 sub getwidth {
   my $self = shift;
-  if (!defined($self->{IMG})) { $self->{ERRSTR} = 'image is empty'; return undef; }
-  return (i_img_info($self->{IMG}))[0];
+
+  $self->_valid_image("getwidth")
+    or return;
+
+  return i_img_get_width($self->{IMG});
 }
 
 # Get the height of an image
 
 sub getheight {
   my $self = shift;
 }
 
 # Get the height of an image
 
 sub getheight {
   my $self = shift;
-  if (!defined($self->{IMG})) { $self->{ERRSTR} = 'image is empty'; return undef; }
-  return (i_img_info($self->{IMG}))[1];
+
+  $self->_valid_image("getheight")
+    or return;
+
+  return i_img_get_height($self->{IMG});
 }
 
 # Get number of channels in an image
 
 sub getchannels {
   my $self = shift;
 }
 
 # Get number of channels in an image
 
 sub getchannels {
   my $self = shift;
-  if (!defined($self->{IMG})) { $self->{ERRSTR} = 'image is empty'; return undef; }
+
+  $self->_valid_image("getchannels")
+    or return;
+
   return i_img_getchannels($self->{IMG});
 }
 
   return i_img_getchannels($self->{IMG});
 }
 
+my @model_names = qw(unknown gray graya rgb rgba);
+
+sub colormodel {
+  my ($self, %opts) = @_;
+
+  $self->_valid_image("colormodel")
+    or return;
+
+  my $model = i_img_color_model($self->{IMG});
+
+  return $opts{numeric} ? $model : $model_names[$model];
+}
+
+sub colorchannels {
+  my ($self) = @_;
+
+  $self->_valid_image("colorchannels")
+    or return;
+
+  return i_img_color_channels($self->{IMG});
+}
+
+sub alphachannel {
+  my ($self) = @_;
+
+  $self->_valid_image("alphachannel")
+    or return;
+
+  return scalar(i_img_alpha_channel($self->{IMG}));
+}
+
 # Get channel mask
 
 sub getmask {
   my $self = shift;
 # Get channel mask
 
 sub getmask {
   my $self = shift;
-  if (!defined($self->{IMG})) { $self->{ERRSTR} = 'image is empty'; return undef; }
+
+  $self->_valid_image("getmask")
+    or return;
+
   return i_img_getmask($self->{IMG});
 }
 
   return i_img_getmask($self->{IMG});
 }
 
@@ -3620,14 +3988,15 @@ sub getmask {
 sub setmask {
   my $self = shift;
   my %opts = @_;
 sub setmask {
   my $self = shift;
   my %opts = @_;
-  if (!defined($self->{IMG})) { 
-    $self->{ERRSTR} = 'image is empty';
-    return undef;
-  }
+
+  $self->_valid_image("setmask")
+    or return;
+
   unless (defined $opts{mask}) {
     $self->_set_error("mask parameter required");
     return;
   }
   unless (defined $opts{mask}) {
     $self->_set_error("mask parameter required");
     return;
   }
+
   i_img_setmask( $self->{IMG} , $opts{mask} );
 
   1;
   i_img_setmask( $self->{IMG} , $opts{mask} );
 
   1;
@@ -3638,7 +4007,10 @@ sub setmask {
 sub getcolorcount {
   my $self=shift;
   my %opts=('maxcolors'=>2**30,@_);
 sub getcolorcount {
   my $self=shift;
   my %opts=('maxcolors'=>2**30,@_);
-  if (!defined($self->{IMG})) { $self->{ERRSTR}='image is empty'; return undef; }
+
+  $self->_valid_image("getcolorcount")
+    or return;
+
   my $rc=i_count_colors($self->{IMG},$opts{'maxcolors'});
   return ($rc==-1? undef : $rc);
 }
   my $rc=i_count_colors($self->{IMG},$opts{'maxcolors'});
   return ($rc==-1? undef : $rc);
 }
@@ -3647,7 +4019,10 @@ sub getcolorcount {
 # values are the number of pixels in this colour.
 sub getcolorusagehash {
   my $self = shift;
 # values are the number of pixels in this colour.
 sub getcolorusagehash {
   my $self = shift;
-  
+
+  $self->_valid_image("getcolorusagehash")
+    or return;
+
   my %opts = ( maxcolors => 2**30, @_ );
   my $max_colors = $opts{maxcolors};
   unless (defined $max_colors && $max_colors > 0) {
   my %opts = ( maxcolors => 2**30, @_ );
   my $max_colors = $opts{maxcolors};
   unless (defined $max_colors && $max_colors > 0) {
@@ -3655,11 +4030,6 @@ sub getcolorusagehash {
     return;
   }
 
     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)
   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)
@@ -3683,6 +4053,9 @@ sub getcolorusagehash {
 sub getcolorusage {
   my $self = shift;
 
 sub getcolorusage {
   my $self = shift;
 
+  $self->_valid_image("getcolorusage")
+    or return;
+
   my %opts = ( maxcolors => 2**30, @_ );
   my $max_colors = $opts{maxcolors};
   unless (defined $max_colors && $max_colors > 0) {
   my %opts = ( maxcolors => 2**30, @_ );
   my $max_colors = $opts{maxcolors};
   unless (defined $max_colors && $max_colors > 0) {
@@ -3690,11 +4063,6 @@ sub getcolorusage {
     return;
   }
 
     return;
   }
 
-  unless (defined $self->{IMG}) {
-    $self->_set_error('empty input image'); 
-    return undef;
-  }
-
   return i_get_anonymous_color_histo($self->{IMG}, $max_colors);
 }
 
   return i_get_anonymous_color_histo($self->{IMG}, $max_colors);
 }
 
@@ -3702,7 +4070,9 @@ sub getcolorusage {
 
 sub string {
   my $self = shift;
 
 sub string {
   my $self = shift;
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
+
+  $self->_valid_image("string")
+    or return;
 
   my %input=('x'=>0, 'y'=>0, @_);
   defined($input{string}) or $input{string} = $input{text};
 
   my %input=('x'=>0, 'y'=>0, @_);
   defined($input{string}) or $input{string} = $input{text};
@@ -3729,10 +4099,9 @@ sub align_string {
 
   my $img;
   if (ref $self) {
 
   my $img;
   if (ref $self) {
-    unless ($self->{IMG}) { 
-      $self->{ERRSTR}='empty input image'; 
-      return;
-    }
+    $self->_valid_image("align_string")
+      or return;
+
     $img = $self;
   }
   else {
     $img = $self;
   }
   else {
@@ -3740,7 +4109,8 @@ sub align_string {
   }
 
   my %input=('x'=>0, 'y'=>0, @_);
   }
 
   my %input=('x'=>0, 'y'=>0, @_);
-  $input{string}||=$input{text};
+  defined $input{string}
+    or $input{string} = $input{text};
 
   unless(exists $input{string}) {
     $self->_set_error("missing required parameter 'string'");
 
   unless(exists $input{string}) {
     $self->_set_error("missing required parameter 'string'");
@@ -3786,11 +4156,49 @@ sub get_file_limits {
   i_get_image_file_limits();
 }
 
   i_get_image_file_limits();
 }
 
+my @check_args = qw(width height channels sample_size);
+
+sub check_file_limits {
+  my $class = shift;
+
+  my %opts =
+    (
+     channels => 3,
+     sample_size => 1,
+     @_,
+    );
+
+  if ($opts{sample_size} && $opts{sample_size} eq 'float') {
+    $opts{sample_size} = length(pack("d", 0));
+  }
+
+  for my $name (@check_args) {
+    unless (defined $opts{$name}) {
+      $class->_set_error("check_file_limits: $name must be defined");
+      return;
+    }
+    unless ($opts{$name} == int($opts{$name})) {
+      $class->_set_error("check_file_limits: $name must be a positive integer");
+      return;
+    }
+  }
+
+  my $result = i_int_check_image_file_limits(@opts{@check_args});
+  unless ($result) {
+    $class->_set_error($class->_error_as_msg());
+  }
+
+  return $result;
+}
+
 # Shortcuts that can be exported
 
 sub newcolor { Imager::Color->new(@_); }
 sub newfont  { Imager::Font->new(@_); }
 # Shortcuts that can be exported
 
 sub newcolor { Imager::Color->new(@_); }
 sub newfont  { Imager::Font->new(@_); }
-sub NCF { Imager::Color::Float->new(@_) }
+sub NCF {
+  require Imager::Color::Float;
+  return Imager::Color::Float->new(@_);
+}
 
 *NC=*newcolour=*newcolor;
 *NF=*newfont;
 
 *NC=*newcolour=*newcolor;
 *NF=*newfont;
@@ -3819,21 +4227,48 @@ sub _set_error {
 
 # Default guess for the type of an image from extension
 
 
 # Default guess for the type of an image from extension
 
+my @simple_types = qw(png tga gif raw ico cur xpm mng jng ilbm pcx psd eps webp xwd xpm dng ras);
+
+my %ext_types =
+  (
+   ( map { $_ => $_ } @simple_types ),
+   tiff => "tiff",
+   tif => "tiff",
+   pbm => "pnm",
+   pgm => "pnm",
+   ppm => "pnm",
+   pnm => "pnm", # technically wrong, but historically it works in Imager
+   jpeg => "jpeg",
+   jpg => "jpeg",
+   bmp => "bmp",
+   dib => "bmp",
+   rgb => "sgi",
+   bw => "sgi",
+   sgi => "sgi",
+   fit => "fits",
+   fits => "fits",
+   rle => "utah",
+  );
+
 sub def_guess_type {
   my $name=lc(shift);
 sub def_guess_type {
   my $name=lc(shift);
-  my $ext;
-  $ext=($name =~ m/\.([^\.]+)$/)[0];
-  return 'tiff' if ($ext =~ m/^tiff?$/);
-  return 'jpeg' if ($ext =~ m/^jpe?g$/);
-  return 'pnm'  if ($ext =~ m/^p[pgb]m$/);
-  return 'png'  if ($ext eq "png");
-  return 'bmp'  if ($ext eq "bmp" || $ext eq "dib");
-  return 'tga'  if ($ext eq "tga");
-  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 ();
+
+  my ($ext) = $name =~ /\.([^.]+)$/
+    or return;
+
+  my $type = $ext_types{$ext}
+    or return;
+
+  return $type;
+}
+
+sub add_type_extensions {
+  my ($class, $type, @exts) = @_;
+
+  for my $ext (@exts) {
+    exists $ext_types{lc $ext} or $ext_types{lc $ext} = lc $type;
+  }
+  1;
 }
 
 sub combines {
 }
 
 sub combines {
@@ -3910,7 +4345,8 @@ sub parseiptc {
 }
 
 sub Inline {
 }
 
 sub Inline {
-  my ($lang) = @_;
+  # Inline added a new argument at the beginning
+  my $lang = $_[-1];
 
   $lang eq 'C'
     or die "Only C language supported";
 
   $lang eq 'C'
     or die "Only C language supported";
@@ -3922,6 +4358,176 @@ sub Inline {
 # threads shouldn't try to close raw Imager objects
 sub Imager::ImgRaw::CLONE_SKIP { 1 }
 
 # threads shouldn't try to close raw Imager objects
 sub Imager::ImgRaw::CLONE_SKIP { 1 }
 
+sub preload {
+  # this serves two purposes:
+  # - a class method to load the file support modules included with Imager
+  #   (or were included, once the library dependent modules are split out)
+  # - something for Module::ScanDeps to analyze
+  # https://rt.cpan.org/Ticket/Display.html?id=6566
+  local $@;
+  local @INC = @INC;
+  pop @INC if $INC[-1] eq '.';
+  eval { require Imager::File::GIF };
+  eval { require Imager::File::JPEG };
+  eval { require Imager::File::PNG };
+  eval { require Imager::File::SGI };
+  eval { require Imager::File::TIFF };
+  eval { require Imager::File::ICO };
+  eval { require Imager::Font::W32 };
+  eval { require Imager::Font::FT2 };
+  eval { require Imager::Font::T1 };
+  eval { require Imager::Color::Table };
+
+  1;
+}
+
+package Imager::IO;
+use IO::Seekable;
+
+sub new_fh {
+  my ($class, $fh) = @_;
+
+  if (tied(*$fh)) {
+    return $class->new_cb
+      (
+       sub {
+        local $\;
+
+        return print $fh $_[0];
+       },
+       sub {
+        my $tmp;
+        my $count = CORE::read $fh, $tmp, $_[1];
+        defined $count
+          or return undef;
+        $count
+          or return "";
+        return $tmp;
+       },
+       sub {
+        if ($_[1] != SEEK_CUR || $_[0] != 0) {
+          unless (CORE::seek $fh, $_[0], $_[1]) {
+            return -1;
+          }
+        }
+
+        return tell $fh;
+       },
+       undef,
+      );
+  }
+  else {
+    return $class->_new_perlio($fh);
+  }
+}
+
+# backward compatibility for %formats
+package Imager::FORMATS;
+use strict;
+use constant IX_FORMATS => 0;
+use constant IX_LIST => 1;
+use constant IX_INDEX => 2;
+use constant IX_CLASSES => 3;
+
+sub TIEHASH {
+  my ($class, $formats, $classes) = @_;
+
+  return bless [ $formats, [ ], 0, $classes ], $class;
+}
+
+sub _check {
+  my ($self, $key) = @_;
+
+  (my $file = $self->[IX_CLASSES]{$key} . ".pm") =~ s(::)(/)g;
+  my $value;
+  my $error;
+  my $loaded = Imager::_load_file($file, \$error);
+  if ($loaded) {
+    $value = 1;
+  }
+  else {
+    if ($error =~ /^Can't locate /) {
+      $error = "Can't locate $file";
+    }
+    $reader_load_errors{$key} = $writer_load_errors{$key} = $error;
+    $value = undef;
+  }
+  $self->[IX_FORMATS]{$key} = $value;
+
+  return $value;
+}
+
+sub FETCH {
+  my ($self, $key) = @_;
+
+  exists $self->[IX_FORMATS]{$key} and return $self->[IX_FORMATS]{$key};
+
+  $self->[IX_CLASSES]{$key} or return undef;
+
+  return $self->_check($key);
+}
+
+sub STORE {
+  die "%Imager::formats is not user monifiable";
+}
+
+sub DELETE {
+  die "%Imager::formats is not user monifiable";
+}
+
+sub CLEAR {
+  die "%Imager::formats is not user monifiable";
+}
+
+sub EXISTS {
+  my ($self, $key) = @_;
+
+  if (exists $self->[IX_FORMATS]{$key}) {
+    my $value = $self->[IX_FORMATS]{$key}
+      or return;
+    return 1;
+  }
+
+  $self->_check($key) or return 1==0;
+
+  return 1==1;
+}
+
+sub FIRSTKEY {
+  my ($self) = @_;
+
+  unless (@{$self->[IX_LIST]}) {
+    # full populate it
+    @{$self->[IX_LIST]} = grep $self->[IX_FORMATS]{$_},
+      keys %{$self->[IX_FORMATS]};
+
+    for my $key (keys %{$self->[IX_CLASSES]}) {
+      $self->[IX_FORMATS]{$key} and next;
+      $self->_check($key)
+       and push @{$self->[IX_LIST]}, $key;
+    }
+  }
+
+  @{$self->[IX_LIST]} or return;
+  $self->[IX_INDEX] = 1;
+  return $self->[IX_LIST][0];
+}
+
+sub NEXTKEY {
+  my ($self) = @_;
+
+  $self->[IX_INDEX] < @{$self->[IX_LIST]}
+    or return;
+
+  return $self->[IX_LIST][$self->[IX_INDEX]++];
+}
+
+sub SCALAR {
+  my ($self) = @_;
+
+  return scalar @{$self->[IX_LIST]};
+}
+
 1;
 __END__
 # Below is the stub of documentation for your module. You better edit it!
 1;
 __END__
 # Below is the stub of documentation for your module. You better edit it!
@@ -3944,7 +4550,7 @@ Imager - Perl extension for Generating 24 bit Images
   my $format;
 
   # see Imager::Files for information on the read() method
   my $format;
 
   # see Imager::Files for information on the read() method
-  my $im = Imager->new(file=>$file)
+  my $img = Imager->new(file=>$file)
     or die Imager->errstr();
 
   $file =~ s/\.[^.]*$//;
     or die Imager->errstr();
 
   $file =~ s/\.[^.]*$//;
@@ -3989,6 +4595,10 @@ Overview.
 
 =item *
 
 
 =item *
 
+L<Imager::Install> - installation notes for Imager.
+
+=item *
+
 L<Imager::Tutorial> - a brief introduction to Imager.
 
 =item *
 L<Imager::Tutorial> - a brief introduction to Imager.
 
 =item *
@@ -4055,6 +4665,10 @@ L<Imager::Fountain> - Helper for making gradient profiles.
 
 =item *
 
 
 =item *
 
+L<Imager::IO> - Imager I/O abstraction.
+
+=item *
+
 L<Imager::API> - using Imager's C API
 
 =item *
 L<Imager::API> - using Imager's C API
 
 =item *
@@ -4069,6 +4683,14 @@ L<Imager::Inline> - using Imager's C API from Inline::C
 
 L<Imager::ExtUtils> - tools to get access to Imager's C API.
 
 
 L<Imager::ExtUtils> - tools to get access to Imager's C API.
 
+=item *
+
+L<Imager::Security> - brief security notes.
+
+=item *
+
+L<Imager::Threads> - brief information on working with threads.
+
 =back
 
 =head2 Basic Overview
 =back
 
 =head2 Basic Overview
@@ -4095,7 +4717,7 @@ the C<errstr()> method to find out why:
 
 =over
 
 
 =over
 
-=item C<errstr>
+=item errstr()
 
 Returns the last error message in that context.
 
 
 Returns the last error message in that context.
 
@@ -4123,230 +4745,271 @@ L<Imager::ImageTypes>.
 
 Where to find information on methods for Imager class objects.
 
 
 Where to find information on methods for Imager class objects.
 
-addcolors() - L<Imager::ImageTypes/addcolors> - add colors to a
+addcolors() - L<Imager::ImageTypes/addcolors()> - add colors to a
 paletted image
 
 paletted image
 
-addtag() -  L<Imager::ImageTypes/addtag> - add image tags
+addtag() -  L<Imager::ImageTypes/addtag()> - add image tags
+
+add_type_extensions() -
+L<Imager::Files/add_type_extensions($type, $ext, ...)> - add extensions for
+new image file types.
 
 
-align_string() - L<Imager::Draw/align_string> - draw text aligned on a
+align_string() - L<Imager::Draw/align_string()> - draw text aligned on a
 point
 
 point
 
-arc() - L<Imager::Draw/arc> - draw a filled arc
+alphachannel() - L<Imager::ImageTypes/alphachannel()> - return the
+channel index of the alpha channel (if any).
 
 
-bits() - L<Imager::ImageTypes/bits> - number of bits per sample for the
+arc() - L<Imager::Draw/arc()> - draw a filled arc
+
+bits() - L<Imager::ImageTypes/bits()> - number of bits per sample for the
 image
 
 image
 
-box() - L<Imager::Draw/box> - draw a filled or outline box.
+box() - L<Imager::Draw/box()> - draw a filled or outline box.
+
+check_file_limits() - L<Imager::Files/check_file_limits()>
+
+circle() - L<Imager::Draw/circle()> - draw a filled circle
 
 
-circle() - L<Imager::Draw/circle> - draw a filled circle
+close_log() - L<Imager::ImageTypes/close_log()> - close the Imager
+debugging log.
 
 
-colorcount() - L<Imager::Draw/colorcount> - the number of colors in an
-image's palette (paletted images only)
+colorchannels() - L<Imager::ImageTypes/colorchannels()> - the number
+of channels used for color.
 
 
-combines() - L<Imager::Draw/combines> - return a list of the different
-combine type keywords
+colorcount() - L<Imager::ImageTypes/colorcount()> - the number of
+colors in an image's palette (paletted images only)
 
 
-compose() - L<Imager::Transformations/compose> - compose one image
+colormodel() - L<Imager::ImageTypes/colorcount()> - how color is
+represented.
+
+combine() - L<Imager::Transformations/combine()> - combine channels
+from one or more images.
+
+combines() - L<Imager::Draw/combines()> - return a list of the
+different combine type keywords
+
+compose() - L<Imager::Transformations/compose()> - compose one image
 over another.
 
 over another.
 
-convert() - L<Imager::Transformations/"Color transformations"> -
-transform the color space
+convert() - L<Imager::Transformations/convert()> - transform the color
+space
 
 
-copy() - L<Imager::Transformations/copy> - make a duplicate of an
+copy() - L<Imager::Transformations/copy()> - make a duplicate of an
 image
 
 image
 
-crop() - L<Imager::Transformations/crop> - extract part of an image
+crop() - L<Imager::Transformations/crop()> - extract part of an image
 
 
-def_guess_type() - L<Imager::Files/def_guess_type> - default function
+def_guess_type() - L<Imager::Files/def_guess_type()> - default function
 used to guess the output file format based on the output file name
 
 used to guess the output file format based on the output file name
 
-deltag() -  L<Imager::ImageTypes/deltag> - delete image tags
+deltag() -  L<Imager::ImageTypes/deltag()> - delete image tags
 
 
-difference() - L<Imager::Filters/"Image Difference"> - produce a
-difference images from two input images.
+difference() - L<Imager::Filters/difference()> - produce a difference
+images from two input images.
 
 
-errstr() - L<"Basic Overview"> - the error from the last failed
-operation.
+errstr() - L</errstr()> - the error from the last failed operation.
 
 
-filter() - L<Imager::Filters> - image filtering
+filter() - L<Imager::Filters/filter()> - image filtering
 
 
-findcolor() - L<Imager::ImageTypes/findcolor> - search the image
+findcolor() - L<Imager::ImageTypes/findcolor()> - search the image
 palette, if it has one
 
 palette, if it has one
 
-flip() - L<Imager::Transformations/flip> - flip an image, vertically,
+flip() - L<Imager::Transformations/flip()> - flip an image, vertically,
 horizontally
 
 horizontally
 
-flood_fill() - L<Imager::Draw/flood_fill> - fill an enclosed or same
+flood_fill() - L<Imager::Draw/flood_fill()> - fill an enclosed or same
 color area
 
 color area
 
-getchannels() - L<Imager::ImageTypes/getchannels> - the number of
+getchannels() - L<Imager::ImageTypes/getchannels()> - the number of
 samples per pixel for an image
 
 samples per pixel for an image
 
-getcolorcount() - L<Imager::ImageTypes/getcolorcount> - the number of
+getcolorcount() - L<Imager::ImageTypes/getcolorcount()> - the number of
 different colors used by an image (works for direct color images)
 
 different colors used by an image (works for direct color images)
 
-getcolors() - L<Imager::ImageTypes/getcolors> - get colors from the image
+getcolors() - L<Imager::ImageTypes/getcolors()> - get colors from the image
 palette, if it has one
 
 palette, if it has one
 
-getcolorusage() - L<Imager::ImageTypes/getcolorusage>
+getcolorusage() - L<Imager::ImageTypes/getcolorusage()>
 
 
-getcolorusagehash() - L<Imager::ImageTypes/getcolorusagehash>
+getcolorusagehash() - L<Imager::ImageTypes/getcolorusagehash()>
 
 
-get_file_limits() - L<Imager::Files/"Limiting the sizes of images you read">
+get_file_limits() - L<Imager::Files/get_file_limits()>
 
 
-getheight() - L<Imager::ImageTypes/getwidth> - height of the image in
+getheight() - L<Imager::ImageTypes/getheight()> - height of the image in
 pixels
 
 pixels
 
-getmask() - L<Imager::ImageTypes/getmask> - write mask for the image
+getmask() - L<Imager::ImageTypes/getmask()> - write mask for the image
 
 
-getpixel() - L<Imager::Draw/getpixel> - retrieve one or more pixel
+getpixel() - L<Imager::Draw/getpixel()> - retrieve one or more pixel
 colors
 
 colors
 
-getsamples() - L<Imager::Draw/getsamples> - retrieve samples from a
+getsamples() - L<Imager::Draw/getsamples()> - retrieve samples from a
 row or partial row of pixels.
 
 row or partial row of pixels.
 
-getscanline() - L<Imager::Draw/getscanline> - retrieve colors for a
+getscanline() - L<Imager::Draw/getscanline()> - retrieve colors for a
 row or partial row of pixels.
 
 row or partial row of pixels.
 
-getwidth() - L<Imager::ImageTypes/getwidth> - width of the image in
+getwidth() - L<Imager::ImageTypes/getwidth()> - width of the image in
 pixels.
 
 pixels.
 
-img_set() - L<Imager::ImageTypes/img_set> - re-use an Imager object
+img_set() - L<Imager::ImageTypes/img_set()> - re-use an Imager object
 for a new image.
 
 for a new image.
 
-init() - L<Imager::ImageTypes/init>
+init() - L<Imager::ImageTypes/init()>
 
 
-is_bilevel() - L<Imager::ImageTypes/is_bilevel> - returns whether
+is_bilevel() - L<Imager::ImageTypes/is_bilevel()> - returns whether
 image write functions should write the image in their bilevel (blank
 and white, no gray levels) format
 
 image write functions should write the image in their bilevel (blank
 and white, no gray levels) format
 
-line() - L<Imager::Draw/line> - draw an interval
+is_logging() L<Imager::ImageTypes/is_logging()> - test if the debug
+log is active.
+
+line() - L<Imager::Draw/line()> - draw an interval
 
 
-load_plugin() - L<Imager::Filters/load_plugin>
+load_plugin() - L<Imager::Filters/load_plugin()>
 
 
-map() - L<Imager::Transformations/"Color Mappings"> - remap color
+log() - L<Imager::ImageTypes/log()> - send a message to the debugging
+log.
+
+make_palette() - L<Imager::ImageTypes/make_palette()> - produce a
+color palette from one or more input images.
+
+map() - L<Imager::Transformations/map()> - remap color
 channel values
 
 channel values
 
-masked() -  L<Imager::ImageTypes/masked> - make a masked image
+masked() -  L<Imager::ImageTypes/masked()> - make a masked image
 
 
-matrix_transform() - L<Imager::Engines/matrix_transform>
+matrix_transform() - L<Imager::Engines/matrix_transform()>
 
 
-maxcolors() - L<Imager::ImageTypes/maxcolors>
+maxcolors() - L<Imager::ImageTypes/maxcolors()>
 
 
-NC() - L<Imager::Handy/NC>
+NC() - L<Imager::Handy/NC()>
 
 
-NCF() - L<Imager::Handy/NCF>
+NCF() - L<Imager::Handy/NCF()>
 
 
-new() - L<Imager::ImageTypes/new>
+new() - L<Imager::ImageTypes/new()>
 
 
-newcolor() - L<Imager::Handy/newcolor>
+newcolor() - L<Imager::Handy/newcolor()>
 
 
-newcolour() - L<Imager::Handy/newcolour>
+newcolour() - L<Imager::Handy/newcolour()>
 
 
-newfont() - L<Imager::Handy/newfont>
+newfont() - L<Imager::Handy/newfont()>
 
 
-NF() - L<Imager::Handy/NF>
+NF() - L<Imager::Handy/NF()>
 
 
-open() - L<Imager::Files> - an alias for read()
+open() - L<Imager::Files/read()> - an alias for read()
+
+open_log() - L<Imager::ImageTypes/open_log()> - open the debug log.
 
 =for stopwords IPTC
 
 
 =for stopwords IPTC
 
-parseiptc() - L<Imager::Files/parseiptc> - parse IPTC data from a JPEG
+parseiptc() - L<Imager::Files/parseiptc()> - parse IPTC data from a JPEG
+image
+
+paste() - L<Imager::Transformations/paste()> - draw an image onto an
 image
 
 image
 
-paste() - L<Imager::Transformations/paste> - draw an image onto an image
+polygon() - L<Imager::Draw/polygon()>
+
+polyline() - L<Imager::Draw/polyline()>
 
 
-polygon() - L<Imager::Draw/polygon>
+polypolygon() - L<Imager::Draw/polypolygon()>
 
 
-polyline() - L<Imager::Draw/polyline>
+preload() - L<Imager::Files/preload()>
 
 
-read() - L<Imager::Files> - read a single image from an image file
+read() - L<Imager::Files/read()> - read a single image from an image file
 
 
-read_multi() - L<Imager::Files> - read multiple images from an image
+read_multi() - L<Imager::Files/read_multi()> - read multiple images from an image
 file
 
 file
 
-read_types() - L<Imager::Files/read_types> - list image types Imager
+read_types() - L<Imager::Files/read_types()> - list image types Imager
 can read.
 
 can read.
 
-register_filter() - L<Imager::Filters/register_filter>
+register_filter() - L<Imager::Filters/register_filter()>
 
 
-register_reader() - L<Imager::Files/register_reader>
+register_reader() - L<Imager::Files/register_reader()>
 
 
-register_writer() - L<Imager::Files/register_writer>
+register_writer() - L<Imager::Files/register_writer()>
 
 
-rotate() - L<Imager::Transformations/rotate>
+rotate() - L<Imager::Transformations/rotate()>
 
 
-rubthrough() - L<Imager::Transformations/rubthrough> - draw an image onto an
-image and use the alpha channel
+rubthrough() - L<Imager::Transformations/rubthrough()> - draw an image
+onto an image and use the alpha channel
 
 
-scale() - L<Imager::Transformations/scale>
+scale() - L<Imager::Transformations/scale()>
 
 
-scale_calculate() - L<Imager::Transformations/scale_calculate>
+scale_calculate() - L<Imager::Transformations/scale_calculate()>
 
 
-scaleX() - L<Imager::Transformations/scaleX>
+scaleX() - L<Imager::Transformations/scaleX()>
 
 
-scaleY() - L<Imager::Transformations/scaleY>
+scaleY() - L<Imager::Transformations/scaleY()>
 
 
-setcolors() - L<Imager::ImageTypes/setcolors> - set palette colors in
-a paletted image
+setcolors() - L<Imager::ImageTypes/setcolors()> - set palette colors
+in a paletted image
 
 
-set_file_limits() - L<Imager::Files/"Limiting the sizes of images you read">
+set_file_limits() - L<Imager::Files/set_file_limits()>
 
 
-setmask() - L<Imager::ImageTypes/setmask>
+setmask() - L<Imager::ImageTypes/setmask()>
 
 
-setpixel() - L<Imager::Draw/setpixel>
+setpixel() - L<Imager::Draw/setpixel()>
 
 
-setsamples() - L<Imager::Draw/setsamples>
+setsamples() - L<Imager::Draw/setsamples()>
 
 
-setscanline() - L<Imager::Draw/setscanline>
+setscanline() - L<Imager::Draw/setscanline()>
 
 
-settag() - L<Imager::ImageTypes/settag>
+settag() - L<Imager::ImageTypes/settag()>
 
 
-string() - L<Imager::Draw/string> - draw text on an image
+string() - L<Imager::Draw/string()> - draw text on an image
 
 
-tags() -  L<Imager::ImageTypes/tags> - fetch image tags
+tags() -  L<Imager::ImageTypes/tags()> - fetch image tags
 
 
-to_paletted() -  L<Imager::ImageTypes/to_paletted>
+to_paletted() -  L<Imager::ImageTypes/to_paletted()>
 
 
-to_rgb16() - L<Imager::ImageTypes/to_rgb16>
+to_rgb16() - L<Imager::ImageTypes/to_rgb16()>
 
 
-to_rgb8() - L<Imager::ImageTypes/to_rgb8>
+to_rgb8() - L<Imager::ImageTypes/to_rgb8()>
 
 
-transform() - L<Imager::Engines/"transform">
+to_rgb_double() - L<Imager::ImageTypes/to_rgb_double()> - convert to
+double per sample image.
 
 
-transform2() - L<Imager::Engines/"transform2">
+transform() - L<Imager::Engines/"transform()">
 
 
-type() -  L<Imager::ImageTypes/type> - type of image (direct vs paletted)
+transform2() - L<Imager::Engines/"transform2()">
 
 
-unload_plugin() - L<Imager::Filters/unload_plugin>
+type() -  L<Imager::ImageTypes/type()> - type of image (direct vs paletted)
 
 
-virtual() - L<Imager::ImageTypes/virtual> - whether the image has it's own
+unload_plugin() - L<Imager::Filters/unload_plugin()>
+
+virtual() - L<Imager::ImageTypes/virtual()> - whether the image has it's own
 data
 
 data
 
-write() - L<Imager::Files> - write an image to a file
+write() - L<Imager::Files/write()> - write an image to a file
 
 
-write_multi() - L<Imager::Files> - write multiple image to an image
+write_multi() - L<Imager::Files/write_multi()> - write multiple image to an image
 file.
 
 file.
 
-write_types() - L<Imager::Files/read_types> - list image types Imager
+write_types() - L<Imager::Files/read_types()> - list image types Imager
 can write.
 
 =head1 CONCEPT INDEX
 
 animated GIF - L<Imager::Files/"Writing an animated GIF">
 
 can write.
 
 =head1 CONCEPT INDEX
 
 animated GIF - L<Imager::Files/"Writing an animated GIF">
 
-aspect ratio - L<Imager::ImageTypes/i_xres>,
-L<Imager::ImageTypes/i_yres>, L<Imager::ImageTypes/i_aspect_only>
+aspect ratio - C<i_xres>, C<i_yres>, C<i_aspect_only> in
+L<Imager::ImageTypes/"Common Tags">.
 
 blend - alpha blending one image onto another
 
 blend - alpha blending one image onto another
-L<Imager::Transformations/rubthrough>
+L<Imager::Transformations/rubthrough()>
 
 
-blur - L<Imager::Filters/guassian>, L<Imager::Filters/conv>
+blur - L<Imager::Filters/gaussian>, L<Imager::Filters/conv>
 
 
-boxes, drawing - L<Imager::Draw/box>
+boxes, drawing - L<Imager::Draw/box()>
 
 changes between image - L<Imager::Filters/"Image Difference">
 
 
 changes between image - L<Imager::Filters/"Image Difference">
 
+channels, combine into one image - L<Imager::Transformations/combine()>
+
 color - L<Imager::Color>
 
 color names - L<Imager::Color>, L<Imager::Color::Table>
 color - L<Imager::Color>
 
 color names - L<Imager::Color>, L<Imager::Color::Table>
@@ -4359,22 +5022,22 @@ contrast - L<Imager::Filters/contrast>, L<Imager::Filters/autolevels>
 
 convolution - L<Imager::Filters/conv>
 
 
 convolution - L<Imager::Filters/conv>
 
-cropping - L<Imager::Transformations/crop>
+cropping - L<Imager::Transformations/crop()>
 
 CUR files - L<Imager::Files/"ICO (Microsoft Windows Icon) and CUR (Microsoft Windows Cursor)">
 
 C<diff> images - L<Imager::Filters/"Image Difference">
 
 
 CUR files - L<Imager::Files/"ICO (Microsoft Windows Icon) and CUR (Microsoft Windows Cursor)">
 
 C<diff> images - L<Imager::Filters/"Image Difference">
 
-dpi - L<Imager::ImageTypes/i_xres>, 
+dpi - C<i_xres>, C<i_yres> in L<Imager::ImageTypes/"Common Tags">,
 L<Imager::Cookbook/"Image spatial resolution">
 
 L<Imager::Cookbook/"Image spatial resolution">
 
-drawing boxes - L<Imager::Draw/box>
+drawing boxes - L<Imager::Draw/box()>
 
 
-drawing lines - L<Imager::Draw/line>
+drawing lines - L<Imager::Draw/line()>
 
 
-drawing text - L<Imager::Draw/string>, L<Imager::Draw/align_string>
+drawing text - L<Imager::Draw/string()>, L<Imager::Draw/align_string()>
 
 
-error message - L<"ERROR HANDLING">
+error message - L</"ERROR HANDLING">
 
 files, font - L<Imager::Font>
 
 
 files, font - L<Imager::Font>
 
@@ -4382,18 +5045,18 @@ files, image - L<Imager::Files>
 
 filling, types of fill - L<Imager::Fill>
 
 
 filling, types of fill - L<Imager::Fill>
 
-filling, boxes - L<Imager::Draw/box>
+filling, boxes - L<Imager::Draw/box()>
 
 
-filling, flood fill - L<Imager::Draw/flood_fill>
+filling, flood fill - L<Imager::Draw/flood_fill()>
 
 
-flood fill - L<Imager::Draw/flood_fill>
+flood fill - L<Imager::Draw/flood_fill()>
 
 fonts - L<Imager::Font>
 
 
 fonts - L<Imager::Font>
 
-fonts, drawing with - L<Imager::Draw/string>,
-L<Imager::Draw/align_string>, L<Imager::Font::Wrap>
+fonts, drawing with - L<Imager::Draw/string()>,
+L<Imager::Draw/align_string()>, L<Imager::Font::Wrap>
 
 
-fonts, metrics - L<Imager::Font/bounding_box>, L<Imager::Font::BBox>
+fonts, metrics - L<Imager::Font/bounding_box()>, L<Imager::Font::BBox>
 
 fonts, multiple master - L<Imager::Font/"MULTIPLE MASTER FONTS">
 
 
 fonts, multiple master - L<Imager::Font/"MULTIPLE MASTER FONTS">
 
@@ -4403,33 +5066,34 @@ L<Imager::Filters/gradgen>
 
 GIF files - L<Imager::Files/"GIF">
 
 
 GIF files - L<Imager::Files/"GIF">
 
-GIF files, animated - L<Imager::File/"Writing an animated GIF">
+GIF files, animated - L<Imager::Files/"Writing an animated GIF">
 
 gradient fill - L<Imager::Fill/"Fountain fills">,
 L<Imager::Filters/fountain>, L<Imager::Fountain>,
 L<Imager::Filters/gradgen>
 
 
 gradient fill - L<Imager::Fill/"Fountain fills">,
 L<Imager::Filters/fountain>, L<Imager::Fountain>,
 L<Imager::Filters/gradgen>
 
-gray scale, convert image to - L<Imager::Transformations/convert>
+gray scale, convert image to - L<Imager::Transformations/convert()>
 
 
-guassian blur - L<Imager::Filters/guassian>
+gaussian blur - L<Imager::Filters/gaussian>
 
 hatch fills - L<Imager::Fill/"Hatched fills">
 
 ICO files - L<Imager::Files/"ICO (Microsoft Windows Icon) and CUR (Microsoft Windows Cursor)">
 
 
 hatch fills - L<Imager::Fill/"Hatched fills">
 
 ICO files - L<Imager::Files/"ICO (Microsoft Windows Icon) and CUR (Microsoft Windows Cursor)">
 
-invert image - L<Imager::Filters/hardinvert>
+invert image - L<Imager::Filters/hardinvert>,
+L<Imager::Filters/hardinvertall>
 
 JPEG - L<Imager::Files/"JPEG">
 
 limiting image sizes - L<Imager::Files/"Limiting the sizes of images you read">
 
 
 JPEG - L<Imager::Files/"JPEG">
 
 limiting image sizes - L<Imager::Files/"Limiting the sizes of images you read">
 
-lines, drawing - L<Imager::Draw/line>
+lines, drawing - L<Imager::Draw/line()>
 
 matrix - L<Imager::Matrix2d>, 
 
 matrix - L<Imager::Matrix2d>, 
-L<Imager::Transformations/"Matrix Transformations">,
-L<Imager::Font/transform>
+L<Imager::Engines/"Matrix Transformations">,
+L<Imager::Font/transform()>
 
 
-metadata, image - L<Imager::ImageTypes/"Tags">
+metadata, image - L<Imager::ImageTypes/"Tags">, L<Image::ExifTool>
 
 mosaic - L<Imager::Filters/mosaic>
 
 
 mosaic - L<Imager::Filters/mosaic>
 
@@ -4438,11 +5102,11 @@ noise, filter - L<Imager::Filters/noise>
 noise, rendered - L<Imager::Filters/turbnoise>,
 L<Imager::Filters/radnoise>
 
 noise, rendered - L<Imager::Filters/turbnoise>,
 L<Imager::Filters/radnoise>
 
-paste - L<Imager::Transformations/paste>,
-L<Imager::Transformations/rubthrough>
+paste - L<Imager::Transformations/paste()>,
+L<Imager::Transformations/rubthrough()>
 
 
-pseudo-color image - L<Imager::ImageTypes/to_paletted>,
-L<Imager::ImageTypes/new>
+pseudo-color image - L<Imager::ImageTypes/to_paletted()>,
+L<Imager::ImageTypes/new()>
 
 =for stopwords posterize
 
 
 =for stopwords posterize
 
@@ -4452,37 +5116,44 @@ PNG files - L<Imager::Files>, L<Imager::Files/"PNG">
 
 PNM - L<Imager::Files/"PNM (Portable aNy Map)">
 
 
 PNM - L<Imager::Files/"PNM (Portable aNy Map)">
 
-rectangles, drawing - L<Imager::Draw/box>
+rectangles, drawing - L<Imager::Draw/box()>
 
 
-resizing an image - L<Imager::Transformations/scale>, 
-L<Imager::Transformations/crop>
+resizing an image - L<Imager::Transformations/scale()>, 
+L<Imager::Transformations/crop()>
 
 RGB (SGI) files - L<Imager::Files/"SGI (RGB, BW)">
 
 saving an image - L<Imager::Files>
 
 
 RGB (SGI) files - L<Imager::Files/"SGI (RGB, BW)">
 
 saving an image - L<Imager::Files>
 
-scaling - L<Imager::Transformations/scale>
+scaling - L<Imager::Transformations/scale()>
+
+security - L<Imager::Security>
 
 SGI files - L<Imager::Files/"SGI (RGB, BW)">
 
 sharpen - L<Imager::Filters/unsharpmask>, L<Imager::Filters/conv>
 
 
 SGI files - L<Imager::Files/"SGI (RGB, BW)">
 
 sharpen - L<Imager::Filters/unsharpmask>, L<Imager::Filters/conv>
 
-size, image - L<Imager::ImageTypes/getwidth>,
-L<Imager::ImageTypes/getheight>
+size, image - L<Imager::ImageTypes/getwidth()>,
+L<Imager::ImageTypes/getheight()>
 
 
-size, text - L<Imager::Font/bounding_box>
+size, text - L<Imager::Font/bounding_box()>
 
 tags, image metadata - L<Imager::ImageTypes/"Tags">
 
 
 tags, image metadata - L<Imager::ImageTypes/"Tags">
 
-text, drawing - L<Imager::Draw/string>, L<Imager::Draw/align_string>,
+text, drawing - L<Imager::Draw/string()>, L<Imager::Draw/align_string()>,
 L<Imager::Font::Wrap>
 
 text, wrapping text in an area - L<Imager::Font::Wrap>
 
 L<Imager::Font::Wrap>
 
 text, wrapping text in an area - L<Imager::Font::Wrap>
 
-text, measuring - L<Imager::Font/bounding_box>, L<Imager::Font::BBox>
+text, measuring - L<Imager::Font/bounding_box()>, L<Imager::Font::BBox>
+
+threads - L<Imager::Threads>
 
 tiles, color - L<Imager::Filters/mosaic>
 
 
 tiles, color - L<Imager::Filters/mosaic>
 
+transparent images - L<Imager::ImageTypes>,
+L<Imager::Cookbook/"Transparent PNG">
+
 =for stopwords unsharp
 
 unsharp mask - L<Imager::Filters/unsharpmask>
 =for stopwords unsharp
 
 unsharp mask - L<Imager::Filters/unsharpmask>
@@ -4491,15 +5162,6 @@ watermark - L<Imager::Filters/watermark>
 
 writing an image to a file - L<Imager::Files>
 
 
 writing an image to a file - L<Imager::Files>
 
-=head1 THREADS
-
-Imager doesn't support perl threads.
-
-Imager has limited code to prevent double frees if you create images,
-colors etc, and then create a thread, but has no code to prevent two
-threads entering Imager's error handling code, and none is likely to
-be added.
-
 =head1 SUPPORT
 
 The best place to get help with Imager is the mailing list.
 =head1 SUPPORT
 
 The best place to get help with Imager is the mailing list.
@@ -4567,22 +5229,52 @@ Tracker.
 
 =head2 Patches
 
 
 =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.
+I accept patches, preferably against the master branch in git.  Please
+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.
 
 
 Your patch should include regression tests where possible, otherwise
 it will be delayed until I get a chance to write them.
 
+To browse Imager's git repository:
+
+  http://git.imager.perl.org/imager.git
+
+To clone:
+
+  git clone git://git.imager.perl.org/imager.git
+
+My preference is that patches are provided in the format produced by
+C<git format-patch>, for example, if you made your changes in a branch
+from master you might do:
+
+  git format-patch -k --stdout master >my-patch.txt
+
+and then attach that to your bug report, either by adding it as an
+attachment in your email client, or by using the Request Tracker
+attachment mechanism.
+
 =head1 AUTHOR
 
 =head1 AUTHOR
 
-Tony Cook <tony@imager.perl.org> is the current maintainer for Imager.
+Tony Cook <tonyc@cpan.org> is the current maintainer for Imager.
 
 Arnar M. Hrafnkelsson is the original author of Imager.
 
 Many others have contributed to Imager, please see the C<README> for a
 complete list.
 
 
 Arnar M. Hrafnkelsson is the original author of Imager.
 
 Many others have contributed to Imager, please see the C<README> for a
 complete list.
 
+=head1 LICENSE
+
+Imager is licensed under the same terms as perl itself.
+
+=for stopwords
+makeblendedfont Fontforge
+
+A test font, generated by the Debian packaged Fontforge,
+F<FT2/fontfiles/MMOne.pfb>, contains a Postscript operator definition
+copyrighted by Adobe.  See F<adobe.txt> in the source for license
+information.
+
 =head1 SEE ALSO
 
 L<perl>(1), L<Imager::ImageTypes>(3), L<Imager::Files>(3),
 =head1 SEE ALSO
 
 L<perl>(1), L<Imager::ImageTypes>(3), L<Imager::Files>(3),
@@ -4597,6 +5289,13 @@ L<Affix::Infix2Postfix>(3), L<Parse::RecDescent>(3)
 
 Other perl imaging modules include:
 
 
 Other perl imaging modules include:
 
-L<GD>(3), L<Image::Magick>(3), L<Graphics::Magick>(3).
+L<GD>(3), L<Image::Magick>(3),
+L<Graphics::Magick|http://www.graphicsmagick.org/perl.html>(3),
+L<Prima::Image>, L<IPA>.
+
+For manipulating image metadata see L<Image::ExifTool>.
+
+If you're trying to use Imager for array processing, you should
+probably using L<PDL>.
 
 =cut
 
 =cut