]> git.imager.perl.org - imager.git/commitdiff
Imager->new now attempts to read a file given any data source
authorTony Cook <tony@develop-help.com>
Sun, 8 Nov 2015 00:09:42 +0000 (11:09 +1100)
committerTony Cook <tony@develop-help.com>
Sun, 8 Nov 2015 00:09:42 +0000 (11:09 +1100)
Previously, attempting to read a raw image would simply return a blank
image, since the xsize and ysize parameters are required when reading
a raw image, and new() checked only those parameters to decide to
return a blank image.

Imager.pm
t/200-file/300-raw.t

index cc4ecb23b2ad2558c8e7612d8fff0e2c6bddd2eb..abac09c23bb93ed058ab52330981f1a02cc1dc3e 100644 (file)
--- a/Imager.pm
+++ b/Imager.pm
@@ -671,18 +671,13 @@ sub new {
   $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} ||
-        defined $hsh{data}) {
+  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;
     
@@ -696,6 +691,12 @@ sub new {
       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;
index ed92efe23b794faff4005a94080f6b91f4071c49..dddd690b02db2baac54314404a2b6dfafe24e345 100644 (file)
@@ -1,8 +1,8 @@
 #!perl -w
 use strict;
-use Test::More tests => 53;
+use Test::More tests => 56;
 use Imager qw(:all);
-use Imager::Test qw/is_color3 is_color4 test_image test_image_mono/;
+use Imager::Test qw/is_color3 is_color4 test_image test_image_mono is_image/;
 
 -d "testout" or mkdir "testout";
 
@@ -294,6 +294,25 @@ SKIP:
   }
 }
 
+{ # https://rt.cpan.org/Ticket/Display.html?id=106836
+  my $im = test_image;
+  my $data;
+  ok($im->write(data => \$data, type => "raw", raw_interleave => 0), "save some raw image")
+    or diag $im->errstr;
+  my $im2 = Imager->new
+    (
+     data => \$data,
+     filetype => "raw",
+     xsize => $im->getwidth,
+     ysize => $im->getheight,
+     raw_datachannels => $im->getchannels,
+     raw_storechannels => $im->getchannels,
+     raw_interleave => 0,
+    );
+  ok($im2, "read raw image using new() method");
+  is_image($im, $im2, "check they match");
+}
+
 Imager->close_log;
 
 unless ($ENV{IMAGER_KEEP_FILES}) {