Post without Account — your post will be reviewed, and if appropriate, posted under Anonymous. You can also use this link to report any problems registering or logging in.

RT 117940 - Allow PNG to be read from an already opened filehandle, just like JP

  • 0 Replies
  • 1287 Views
*

Offline Phil

  • Global Moderator
  • Sr. Member
  • *****
  • 437
    • View Profile
Sun Sep 18 11:00:08 2016 jv [...] cpan.org - Ticket created
Subject:    Allow PNG to be read from an already opened filehandle, just like JPG

The following patch against 2.028 allows PNG files to be read from an already openened filehandle.

It also eliminates superfluous open/binmode calls, and protects against dangerous file names by using the 3-argument form of open.
Code: [Select]
*** PDF/API2/Resource/XObject/Image/PNG.pm~ 2016-06-09 00:24:09.000000000 +0200
--- PDF/API2/Resource/XObject/Image/PNG.pm 2016-09-18 16:38:37.563107168 +0200
***************
*** 25,36 ****
      $self->{' apipdf'}=$pdf;
 
      my $fh = IO::File->new;
!     open($fh,$file);
!     binmode($fh,':raw');
 
      my ($buf,$l,$crc,$w,$h,$bpc,$cs,$cm,$fm,$im,$palete,$trns);
-     open($fh,$file);
-     binmode($fh);
      seek($fh,8,0);
      $self->{' stream'}='';
      $self->{' nofilt'}=1;
--- 25,38 ----
      $self->{' apipdf'}=$pdf;
 
      my $fh = IO::File->new;
!     if ( ref($file) ) {
! $fh = $file;
!     }
!     else {
! open( $fh, '<:raw', $file);
!     }
 
      my ($buf,$l,$crc,$w,$h,$bpc,$cs,$cm,$fm,$im,$palete,$trns);
      seek($fh,8,0);
      $self->{' stream'}='';
      $self->{' nofilt'}=1;
#
Fri Oct 07 12:35:38 2016 steve [...] deefs.net - Correspondence added

I just tried creating a test for this, and it seems that the open() function already works when given a filehandle instead of a filename.  Were you seeing different behavior?
#
Fri Oct 07 12:35:38 2016 The RT System itself - Status changed from 'new' to 'open'
#
Fri Oct 07 13:43:39 2016 jvromans [...] squirrel.nl - Correspondence added
Subject:    Re: [rt.cpan.org #117940] Allow PNG to be read from an already opened filehandle, just like JPG
Date:    Fri, 7 Oct 2016 19:43:20 +0200
To:    "Steve Simms via RT" <bug-PDF-API2 [...] rt.cpan.org>
From:    Johan Vromans <jvromans [...] squirrel.nl>

I tried:

Code: [Select]
  my $pdf = PDF::API2->new;
  open( my $fd, '<:raw', "icon.png");
  my $p = $pdf->image_png($fd);
  print $PDF::API2::VERSION, ", width = ", $p->width, "\n";

This gives:

  Use of uninitialized value in print at x.pl line 11.
  2.028, width =

OTOH:
Code: [Select]
  my $p = $pdf->image_png("icon.png");
  print $PDF::API2::VERSION, ", width = ", $p->width, "\n";

This gives:

  2.028, width = 500

So the former does something, but it definitely doesn't create the desired
image.

Does this help?

Thanks,
        Johan
#
Sun Oct 09 17:05:45 2016 steve [...] deefs.net - Correspondence added

Yes, that helped me create relevant tests, thanks.

I extended the patch to cover GIF and PNM images as well, and updated the POD.  This will be in the next release (2.029).

#
Sun Oct 09 17:05:46 2016 steve [...] deefs.net - Status changed from 'open' to 'patched'
#
Mon Oct 10 09:21:50 2016 steve [...] deefs.net - Status changed from 'patched' to 'resolved'
#
Mon Oct 10 09:21:50 2016 steve [...] deefs.net - Fixed in 2.029 added