Allow GIF, PNG, and PNM files to be added using filehandles
Steve Simms
7 years ago
18 | 18 |
methods in ExtGState (reported by Vadim Repin and Phil Perry).
|
19 | 19 |
|
20 | 20 |
- Satisfy all Perl::Critic severity 5 policies.
|
|
21 |
|
|
22 |
- [RT #117940] Allow PNG, GIF, and PNM files to be opened from
|
|
23 |
filehandles in addition to filenames (patch by Johan Vromans).
|
21 | 24 |
|
22 | 25 |
|
23 | 26 |
2.028 2016-06-08
|
121 | 121 |
$self->{' apipdf'}=$pdf;
|
122 | 122 |
|
123 | 123 |
my $fh = IO::File->new;
|
124 | |
open($fh, "<", $file);
|
125 | |
binmode($fh,':raw');
|
|
124 |
if (ref($file)) {
|
|
125 |
$fh = $file;
|
|
126 |
}
|
|
127 |
else {
|
|
128 |
open $fh, "<", $file;
|
|
129 |
}
|
|
130 |
binmode $fh, ':raw';
|
126 | 131 |
my $buf;
|
|
132 |
$fh->seek(0,0);
|
127 | 133 |
$fh->read($buf,6); # signature
|
128 | 134 |
die "unknown image signature '$buf' -- not a gif." unless($buf=~/^GIF[0-9][0-9][a-b]/);
|
129 | 135 |
|
25 | 25 |
$self->{' apipdf'}=$pdf;
|
26 | 26 |
|
27 | 27 |
my $fh = IO::File->new;
|
28 | |
open($fh, "<", $file);
|
29 | |
binmode($fh,':raw');
|
|
28 |
if (ref($file)) {
|
|
29 |
$fh = $file;
|
|
30 |
}
|
|
31 |
else {
|
|
32 |
open $fh, "<", $file;
|
|
33 |
}
|
|
34 |
binmode $fh, ':raw';
|
30 | 35 |
|
31 | 36 |
my ($buf,$l,$crc,$w,$h,$bpc,$cs,$cm,$fm,$im,$palete,$trns);
|
32 | |
open($fh, "<", $file);
|
33 | |
binmode($fh);
|
34 | 37 |
seek($fh,8,0);
|
35 | 38 |
$self->{' stream'}='';
|
36 | 39 |
$self->{' nofilt'}=1;
|
121 | 121 |
|
122 | 122 |
my ($buf,$t,$s,$line);
|
123 | 123 |
my ($w,$h,$bpc,$cs,$img,@img)=(0,0,'','','');
|
124 | |
open(my $inf, "<", $file);
|
|
124 |
my $inf;
|
|
125 |
if (ref($file)) {
|
|
126 |
$inf = $file;
|
|
127 |
}
|
|
128 |
else {
|
|
129 |
open $inf, "<", $file;
|
|
130 |
}
|
125 | 131 |
binmode($inf,':raw');
|
|
132 |
$inf->seek(0,0);
|
126 | 133 |
my $info=readppmheader($inf);
|
127 | 134 |
if($info->{type} == 4) {
|
128 | 135 |
$bpc=1;
|
0 | |
use Test::More tests => 2;
|
|
0 |
use Test::More tests => 5;
|
1 | 1 |
|
2 | 2 |
use warnings;
|
3 | 3 |
use strict;
|
4 | 4 |
|
5 | 5 |
use PDF::API2;
|
6 | 6 |
|
|
7 |
# Filename
|
|
8 |
|
7 | 9 |
my $pdf = PDF::API2->new();
|
8 | 10 |
$pdf->{forcecompress} = 0;
|
9 | 11 |
|
10 | 12 |
my $gif = $pdf->image_gif('t/resources/1x1.gif');
|
11 | 13 |
isa_ok($gif, 'PDF::API2::Resource::XObject::Image::GIF',
|
12 | |
q{$pdf->image_gif()});
|
|
14 |
q{$pdf->image_gif(filename)});
|
|
15 |
|
|
16 |
is($gif->width(), 1,
|
|
17 |
q{Image from filename has a width});
|
13 | 18 |
|
14 | 19 |
my $gfx = $pdf->page->gfx();
|
15 | 20 |
$gfx->image($gif, 72, 144, 216, 288);
|
16 | 21 |
like($pdf->stringify(), qr/q 216 0 0 288 72 144 cm \S+ Do Q/,
|
17 | 22 |
q{Add GIF to PDF});
|
|
23 |
|
|
24 |
# Filehandle
|
|
25 |
|
|
26 |
$pdf = PDF::API2->new();
|
|
27 |
open my $fh, '<', 't/resources/1x1.gif';
|
|
28 |
$gif = $pdf->image_gif($fh);
|
|
29 |
isa_ok($gif, 'PDF::API2::Resource::XObject::Image::GIF',
|
|
30 |
q{$pdf->image_gif(filehandle)});
|
|
31 |
|
|
32 |
is($gif->width(), 1,
|
|
33 |
q{Image from filehandle has a width});
|
|
34 |
|
|
35 |
close $fh;
|
0 | |
use Test::More tests => 2;
|
|
0 |
use Test::More tests => 5;
|
1 | 1 |
|
2 | 2 |
use warnings;
|
3 | 3 |
use strict;
|
4 | 4 |
|
5 | 5 |
use PDF::API2;
|
6 | 6 |
|
|
7 |
# Filename
|
|
8 |
|
7 | 9 |
my $pdf = PDF::API2->new();
|
8 | 10 |
$pdf->{forcecompress} = 0;
|
9 | 11 |
|
10 | 12 |
my $png = $pdf->image_png('t/resources/1x1.png');
|
11 | 13 |
isa_ok($png, 'PDF::API2::Resource::XObject::Image::PNG',
|
12 | |
q{$pdf->image_png()});
|
|
14 |
q{$pdf->image_png(filename)});
|
|
15 |
|
|
16 |
is($png->width(), 1,
|
|
17 |
q{Image from filename has a width});
|
13 | 18 |
|
14 | 19 |
my $gfx = $pdf->page->gfx();
|
15 | 20 |
$gfx->image($png, 72, 144, 216, 288);
|
16 | 21 |
like($pdf->stringify(), qr/q 216 0 0 288 72 144 cm \S+ Do Q/,
|
17 | 22 |
q{Add PNG to PDF});
|
|
23 |
|
|
24 |
# Filehandle
|
|
25 |
|
|
26 |
$pdf = PDF::API2->new();
|
|
27 |
open my $fh, '<', 't/resources/1x1.png';
|
|
28 |
$png = $pdf->image_png($fh);
|
|
29 |
isa_ok($png, 'PDF::API2::Resource::XObject::Image::PNG',
|
|
30 |
q{$pdf->image_png(filehandle)});
|
|
31 |
|
|
32 |
is($png->width(), 1,
|
|
33 |
q{Image from filehandle has a width});
|
|
34 |
|
|
35 |
close $fh;
|
|
0 |
use Test::More tests => 5;
|
|
1 |
|
|
2 |
use warnings;
|
|
3 |
use strict;
|
|
4 |
|
|
5 |
use PDF::API2;
|
|
6 |
|
|
7 |
# Filename
|
|
8 |
|
|
9 |
my $pdf = PDF::API2->new();
|
|
10 |
$pdf->{forcecompress} = 0;
|
|
11 |
|
|
12 |
my $pnm = $pdf->image_pnm('t/resources/1x1.pbm');
|
|
13 |
isa_ok($pnm, 'PDF::API2::Resource::XObject::Image::PNM',
|
|
14 |
q{$pdf->image_pnm(filename)});
|
|
15 |
|
|
16 |
is($pnm->width(), 1,
|
|
17 |
q{Image from filename has a width});
|
|
18 |
|
|
19 |
my $gfx = $pdf->page->gfx();
|
|
20 |
$gfx->image($pnm, 72, 144, 216, 288);
|
|
21 |
like($pdf->stringify(), qr/q 216 0 0 288 72 144 cm \S+ Do Q/,
|
|
22 |
q{Add PNM to PDF});
|
|
23 |
|
|
24 |
# Filehandle
|
|
25 |
|
|
26 |
$pdf = PDF::API2->new();
|
|
27 |
open my $fh, '<', 't/resources/1x1.pbm';
|
|
28 |
$pnm = $pdf->image_pnm($fh);
|
|
29 |
isa_ok($pnm, 'PDF::API2::Resource::XObject::Image::PNM',
|
|
30 |
q{$pdf->image_pnm(filehandle)});
|
|
31 |
|
|
32 |
is($pnm->width(), 1,
|
|
33 |
q{Image from filehandle has a width});
|
|
34 |
|
|
35 |
close $fh;
|
0 | |
use Test::More tests => 4;
|
|
0 |
use Test::More tests => 7;
|
1 | 1 |
|
2 | 2 |
use warnings;
|
3 | 3 |
use strict;
|
4 | 4 |
|
5 | 5 |
use PDF::API2;
|
6 | 6 |
|
|
7 |
# Filename
|
|
8 |
|
7 | 9 |
my $pdf = PDF::API2->new();
|
8 | 10 |
$pdf->{forcecompress} = 0;
|
9 | 11 |
|
10 | 12 |
my $tiff = $pdf->image_tiff('t/resources/1x1.tif');
|
11 | 13 |
isa_ok($tiff, 'PDF::API2::Resource::XObject::Image::TIFF',
|
12 | |
q{$pdf->image_tiff()});
|
|
14 |
q{$pdf->image_tiff(filename)});
|
|
15 |
|
|
16 |
is($tiff->width(), 1,
|
|
17 |
q{Image from filename has a width});
|
13 | 18 |
|
14 | 19 |
my $gfx = $pdf->page->gfx();
|
15 | 20 |
$gfx->image($tiff, 72, 144, 216, 288);
|
16 | 21 |
like($pdf->stringify(), qr/q 216 0 0 288 72 144 cm \S+ Do Q/,
|
17 | 22 |
q{Add TIFF to PDF});
|
|
23 |
|
|
24 |
# Filehandle
|
|
25 |
|
|
26 |
$pdf = PDF::API2->new();
|
|
27 |
open my $fh, '<', 't/resources/1x1.tif';
|
|
28 |
$tiff = $pdf->image_tiff($fh);
|
|
29 |
isa_ok($tiff, 'PDF::API2::Resource::XObject::Image::TIFF',
|
|
30 |
q{$pdf->image_tiff(filehandle)});
|
|
31 |
|
|
32 |
is($tiff->width(), 1,
|
|
33 |
q{Image from filehandle has a width});
|
|
34 |
|
|
35 |
close $fh;
|
|
36 |
|
|
37 |
# LZW Compression
|
18 | 38 |
|
19 | 39 |
$pdf = PDF::API2->new();
|
20 | 40 |
$pdf->{forcecompress} = 0;
|