Codebase list libpdf-api2-perl / c1decc4
Allow GIF, PNG, and PNM files to be added using filehandles Steve Simms 7 years ago
10 changed file(s) with 130 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
1818 methods in ExtGState (reported by Vadim Repin and Phil Perry).
1919
2020 - 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).
2124
2225
2326 2.028 2016-06-08
121121 $self->{' apipdf'}=$pdf;
122122
123123 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';
126131 my $buf;
132 $fh->seek(0,0);
127133 $fh->read($buf,6); # signature
128134 die "unknown image signature '$buf' -- not a gif." unless($buf=~/^GIF[0-9][0-9][a-b]/);
129135
2525 $self->{' apipdf'}=$pdf;
2626
2727 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';
3035
3136 my ($buf,$l,$crc,$w,$h,$bpc,$cs,$cm,$fm,$im,$palete,$trns);
32 open($fh, "<", $file);
33 binmode($fh);
3437 seek($fh,8,0);
3538 $self->{' stream'}='';
3639 $self->{' nofilt'}=1;
121121
122122 my ($buf,$t,$s,$line);
123123 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 }
125131 binmode($inf,':raw');
132 $inf->seek(0,0);
126133 my $info=readppmheader($inf);
127134 if($info->{type} == 4) {
128135 $bpc=1;
22 use strict;
33 no warnings qw[ deprecated recursion uninitialized ];
44
5 # VERSION
5 our $VERSION = '0.001'; # VERSION
66
77 use Carp;
88 use Encode qw(:all);
18921892
18931893 =item $jpeg = $pdf->image_jpeg($file)
18941894
1895 Imports and returns a new JPEG image object.
1895 Imports and returns a new JPEG image object. C<$file> may be either a filename or a filehandle.
18961896
18971897 =cut
18981898
19081908
19091909 =item $tiff = $pdf->image_tiff($file)
19101910
1911 Imports and returns a new TIFF image object.
1911 Imports and returns a new TIFF image object. C<$file> may be either a filename or a filehandle.
19121912
19131913 =cut
19141914
19241924
19251925 =item $pnm = $pdf->image_pnm($file)
19261926
1927 Imports and returns a new PNM image object.
1927 Imports and returns a new PNM image object. C<$file> may be either a filename or a filehandle.
19281928
19291929 =cut
19301930
19401940
19411941 =item $png = $pdf->image_png($file)
19421942
1943 Imports and returns a new PNG image object.
1943 Imports and returns a new PNG image object. C<$file> may be either a filename or a filehandle.
19441944
19451945 =cut
19461946
19561956
19571957 =item $gif = $pdf->image_gif($file)
19581958
1959 Imports and returns a new GIF image object.
1959 Imports and returns a new GIF image object. C<$file> may be either a filename or a filehandle.
19601960
19611961 =cut
19621962
0 use Test::More tests => 2;
0 use Test::More tests => 5;
11
22 use warnings;
33 use strict;
44
55 use PDF::API2;
66
7 # Filename
8
79 my $pdf = PDF::API2->new();
810 $pdf->{forcecompress} = 0;
911
1012 my $gif = $pdf->image_gif('t/resources/1x1.gif');
1113 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});
1318
1419 my $gfx = $pdf->page->gfx();
1520 $gfx->image($gif, 72, 144, 216, 288);
1621 like($pdf->stringify(), qr/q 216 0 0 288 72 144 cm \S+ Do Q/,
1722 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;
11
22 use warnings;
33 use strict;
44
55 use PDF::API2;
66
7 # Filename
8
79 my $pdf = PDF::API2->new();
810 $pdf->{forcecompress} = 0;
911
1012 my $png = $pdf->image_png('t/resources/1x1.png');
1113 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});
1318
1419 my $gfx = $pdf->page->gfx();
1520 $gfx->image($png, 72, 144, 216, 288);
1621 like($pdf->stringify(), qr/q 216 0 0 288 72 144 cm \S+ Do Q/,
1722 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;
Binary diff not shown
0 use Test::More tests => 4;
0 use Test::More tests => 7;
11
22 use warnings;
33 use strict;
44
55 use PDF::API2;
66
7 # Filename
8
79 my $pdf = PDF::API2->new();
810 $pdf->{forcecompress} = 0;
911
1012 my $tiff = $pdf->image_tiff('t/resources/1x1.tif');
1113 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});
1318
1419 my $gfx = $pdf->page->gfx();
1520 $gfx->image($tiff, 72, 144, 216, 288);
1621 like($pdf->stringify(), qr/q 216 0 0 288 72 144 cm \S+ Do Q/,
1722 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
1838
1939 $pdf = PDF::API2->new();
2040 $pdf->{forcecompress} = 0;