API2/Resource/XObject/Image/PNM.pm

filter: Statementstmt 48.5% 85 / 175 Branchbran 30.0% 27 / 90 Conditioncond 27.7% 5 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 100% 10 / 10 Podpod 50.0% 1 / 2 totaltotal 41.6% 128 / 307 SCAR 64.7
CC
54
Cov
41%
read_pnm
660.1
_read_header
36.0
new
4.0
CRAP 642.5
« API2/ViewerPreferences.pm ↑ Summary API2/Resource/BaseFont.pm »
1 package PDF::API2::Resource::XObject::Image::PNM;
2  
3 # For spec details, see man pages pam(5), pbm(5), pgm(5), pnm(5),
4 # ppm(5), which were pasted into the __END__ of this file in an
5 # earlier revision.
6  
7 2 ▶use base 'PDF::API2::Resource::XObject::Image';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '2.044'; # VERSION
12  
13 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use PDF::API2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use PDF::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 sub new {
20 3 ▶ my ($class, $pdf, $file, %opts) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
21 3 my $self;
22  
23 3 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 3 3
24  
25 3 $self = $class->SUPER::new($pdf, 'Nx' . pdfkey());
26 3 ▶ $pdf->new_obj($self) unless $self->is_obj($pdf);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_obj($pdf) 0 3 3
27  
28 3 $self->read_pnm($pdf, $file);
29  
30 2 ▶ if ($opts{'-compress'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'-compress'}) { } 1 1 2
31 1 $self->filters('FlateDecode');
32 }
33 else {
34 1 $self->filters('ASCIIHexDecode');
35 }
36  
37 2 return $self;
38 }
39  
40 # Originally from Image::PBMLib by Elijah Griffin (28 Feb 2003)
41 sub _read_header {
42 2 ▶ my $fh = shift();
Subroutine _read_header: called CC=15 SCAR=35.8
43 2 my $in;
44 my $no_comments;
45 2 my %info;
46 2 my $rc;
47 2 $info{'error'} = undef;
48  
49 2 $rc = read($fh, $in, 3);
50  
51 2 ▶ if (!defined($rc) or $rc != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rc or $rc != 3) 0 2 2
Condition33%1/3
not defined $rc or $rc != 3
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $rc or $rc != 3
not defined $rc $rc != 3
Truth table33%1/3
not(A) or B
A= not defined $rcB= $rc != 3
ABresult
1X1
011
000
52 0 $info{'error'} = 'Read error or EOF';
53 0 return \%info;
54 }
55  
56 2 ▶ unless ($in =~ /^P([123456])\s/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in =~ /^P([123456])\s/) 0 2 2
57 0 $info{'error'} = 'Wrong magic number';
58 0 return \%info;
59 }
60  
61 2 $info{'type'} = $1;
62 2 ▶ if ($info{'type'} > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info{'type'} > 3) { } 2 0 2
63 2 $info{'raw'} = 1;
64 }
65 else {
66 0 $info{'raw'} = 0;
67 }
68  
69 2 ▶ if ($info{'type'} == 1 or $info{'type'} == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info{'type'} == 1 or $info{'type'} == 4) { } 0 2 2
elsif ($info{'type'} == 2 or $info{'type'} == 5) { } 0 2 2
Condition33%1/3
$info{"type"} == 1 or $info{"type"} == 4
l!l&&r!l&&!r
002
MC/DC0%0/2
$info{"type"} == 1 or $info{"type"} == 4
$info{"type"} == 1 $info{"type"} == 4
Truth table33%1/3
A or B
A= $info{"type"} == 1B= $info{"type"} == 4
ABresult
1X1
011
000
70 0 $info{'max'} = 1;
71 0 $info{'bgp'} = 'b';
72 }
73 ▶ elsif ($info{'type'} == 2 or $info{'type'} == 5) {
Condition33%1/3
$info{"type"} == 2 or $info{"type"} == 5
l!l&&r!l&&!r
002
MC/DC0%0/2
$info{"type"} == 2 or $info{"type"} == 5
$info{"type"} == 2 $info{"type"} == 5
Truth table33%1/3
A or B
A= $info{"type"} == 2B= $info{"type"} == 5
ABresult
1X1
011
000
74 0 $info{'bgp'} = 'g';
75 }
76 else {
77 2 $info{'bgp'} = 'p';
78 }
79  
80 2 while (1) {
81 16 $rc = read($fh, $in, 1, length($in));
82 16 ▶ if (!defined($rc) or $rc != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rc or $rc != 1) 0 16 16
Condition33%1/3
not defined $rc or $rc != 1
l!l&&r!l&&!r
0016
MC/DC0%0/2
not defined $rc or $rc != 1
not defined $rc $rc != 1
Truth table33%1/3
not(A) or B
A= not defined $rcB= $rc != 1
ABresult
1X1
011
000
83 0 $info{'error'} = 'Read error or EOF';
84 0 return \%info;
85 }
86  
87 16 $no_comments = $in;
88 16 $info{'comments'} = '';
89 16 ▶ while ($no_comments =~ /#.*\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_comments =~ /#.*\n/) 0 16 16
90 0 $no_comments =~ s/#(.*\n)/ /;
91 0 $info{'comments'} .= $1;
92 }
93  
94 16 ▶ if ($info{'bgp'} eq 'b') {
Branch: 1 branch
BranchTrueFalseTotal
if ($info{'bgp'} eq 'b') { } 0 16 16
95 0 ▶ if ($no_comments =~ /^P\d\s+(\d+)\s+(\d+)\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_comments =~ /^P\d\s+(\d+)\s+(\d+)\s/) 0 0 0
96 0 $info{'width'} = $1;
97 0 $info{'height'} = $2;
98 0 last;
99 }
100 }
101 else {
102 16 ▶ if ($no_comments =~ /^P\d\s+(\d+)\s+(\d+)\s+(\d+)\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_comments =~ /^P\d\s+(\d+)\s+(\d+)\s+(\d+)\s/) 2 14 16
103 2 $info{'width'} = $1;
104 2 $info{'height'} = $2;
105 2 $info{'max'} = $3;
106 2 last;
107 }
108 }
109 } # while reading header
110  
111 2 $info{'fullheader'} = $in;
112  
113 2 return \%info;
114 }
115  
116 sub read_pnm {
117 3 ▶ my ($self, $pdf, $file) = @_;
Subroutine read_pnm: called CC=37 SCAR=64.9
Pod undocumented
118  
119 3 my ($buf, $t, $scale, $line);
120 3 my $bpc;
121 3 my $cs;
122  
123 3 my $fh;
124 3 ▶ if (ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 1 2 3
125 1 $fh = $file;
126 }
127 else {
128 2 ▶ open $fh, '<', $file or die "$!: $file";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or die "$!: $file" 1 1 2
129 }
130 2 binmode($fh, ':raw');
131 2 $fh->seek(0, 0);
132  
133 2 my $info = _read_header($fh);
134 2 ▶ if ($info->{'type'} == 1) { # ASCII PBM
Branch: 6 branches
BranchTrueFalseTotal
if ($info->{'type'} == 1) { } 0 2 2
elsif ($info->{'type'} == 2) { } 0 2 2
elsif ($info->{'type'} == 3) { } 0 2 2
elsif ($info->{'type'} == 4) { } 0 2 2
elsif ($info->{'type'} == 5) { } 0 2 2
elsif ($info->{'type'} == 6) { } 2 0 2
135 0 $bpc = 1;
136 0 $cs = 'DeviceGray';
137 0 $self->{'Decode'} = PDFArray(PDFNum(1), PDFNum(0));
138  
139 # Read the remainder of the file
140 0 local $/ = undef;
141 0 my $plain = <$fh>;
142  
143 # Discard everything other than ASCII 1 and 0
144 0 $plain =~ s/[^01]+//g;
145  
146 # Check length
147 0 my $size = $info->{'width'} * $info->{'height'};
148 0 ▶ croak "Incomplete ASCII PBM" if length($plain) < $size;
Branch: 1 branch
BranchTrueFalseTotal
if length $plain < $size 0 0 0
149  
150 # Discard any additional bits
151 0 ▶ $plain = substr($plain, 0, $size) if length($plain) > $size;
Branch: 1 branch
BranchTrueFalseTotal
if length $plain > $size 0 0 0
152  
153 # Pad with zeroes
154 0 $plain .= '0' x (8 - (length($plain) % 8));
155  
156 # Convert to binary
157 0 $self->{' stream'} = pack('B*', $plain);
158 }
159 elsif ($info->{'type'} == 2) { # ASCII PGM
160 0 $cs = 'DeviceGray';
161  
162 # Read the remainder of the file
163 0 local $/ = undef;
164 0 my $plain = <$fh>;
165  
166 # Discard everything other than digits and whitespace
167 0 $plain =~ s/[^\d\s]+//gs;
168 0 $plain =~ s/^\s+//;
169  
170 # Convert to an array of integers
171 0 my @raster = split m/\s+/, $plain;
172  
173 # Check length
174 0 my $size = $info->{'width'} * $info->{'height'};
175 0 ▶ croak "Incomplete ASCII PGM" if scalar(@raster) < $size;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @raster < $size 0 0 0
176  
177 # Discard any additional integers
178 0 ▶ splice @raster, $size if scalar(@raster) > $size;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @raster > $size 0 0 0
179  
180 # Scale
181 0 $scale = 1;
182 0 ▶ if ($info->{'max'} <= 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'max'} <= 255) { } 0 0 0
183 0 $bpc = 8;
184 0 ▶ $scale = 255 / $info->{'max'} unless $info->{'max'} == 255;
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"max"} == 255 0 0 0
185 }
186 else {
187 0 $bpc = 16;
188 0 ▶ $scale = 65535 / $info->{'max'} unless $info->{'max'} == 65535;
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"max"} == 65535 0 0 0
189 }
190 0 @raster = map { $_ * $scale } @raster;
191  
192 # Convert to bytes
193 0 ▶ if ($bpc == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpc == 8) { } 0 0 0
194 0 $self->{' stream'} = pack('C*', @raster);
195 }
196 else {
197 0 $self->{' stream'} = pack('S*', @raster);
198 }
199 }
200 elsif ($info->{'type'} == 3) { # ASCII PPM
201 0 $cs = 'DeviceRGB';
202  
203 # Read the remainder of the file
204 0 local $/ = undef;
205 0 my $plain = <$fh>;
206  
207 # Discard everything other than digits and whitespace
208 0 $plain =~ s/[^\d\s]+//gs;
209 0 $plain =~ s/^\s+//;
210  
211 # Convert to an array of integers
212 0 my @raster = split m/\s+/, $plain;
213  
214 # Check length
215 0 my $size = $info->{'width'} * $info->{'height'};
216 0 ▶ croak "Incomplete ASCII PGM" if scalar(@raster) < $size * 3;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @raster < $size * 3 0 0 0
217  
218 # Discard any additional integers
219 0 ▶ splice @raster, $size if scalar(@raster) > $size * 3;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @raster > $size * 3 0 0 0
220  
221 # Scale
222 0 $scale = 1;
223 0 ▶ if ($info->{'max'} <= 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'max'} <= 255) { } 0 0 0
224 0 $bpc = 8;
225 0 ▶ $scale = 255 / $info->{'max'} unless $info->{'max'} == 255;
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"max"} == 255 0 0 0
226 }
227 else {
228 0 $bpc = 16;
229 0 ▶ $scale = 65535 / $info->{'max'} unless $info->{'max'} == 65535;
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"max"} == 65535 0 0 0
230 }
231 0 @raster = map { $_ * $scale } @raster;
232  
233 # Convert to bytes
234 0 ▶ if ($bpc == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpc == 8) { } 0 0 0
235 0 $self->{' stream'} = pack('C*', @raster);
236 }
237 else {
238 0 $self->{' stream'} = pack('S*', @raster);
239 }
240 }
241 elsif ($info->{'type'} == 4) { # Raw PBM
242 0 $cs = 'DeviceGray';
243 0 $bpc = 1;
244 0 $self->{'Decode'} = PDFArray(PDFNum(1), PDFNum(0));
245 0 read($fh, $self->{' stream'}, ($info->{'width'} * $info->{'height'} / 8));
246 }
247 elsif ($info->{'type'} == 5) { # Raw PGM
248 0 $cs = 'DeviceGray';
249 0 ▶ $bpc = $info->{'max'} <= 255 ? 8 : 16;
Branch: 1 branch
BranchTrueFalseTotal
$info->{'max'} <= 255 ? : 0 0 0
250  
251 0 ▶ if ($info->{'max'} == 255 or $info->{'max'} == 65535) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'max'} == 255 or $info->{'max'} == 65535) { } 0 0 0
Condition0%0/3
$info->{"max"} == 255 or $info->{"max"} == 65535
l!l&&r!l&&!r
000
MC/DC0%0/2
$info->{"max"} == 255 or $info->{"max"} == 65535
$info->{"max"} == 255 $info->{"max"} == 65535
Truth table0%0/3
A or B
A= $info->{"max"} == 255B= $info->{"max"} == 65535
ABresult
1X1
011
000
252 0 $scale = 1;
253 }
254 else {
255 0 ▶ $scale = ($bpc == 8 ? 255 : 65535) / $info->{'max'};
Branch: 1 branch
BranchTrueFalseTotal
$bpc == 8 ? : 0 0 0
256 }
257  
258 0 my $size = $info->{'width'} * $info->{'height'};
259 0 ▶ if ($scale == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale == 1) { } 0 0 0
260 0 read($fh, $self->{' stream'}, $size * ($bpc / 8));
261 }
262 else {
263 0 for (1 .. $size) {
264 0 read($fh, $buf, $bpc / 8);
265 0 ▶ if ($bpc == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpc == 8) { } 0 0 0
266 0 $self->{' stream'} .= pack('C', (unpack('C', $buf) * $scale));
267 }
268 else {
269 0 $self->{' stream'} .= pack('S', (unpack('S', $buf) * $scale));
270 }
271 }
272 }
273 }
274 elsif ($info->{'type'} == 6) { # Raw PPM
275 2 $cs = 'DeviceRGB';
276 2 ▶ $bpc = $info->{'max'} <= 255 ? 8 : 16;
Branch: 1 branch
BranchTrueFalseTotal
$info->{'max'} <= 255 ? : 2 0 2
277  
278 2 ▶ if ($info->{'max'} == 255 or $info->{'max'} == 65535) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'max'} == 255 or $info->{'max'} == 65535) { } 2 0 2
Condition33%1/3
$info->{"max"} == 255 or $info->{"max"} == 65535
l!l&&r!l&&!r
200
MC/DC0%0/2
$info->{"max"} == 255 or $info->{"max"} == 65535
$info->{"max"} == 255 $info->{"max"} == 65535
Truth table33%1/3
A or B
A= $info->{"max"} == 255B= $info->{"max"} == 65535
ABresult
1X1
011
000
279 2 $scale = 1;
280 }
281 else {
282 0 ▶ $scale = ($bpc == 8 ? 255 : 65535) / $info->{'max'};
Branch: 1 branch
BranchTrueFalseTotal
$bpc == 8 ? : 0 0 0
283 }
284  
285 2 my $size = $info->{'width'} * $info->{'height'};
286 2 ▶ if ($scale == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale == 1) { } 2 0 2
287 2 read($fh, $self->{' stream'}, $size * ($bpc / 8) * 3);
288 }
289 else {
290 0 for (1 .. ($size * 3)) {
291 0 read($fh, $buf, $bpc / 8);
292 0 ▶ if ($bpc == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpc == 8) { } 0 0 0
293 0 $self->{' stream'} .= pack('C', (unpack('C', $buf) * $scale));
294 }
295 else {
296 0 $self->{' stream'} .= pack('S', (unpack('S', $buf) * $scale));
297 }
298 }
299 }
300 }
301 2 close $fh;
302  
303 2 $self->width($info->{'width'});
304 2 $self->height($info->{'height'});
305  
306 2 $self->bits_per_component($bpc);
307  
308 2 $self->colorspace($cs);
309  
310 2 return $self;
311 }
312  
313 2 1;
« API2/ViewerPreferences.pm ↑ Summary API2/Resource/BaseFont.pm »