blib/lib/Image/PBMlib.pm

filter: Statementstmt 81.8% 971 / 1186 Branchbran 72.7% 425 / 584 Conditioncond 43.9% 83 / 189 MC/DCmcdc 20.0% 22 / 110 Subroutinesub 88.2% 45 / 51 Podpod 50.0% 23 / 46 totaltotal 72.4% 1569 / 2166 CC 360 SCAR 31.6
CC
360
Cov
75%
readpnmpixels
90.0
makepnmheader
80.7
checkpnminfo
42.2
CRAP 23.5
↑ Summary
1 #! perl -w
2 # A PBM/PGM/PPM library.
3 # Benjamin Elijah Griffin 28 Feb 2012
4 # elijah@cpan.org
5  
6 package Image::PBMlib;
7 14 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 14 ▶use vars qw( @ISA @EXPORT );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 require Exporter;
13 14 @ISA = qw(Exporter);
14  
15 14 @EXPORT = qw( readpnmfile checkpnminfo readpnmheader readpnmpixels
16 makepnmheader encodepixels writepnmfile inspectpixels
17 explodetriple rescaleval rescaletriple
18 hextripletofloat dectripletofloat
19 hexvaltofloat decvaltofloat
20 floattripletodec floattripletohex
21 floatvaltodec floatvaltohex
22 comparefloatval comparefloattriple
23 comparepixelval comparepixeltriple
24 );
25  
26 14 $Image::PBMlib::VERSION = '2.00';
27  
28 =head1 NAME
29  
30 Image::PBMlib - Helper functions for PBM/PGM/PPM image file formats
31  
32 =head1 SYNOPSIS
33  
34 use Image::PBMlib;
35  
36 ... open(PNM, '<:raw', "image.ppm")...
37  
38 my (%info, @pixels);
39 # fourth is encoding of float, dec, or hex
40 readpnmfile( \*PNM, \%info, \@pixels, 'float' );
41 # sets $info{error} if an error
42  
43 readpnmheader( \*PNM, \%info );
44 # sets $info{error} if an error
45  
46 checkpnminfo( \%info );
47 # sets $info{error} if an error
48  
49 # float, dec, or hex
50 readpnmpixels( \*PNM, \%info, \@pixels, 'float')
51 # sets $info{error} if an error
52  
53 # R/G/B to RRRR/GGGG/BBBB, max 1 to 65535
54 my $rgb = hextripletofloat( "F00/B/A4", $maxvalue );
55  
56 # R:G:B, max 1 to 65535
57 my $rgb = dectripletofloat( "3840:11:164", $maxvalue );
58  
59 # returns the number of bytes written, as a positive
60 # number if no error, and zero or -1*bytes if error
61 my $return = writepnmfile(\*PNM, \%info, \@pixels);
62  
63 # this header can contain comments
64 my $header = makepnmheader(\%info);
65  
66 # this header will not contain comments
67 # 1 for ascii PBM, 2 for ascii PGM, 3 for ascii PPM,
68 # 4 for raw PBM, 5 for raw PGM, 6 for raw PPM
69 my $header = makepnmheader('5', $width, $height, $maxvalue);
70  
71 # raw, dec, or hex format pixels, in 'raw' or 'ascii'
72 # for writing to a file
73 my $block = encodepixels('raw', $maxvalue, \@pixels);
74  
75 =head1 DESCRIPTION
76  
77 This is primarily a library for reading and writing portable bitmap (PBM),
78 portable graymap (PGM), and portable pixmap (PPM) files. As a
79 set they are portable anymap (PNM). There is a separate PAM
80 format that is not yet supported. Within each format there are
81 two representations on disk, ASCII and RAW. ASCII is suitable
82 for raw email transmission, short lines, all 7-bit characters.
83 RAW is much more compact and generally preferred. A single RAW
84 formatted file can contain multiple concatenated images.
85  
86 These image formats are only the barest step up from raw raster
87 data, and have a very simple format which is the key to be "portable".
88 Writing out images in these formats is very easy. Reading only
89 slightly more complicated.
90  
91 =head2 Maxvalue
92  
93 Version 1.x of this library had serious bugs except for the most
94 basic versions of PGM and PPM files, by not properly observing
95 the maxvalue. Version 2.x fixes that at a compatiblity cost. Raw
96 gray and color channel information is now stored as a floating
97 point number from 0.0 as full black to 1.0 as full white, and
98 it is scaled to the approprate maxvalue, which is a decimal integer
99 from 1 to 65535 inclusive.
100  
101 =head2 Pixels
102  
103 When this version of the library returns a pixel it will be:
104 "0" or "1" for PBM files; "0.0," to "1.0," for PGM in float
105 format, "0:" to "65535:" for PGM in decimal, "0/" to "FFFF/"
106 for PGM in hexadecimal; "0.0,0.0,0.0" to "1.0,1.0,1.0" for
107 PPM in float, "0:0:0" to "65535:65535:65535" for PPM in decimal,
108 and "FFFF/FFFF/FFFF" for PPM in hexadecimal.
109  
110 That is to say PBM files always return just zeros and ones,
111 regardless of float, dec, or hex settings.
112  
113 PGM files return a floating point number, an unrescaled dec or
114 hex value, but always followed by a comma if float, a colon if
115 decimal, and a slash if hex. Unrescaled means that if the
116 maxvalue is 1000 (decimal integer), then white is "1.0," in
117 float, "1000:" in dec, and "3E8/" in hex.
118  
119 PPM files return a RGB set of floating point numbers, an
120 unrescaled set of dec or hex values, which are always separated
121 by commas if float, colons if decimal, and slashes if hex. Be sure
122 to read what unscaled means in the previous paragraph.
123  
124 Image::PBMlib likes pixels in a two dimensional array, but can
125 use a single dimensional array.
126  
127 =cut
128  
129 14 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
130 } # end BEGIN
131  
132  
133 # Internal read header function. Does not do argument checks.
134 13 ▶sub int_readpnmheader {
Subroutine int_readpnmheader: called CC=18 SCAR=30.5
Pod undocumented
135 13 my $gr = shift; # input file glob ref
136 13 my $ir = shift; # image info hash ref
137 13 my $in = '';
138 13 my $pre = '';
139 13 my $no_comments;
140 my $rc;
141  
142 13 $rc = read($gr, $in, 3);
143  
144 13 ▶ if (!defined($rc) or $rc != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rc or $rc != 3) 0 13 13
Condition33%1/3
not defined $rc or $rc != 3
l!l&&r!l&&!r
0013
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
145 0 $$ir{error} = 'Read error or EOF on magic number';
146 0 $$ir{fullheader} = $in;
147 0 return;
148 }
149  
150 13 ▶ if ($in =~ /\nP[123456]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /\nP[123456]/) 2 11 13
151 # hmmm. bad concatenated file?
152 2 my $peek;
153 2 $rc = read($gr, $peek, 1);
154 2 ▶ if($rc and $peek eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc and $peek eq "\n") 2 0 2
Condition33%1/3
$rc and $peek eq "\n"
!ll&&!rl&&r
002
MC/DC0%0/2
$rc and $peek eq "\n"
$rc $peek eq "\n"
Truth table33%1/3
A and B
A= $rcB= $peek eq "\n"
ABresult
0X0
100
111
155 2 $in =~ s/^\n//;
156 2 $in .= "\n";
157 }
158 }
159  
160 13 ▶ if ($in =~ /^P([123456])\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /^P([123456])\s/) { } 13 0 13
161 13 $$ir{type} = $1;
162 13 ▶ if ($$ir{type} > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ir->{'type'} > 3) { } 8 5 13
163 8 $$ir{raw} = 1;
164 8 $$ir{format} = 'raw';
165 } else {
166 5 $$ir{raw} = 0;
167 5 $$ir{format} = 'ascii';
168 }
169  
170 13 ▶ if ($$ir{type} == 1 or $$ir{type} == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ir->{'type'} == 1 or $ir->{'type'} == 4) { } 5 8 13
elsif ($ir->{'type'} == 2 or $ir->{'type'} == 5) { } 4 4 8
Condition100%3/3
$ir->{"type"} == 1 or $ir->{"type"} == 4
l!l&&r!l&&!r
148
MC/DC100%2/2
$ir->{"type"} == 1 or $ir->{"type"} == 4
$ir->{"type"} == 1 $ir->{"type"} == 4
Truth table100%3/3
A or B
A= $ir->{"type"} == 1B= $ir->{"type"} == 4
ABresult
1X1
011
000
171 5 $$ir{max} = 1;
172 5 $$ir{bgp} = 'b';
173 ▶ } elsif ($$ir{type} == 2 or $$ir{type} == 5) {
Condition100%3/3
$ir->{"type"} == 2 or $ir->{"type"} == 5
l!l&&r!l&&!r
224
MC/DC100%2/2
$ir->{"type"} == 2 or $ir->{"type"} == 5
$ir->{"type"} == 2 $ir->{"type"} == 5
Truth table100%3/3
A or B
A= $ir->{"type"} == 2B= $ir->{"type"} == 5
ABresult
1X1
011
000
174 4 $$ir{bgp} = 'g';
175 } else {
176 4 $$ir{bgp} = 'p';
177 }
178  
179 13 while(1) {
180 88 $rc = read($gr, $in, 1, length($in));
181 88 ▶ if (!defined($rc) or $rc != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rc or $rc != 1) 0 88 88
Condition33%1/3
not defined $rc or $rc != 1
l!l&&r!l&&!r
0088
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
182 0 $$ir{error} = 'Read error or EOF during header';
183 0 $$ir{fullheader} = $in;
184 0 return;
185 }
186  
187 # yes, really reset ir{comments} every time through loop
188 88 $no_comments = $in;
189 88 $$ir{comments} = '';
190 88 ▶ while ($no_comments =~ /#.*\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_comments =~ /#.*\n/) 0 88 88
191 0 $no_comments =~ s/#(.*\n)/ /;
192 0 $$ir{comments} .= $1;
193 }
194  
195 88 ▶ if ($$ir{bgp} eq 'b') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ir->{'bgp'} eq 'b') { } 20 68 88
196 20 ▶ if ($no_comments =~ /^P\d\s+(\d+)\s+(\d+)\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_comments =~ /^P\d\s+(\d+)\s+(\d+)\s/) 5 15 20
197 5 $$ir{width} = $1;
198 5 $$ir{height} = $2;
199 5 $$ir{pixels} = $1*$2;
200 5 last;
201 }
202 } else {
203 # graymap and pixmap
204 68 ▶ 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/) 8 60 68
205 8 $$ir{width} = $1;
206 8 $$ir{height} = $2;
207 8 $$ir{max} = $3;
208 8 $$ir{pixels} = $1*$2;
209 8 last;
210 }
211 }
212 } # while reading header
213  
214 13 $$ir{error} = '';
215 } else {
216 0 $$ir{error} = 'Wrong magic number';
217 }
218  
219 13 $$ir{fullheader} = $in;
220 13 return;
221 } # end &int_readpnmheader
222  
223 # internal single value to float function
224 16 ▶sub int_decvaltofloat {
Subroutine int_decvaltofloat: called CC=3 SCAR=11.1
Pod undocumented
225 16 my $v = shift;
226 16 my $m = shift;
227 16 my $p;
228  
229 # eat our own dog food for indicating a decimal value
230 16 $v =~ s/:$//;
231  
232 16 ▶ if($v >= $m) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v >= $m) { } 4 12 16
elsif ($v == 0) { } 0 12 12
233 4 $p = '1.0,';
234 } elsif ($v == 0) {
235 0 $p = '0.0,';
236 } else {
237 12 $p = sprintf('%0.8f,', ($v/$m));
238 }
239  
240 16 return $p;
241 } # end &int_decvaltofloat
242  
243 # internal RGB to float function
244 44 ▶sub int_dectripletofloat {
Subroutine int_dectripletofloat: called CC=4 SCAR=13.9
Pod undocumented
245 44 my $r = shift;
246 44 my $g = shift;
247 44 my $b = shift;
248 44 my $m = shift;
249 44 my $p;
250  
251 # eat our own dog food for indicating a decimal value
252 44 $r =~ s/:$//;
253 44 $g =~ s/:$//;
254 44 $b =~ s/:$//;
255  
256 44 ▶ if($r > $m) { $r = $m; }
Branch: 1 branch
BranchTrueFalseTotal
if ($r > $m) 2 42 44
257 0 ▶ if($g > $m) { $g = $m; }
Branch: 1 branch
BranchTrueFalseTotal
if ($g > $m) 0 44 44
258 44 ▶ if($b > $m) { $b = $m; }
Branch: 1 branch
BranchTrueFalseTotal
if ($b > $m) 2 42 44
259  
260 44 $p = sprintf('%0.8f,%0.8f,%0.8f', ($r/$m), ($g/$m), ($b/$m));
261  
262 # paranoia: I don't trust floating point to get 1.0 exactly
263 44 $p =~ s/1[.]\d+/1.0/g;
264  
265 # more compact
266 44 $p =~ s/0[.]0+\b/0.0/g;
267  
268 44 return $p;
269 } # end &int_dectripletofloat
270  
271 # internal single float to dec function
272 93 ▶sub int_floatvaltodec {
Subroutine int_floatvaltodec: called CC=3 SCAR=11.0
Pod undocumented
273 93 my $v = shift;
274 93 my $m = shift;
275 93 my $p;
276  
277 # eat our own dog food for indicating a float value
278 93 $v =~ s/,$//;
279  
280 # 1/65535 is about .0000152590
281 93 ▶ if($v >= 0.999999) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v >= 0.999999) { } 17 76 93
elsif ($v <= 1e-06) { } 65 11 76
282 17 $p = "$m:";
283 } elsif ($v <= 0.000001) {
284 65 $p = '0:';
285 } else {
286 # counter-intuitive way to round to an interger, but int() is
287 # rather broken.
288 11 $p = sprintf('%1.0f:', ($v*$m));
289 }
290  
291 93 return $p;
292 } # end &int_floatvaltodec
293  
294 # internal RGB float to dec function
295 0 ▶sub int_floattripletodec {
Subroutine int_floattripletodec: not called CC=1 SCAR=6.9
Pod undocumented
296 0 my $r = shift;
297 0 my $g = shift;
298 0 my $b = shift;
299 0 my $m = shift;
300 0 my $p;
301  
302 0 $r = int_floatvaltodec($r, $m);
303 0 $g = int_floatvaltodec($g, $m);
304 0 $b = int_floatvaltodec($b, $m);
305  
306 0 $p = "$r$g$b";
307 # remove final (extra) comma
308 0 $p =~ s/,$//;
309  
310 0 return $p;
311 } # end &int_floattripletodec
312  
313 # internal single float to hex function
314 8 ▶sub int_floatvaltohex {
Subroutine int_floatvaltohex: called CC=3 SCAR=11.1
Pod undocumented
315 8 my $v = shift;
316 8 my $m = shift;
317 8 my $p;
318  
319 # eat our own dog food for indicating a float value
320 8 $v =~ s/,$//;
321  
322 # 1/65535 is about .0000152590
323 8 ▶ if($v >= 0.999999) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v >= 0.999999) { } 1 7 8
elsif ($v <= 1e-06) { } 0 7 7
324 1 $p = sprintf("%X/", $m);
325 } elsif ($v <= 0.000001) {
326 0 $p = '0/';
327 } else {
328 # counter-intuitive way to round to an interger, but int() is
329 # rather broken.
330 7 $p = sprintf("%X/", sprintf('%1.0f', ($v*$m)));
331 }
332  
333 8 return $p;
334 } # end &int_floatvaltohex
335  
336 # internal RGB float to hex function
337 0 ▶sub int_floattripletodhex{
Subroutine int_floattripletodhex: not called CC=1 SCAR=6.9
Pod undocumented
338 0 my $r = shift;
339 0 my $g = shift;
340 0 my $b = shift;
341 0 my $m = shift;
342 0 my $p;
343  
344 0 $r = int_floatvaltohex($r, $m);
345 0 $g = int_floatvaltohex($g, $m);
346 0 $b = int_floatvaltohex($b, $m);
347  
348 0 $p = "$r$g$b";
349 # remove final (extra) slash
350 0 $p =~ s:/$::;
351  
352 0 return $p;
353 } # end &int_floattripletohex
354  
355 # hands off to correct int_encodepixels_N type
356 39 ▶sub int_encodepixels {
Subroutine int_encodepixels: called CC=7 SCAR=19.5
Pod undocumented
357 39 my $type = shift;
358 39 my $p_r = shift;
359 39 my $deep = shift;
360 39 my $encode = shift;
361 39 my $max = shift;
362  
363 # most common to least common
364 # type 7 is PAM, not supported here (yet)
365 # types 1 and 4 are PBM and don't need a max
366  
367 39 ▶ if($type == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 6) 12 27 39
368 12 return int_encodepixels_6($p_r, $deep, $encode, $max);
369 }
370 27 ▶ if($type == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 5) 9 18 27
371 9 return int_encodepixels_5($p_r, $deep, $encode, $max);
372 }
373 18 ▶ if($type == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 4) 3 15 18
374 3 return int_encodepixels_4($p_r, $deep, $encode );
375 }
376 15 ▶ if($type == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 3) 5 10 15
377 5 return int_encodepixels_3($p_r, $deep, $encode, $max);
378 }
379 10 ▶ if($type == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 2) 7 3 10
380 7 return int_encodepixels_2($p_r, $deep, $encode, $max);
381 }
382 3 ▶ if($type == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1) 3 0 3
383 3 return int_encodepixels_1($p_r, $deep, $encode );
384 }
385  
386 # should never reach here
387 0 return undef;
388  
389 } # end &int_encodepixels
390  
391 # Internal read pixels for P1: ascii bitmap. Does not do argument checks.
392 1 ▶sub int_readpixels_1 {
Subroutine int_readpixels_1: called CC=7 SCAR=19.5
Pod undocumented
393 1 my $gr = shift; # input file glob ref
394 1 my $ir = shift; # image info hash ref
395 1 my $pr = shift; # pixel array ref
396 1 my $enc = shift; # target pixel encoding
397  
398 1 my $used = 0;
399 1 my $read;
400 my $bit;
401 1 my $w = 0;
402 1 my $h = 0;
403  
404 1 ▶ while(defined($read = <$gr>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($read = readline $gr)) 5 1 6
405 5 ▶ while($read =~ /\b(\d+)\b/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read =~ /\b(\d+)\b/g) 20 4 24
406 20 ▶ $bit = ($1)? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 16 4 20
407 20 $$pr[$h][$w] = $bit;
408 20 $used ++;
409 20 ▶ if($used >= $$ir{pixels}) { last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($used >= $ir->{"pixels"}) 1 19 20
410 19 $w ++;
411 19 ▶ if($w >= $$ir{width}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w >= $ir->{"width"}) 4 15 19
412 4 $w = 0;
413 4 $h ++;
414 }
415 }
416 } # while read from file
417  
418 1 ▶ if($used < $$ir{pixels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used < $ir->{'pixels'}) { } 0 1 1
419 0 $$ir{error} = 'type 1 read: not enough pixels';
420 } else {
421 1 $$ir{error} = '';
422 }
423 } # end &int_readpixels_1
424  
425 # Internal write pixels for P1: ascii bitmap. Does not do argument checks.
426 3 ▶sub int_encodepixels_1 {
Subroutine int_encodepixels_1: called CC=14 SCAR=27.0
Pod undocumented
427 3 my $pr = shift; # pixel array ref
428 3 my $deep = shift; # how deep is our array
429 3 my $enc = shift; # source pixel encoding
430  
431 3 my $w = 0;
432 3 my $h = 0;
433 3 my $out = '';
434 3 my $wide = 0;
435 3 my $pix;
436 my $cur;
437  
438 3 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq '1d') { } 1 2 3
439 # $#{array} returns counts starting at -1 for empty array
440 1 $pix = 1+ $#{$pr};
441 1 $cur = $$pr[$w];
442 } else {
443 # deep = 3d only allowed for P3/P6
444 2 $pix = (1+ $#{$pr}) * (1+ $#{$$pr[0]});
445 2 $cur = $$pr[$h][$w];
446 }
447  
448 3 ▶ while($pix > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pix > 0) 60 3 63
449 60 $cur =~ s![,:/]$!!;
450 60 ▶ if($enc eq 'float') {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq 'float') { } 0 60 60
451 0 ▶ if($cur > 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur > 0.5) { } 0 0 0
452 0 $out .= '1 ';
453 } else {
454 0 $out .= '0 ';
455 }
456 } else {
457 # for PBM, we assume $max is 1
458 60 ▶ if($cur) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur) { } 12 48 60
459 12 $out .= '1 ';
460 } else {
461 48 $out .= '0 ';
462 }
463 }
464  
465 60 $wide += 2;
466 60 ▶ if($wide > 70) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wide > 70) 0 60 60
467 0 $out .= "\n";
468 0 $wide = 0;
469 }
470  
471 60 $pix --;
472 60 $w ++;
473 60 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq '1d') { } 20 40 60
474 20 ▶ if(exists($$pr[$w]) and defined($$pr[$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pr->[$w] and defined $pr->[$w]) { } 19 1 20
Condition66%2/3
exists $pr->[$w] and defined $pr->[$w]
!ll&&!rl&&r
1019
MC/DC50%1/2
exists $pr->[$w] and defined $pr->[$w]
exists $pr->[$w] defined $pr->[$w]
Truth table66%2/3
A and B
A= exists $pr->[$w]B= defined $pr->[$w]
ABresult
0X0
100
111
475 19 $cur = $$pr[$w];
476 } else {
477 1 $cur = 0;
478 }
479 } else {
480 40 ▶ if(!exists($$pr[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pr->[$h][$w]) 11 29 40
481 11 $w = 0;
482 11 $h ++;
483 }
484 40 ▶ if(exists($$pr[$h][$w]) and defined($$pr[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pr->[$h][$w] and defined $pr->[$h][$w]) { } 37 3 40
Condition66%2/3
exists $pr->[$h][$w] and defined $pr->[$h][$w]
!ll&&!rl&&r
3037
MC/DC50%1/2
exists $pr->[$h][$w] and defined $pr->[$h][$w]
exists $pr->[$h][$w] defined $pr->[$h][$w]
Truth table66%2/3
A and B
A= exists $pr->[$h][$w]B= defined $pr->[$h][$w]
ABresult
0X0
100
111
485 37 $cur = $$pr[$h][$w];
486 } else {
487 3 $cur = 0;
488 }
489 }
490 } # while pix
491  
492 3 ▶ if($wide) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wide) 3 0 3
493 3 $out .= "\n";
494 }
495 3 return($out);
496 } # end &int_encodepixels_1
497  
498 # Internal read pixels for P2: ascii graymap. Does not do argument checks.
499 2 ▶sub int_readpixels_2 {
Subroutine int_readpixels_2: called CC=10 SCAR=23.5
Pod undocumented
500 2 my $gr = shift; # input file glob ref
501 2 my $ir = shift; # image info hash ref
502 2 my $pr = shift; # pixel array ref
503 2 my $enc = shift; # target pixel encoding
504  
505 2 my $used = 0;
506 2 my $read;
507 my $val;
508 2 my $pix;
509 2 my $w = 0;
510 2 my $h = 0;
511
512 2 ▶ while(defined($read = <$gr>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($read = readline $gr)) 10 2 12
513 10 ▶ while($read =~ /\b(\d+)\b/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read =~ /\b(\d+)\b/g) 40 8 48
514 40 $val = $1;
515  
516 40 ▶ if($enc eq 'dec') {
Branch: 2 branches
BranchTrueFalseTotal
if ($enc eq 'dec') { } 0 40 40
elsif ($enc eq 'hex') { } 0 40 40
517 0 $pix = "$val:";
518 } elsif ($enc eq 'hex') {
519 0 $pix = sprintf('%X:', $val);
520 } else {
521 40 ▶ if($val >= $$ir{max}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val >= $ir->{'max'}) { } 32 8 40
elsif ($val == 0) { } 8 0 8
522 32 $pix = '1.0,';
523 } elsif ($val == 0) {
524 8 $pix = '0.0,';
525 } else {
526 0 $pix = sprintf('%0.8f,', $val/$$ir{max});
527 }
528 }
529  
530 40 $$pr[$h][$w] = $pix;
531 40 $used ++;
532 40 ▶ if($used >= $$ir{pixels}) { last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($used >= $ir->{"pixels"}) 2 38 40
533 38 $w ++;
534 38 ▶ if($w >= $$ir{width}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w >= $ir->{"width"}) 8 30 38
535 8 $w = 0;
536 8 $h ++;
537 }
538 }
539 } # while read from file
540  
541 2 ▶ if($used < $$ir{pixels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used < $ir->{'pixels'}) { } 0 2 2
542 0 $$ir{error} = 'type 2 read: not enough pixels';
543 } else {
544 2 $$ir{error} = '';
545 }
546 } # end &int_readpixels_2
547  
548 # Internal write pixels for P2: ascii graymap. Does not do argument checks.
549 7 ▶sub int_encodepixels_2 {
Subroutine int_encodepixels_2: called CC=14 SCAR=26.5
Pod undocumented
550 7 my $pr = shift; # pixel array ref
551 7 my $deep = shift; # how deep is our array
552 7 my $enc = shift; # source pixel encoding
553 7 my $max = shift; # max value
554  
555 7 my $w = 0;
556 7 my $h = 0;
557 7 my $out = '';
558 7 my $val;
559 7 my $wide = 0;
560 7 my $pix;
561 my $cur;
562  
563 7 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq '1d') { } 3 4 7
564 # $#{array} returns counts starting at -1 for empty array
565 3 $pix = 1+ $#{$pr};
566 3 $cur = $$pr[$w];
567 } else {
568 # deep = 3d only allowed for P3/P6
569 4 $pix = (1+ $#{$pr}) * (1+ $#{$$pr[0]});
570 4 $cur = $$pr[$h][$w];
571 }
572  
573 7 ▶ while($pix > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pix > 0) 129 7 136
574  
575 129 ▶ if($enc eq 'float') {
Branch: 2 branches
BranchTrueFalseTotal
if ($enc eq 'float') { } 20 109 129
elsif ($enc eq 'hex') { } 29 80 109
576 20 $val = int_floatvaltodec($cur, $max);
577 20 chop($val); # eat last ':'
578 } elsif($enc eq 'hex') {
579 29 $cur =~ s!/$!!;
580 29 $val = hex($cur);
581 } else {
582 80 $cur =~ s!:$!!;
583 80 $val = 0+$cur; # normalize numbers
584 }
585  
586 129 ▶ if($val > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val > $max) 2 127 129
587 2 $val = $max;
588 }
589  
590 129 ▶ if(70 < ($wide + 1 + length($val))) {
Branch: 1 branch
BranchTrueFalseTotal
if (70 < $wide + 1 + length($val)) 0 129 129
591 0 $wide = 0;
592 0 $out .= "\n";
593 }
594 129 $out .= $val . ' ';
595 129 $wide += 1 + length($val);
596  
597 129 $pix --;
598 129 $w ++;
599 129 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq '1d') { } 49 80 129
600 49 ▶ if(exists($$pr[$w]) and defined($$pr[$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pr->[$w] and defined $pr->[$w]) { } 46 3 49
Condition66%2/3
exists $pr->[$w] and defined $pr->[$w]
!ll&&!rl&&r
3046
MC/DC50%1/2
exists $pr->[$w] and defined $pr->[$w]
exists $pr->[$w] defined $pr->[$w]
Truth table66%2/3
A and B
A= exists $pr->[$w]B= defined $pr->[$w]
ABresult
0X0
100
111
601 46 $cur = $$pr[$w];
602 } else {
603 3 $cur = 0;
604 }
605 } else {
606 80 ▶ if(!exists($$pr[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pr->[$h][$w]) 23 57 80
607 23 $w = 0;
608 23 $h ++;
609 }
610 80 ▶ if(exists($$pr[$h][$w]) and defined($$pr[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pr->[$h][$w] and defined $pr->[$h][$w]) { } 73 7 80
Condition66%2/3
exists $pr->[$h][$w] and defined $pr->[$h][$w]
!ll&&!rl&&r
7073
MC/DC50%1/2
exists $pr->[$h][$w] and defined $pr->[$h][$w]
exists $pr->[$h][$w] defined $pr->[$h][$w]
Truth table66%2/3
A and B
A= exists $pr->[$h][$w]B= defined $pr->[$h][$w]
ABresult
0X0
100
111
611 73 $cur = $$pr[$h][$w];
612 } else {
613 7 $cur = 0;
614 }
615 }
616 } # while pix
617  
618 7 ▶ if($wide) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wide) 7 0 7
619 7 $out .= "\n";
620 }
621  
622 7 return($out);
623 } # end &int_encodepixels_2
624  
625 # Internal read pixels for P3: ascii pixmap. Does not do argument checks.
626 2 ▶sub int_readpixels_3 {
Subroutine int_readpixels_3: called CC=12 SCAR=25.1
Pod undocumented
627 2 my $gr = shift; # input file glob ref
628 2 my $ir = shift; # image info hash ref
629 2 my $pr = shift; # pixel array ref
630 2 my $enc = shift; # target pixel encoding
631  
632 2 my $used = 0;
633 2 my $read;
634 my $val;
635 2 my $pix;
636 2 my $w = 0;
637 2 my $h = 0;
638 2 my $r;
639 my $g;
640 2 my $state = 'r';
641  
642 2 ▶ while(defined($read = <$gr>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($read = readline $gr)) 10 2 12
643 10 ▶ while($read =~ /\b(\d+)\b/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read =~ /\b(\d+)\b/g) 120 8 128
644 120 $val = $1;
645  
646 120 ▶ if($enc eq 'dec') {
Branch: 2 branches
BranchTrueFalseTotal
if ($enc eq 'dec') { } 0 120 120
elsif ($enc eq 'hex') { } 0 120 120
647 0 $pix = "$val:";
648 } elsif ($enc eq 'hex') {
649 0 $pix = sprintf('%X:', $val);
650 } else {
651 120 ▶ if($val >= $$ir{max}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val >= $ir->{'max'}) { } 96 24 120
elsif ($val == 0) { } 24 0 24
652 96 $pix = '1.0,';
653 } elsif ($val == 0) {
654 24 $pix = '0.0,';
655 } else {
656 0 $pix = sprintf('%0.8f,', $val/$$ir{max});
657 }
658 }
659  
660 120 ▶ if($state eq 'r') {
Branch: 2 branches
BranchTrueFalseTotal
if ($state eq 'r') { } 40 80 120
elsif ($state eq 'g') { } 40 40 80
661 40 $r = $pix;
662 40 $state = 'g';
663 } elsif($state eq 'g') {
664 40 $g = $pix;
665 40 $state = 'b';
666 } else {
667  
668 40 chop($pix);
669 40 $$pr[$h][$w] = "$r$g$pix";
670 40 $used ++;
671 40 ▶ if($used >= $$ir{pixels}) { last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($used >= $ir->{"pixels"}) 2 38 40
672 38 $w ++;
673 38 ▶ if($w >= $$ir{width}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w >= $ir->{"width"}) 8 30 38
674 8 $w = 0;
675 8 $h ++;
676 }
677  
678 38 $state = 'r';
679 }
680 }
681 } # while read from file
682  
683 2 ▶ if($used < $$ir{pixels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used < $ir->{'pixels'}) { } 0 2 2
684 0 $$ir{error} = 'type 3 read: not enough pixels';
685 } else {
686 2 $$ir{error} = '';
687 }
688 } # end &int_readpixels_3
689  
690 # Internal write pixels for P3: ascii pixmap. Does not do argument checks.
691 5 ▶sub int_encodepixels_3 {
Subroutine int_encodepixels_3: called CC=15 SCAR=28.5
Pod undocumented
692 5 my $pr = shift; # pixel array ref
693 5 my $deep = shift; # how deep is our array
694 5 my $enc = shift; # source pixel encoding
695 5 my $max = shift; # max value
696  
697 5 my $w = 0;
698 5 my $h = 0;
699 5 my $out = '';
700 5 my $val;
701 5 my $wide = 0;
702 5 my $pix;
703 my @cur;
704 5 my $rgb;
705  
706 5 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq '1d') { } 0 5 5
707 # $#{array} returns counts starting at -1 for empty array
708 0 $pix = 1+ $#{$pr};
709 0 @cur = explodetriple($$pr[$w]);
710 } else {
711 # explodetriple makes deep = 2d work like deep = 3d
712 5 $pix = (1+ $#{$pr}) * (1+ $#{$$pr[0]});
713 5 @cur = explodetriple($$pr[$h][$w]);
714 }
715  
716 5 ▶ while($pix > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pix > 0) 100 5 105
717  
718 100 for $rgb (0,1,2) {
719 300 ▶ if($enc eq 'float') {
Branch: 2 branches
BranchTrueFalseTotal
if ($enc eq 'float') { } 60 240 300
elsif ($enc eq 'hex') { } 60 180 240
720 60 $val = int_floatvaltodec($cur[$rgb], $max);
721 60 chop($val); # eat last ':'
722 } elsif($enc eq 'hex') {
723 60 $cur[$rgb] =~ s!/$!!;
724 60 $val = hex($cur[$rgb]);
725 } else {
726 180 $cur[$rgb] =~ s!:$!!;
727 180 $val = 0+$cur[$rgb]; # normalize numbers
728 }
729  
730 300 ▶ if($val > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val > $max) 0 300 300
731 0 $val = $max;
732 }
733  
734 300 ▶ if(70 < ($wide + 1 + length($val))) {
Branch: 1 branch
BranchTrueFalseTotal
if (70 < $wide + 1 + length($val)) 6 294 300
735 6 $wide = 0;
736 6 $out .= "\n";
737 }
738 300 $out .= $val . ' ';
739 300 $wide += 1 + length($val);
740 } # for rgb
741  
742 100 $pix --;
743 100 $w ++;
744 100 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq '1d') { } 0 100 100
745 0 ▶ if(exists($$pr[$w]) and defined($$pr[$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pr->[$w] and defined $pr->[$w]) { } 0 0 0
Condition0%0/3
exists $pr->[$w] and defined $pr->[$w]
!ll&&!rl&&r
000
MC/DC0%0/2
exists $pr->[$w] and defined $pr->[$w]
exists $pr->[$w] defined $pr->[$w]
Truth table0%0/3
A and B
A= exists $pr->[$w]B= defined $pr->[$w]
ABresult
0X0
100
111
746 0 @cur = explodetriple($$pr[$w]);
747 } else {
748 0 @cur = (0,0,0);
749 }
750 } else {
751 100 ▶ if(!exists($$pr[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pr->[$h][$w]) 25 75 100
752 25 $w = 0;
753 25 $h ++;
754 }
755 100 ▶ if(exists($$pr[$h][$w]) and defined($$pr[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pr->[$h][$w] and defined $pr->[$h][$w]) { } 95 5 100
Condition66%2/3
exists $pr->[$h][$w] and defined $pr->[$h][$w]
!ll&&!rl&&r
5095
MC/DC50%1/2
exists $pr->[$h][$w] and defined $pr->[$h][$w]
exists $pr->[$h][$w] defined $pr->[$h][$w]
Truth table66%2/3
A and B
A= exists $pr->[$h][$w]B= defined $pr->[$h][$w]
ABresult
0X0
100
111
756 95 @cur = explodetriple($$pr[$h][$w]);
757 } else {
758 5 @cur = (0,0,0);
759 }
760 }
761 } # while pix
762  
763 5 ▶ if($wide) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wide) 5 0 5
764 5 $out .= "\n";
765 }
766 5 return($out);
767 } # end &int_encodepixels_3
768  
769 # Internal read pixels for P4: raw bitmap. Does not do argument checks.
770 4 ▶sub int_readpixels_4 {
Subroutine int_readpixels_4: called CC=6 SCAR=18.0
Pod undocumented
771 4 my $gr = shift; # input file glob ref
772 4 my $ir = shift; # image info hash ref
773 4 my $pr = shift; # pixel array ref
774 4 my $enc = shift; # target pixel encoding
775  
776 4 my $used = 0;
777 4 my $read;
778 my $bits;
779 4 my $bit;
780 4 my $w = 0;
781 4 my $h = 0;
782  
783 READ:
784 4 ▶ while(read($gr,$read,1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $gr, $read, 1) 20 0 20
785 # $bits will be '01000001' if $read is 'A'
786 20 $bits = unpack('B*', $read);
787  
788 20 for $bit ($bits =~ /([01])/g) {
789 80 $$pr[$h][$w] = $bit;
790 80 $used ++;
791 80 ▶ if($used >= $$ir{pixels}) { last READ; }
Branch: 1 branch
BranchTrueFalseTotal
if ($used >= $ir->{"pixels"}) 4 76 80
792 76 $w ++;
793 76 ▶ if($w >= $$ir{width}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w >= $ir->{"width"}) 16 60 76
794 16 $w = 0;
795 16 $h ++;
796 # pbm pads each row with unused bits, if (width % 8) != 0
797 16 next READ;
798 }
799 }
800 } # while read from file
801  
802 4 ▶ if($used < $$ir{pixels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used < $ir->{'pixels'}) { } 0 4 4
803 0 $$ir{error} = 'type 4 read: not enough pixels';
804 } else {
805 4 $$ir{error} = '';
806 }
807 } # end &int_readpixels_4
808  
809 # Internal write pixels for P4: raw bitmap. Does not do argument checks.
810 3 ▶sub int_encodepixels_4 {
Subroutine int_encodepixels_4: called CC=15 SCAR=29.8
Pod undocumented
811 3 my $pr = shift; # pixel array ref
812 3 my $deep = shift; # how deep is our array
813 3 my $enc = shift; # source pixel encoding
814  
815 3 my $w = 0;
816 3 my $h = 0;
817 3 my $out = '';
818 3 my $used = 0;
819 3 my $pix;
820 my $cur;
821 3 my $val = '';
822  
823 3 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq '1d') { } 0 3 3
824 # $#{array} returns counts starting at -1 for empty array
825 0 $pix = 1+ $#{$pr};
826 0 $cur = $$pr[$w];
827 } else {
828 # deep = 3d only allowed for P3/P6
829 3 $pix = (1+ $#{$pr}) * (1+ $#{$$pr[0]});
830 3 $cur = $$pr[$h][$w];
831 }
832  
833 3 ▶ while($pix > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pix > 0) 57 3 60
834 57 $cur =~ s![,:/]$!!;
835 57 ▶ if($enc eq 'float') {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq 'float') { } 0 57 57
836 0 ▶ if($cur > 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur > 0.5) { } 0 0 0
837 0 $val .= '1';
838 } else {
839 0 $val .= '0';
840 }
841 } else {
842 # for PBM, we assume $max is 1
843 57 ▶ if($cur) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur) { } 32 25 57
844 32 $val .= '1';
845 } else {
846 25 $val .= '0';
847 }
848 }
849  
850 57 $used ++;
851 57 ▶ if($used == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used == 8) 3 54 57
852 3 $out .= pack("B*", $val);
853 3 $used = 0;
854 3 $val = '';
855 }
856  
857 57 $pix --;
858 57 $w ++;
859 57 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq '1d') { } 0 57 57
860 0 ▶ if(exists($$pr[$w]) and defined($$pr[$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pr->[$w] and defined $pr->[$w]) { } 0 0 0
Condition0%0/3
exists $pr->[$w] and defined $pr->[$w]
!ll&&!rl&&r
000
MC/DC0%0/2
exists $pr->[$w] and defined $pr->[$w]
exists $pr->[$w] defined $pr->[$w]
Truth table0%0/3
A and B
A= exists $pr->[$w]B= defined $pr->[$w]
ABresult
0X0
100
111
861 0 $cur = $$pr[$w];
862 } else {
863 0 $cur = 0;
864 }
865 } else {
866 57 ▶ if(!exists($$pr[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pr->[$h][$w]) 11 46 57
867 11 $w = 0;
868 11 $h ++;
869  
870 # PBM raw is padded to full byte at end of each row
871 11 ▶ if($used) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used) 8 3 11
872 8 $out .= pack("B*", substr($val.'0000000',0,8) );
873 8 $used = 0;
874 8 $val = '';
875 }
876 }
877 57 ▶ if(exists($$pr[$h][$w]) and defined($$pr[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pr->[$h][$w] and defined $pr->[$h][$w]) { } 52 5 57
Condition66%2/3
exists $pr->[$h][$w] and defined $pr->[$h][$w]
!ll&&!rl&&r
5052
MC/DC50%1/2
exists $pr->[$h][$w] and defined $pr->[$h][$w]
exists $pr->[$h][$w] defined $pr->[$h][$w]
Truth table66%2/3
A and B
A= exists $pr->[$h][$w]B= defined $pr->[$h][$w]
ABresult
0X0
100
111
878 52 $cur = $$pr[$h][$w];
879 } else {
880 5 $cur = 0;
881 }
882 }
883 } # while pix
884  
885 3 ▶ if($used) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used) 0 3 3
886 0 $out .= pack("B*", substr($val.'0000000',0,8) );
887 }
888 3 return($out);
889 } # end &int_encodepixels_4
890  
891 # Internal read pixels for P5: raw graymap. Does not do argument checks.
892 2 ▶sub int_readpixels_5 {
Subroutine int_readpixels_5: called CC=12 SCAR=25.4
Pod undocumented
893 2 my $gr = shift; # input file glob ref
894 2 my $ir = shift; # image info hash ref
895 2 my $pr = shift; # pixel array ref
896 2 my $enc = shift; # target pixel encoding
897  
898 2 my $used = 0;
899 2 my $read;
900 my $val;
901 2 my $pix;
902 2 my $rc;
903 2 my $w = 0;
904 2 my $h = 0;
905 2 my $expect = 1;
906  
907 2 ▶ if ($$ir{max} > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ir->{"max"} > 255) 1 1 2
908 1 $expect = 2;
909 }
910
911 2 ▶ while($rc = read($gr,$read,$expect)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc = read($gr, $read, $expect)) 40 0 40
912 40 ▶ if($rc == $expect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc == $expect) 40 0 40
913 40 ▶ if($expect == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect == 1) { } 20 20 40
914 # $val will be 65 if $read is 'A'
915 20 $val = unpack('C', $read);
916 } else {
917 # $val will be 16706 if $read is 'AB'
918 20 $val = unpack('n', $read);
919 }
920  
921 40 ▶ if($enc eq 'dec') {
Branch: 2 branches
BranchTrueFalseTotal
if ($enc eq 'dec') { } 0 40 40
elsif ($enc eq 'hex') { } 0 40 40
922 0 $pix = "$val:";
923 } elsif ($enc eq 'hex') {
924 0 $pix = sprintf('%X:', $val);
925 } else {
926 40 ▶ if($val >= $$ir{max}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val >= $ir->{'max'}) { } 32 8 40
elsif ($val == 0) { } 8 0 8
927 32 $pix = '1.0,';
928 } elsif ($val == 0) {
929 8 $pix = '0.0,';
930 } else {
931 0 $pix = sprintf('%0.8f,', $val/$$ir{max});
932 }
933 }
934  
935 40 $$pr[$h][$w] = $pix;
936 40 $used ++;
937 40 ▶ if($used >= $$ir{pixels}) { last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($used >= $ir->{"pixels"}) 2 38 40
938 38 $w ++;
939 38 ▶ if($w >= $$ir{width}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w >= $ir->{"width"}) 8 30 38
940 8 $w = 0;
941 8 $h ++;
942 }
943 }
944 } # while read from file
945  
946 2 ▶ if($used < $$ir{pixels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used < $ir->{'pixels'}) { } 0 2 2
947 0 $$ir{error} = 'type 5 read: not enough pixels';
948 } else {
949 2 $$ir{error} = '';
950 }
951 } # end &int_readpixels_5
952  
953  
954 # Internal write pixels for P5: raw graymap. Does not do argument checks.
955 9 ▶sub int_encodepixels_5 {
Subroutine int_encodepixels_5: called CC=13 SCAR=25.8
Pod undocumented
956 9 my $pr = shift; # pixel array ref
957 9 my $deep = shift; # how deep is our array
958 9 my $enc = shift; # source pixel encoding
959 9 my $max = shift; # max value
960  
961 9 my $w = 0;
962 9 my $h = 0;
963 9 my $out = '';
964 9 my $val;
965 my $pix;
966 9 my $cur;
967 9 my $packer;
968  
969 9 ▶ if($max > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max > 255) { } 2 7 9
970 2 $packer = 'n';
971 } else {
972 7 $packer = 'C';
973 }
974 9 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq '1d') { } 2 7 9
975 # $#{array} returns counts starting at -1 for empty array
976 2 $pix = 1+ $#{$pr};
977 2 $cur = $$pr[$w];
978 } else {
979 # deep = 3d only allowed for P3/P6
980 7 $pix = (1+ $#{$pr}) * (1+ $#{$$pr[0]});
981 7 $cur = $$pr[$h][$w];
982 }
983  
984 9 ▶ while($pix > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pix > 0) 72 9 81
985  
986 72 ▶ if($enc eq 'float') {
Branch: 2 branches
BranchTrueFalseTotal
if ($enc eq 'float') { } 3 69 72
elsif ($enc eq 'hex') { } 69 0 69
987 3 $val = int_floatvaltodec($cur, $max);
988 3 chop($val); # eat last ':'
989 } elsif($enc eq 'hex') {
990 69 $cur =~ s!/$!!;
991 69 $val = hex($cur);
992 } else {
993 0 $cur =~ s!:$!!;
994 0 $val = 0+$cur; # normalize numbers
995 }
996  
997 72 ▶ if($val > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val > $max) 0 72 72
998 0 $val = $max;
999 }
1000  
1001 72 $out .= pack($packer, $val);
1002  
1003 72 $pix --;
1004 72 $w ++;
1005 72 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq '1d') { } 18 54 72
1006 18 ▶ if(exists($$pr[$w]) and defined($$pr[$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pr->[$w] and defined $pr->[$w]) { } 16 2 18
Condition66%2/3
exists $pr->[$w] and defined $pr->[$w]
!ll&&!rl&&r
2016
MC/DC50%1/2
exists $pr->[$w] and defined $pr->[$w]
exists $pr->[$w] defined $pr->[$w]
Truth table66%2/3
A and B
A= exists $pr->[$w]B= defined $pr->[$w]
ABresult
0X0
100
111
1007 16 $cur = $$pr[$w];
1008 } else {
1009 2 $cur = 0;
1010 }
1011 } else {
1012 54 ▶ if(!exists($$pr[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pr->[$h][$w]) 11 43 54
1013 11 $w = 0;
1014 11 $h ++;
1015 }
1016 54 ▶ if(exists($$pr[$h][$w]) and defined($$pr[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pr->[$h][$w] and defined $pr->[$h][$w]) { } 47 7 54
Condition66%2/3
exists $pr->[$h][$w] and defined $pr->[$h][$w]
!ll&&!rl&&r
7047
MC/DC50%1/2
exists $pr->[$h][$w] and defined $pr->[$h][$w]
exists $pr->[$h][$w] defined $pr->[$h][$w]
Truth table66%2/3
A and B
A= exists $pr->[$h][$w]B= defined $pr->[$h][$w]
ABresult
0X0
100
111
1017 47 $cur = $$pr[$h][$w];
1018 } else {
1019 7 $cur = 0;
1020 }
1021 }
1022 } # while pix
1023  
1024 9 return($out);
1025  
1026 } # end &int_encodepixels_5
1027  
1028  
1029 # Internal read pixels for P6: raw pixmap. Does not do argument checks.
1030 2 ▶sub int_readpixels_6 {
Subroutine int_readpixels_6: called CC=10 SCAR=23.4
Pod undocumented
1031 2 my $gr = shift; # input file glob ref
1032 2 my $ir = shift; # image info hash ref
1033 2 my $pr = shift; # pixel array ref
1034 2 my $enc = shift; # target pixel encoding
1035  
1036 2 my $used = 0;
1037 2 my $read;
1038 my $val;
1039 2 my $pix;
1040 2 my $rc;
1041 2 my $w = 0;
1042 2 my $h = 0;
1043 2 my $r;
1044 my $g;
1045 2 my $b;
1046 2 my $expect = 3;
1047  
1048 2 ▶ if ($$ir{max} > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ir->{"max"} > 255) 1 1 2
1049 1 $expect = 6;
1050 }
1051  
1052 2 ▶ while($rc = read($gr,$read,$expect)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc = read($gr, $read, $expect)) 40 0 40
1053 40 ▶ if($rc == $expect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc == $expect) 40 0 40
1054 40 ▶ if($expect == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect == 3) { } 20 20 40
1055 # ($r,$g,$b) will be (65,66,0) if $read is 'AB<nul>'
1056 20 ($r,$g,$b) = unpack('CCC', $read);
1057 } else {
1058 # ($r,$g,$b) will be (16706,49,12544) if $read is 'AB<nul>11<nul>'
1059 20 ($r,$g,$b) = unpack('nnn', $read);
1060 }
1061
1062  
1063 40 ▶ if($enc eq 'dec') {
Branch: 2 branches
BranchTrueFalseTotal
if ($enc eq 'dec') { } 0 40 40
elsif ($enc eq 'hex') { } 0 40 40
1064 0 $pix = "$r:$g:$b";
1065 } elsif ($enc eq 'hex') {
1066 0 $pix = sprintf('%X:%X:%X', $r, $g, $b);
1067 } else {
1068 40 $pix = int_dectripletofloat($r,$g,$b,$$ir{max});
1069 }
1070  
1071 40 $$pr[$h][$w] = $pix;
1072 40 $used ++;
1073 40 ▶ if($used >= $$ir{pixels}) { last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($used >= $ir->{"pixels"}) 2 38 40
1074 38 $w ++;
1075 38 ▶ if($w >= $$ir{width}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w >= $ir->{"width"}) 8 30 38
1076 8 $w = 0;
1077 8 $h ++;
1078 }
1079  
1080 }
1081 } # while read from file
1082  
1083 2 ▶ if($used < $$ir{pixels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used < $ir->{'pixels'}) { } 0 2 2
1084 0 $$ir{error} = 'type 6 read: not enough pixels';
1085 } else {
1086 2 $$ir{error} = '';
1087 }
1088 } # end &int_readpixels_6
1089  
1090 # Internal write pixels for P6: raw pixmap. Does not do argument checks.
1091 12 ▶sub int_encodepixels_6 {
Subroutine int_encodepixels_6: called CC=14 SCAR=29.6
Pod undocumented
1092 12 my $pr = shift; # pixel array ref
1093 12 my $deep = shift; # how deep is our array
1094 12 my $enc = shift; # source pixel encoding
1095 12 my $max = shift; # max value
1096  
1097 12 my $w = 0;
1098 12 my $h = 0;
1099 12 my $out = '';
1100 12 my $val;
1101 my $pix;
1102 12 my @cur;
1103 12 my $rgb;
1104 12 my $packer;
1105  
1106 12 ▶ if($max > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max > 255) { } 1 11 12
1107 1 $packer = 'n';
1108 } else {
1109 11 $packer = 'C';
1110 }
1111  
1112 12 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq '1d') { } 0 12 12
1113 # $#{array} returns counts starting at -1 for empty array
1114 0 $pix = 1+ $#{$pr};
1115 0 @cur = explodetriple($$pr[$w]);
1116 } else {
1117 # explodetriple makes deep = 2d work like deep = 3d
1118 12 $pix = (1+ $#{$pr}) * (1+ $#{$$pr[0]});
1119 12 @cur = explodetriple($$pr[$h][$w]);
1120 }
1121  
1122 12 ▶ while($pix > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pix > 0) 25 12 37
1123  
1124 25 for $rgb (0,1,2) {
1125 75 ▶ if($enc eq 'float') {
Branch: 2 branches
BranchTrueFalseTotal
if ($enc eq 'float') { } 0 75 75
elsif ($enc eq 'hex') { } 0 75 75
1126 0 $val = int_floatvaltodec($cur[$rgb], $max);
1127 0 chop($val); # eat last ':'
1128 } elsif($enc eq 'hex') {
1129 0 $cur[$rgb] =~ s!/$!!;
1130 0 $val = hex($cur[$rgb]);
1131 } else {
1132 75 $cur[$rgb] =~ s!:$!!;
1133 75 $val = 0+$cur[$rgb]; # normalize numbers
1134 }
1135  
1136 75 ▶ if($val > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val > $max) 0 75 75
1137 0 $val = $max;
1138 }
1139  
1140 75 $out .= pack($packer, $val);
1141 } # for rgb
1142  
1143 25 $pix --;
1144 25 $w ++;
1145 25 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq '1d') { } 0 25 25
1146 0 ▶ if(exists($$pr[$w]) and defined($$pr[$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pr->[$w] and defined $pr->[$w]) { } 0 0 0
Condition0%0/3
exists $pr->[$w] and defined $pr->[$w]
!ll&&!rl&&r
000
MC/DC0%0/2
exists $pr->[$w] and defined $pr->[$w]
exists $pr->[$w] defined $pr->[$w]
Truth table0%0/3
A and B
A= exists $pr->[$w]B= defined $pr->[$w]
ABresult
0X0
100
111
1147 0 @cur = explodetriple($$pr[$w]);
1148 } else {
1149 0 @cur = (0,0,0);
1150 }
1151 } else {
1152 25 ▶ if(!exists($$pr[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pr->[$h][$w]) 14 11 25
1153 14 $w = 0;
1154 14 $h ++;
1155 }
1156 25 ▶ if(exists($$pr[$h][$w]) and defined($$pr[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pr->[$h][$w] and defined $pr->[$h][$w]) { } 13 12 25
Condition66%2/3
exists $pr->[$h][$w] and defined $pr->[$h][$w]
!ll&&!rl&&r
12013
MC/DC50%1/2
exists $pr->[$h][$w] and defined $pr->[$h][$w]
exists $pr->[$h][$w] defined $pr->[$h][$w]
Truth table66%2/3
A and B
A= exists $pr->[$h][$w]B= defined $pr->[$h][$w]
ABresult
0X0
100
111
1157 13 @cur = explodetriple($$pr[$h][$w]);
1158 } else {
1159 12 @cur = (0,0,0);
1160 }
1161 }
1162 } # while pix
1163  
1164 12 return($out);
1165  
1166 } # end &int_encodepixels_6
1167  
1168 # Internal read pixels function. Does not do argument checks.
1169 13 ▶sub int_readpixels {
Subroutine int_readpixels: called CC=7 SCAR=19.5
Pod undocumented
1170 13 my $gr = shift; # input file glob ref
1171 13 my $ir = shift; # image info hash ref
1172 13 my $pr = shift; # pixel array ref
1173 13 my $enc = shift; # target pixel encoding
1174  
1175 # most common to least common
1176 # type 7 is PAM, not supported here (yet)
1177 13 ▶ if($$ir{type} == 6) { return int_readpixels_6($gr, $ir, $pr, $enc); }
Branch: 1 branch
BranchTrueFalseTotal
if ($ir->{"type"} == 6) 2 11 13
1178 11 ▶ if($$ir{type} == 5) { return int_readpixels_5($gr, $ir, $pr, $enc); }
Branch: 1 branch
BranchTrueFalseTotal
if ($ir->{"type"} == 5) 2 9 11
1179 9 ▶ if($$ir{type} == 4) { return int_readpixels_4($gr, $ir, $pr, $enc); }
Branch: 1 branch
BranchTrueFalseTotal
if ($ir->{"type"} == 4) 4 5 9
1180 5 ▶ if($$ir{type} == 3) { return int_readpixels_3($gr, $ir, $pr, $enc); }
Branch: 1 branch
BranchTrueFalseTotal
if ($ir->{"type"} == 3) 2 3 5
1181 3 ▶ if($$ir{type} == 2) { return int_readpixels_2($gr, $ir, $pr, $enc); }
Branch: 1 branch
BranchTrueFalseTotal
if ($ir->{"type"} == 2) 2 1 3
1182 1 ▶ if($$ir{type} == 1) { return int_readpixels_1($gr, $ir, $pr, $enc); }
Branch: 1 branch
BranchTrueFalseTotal
if ($ir->{"type"} == 1) 1 0 1
1183
1184 0 $$ir{error} = 'image type not recognized';
1185 } # end &int_readpixels
1186  
1187 # Internal argument check for encodepixels() and inspectpixels()
1188 36 ▶sub int_prelim_inspect {
Subroutine int_prelim_inspect: called CC=21 SCAR=31.8
Pod undocumented
1189 36 my $fmt = shift;
1190 36 my $max = shift;
1191 36 my $p_r = shift;
1192 36 my %inspect;
1193  
1194 36 $inspect{error} = '';
1195
1196 36 ▶ if($fmt =~ /^raw$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fmt =~ /^raw$/i) { } 18 18 36
elsif ($fmt =~ /^ascii$/i) { } 17 1 18
1197 18 $inspect{type} = 3; # will be modified later
1198 } elsif($fmt =~ /^ascii$/i) {
1199 17 $inspect{type} = 0; # will be modified later
1200 } else {
1201 1 $inspect{error} = 'invalid format';
1202 1 return \%inspect;
1203 }
1204  
1205 35 ▶ if(($max !~ /^\d+$/) or ($max < 1) or ($max > 65535)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $max =~ /^\d+$/ or $max < 1 or $max > 65535) 0 35 35
Condition33%2/6
not $max =~ /^\d+$/ or $max < 1 or $max > 65535
l!l&&r!l&&!r
0035
not $max =~ /^\d+$/ or $max < 1
l!l&&r!l&&!r
0035
MC/DC0%0/3
not $max =~ /^\d+$/ or $max < 1 or $max > 65535
not $max =~ /^\d+$/ $max < 1 $max > 65535
Truth table25%1/4
not(A) or B or C
A= not $max =~ /^\d+$/B= $max < 1C= $max > 65535
ABCresult
1XX1
01X1
0011
0000
1206 0 $inspect{error} = 'invalid max';
1207 0 return \%inspect;
1208 }
1209 35 ▶ if($max > 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max > 255) { } 11 24 35
1210 11 $inspect{bytes} = 2;
1211 } else {
1212 24 $inspect{bytes} = 1;
1213 }
1214  
1215 35 ▶ if( ref($p_r) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p_r ne "ARRAY") 0 35 35
1216 0 $inspect{error} = 'pixels not an array';
1217 0 return \%inspect;
1218 }
1219  
1220 35 ▶ if( ref($$p_r[0]) eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $p_r->[0] eq '') { } 8 27 35
elsif (ref $p_r->[0] eq 'ARRAY' and ref $p_r->[0][0] eq '') { } 21 6 27
elsif (ref $p_r->[0][0] eq 'ARRAY' and ref $p_r->[0][0][0] eq '') { } 6 0 6
1221 8 $inspect{deep} = '1d';
1222 8 $inspect{first} = $$p_r[0];
1223 8 $inspect{pixels} = 1+ $#{$p_r};
1224  
1225 ▶ } elsif(ref($$p_r[0]) eq 'ARRAY' and ref($$p_r[0][0]) eq '') {
Condition66%2/3
ref $p_r->[0] eq "ARRAY" and ref $p_r->[0][0] eq ""
!ll&&!rl&&r
0621
MC/DC50%1/2
ref $p_r->[0] eq "ARRAY" and ref $p_r->[0][0] eq ""
ref $p_r->[0] eq "ARRAY" ref $p_r->[0][0] eq ""
Truth table66%2/3
A and B
A= ref $p_r->[0] eq "ARRAY"B= ref $p_r->[0][0] eq ""
ABresult
0X0
100
111
1226 21 $inspect{deep} = '2d';
1227 21 $inspect{first} = $$p_r[0][0];
1228 21 $inspect{height} = 1+ $#{$p_r};
1229 21 $inspect{width} = 1+ $#{$$p_r[0]};
1230 21 $inspect{pixels} = $inspect{width} * $inspect{height};
1231  
1232 ▶ } elsif(ref($$p_r[0][0]) eq 'ARRAY' and ref($$p_r[0][0][0]) eq '') {
Condition33%1/3
ref $p_r->[0][0] eq "ARRAY" and ref $p_r->[0][0][0] eq ""
!ll&&!rl&&r
006
MC/DC0%0/2
ref $p_r->[0][0] eq "ARRAY" and ref $p_r->[0][0][0] eq ""
ref $p_r->[0][0] eq "ARRAY" ref $p_r->[0][0][0] eq ""
Truth table33%1/3
A and B
A= ref $p_r->[0][0] eq "ARRAY"B= ref $p_r->[0][0][0] eq ""
ABresult
0X0
100
111
1233 6 $inspect{deep} = '3d';
1234 6 $inspect{first} = $$p_r[0][0][0];
1235 6 $inspect{height} = 1+ $#{$p_r};
1236 6 $inspect{width} = 1+ $#{$$p_r[0]};
1237 6 $inspect{pixels} = $inspect{width} * $inspect{height};
1238  
1239 } else {
1240 # too many levels?
1241 0 $inspect{error} = 'pixels not expected structure';
1242 0 return \%inspect;
1243 }
1244  
1245 35 ▶ if(!defined($inspect{first})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $inspect{"first"}) 1 34 35
1246 1 $inspect{error} = 'first pixel undef';
1247 1 return \%inspect;
1248 }
1249 34 ▶ if($inspect{first} =~ m!^[.0-9]+,!) {
Branch: 4 branches
BranchTrueFalseTotal
if ($inspect{'first'} =~ /^[.0-9]+,/) { } 4 30 34
elsif ($inspect{'first'} =~ /^[0-9]+:/) { } 14 16 30
elsif ($inspect{'first'} =~ m[^[0-9a-fA-F]+/]) { } 8 8 16
elsif ($inspect{'first'} =~ /^[01]+$/) { } 8 0 8
1250 4 $inspect{encode} = 'float';
1251  
1252 } elsif($inspect{first} =~ m!^[0-9]+:!) {
1253 14 $inspect{encode} = 'dec';
1254  
1255 } elsif($inspect{first} =~ m!^[0-9a-fA-F]+/!) {
1256 8 $inspect{encode} = 'hex';
1257  
1258 } elsif($inspect{first} =~ m!^[01]+$!) {
1259 # for PBM
1260 8 $inspect{encode} = 'dec';
1261  
1262 } else {
1263 0 $inspect{error} = 'first pixel unrecognized';
1264 0 return \%inspect;
1265 }
1266  
1267 34 ▶ if($max == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($max == 1) { } 6 28 34
elsif ($inspect{'deep'} eq '3d') { } 6 22 28
1268 6 $inspect{type} += 1; # now either 1 or 4
1269  
1270 } elsif($inspect{deep} eq '3d') {
1271 6 $inspect{type} += 3; # now either 3 or 6
1272  
1273 } else {
1274 # still could be 2, 3, 5, 6
1275 22 ▶ if($inspect{first} =~ m!^[.0-9a-fA-F]+[,:/][.0-9a-fA-F]+[,:/][.0-9a-fA-F]+!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inspect{'first'} =~ m[^[.0-9a-fA-F]+[,:/][.0-9a-fA-F]+[,:/][.0-9a-fA-F]+]) { } 7 15 22
1276 7 $inspect{type} += 3; # now either 3 or 6
1277 } else {
1278 15 $inspect{type} += 2; # now either 2 or 5
1279 }
1280 }
1281  
1282 34 return \%inspect;
1283 } # end &int_prelim_inspect
1284  
1285  
1286 =head1 FUNCTIONS
1287  
1288 =head2 readpnmfile( \*PNM, \%info, \@pixels, $encoding );
1289  
1290 Reads from a file handle and sets hash %info with properties,
1291 puts pixels into @pixels, formated as "float", "dec", or "hex".
1292 The @pixels structure is an array of rows, each row being an
1293 array of pixel strings.
1294  
1295 The %info hash has numerous properties about the source file.
1296 The function itself returns 'error' for usage errors, and the
1297 empty string normally.
1298  
1299 This function essentially chains readpnmheader(),
1300 checkpnminfo(), and readpnmpixels().
1301  
1302 A single file, if in the RAW format, can contain multiple
1303 concatenated images. This function will only read one at a
1304 time, but can be called multiple times on the same file handle.
1305  
1306 =over
1307  
1308 =item *
1309  
1310 $info{bgp}
1311  
1312 Will contain one of "b", "g", or "p" for pbm (bitmap), pgm (graymap),
1313 or ppm (pixmap). This is an informational value not used by this library.
1314  
1315 =item *
1316  
1317 $info{type}
1318  
1319 Will contain one of "1" for ASCII PBM, "2" for ASCII PGM, "3" for
1320 ASCII PPM, "4" for raw PBM, "5" for raw PGM, or "6" for raw PPM.
1321 This numerical value is right out of the header of the PBM family
1322 of files and is essential to understanding the pixel format.
1323  
1324 =item *
1325  
1326 $info{max}
1327  
1328 Will contain the max value of the image as a decimal integer. This
1329 is needed to properly understand what a decimal or hexadecimal
1330 pixel value means. It is used to convert raw pixel data into
1331 floating point values (and back to integers).
1332  
1333 =item *
1334  
1335 $info{format}
1336  
1337 Will contain 'raw' or 'ascii'.
1338  
1339 =item *
1340  
1341 $info{raw}
1342  
1343 Will contain a true value if the file is raw encoded, and false
1344 for ASCII. This is an informational value not used by this library.
1345  
1346 =item *
1347  
1348 $info{height}
1349  
1350 Will contain the height of the image in pixels.
1351  
1352 =item *
1353  
1354 $info{width}
1355  
1356 Will contain the width of the image in pixels.
1357  
1358 =item *
1359  
1360 $info{pixels}
1361  
1362 Will contain the number of pixels (height * width).
1363  
1364 =item *
1365  
1366 $info{comments}
1367  
1368 Will contain any comments found in the header, concatenated.
1369  
1370 =item *
1371  
1372 $info{fullheader}
1373  
1374 Will contain the complete, unparsed, header.
1375  
1376 =item *
1377  
1378 $info{error}
1379  
1380 Will contain an empty string if no errors occured, or an error
1381 message, including usage errors.
1382  
1383 =back
1384  
1385 =cut
1386  
1387 # readpnmfile(\*PNM, \%imageinfo, \@pixels, 'float' );
1388 13 ▶sub readpnmfile {
Subroutine readpnmfile: called CC=9 SCAR=28.5
Pod documented
1389 13 my $f_r = shift; # file
1390 13 my $i_r = shift; # image info
1391 13 my $p_r = shift; # 2d array of pixels
1392 13 my $enc = shift; # encoding string
1393  
1394 13 ▶ if('HASH' ne ref($i_r)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" ne ref $i_r) 0 13 13
1395 # not a hash, can't return errors the normal way
1396 0 return 'error';
1397 }
1398  
1399 13 ▶ if('GLOB' ne ref($f_r)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("GLOB" ne ref $f_r) 0 13 13
1400 0 $$i_r{error} = 'readpnmfile: first arg not a file handle ref';
1401 0 return 'error';
1402 }
1403  
1404 13 ▶ if('ARRAY' ne ref($p_r)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" ne ref $p_r) 0 13 13
1405 0 $$i_r{error} = 'readpnmfile: third arg not an array ref';
1406 0 return 'error';
1407 }
1408  
1409 13 ▶ if($enc =~ /^(float|dec|raw)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc =~ /^(float|dec|raw)/i) { } 13 0 13
1410 13 $enc = lc($1);
1411 } else {
1412 0 $$i_r{error} = 'readpnmfile: fourth arg not recognized pixel encoding';
1413 0 return 'error';
1414 }
1415  
1416 13 int_readpnmheader($f_r, $i_r);
1417  
1418 13 ▶ if(length($$i_r{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $i_r->{"error"}) 0 13 13
1419 0 $$i_r{error} = 'readpnmfile: ' . $$i_r{error};
1420 0 return '';
1421 }
1422  
1423 13 checkpnminfo($i_r);
1424 13 ▶ if(exists($$i_r{error}) and length($$i_r{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $i_r->{"error"} and length $i_r->{"error"}) 0 13 13
Condition33%1/3
exists $i_r->{"error"} and length $i_r->{"error"}
!ll&&!rl&&r
0130
MC/DC0%0/2
exists $i_r->{"error"} and length $i_r->{"error"}
exists $i_r->{"error"} length $i_r->{"error"}
Truth table33%1/3
A and B
A= exists $i_r->{"error"}B= length $i_r->{"error"}
ABresult
0X0
100
111
1425 0 $$i_r{error} = 'readpnmfile: ' . $$i_r{error};
1426 0 return 'error';
1427 }
1428  
1429 13 int_readpixels($f_r, $i_r, $p_r, $enc);
1430 13 ▶ if(length($$i_r{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $i_r->{"error"}) 0 13 13
1431 0 $$i_r{error} = 'readpnmfile: ' . $$i_r{error};
1432 }
1433  
1434 13 return '';
1435 } # end &readpnmfile
1436  
1437  
1438 ##################################################################
1439  
1440  
1441 =head2 checkpnminfo( \%info )
1442  
1443 Checks the values in the image info hash for completeness. Used
1444 internally between reading the header and reading the pixels of
1445 an image, but might be useful generally. Expects to find numerical
1446 values for type, pixels, max, width, and height.
1447  
1448 =cut
1449  
1450 13 ▶sub checkpnminfo {
Subroutine checkpnminfo: called CC=11 SCAR=37.4
Pod documented
1451 13 my $i_r = shift; # image info
1452  
1453 13 ▶ if((!exists($$i_r{type}) or ($$i_r{type} !~ /^\d/)) or
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $i_r->{"type"} && $i_r->{"type"} =~ /^\d/ && (exists $i_r->{"pixels"} && $i_r->{"pixels"} =~ /^\d/) && (exists $i_r->{"max"} && $i_r->{"max"} =~ /^\d/) && (exists $i_r->{"width"} && $i_r->{"width"} =~ /^\d/) && (exists $i_r->{"height"} && $i_r->{"height"} =~ /^\d/)) 0 13 13
Condition33%9/27
exists $i_r->{'type'} && $i_r->{'type'} =~ /^\d/ && (exists $i_r->{'pixels'} && $i_r->{'pixels'} =~ /^\d/) && (exists $i_r->{'max'} && $i_r->{'max'} =~ /^\d/) && (exists $i_r->{'width'} && $i_r->{'width'} =~ /^\d/) && (exists $i_r->{'height'} && $i_r->{'height'} =~ /^\d/)
!ll&&!rl&&r
0013
exists $i_r->{"type"} and $i_r->{"type"} =~ /^\d/ and (exists $i_r->{"pixels"} and $i_r->{"pixels"} =~ /^\d/) and (exists $i_r->{"max"} and $i_r->{"max"} =~ /^\d/) and (exists $i_r->{"width"} and $i_r->{"width"} =~ /^\d/)
!ll&&!rl&&r
0013
exists $i_r->{"type"} and $i_r->{"type"} =~ /^\d/ and (exists $i_r->{"pixels"} and $i_r->{"pixels"} =~ /^\d/) and (exists $i_r->{"max"} and $i_r->{"max"} =~ /^\d/)
!ll&&!rl&&r
0013
exists $i_r->{"type"} and $i_r->{"type"} =~ /^\d/ and (exists $i_r->{"pixels"} and $i_r->{"pixels"} =~ /^\d/)
!ll&&!rl&&r
0013
exists $i_r->{"type"} and $i_r->{"type"} =~ /^\d/
!ll&&!rl&&r
0013
exists $i_r->{"pixels"} and $i_r->{"pixels"} =~ /^\d/
!ll&&!rl&&r
0013
exists $i_r->{"max"} and $i_r->{"max"} =~ /^\d/
!ll&&!rl&&r
0013
exists $i_r->{"width"} and $i_r->{"width"} =~ /^\d/
!ll&&!rl&&r
0013
exists $i_r->{"height"} and $i_r->{"height"} =~ /^\d/
!ll&&!rl&&r
0013
MC/DC0%0/10
exists $i_r->{'type'} && $i_r->{'type'} =~ /^\d/ && (exists $i_r->{'pixels'} && $i_r->{'pixels'} =~ /^\d/) && (exists $i_r->{'max'} && $i_r->{'max'} =~ /^\d/) && (exists $i_r->{'width'} && $i_r->{'width'} =~ /^\d/) && (exists $i_r->{'height'} && $i_r->{'height'} =~ /^\d/)
exists $i_r->{"type"} $i_r->{"type"} =~ /^\d/ exists $i_r->{"pixels"} $i_r->{"pixels"} =~ /^\d/ exists $i_r->{"max"} $i_r->{"max"} =~ /^\d/ exists $i_r->{"width"} $i_r->{"width"} =~ /^\d/ exists $i_r->{"height"} $i_r->{"height"} =~ /^\d/
Truth table9%1/11
A and B and C and D and E and F and G and H && I and J
A= exists $i_r->{"type"}B= $i_r->{"type"} =~ /^\d/C= exists $i_r->{"pixels"}D= $i_r->{"pixels"} =~ /^\d/E= exists $i_r->{"max"}F= $i_r->{"max"} =~ /^\d/G= exists $i_r->{"width"}H= $i_r->{"width"} =~ /^\d/I= exists $i_r->{"height"}J= $i_r->{"height"} =~ /^\d/
ABCDEFGHIJresult
0XXXXXXXXX0
10XXXXXXXX0
110XXXXXXX0
1110XXXXXX0
11110XXXXX0
111110XXXX0
1111110XXX0
11111110XX0
111111110X0
11111111100
11111111111
1454 (!exists($$i_r{pixels}) or ($$i_r{pixels} !~ /^\d/)) or
1455 (!exists($$i_r{max}) or ($$i_r{max} !~ /^\d/)) or
1456 (!exists($$i_r{width}) or ($$i_r{width} !~ /^\d/)) or
1457 (!exists($$i_r{height}) or ($$i_r{height} !~ /^\d/)) ) {
1458 0 $$i_r{error} = 'image info incomplete';
1459 0 return 'error';
1460 }
1461 } # end &checkheader
1462  
1463  
1464  
1465 ##################################################################
1466  
1467  
1468  
1469 =head2 readpnminfo( \*PNM, \%info )
1470  
1471 Reads just the header of a PBM/PGM/PPM file from the file handle
1472 and populates the image info hash. See C<readpnmfile> for a
1473 description of the image info hash. Returns the string 'error'
1474 if there is an problem, and the empty string otherwise. Sets
1475 the $info{error} value with an error string.
1476  
1477 =cut
1478  
1479 0 ▶sub readpnmheader {
Subroutine readpnmheader: not called CC=6 SCAR=37.4
Pod undocumented
1480 0 my $f_r = shift; # file
1481 0 my $i_r = shift; # image info
1482  
1483 0 ▶ if('HASH' ne ref($i_r)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" ne ref $i_r) 0 0 0
1484 # not a hash, can't return errors the normal way
1485 0 return 'error';
1486 }
1487  
1488 0 ▶ if('GLOB' ne ref($f_r)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("GLOB" ne ref $f_r) 0 0 0
1489 0 $$i_r{error} = 'readpnmfile: first arg not a file handle ref';
1490 0 return 'error';
1491 }
1492  
1493 0 int_readpnmheader($f_r, $i_r);
1494  
1495 0 ▶ if(length($$i_r{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $i_r->{"error"}) 0 0 0
1496 0 $$i_r{error} = 'readpnmheader: ' . $$i_r{error};
1497 0 return '';
1498 }
1499  
1500 0 checkpnminfo($i_r);
1501 0 ▶ if(exists($$i_r{error}) and length($$i_r{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $i_r->{"error"} and length $i_r->{"error"}) 0 0 0
Condition0%0/3
exists $i_r->{"error"} and length $i_r->{"error"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $i_r->{"error"} and length $i_r->{"error"}
exists $i_r->{"error"} length $i_r->{"error"}
Truth table0%0/3
A and B
A= exists $i_r->{"error"}B= length $i_r->{"error"}
ABresult
0X0
100
111
1502 0 $$i_r{error} = 'readpnmheader: ' . $$i_r{error};
1503 0 return 'error';
1504 }
1505  
1506 0 return '';
1507 } # end &readpnmheader
1508  
1509  
1510  
1511 ##################################################################
1512  
1513  
1514 =head2 readpnmpixels( \*PNM, \%info, \@pixels, $encoding )
1515  
1516 Reads just the pixels of a PBM/PGM/PPM file from the file handle
1517 and populates the pixels array. See C<readpnmfile> for a
1518 description of the image info hash, pixel array output format,
1519 and encoding details. Returns 'error' if there is an problem, and
1520 the empty string otherwise. Sets the $info{error} value with an
1521 error string.
1522  
1523 =cut
1524  
1525 0 ▶sub readpnmpixels {
Subroutine readpnmpixels: not called CC=9 SCAR=45.0
Pod documented
1526 0 my $g_r = shift; # input file glob ref
1527 0 my $i_r = shift; # image info hash ref
1528 0 my $p_r = shift; # pixel array ref
1529 0 my $enc = shift; # target pixel encoding
1530  
1531 0 ▶ if('HASH' ne ref($i_r)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" ne ref $i_r) 0 0 0
1532 # not a hash, can't return errors the normal way
1533 0 return 'error';
1534 }
1535  
1536 0 ▶ if('GLOB' ne ref($g_r)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("GLOB" ne ref $g_r) 0 0 0
1537 0 $$i_r{error} = 'readpnmpixels: first arg not a file handle ref';
1538 0 return 'error';
1539 }
1540  
1541 0 ▶ if('ARRAY' ne ref($p_r)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" ne ref $p_r) 0 0 0
1542 0 $$i_r{error} = 'readpnmpixels: third arg not an array ref';
1543 0 return 'error';
1544 }
1545  
1546 0 ▶ if($enc =~ /^(float|dec|raw)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc =~ /^(float|dec|raw)/i) { } 0 0 0
1547 0 $enc = lc($1);
1548 } else {
1549 0 $$i_r{error} = 'readpnmpixels: fourth arg not recognized pixel encoding';
1550 0 return 'error';
1551 }
1552  
1553 0 checkpnminfo($i_r);
1554 0 ▶ if(exists($$i_r{error}) and length($$i_r{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $i_r->{"error"} and length $i_r->{"error"}) 0 0 0
Condition0%0/3
exists $i_r->{"error"} and length $i_r->{"error"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $i_r->{"error"} and length $i_r->{"error"}
exists $i_r->{"error"} length $i_r->{"error"}
Truth table0%0/3
A and B
A= exists $i_r->{"error"}B= length $i_r->{"error"}
ABresult
0X0
100
111
1555 0 $$i_r{error} = 'readpnmpixels: ' . $$i_r{error};
1556 0 return 'error';
1557 }
1558  
1559 0 int_readpixels($g_r,$i_r,$p_r,$enc);
1560 0 ▶ if(exists($$i_r{error}) and length($$i_r{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $i_r->{"error"} and length $i_r->{"error"}) 0 0 0
Condition0%0/3
exists $i_r->{"error"} and length $i_r->{"error"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $i_r->{"error"} and length $i_r->{"error"}
exists $i_r->{"error"} length $i_r->{"error"}
Truth table0%0/3
A and B
A= exists $i_r->{"error"}B= length $i_r->{"error"}
ABresult
0X0
100
111
1561 0 $$i_r{error} = 'readpnmpixels: ' . $$i_r{error};
1562 0 return 'error';
1563 }
1564  
1565 0 return '';
1566 } # end &readpnmpixels
1567  
1568  
1569  
1570 ##################################################################
1571  
1572  
1573 =head2 $float_pixel = hextripletofloat( $hex_pixel, $max )
1574  
1575 =head2 $float_pixel = hextripletofloat( \@hex_pixel, $max )
1576  
1577 For a pixel string with hex red green and blue values separated by
1578 slashes (R/G/B to RRRR/GGGG/BBBB) or an array of hex values, and a
1579 of max 1 to 65535, convert to the comma separated floating point
1580 pixel format.
1581  
1582 No error is returned if $max is outside of the allowed range, but 0
1583 will kill the program. Any value larger than max is clipped.
1584  
1585 C<$hex_pixel> can be a scalar or an array ref (eg C<\@triple>) and
1586 C<$float_pixel> can be a scalar or an array (eg C<@triple>).
1587  
1588 Returns undef if $hex_pixel is malformed.
1589  
1590 =cut
1591  
1592 4 ▶sub hextripletofloat {
Subroutine hextripletofloat: called CC=6 SCAR=17.9
Pod documented
1593 4 my $trip = shift;
1594 4 my $max = shift;
1595 4 my $rgb = undef;
1596 4 my @val;
1597  
1598 4 ▶ if(wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 2 2 4
1599 2 my @set;
1600  
1601 2 ▶ if(ref($trip) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $trip eq 'ARRAY') { } 1 1 2
elsif ($trip =~ m[^([0-9a-fA-F]+)/([0-9a-fA-F]+)/([0-9a-fA-F]+)/?$]) { } 1 0 1
1602 1 @val = ( $$trip[0], $$trip[1], $$trip[2]);
1603 1 map { s:/$:: } @val;
1604  
1605 } elsif($trip =~ m:^([0-9a-fA-F]+)/([0-9a-fA-F]+)/([0-9a-fA-F]+)/?$:) {
1606 1 @val = ( $1, $2, $3 );
1607 }
1608  
1609 2 @set = ( int_decvaltofloat(hex($val[0]), $max),
1610 int_decvaltofloat(hex($val[1]), $max),
1611 int_decvaltofloat(hex($val[2]), $max) );
1612 2 return @set;
1613 }
1614  
1615 2 ▶ if(ref($trip) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $trip eq 'ARRAY') { } 1 1 2
elsif ($trip =~ m[^([0-9a-fA-F]+)/([0-9a-fA-F]+)/([0-9a-fA-F]+)/?$]) { } 1 0 1
1616 1 @val = ( $$trip[0], $$trip[1], $$trip[2]);
1617 1 map { s:/$:: } @val;
1618 1 $rgb = int_dectripletofloat(hex($val[0]),
1619 hex($val[1]),
1620 hex($val[2]), $max)
1621 } elsif($trip =~ m:^([0-9a-fA-F]+)/([0-9a-fA-F]+)/([0-9a-fA-F]+)/?$:) {
1622 1 $rgb = int_dectripletofloat(hex($1), hex($2), hex($3), $max);
1623 }
1624 2 return $rgb;
1625 } # end hextripletofloat
1626  
1627  
1628  
1629 ##################################################################
1630  
1631  
1632 =head2 $float_pixel = dectripletofloat( $dec_pixel, $max )
1633  
1634 =head2 $float_pixel = dectripletofloat( \@dec_pixel, $max )
1635  
1636 For a pixel string with decimal red green and blue values separated by
1637 colons (eg R:G:B), or an array of decimal values, and a max of 1 to 65535,
1638 convert to the comma separated floating point pixel format.
1639  
1640 No error is returned if $max is outside of the allowed range, but 0 will
1641 kill the program. Any value larger than max is clipped.
1642  
1643 C<$dec_pixel> can be a scalar or an array ref (eg C<\@triple>) and
1644 C<$float_pixel> can be a scalar or an array (eg C<@triple>).
1645  
1646 Returns undef if $dec_pixel is malformed.
1647  
1648 =cut
1649  
1650 # R:G:B, max 1 to 65535
1651 4 ▶sub dectripletofloat {
Subroutine dectripletofloat: called CC=6 SCAR=18.0
Pod documented
1652 4 my $trip = shift;
1653 4 my $max = shift;
1654 4 my $rgb = undef;
1655  
1656 4 ▶ if(wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 2 2 4
1657 2 my @set;
1658  
1659 2 ▶ if(ref($trip) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $trip eq 'ARRAY') { } 1 1 2
elsif ($trip =~ /^(\d+):(\d+):(\d+):?$/) { } 1 0 1
1660 1 @set = ( int_decvaltofloat($$trip[0], $max),
1661 int_decvaltofloat($$trip[1], $max),
1662 int_decvaltofloat($$trip[2], $max) );
1663  
1664 } elsif($trip =~ m/^(\d+):(\d+):(\d+):?$/) {
1665 1 @set = ( int_decvaltofloat($1, $max),
1666 int_decvaltofloat($2, $max),
1667 int_decvaltofloat($3, $max) );
1668 }
1669 2 return @set;
1670 }
1671  
1672 2 ▶ if(ref($trip) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $trip eq 'ARRAY') { } 1 1 2
elsif ($trip =~ /^(\d+):(\d+):(\d+):?$/) { } 1 0 1
1673 1 $rgb = int_dectripletofloat($$trip[0],
1674 $$trip[1],
1675 $$trip[2], $max);
1676 } elsif($trip =~ m/^(\d+):(\d+):(\d+):?$/) {
1677 1 $rgb = int_dectripletofloat($1, $2, $3, $max);
1678 }
1679 2 return $rgb;
1680 }
1681  
1682  
1683  
1684 ##################################################################
1685  
1686  
1687 =head2 $float_pixel = hexvaltofloat( $hex_val, $max )
1688  
1689 For a pixel value in hexadecimal and a max of 1 to 65535,
1690 convert to the comma separated floating point pixel value format.
1691  
1692 No error is returned if $max is outside of the allowed range, but 0 will
1693 kill the program. Any value larger than max is clipped.
1694  
1695 Returns undef if $hex_pixel is malformed.
1696  
1697 =cut
1698  
1699 2 ▶sub hexvaltofloat {
Subroutine hexvaltofloat: called CC=2 SCAR=7.0
Pod documented
1700 2 my $val = shift;
1701 2 my $max = shift;
1702 2 my $fl = undef;
1703  
1704 # allow trailing slash, since we use them
1705 2 ▶ if($val =~ m:^([a-fA-F0-9]+)/?$:) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ m[^([a-fA-F0-9]+)/?$]) 2 0 2
1706 2 $fl = int_decvaltofloat(hex($1), $max);
1707 }
1708  
1709 2 return $fl;
1710 } # end &hexvaltofloat
1711  
1712  
1713  
1714 ##################################################################
1715  
1716  
1717 =head2 $float_pixel = decvaltofloat( $dec_val, $max )
1718  
1719 For a pixel value in decimal and a max of 1 to 65535,
1720 convert to the comma separated floating point pixel value format.
1721  
1722 No error is returned if $max is outside of the allowed range, but 0 will
1723 kill the program. Any value larger than max is clipped.
1724  
1725 Returns undef if $dec_pixel is malformed.
1726  
1727 =cut
1728  
1729 2 ▶sub decvaltofloat {
Subroutine decvaltofloat: called CC=2 SCAR=7.0
Pod documented
1730 2 my $val = shift;
1731 2 my $max = shift;
1732 2 my $fl = undef;
1733  
1734 # allow trailing colon, since we use them
1735 2 ▶ if($val =~ /^(\d+):?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(\d+):?$/) 2 0 2
1736 2 $fl = int_decvaltofloat($1, $max);
1737 }
1738  
1739 2 return $fl;
1740 } # end &decvaltofloat
1741  
1742  
1743  
1744 ##################################################################
1745  
1746  
1747 =head2 $dec_pixel = floattripletodec( \@float_pixel, $max )
1748  
1749 =head2 $dec_pixel = floattripletodec( $float_pixel, $max )
1750  
1751 For a pixel string with floating red green and blue values separated by
1752 commas (eg R:G:B), and max 1 to 65535, convert to the colon separated
1753 decimal pixel format. No error is returned
1754 if $max is outside of the allowed range, but 0 will kill the program.
1755 Any value larger than max is clipped.
1756  
1757 C<$float_pixel> can be a scalar or an array ref (eg C<\@triple>) and
1758 C<$dec_pixel> can be a scalar or an array (eg C<@triple>).
1759  
1760 Returns undef if $float_pixel is malformed.
1761  
1762 =cut
1763  
1764 0 ▶sub floattripletodec {
Subroutine floattripletodec: not called CC=6 SCAR=37.4
Pod documented
1765 0 my $trip = shift;
1766 0 my $max = shift;
1767 0 my $rgb = undef;
1768  
1769 0 ▶ if(wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
1770 0 my @set;
1771  
1772 0 ▶ if(ref($trip) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $trip eq 'ARRAY') { } 0 0 0
elsif ($trip =~ /^([.\d+]),([.\d+]),([.\d+]),?$/) { } 0 0 0
1773 0 @set = ( int_floatvaltodec($$trip[0], $max),
1774 int_floatvaltodec($$trip[1], $max),
1775 int_floatvaltodec($$trip[2], $max) );
1776  
1777 } elsif($trip =~ m/^([.\d+]),([.\d+]),([.\d+]),?$/) {
1778 0 @set = ( int_floatvaltodec($1, $max),
1779 int_floatvaltodec($2, $max),
1780 int_floatvaltodec($3, $max) );
1781 }
1782 0 return @set;
1783 }
1784  
1785 0 ▶ if(ref($trip) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $trip eq 'ARRAY') { } 0 0 0
elsif ($trip =~ /^([.\d+]),([.\d+]),([.\d+]),?$/) { } 0 0 0
1786 0 $rgb = int_floattripletodec($$trip[0],
1787 $$trip[1],
1788 $$trip[2], $max);
1789 } elsif($trip =~ m/^([.\d+]),([.\d+]),([.\d+]),?$/) {
1790 0 $rgb = int_floattripletodec($1, $2, $3, $max);
1791 }
1792 0 return $rgb;
1793  
1794 } # end &floattripletodec
1795  
1796  
1797  
1798 ##################################################################
1799  
1800  
1801 =head2 $hex_pixel = floattripletohex( \@float_pixel, $max )
1802  
1803 =head2 $hex_pixel = floattripletohex( $float_pixel, $max )
1804  
1805 For a pixel string with floating red green and blue values separated by
1806 commas (eg R:G:B), and max 1 to 65535, convert to the slash separated
1807 hex pixel format. No error is returned
1808 if $max is outside of the allowed range, but 0 will kill the program.
1809 Any value larger than max is clipped.
1810  
1811 C<$float_pixel> can be a scalar or an array ref (eg C<\@triple>) and
1812 C<$hex_pixel> can be a scalar or an array (eg C<@triple>).
1813  
1814 Returns undef if $float_pixel is malformed.
1815  
1816 =cut
1817  
1818 0 ▶sub floattripletohex {
Subroutine floattripletohex: not called CC=6 SCAR=37.4
Pod documented
1819 0 my $trip = shift;
1820 0 my $max = shift;
1821 0 my $rgb = undef;
1822  
1823 0 ▶ if(wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
1824 0 my @set;
1825  
1826 0 ▶ if(ref($trip) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $trip eq 'ARRAY') { } 0 0 0
elsif ($trip =~ /^([.\d+]),([.\d+]),([.\d+]),?$/) { } 0 0 0
1827 0 @set = ( int_floatvaltohex($$trip[0], $max),
1828 int_floatvaltohex($$trip[1], $max),
1829 int_floatvaltohex($$trip[2], $max) );
1830  
1831 } elsif($trip =~ m/^([.\d+]),([.\d+]),([.\d+]),?$/) {
1832 0 @set = ( int_floatvaltohex($1, $max),
1833 int_floatvaltohex($2, $max),
1834 int_floatvaltohex($3, $max) );
1835 }
1836 0 return @set;
1837 }
1838  
1839 0 ▶ if(ref($trip) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $trip eq 'ARRAY') { } 0 0 0
elsif ($trip =~ /^([.\d+]),([.\d+]),([.\d+]),?$/) { } 0 0 0
1840 0 $rgb = int_floattripletohex($$trip[0],
1841 $$trip[1],
1842 $$trip[2], $max);
1843 } elsif($trip =~ m/^([.\d+]),([.\d+]),([.\d+]),?$/) {
1844 0 $rgb = int_floattripletohex($1, $2, $3, $max);
1845 }
1846 0 return $rgb;
1847  
1848 } # end &floattripletodec
1849  
1850  
1851  
1852 ##################################################################
1853  
1854  
1855 =head2 $dec_pixel = floatvaltodec( $float_pixel, $max )
1856  
1857 For a floating point pixel value and max 1 to 65535, convert to the decimal
1858 pixel format. No error is returned
1859 if $max is outside of the allowed range, but 0 will kill the program.
1860 Any value larger than max is clipped.
1861  
1862 Returns undef if $float_pixel is malformed.
1863  
1864 =cut
1865  
1866 3 ▶sub floatvaltodec {
Subroutine floatvaltodec: called CC=1 SCAR=0.0
Pod documented
1867 3 my $trip = shift;
1868 3 my $max = shift;
1869 3 my $p = undef;
1870  
1871 3 $p = int_floatvaltodec($trip, $max);
1872
1873 3 return $p;
1874  
1875 } # end &floatvaltodec
1876  
1877  
1878  
1879 ##################################################################
1880  
1881  
1882 =head2 $hex_pixel = floatvaltohex( $float_pixel, $max )
1883  
1884 For a floating point pixel value and max 1 to 65535, convert to the hexadecimal
1885 pixel format. No error is returned
1886 if $max is outside of the allowed range, but 0 will kill the program.
1887 Any value larger than max is clipped.
1888  
1889 Returns undef if $float_pixel is malformed.
1890  
1891 =cut
1892  
1893 3 ▶sub floatvaltohex {
Subroutine floatvaltohex: called CC=1 SCAR=0.0
Pod documented
1894 3 my $trip = shift;
1895 3 my $max = shift;
1896 3 my $p = undef;
1897  
1898 3 $p = int_floatvaltohex($trip, $max);
1899
1900 3 return $p;
1901  
1902 } # end &floatvaltohex
1903  
1904  
1905  
1906 ##################################################################
1907  
1908  
1909 =head2 $status = comparefloattriple(\@a, \@b)
1910  
1911 =head2 $status = comparefloattriple($a, $b)
1912  
1913 Returns -1, 0, or 1 much like <=>, but allows a variance of up
1914 to half 1/65535. Checks only a single pair at a time (red value
1915 of $a to red value of $b, etc) and stops at the first obvious
1916 non-equal value. Does not check if any value is outside of 0.0
1917 to 1.0. Returns undef if either triple can't be understood.
1918  
1919 =cut
1920  
1921 4 ▶sub comparefloattriple {
Subroutine comparefloattriple: called CC=5 SCAR=16.5
Pod documented
1922 4 my $a = shift;
1923 4 my $b = shift;
1924 4 my $v;
1925  
1926 4 my $a_r; my $a_g; my $a_b;
1927 4 my $b_r; my $b_g; my $b_b;
1928  
1929 4 ($a_r, $a_g, $a_b) = explodetriple($a);
1930 4 ($b_r, $b_g, $b_b) = explodetriple($b);
1931  
1932 0 ▶ if(!defined($a_r) or !defined($b_r)) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a_r and defined $b_r) 0 4 4
Condition33%1/3
defined $a_r and defined $b_r
!ll&&!rl&&r
004
MC/DC0%0/2
defined $a_r and defined $b_r
defined $a_r defined $b_r
Truth table33%1/3
A and B
A= defined $a_rB= defined $b_r
ABresult
0X0
100
111
1933  
1934 4 $v = comparefloatval($a_r, $b_r);
1935 0 ▶ if($v) { return $v; }
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 4 4
1936  
1937 4 $v = comparefloatval($a_g, $b_g);
1938 4 ▶ if($v) { return $v; }
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 1 3 4
1939  
1940 3 $v = comparefloatval($a_b, $b_b);
1941 3 return $v;
1942 } # end &comparefloattriple
1943  
1944  
1945  
1946 ##################################################################
1947  
1948  
1949 =head2 $status = comparefloatval($a, $b)
1950  
1951 Returns -1, 0, or 1 much like <=>, but allows a variance of up
1952 to half 1/65535. Checks only a single pair (not an RGB triple),
1953 does not check if either value is outside of 0.0 to 1.0.
1954  
1955 =cut
1956  
1957 15 ▶sub comparefloatval {
Subroutine comparefloatval: called CC=3 SCAR=11.0
Pod documented
1958 15 my $a = shift;
1959 15 my $b = shift;
1960 # 1/65535 ~ .0000152590; .0000152590 / 2 = .0000076295
1961 15 my $alpha = 0.0000076295;
1962  
1963 # eat our own dog food for indicating a float value
1964 15 $a =~ s/,$//;
1965 15 $b =~ s/,$//;
1966  
1967 15 my $low_a = $a - $alpha;
1968 15 my $hi_a = $a + $alpha;
1969  
1970 15 ▶ if($low_a > $b) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($low_a > $b) 2 13 15
1971 13 ▶ if($hi_a < $b) { return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($hi_a < $b) 2 11 13
1972  
1973 11 return 0;
1974 } # end &comparefloatval
1975  
1976  
1977 ##################################################################
1978  
1979 =head2 $status = comparepixelval($a, $max_a, $b, $max_b)
1980  
1981 Returns -1, 0, or 1 much like <=>, taking into account that
1982 each is really a fraction: C<$v / $max_v>. Decimal values should
1983 have a colon (eg "123:"), while hex values should have a slash
1984 (eg "7B/"). Uses integer comparisions and should not be used with
1985 floating point values. Max should always be a regular decimal integer.
1986 Checks only a single pair (not an RGB triple),
1987 does not enforce checks on the max values.
1988  
1989 This is a less forgiving comparison than C<comparefloatval()>.
1990  
1991 =cut
1992  
1993 4 ▶sub comparepixelval {
Subroutine comparepixelval: called CC=4 SCAR=13.9
Pod documented
1994 4 my $a = shift;
1995 4 my $a_m = shift;
1996 4 my $b = shift;
1997 4 my $b_m = shift;
1998  
1999 # eat our own dog food for indicating a dec / hex value
2000 4 ▶ if($a =~ s:/$::) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ s[/$][]) { } 2 2 4
2001 2 $a = hex($a);
2002 } else {
2003 2 $a =~ s/:$//;
2004 }
2005 4 ▶ if($b =~ s:/$::) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b =~ s[/$][]) { } 2 2 4
2006 2 $b = hex($b);
2007 } else {
2008 2 $b =~ s/:$//;
2009 }
2010  
2011 4 ▶ if($a_m == $b_m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a_m == $b_m) 2 2 4
2012 2 return ($a <=> $b);
2013 }
2014  
2015 # simple way to get to common denominator
2016 2 $a = $a * $b_m;
2017 2 $b = $b * $a_m;
2018  
2019 2 return ($a <=> $b);
2020 } # end &comparepixelval
2021  
2022  
2023 ##################################################################
2024  
2025 =head2 $status = comparepixeltriple(\@a, $max_a, \@b, $max_b)
2026  
2027 =head2 $status = comparepixeltriple($a, $max_a, $b, $max_b)
2028  
2029 Returns -1, 0, or 1 much like <=>, taking into account that
2030 RGB each is really a fraction: C<$v / $max_v>. Decimal values should
2031 be colon separated (eg "123:1:1024" or terminated ["123:", "1:", "1024:"]),
2032 while hex values should have slashes
2033 (eg "7B/1/400" or ["7B/", "1/", "400/"]). Uses integer comparisions and
2034 should not be used with floating point values. Max should always be a
2035 regular decimal integer. Checks only a single pair at a time (red value
2036 of $a to red value of $b, etc) and stops at the first obvious
2037 non-equal value. Does not enforce checks on the max values.
2038 Returns undef if either triple can't be understood.
2039  
2040 This is a less forgiving comparison than C<comparefloattriple()>.
2041  
2042 =cut
2043  
2044 4 ▶sub comparepixeltriple {
Subroutine comparepixeltriple: called CC=14 SCAR=26.7
Pod documented
2045 4 my $a = shift;
2046 4 my $a_m = shift;
2047 4 my $b = shift;
2048 4 my $b_m = shift;
2049 4 my $v;
2050  
2051 4 my $a_r; my $a_g; my $a_b;
2052 4 my $b_r; my $b_g; my $b_b;
2053  
2054 4 ($a_r, $a_g, $a_b) = explodetriple($a);
2055 4 ($b_r, $b_g, $b_b) = explodetriple($b);
2056  
2057 0 ▶ if(!defined($a_r) or !defined($b_r)) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a_r and defined $b_r) 0 4 4
Condition33%1/3
defined $a_r and defined $b_r
!ll&&!rl&&r
004
MC/DC0%0/2
defined $a_r and defined $b_r
defined $a_r defined $b_r
Truth table33%1/3
A and B
A= defined $a_rB= defined $b_r
ABresult
0X0
100
111
2058  
2059 # eat our own dog food for indicating a dec / hex value
2060 4 ▶ if($a_r =~ s:/$::) { $a_r = hex($a_r); } else { $a_r =~ s/:$//; }
Branch: 1 branch
BranchTrueFalseTotal
if ($a_r =~ s[/$][]) { } 2 2 4
2061 4 ▶ if($a_g =~ s:/$::) { $a_g = hex($a_g); } else { $a_g =~ s/:$//; }
Branch: 1 branch
BranchTrueFalseTotal
if ($a_g =~ s[/$][]) { } 2 2 4
2062 4 ▶ if($a_b =~ s:/$::) { $a_b = hex($a_b); } else { $a_b =~ s/:$//; }
Branch: 1 branch
BranchTrueFalseTotal
if ($a_b =~ s[/$][]) { } 2 2 4
2063 4 ▶ if($b_r =~ s:/$::) { $b_r = hex($b_r); } else { $b_r =~ s/:$//; }
Branch: 1 branch
BranchTrueFalseTotal
if ($b_r =~ s[/$][]) { } 2 2 4
2064 4 ▶ if($b_g =~ s:/$::) { $b_g = hex($b_g); } else { $b_g =~ s/:$//; }
Branch: 1 branch
BranchTrueFalseTotal
if ($b_g =~ s[/$][]) { } 2 2 4
2065 4 ▶ if($b_b =~ s:/$::) { $b_b = hex($b_b); } else { $b_b =~ s/:$//; }
Branch: 1 branch
BranchTrueFalseTotal
if ($b_b =~ s[/$][]) { } 2 2 4
2066  
2067 4 ▶ if($a_m == $b_m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a_m == $b_m) 2 2 4
2068 2 ▶ return (($a_r <=> $b_r) or ($a_g <=> $b_g) or ($a_b <=> $b_b));
Condition66%4/6
$a_r <=> $b_r || $a_g <=> $b_g || $a_b <=> $b_b
l!l&&r!l&&!r
110
$a_r <=> $b_r or $a_g <=> $b_g
l!l&&r!l&&!r
101
MC/DC0%0/3
$a_r <=> $b_r || $a_g <=> $b_g || $a_b <=> $b_b
$a_r <=> $b_r $a_g <=> $b_g $a_b <=> $b_b
Truth table50%2/4
A or B || C
A= $a_r <=> $b_rB= $a_g <=> $b_gC= $a_b <=> $b_b
ABCresult
1XX1
01X1
0011
0000
2069 }
2070  
2071 # simple way to get to common denominator
2072 2 $a_r = $a_r * $b_m;
2073 2 $b_r = $b_r * $a_m;
2074  
2075 2 $v = ($a_r <=> $b_r);
2076 0 ▶ if($v) { return $v; }
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 2 2
2077  
2078 2 $a_g = $a_g * $b_m;
2079 2 $b_g = $b_g * $a_m;
2080  
2081 2 $v = ($a_g <=> $b_g);
2082 0 ▶ if($v) { return $v; }
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 2 2
2083  
2084 2 $a_b = $a_b * $b_m;
2085 2 $b_b = $b_b * $a_m;
2086  
2087 2 return ($a_g <=> $b_g);
2088  
2089 } # end &comparepixeltriple
2090  
2091  
2092 ##################################################################
2093  
2094 =head2 ($r, $g, $b) = explodetriple( \@pixel );
2095  
2096 =head2 ($r, $g, $b) = explodetriple( $pixel );
2097  
2098 Helper function to separate the values of an RGB pixel, either in
2099 array or string format. Float pixels have comma separated triples,
2100 and comma suffixed single values. Decimal pixels use colons, and
2101 hex pixels use slashes. Does not enforce values to be within the
2102 allowed range.
2103  
2104 Returns undef if the pixel could not be understood.
2105  
2106 =cut
2107  
2108 158 ▶sub explodetriple {
Subroutine explodetriple: called CC=5 SCAR=16.1
Pod documented
2109 158 my $a = shift;
2110 158 my $a_r;
2111 my $a_g;
2112 158 my $a_b;
2113  
2114 158 ▶ if(ref($a) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq 'ARRAY') { } 49 109 158
2115 49 $a_r = $$a[0];
2116 49 $a_g = $$a[1];
2117 49 $a_b = $$a[2];
2118 } else {
2119 109 ▶ if($a =~ m/^(\d+):(\d+):(\d+):?$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($a =~ /^(\d+):(\d+):(\d+):?$/) { } 58 51 109
elsif ($a =~ m[^([0-9a-fA-F]+)/([0-9a-fA-F]+)/([0-9a-fA-F]+)/?$]) { } 27 24 51
elsif ($a =~ /^([.0-9]+),([.0-9]+),([.0-9]+),?$/) { } 24 0 24
2120 58 $a_r = $1 .':';
2121 58 $a_g = $2 .':';
2122 58 $a_b = $3 .':';
2123 } elsif ($a =~ m:^([0-9a-fA-F]+)/([0-9a-fA-F]+)/([0-9a-fA-F]+)/?$:) {
2124 27 $a_r = $1 .'/';
2125 27 $a_g = $2 .'/';
2126 27 $a_b = $3 .'/';
2127 } elsif ($a =~ m/^([.0-9]+),([.0-9]+),([.0-9]+),?$/) {
2128 24 $a_r = $1 .',';
2129 24 $a_g = $2 .',';
2130 24 $a_b = $3 .',';
2131 } else {
2132 0 return undef;
2133 }
2134 }
2135  
2136 158 return ($a_r, $a_g, $a_b);
2137  
2138 } # end &explodetriple
2139  
2140  
2141 ##################################################################
2142  
2143 =head2 @pixel = rescaletriple( \@pixel, $old_max, $new_max );
2144  
2145 =head2 $pixel = rescaletriple( $pixel, $old_max, $new_max );
2146  
2147 Helper function to rescale the values of an RGB pixel to a new max
2148 value, either in array or string format. Float pixels do not need
2149 rescaling. Decimal pixels use colons as separator / suffix, and
2150 hex pixels use slashes. Does not enforce values to be within the
2151 allowed range.
2152  
2153 Returns undef if the pixel could not be understood.
2154  
2155 =cut
2156  
2157 6 ▶sub rescaletriple {
Subroutine rescaletriple: called CC=6 SCAR=17.9
Pod documented
2158 6 my $p = shift;
2159 6 my $o_m = shift;
2160 6 my $n_m = shift;
2161 6 my $p_r;
2162 my $p_g;
2163 6 my $p_b;
2164 6 my $enc;
2165 6 my $r;
2166  
2167 6 ($p_r, $p_g, $p_b) = explodetriple($p);
2168  
2169 0 ▶ if(!defined($p_r)) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p_r) 0 6 6
2170  
2171 6 ▶ if($p_r =~ /:/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p_r =~ /:/) { } 3 3 6
elsif ($p_r =~ m[/]) { } 2 1 3
2172 3 $enc = 'dec';
2173 } elsif ($p_r =~ m:/:) {
2174 2 $enc = 'hex';
2175 }
2176  
2177 # undef if it was a float triple
2178 6 ▶ if(defined($enc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $enc) 5 1 6
2179 5 $p_r = rescaleval($p_r, $o_m, $n_m);
2180 5 $p_g = rescaleval($p_g, $o_m, $n_m);
2181 5 $p_b = rescaleval($p_b, $o_m, $n_m);
2182 }
2183  
2184 6 ▶ if(wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 2 4 6
2185 2 return ($p_r, $p_g, $p_b);
2186 } else {
2187 4 $r = "$p_r$p_g$p_b";
2188 4 chop $r;
2189 4 return $r;
2190 }
2191  
2192 } # end &rescaletriple
2193  
2194  
2195 ##################################################################
2196  
2197  
2198 =head2 $value = rescaleval( $value, $old_max, $new_max );
2199  
2200 Helper function to rescale a single value to a new max
2201 value, either in array or string format. Float values do not need
2202 rescaling. Decimal values use colons as suffix, and
2203 hex values use slashes. Does not enforce values to be within the
2204 allowed range.
2205  
2206 Returns undef if the value could not be understood.
2207  
2208 =cut
2209  
2210 19 ▶sub rescaleval {
Subroutine rescaleval: called CC=5 SCAR=16.1
Pod documented
2211 19 my $v = shift;
2212 19 my $o_m = shift;
2213 19 my $n_m = shift;
2214 19 my $r;
2215  
2216 19 ▶ if($o_m == $n_m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o_m == $n_m) 6 13 19
2217 # no change
2218 6 return $v;
2219 }
2220  
2221 13 ▶ if($v =~ /:$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($v =~ /:$/) { } 7 6 13
elsif ($v =~ m[/$]) { } 5 1 6
elsif ($v =~ /,$/) { } 1 0 1
2222 7 $v =~ s/:$//;
2223  
2224 7 $r = int_floatvaltodec( ($v / $o_m), $n_m);
2225 } elsif ($v =~ m:/$:) {
2226 5 $v =~ s:/$::; $v = hex($v);
2227  
2228 5 $r = int_floatvaltohex( ($v / $o_m), $n_m);
2229 } elsif ($v =~ m/,$/) {
2230 # no change
2231 1 return $v;
2232 } else {
2233 0 return undef;
2234 }
2235  
2236 12 return $r;
2237 } # end &rescaleval
2238  
2239  
2240  
2241 ##################################################################
2242  
2243  
2244 =head2 $header = makepnmheader( \%info );
2245  
2246 =head2 $header = makepnmheader($type, $width, $height, $max);
2247  
2248 Takes a hash reference similar to C<readpnmheader()> or
2249 C<readpnmfile> would return and makes a PBM, PGM, or PPM header string
2250 from it. C<makeppmheader> first looks for a B<type> in the hash and
2251 uses that, otherwise it expects B<bgp> and B<format> to be set in the hash
2252 (and it will set B<type> for you then). If there is a non-empty
2253 B<comments> in the hash, that will be put in as one or more lines
2254 of comments. There must be sizes for B<width> and B<height>, and if
2255 the image is not a bitmap, there should be one for B<max>. A missing
2256 B<max> will result in C<makeppmheader> guessing 255 and setting
2257 B<max> accordingly.
2258  
2259 The numerical types are 1 for ASCII PBM, 2 for ASCII PGM, 3 for
2260 ASCII PPM, 4 for raw PBM, 5 for raw PGM, and 6 for raw PPM. The
2261 maxvalue is ignored for PBM files.
2262  
2263 Returns the header string if successful.
2264 Returns undef if there is an error.
2265  
2266 =cut
2267  
2268 16 ▶sub makepnmheader {
Subroutine makepnmheader: called CC=34 SCAR=43.9
Pod documented
2269 16 my $type;
2270 my $w;
2271 16 my $h;
2272 16 my $max;
2273  
2274 16 my $hr = shift; # header hash ref
2275 16 my $head = '';
2276 16 my $com = '';
2277 16 my $setmax;
2278  
2279 16 ▶ if(ref($hr) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hr ne 'HASH') { } 2 14 16
2280 2 $type = $hr;
2281 2 $w = shift;
2282 2 $h = shift;
2283 2 $max = shift;
2284  
2285 2 ▶ if(!defined($type) or !defined($w) or !defined($h)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type and defined $w and defined $h) 0 2 2
Condition33%2/6
defined $type and defined $w and defined $h
!ll&&!rl&&r
002
defined $type and defined $w
!ll&&!rl&&r
002
MC/DC0%0/3
defined $type and defined $w and defined $h
defined $type defined $w defined $h
Truth table25%1/4
A and B and C
A= defined $typeB= defined $wC= defined $h
ABCresult
0XX0
10X0
1100
1111
2286 0 return undef;
2287 }
2288  
2289 2 ▶ if($type !~ /^[123456]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /^[123456]$/) 0 2 2
2290 0 return undef;
2291 }
2292 2 ▶ if($w !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($w =~ /^\d+$/) 0 2 2
2293 0 return undef;
2294 }
2295 2 ▶ if($h !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($h =~ /^\d+$/) 0 2 2
2296 0 return undef;
2297 }
2298  
2299 } else {
2300  
2301 14 ▶ if (defined($$hr{width}) and $$hr{width} =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hr->{'width'} and $hr->{'width'} =~ /^\d+$/) { } 14 0 14
Condition33%1/3
defined $hr->{"width"} and $hr->{"width"} =~ /^\d+$/
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $hr->{"width"} and $hr->{"width"} =~ /^\d+$/
defined $hr->{"width"} $hr->{"width"} =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $hr->{"width"}B= $hr->{"width"} =~ /^\d+$/
ABresult
0X0
100
111
2302 14 $w = $$hr{width};
2303 } else {
2304 0 return undef;
2305 }
2306  
2307 14 ▶ if (defined($$hr{height}) and $$hr{height} =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hr->{'height'} and $hr->{'height'} =~ /^\d+$/) { } 14 0 14
Condition33%1/3
defined $hr->{"height"} and $hr->{"height"} =~ /^\d+$/
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $hr->{"height"} and $hr->{"height"} =~ /^\d+$/
defined $hr->{"height"} $hr->{"height"} =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $hr->{"height"}B= $hr->{"height"} =~ /^\d+$/
ABresult
0X0
100
111
2308 14 $h = $$hr{height};
2309 } else {
2310 0 return undef;
2311 }
2312  
2313 14 ▶ if (defined($$hr{max}) and $$hr{max} =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hr->{'max'} and $hr->{'max'} =~ /^\d+$/) { } 12 2 14
Condition66%2/3
defined $hr->{"max"} and $hr->{"max"} =~ /^\d+$/
!ll&&!rl&&r
2012
MC/DC50%1/2
defined $hr->{"max"} and $hr->{"max"} =~ /^\d+$/
defined $hr->{"max"} $hr->{"max"} =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $hr->{"max"}B= $hr->{"max"} =~ /^\d+$/
ABresult
0X0
100
111
2314 12 $max = $$hr{max};
2315 } else {
2316 2 $max = 255;
2317 2 $setmax = 1;
2318 }
2319  
2320 14 ▶ if (defined($$hr{type}) and $$hr{type} =~ /^[123456]$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $hr->{'type'} and $hr->{'type'} =~ /^[123456]$/) { } 12 2 14
elsif (defined $hr->{'bgp'} and defined $hr->{'format'} and $hr->{'bgp'} =~ /^([bgp])$/i) { } 2 0 2
Condition66%2/3
defined $hr->{"type"} and $hr->{"type"} =~ /^[123456]$/
!ll&&!rl&&r
2012
MC/DC50%1/2
defined $hr->{"type"} and $hr->{"type"} =~ /^[123456]$/
defined $hr->{"type"} $hr->{"type"} =~ /^[123456]$/
Truth table66%2/3
A and B
A= defined $hr->{"type"}B= $hr->{"type"} =~ /^[123456]$/
ABresult
0X0
100
111
2321 12 $type = $$hr{type};
2322  
2323 ▶ } elsif(defined($$hr{bgp}) and defined($$hr{format}) and
Condition33%2/6
defined $hr->{"bgp"} and defined $hr->{"format"} and $hr->{"bgp"} =~ /^([bgp])$/i
!ll&&!rl&&r
002
defined $hr->{"bgp"} and defined $hr->{"format"}
!ll&&!rl&&r
002
MC/DC0%0/3
defined $hr->{"bgp"} and defined $hr->{"format"} and $hr->{"bgp"} =~ /^([bgp])$/i
defined $hr->{"bgp"} defined $hr->{"format"} $hr->{"bgp"} =~ /^([bgp])$/i
Truth table25%1/4
A and B and C
A= defined $hr->{"bgp"}B= defined $hr->{"format"}C= $hr->{"bgp"} =~ /^([bgp])$/i
ABCresult
0XX0
10X0
1100
1111
2324 $$hr{bgp} =~ /^([bgp])$/i) {
2325
2326 2 my $bgp = lc($1);
2327 2 ▶ if ($bgp eq 'b') {
Branch: 2 branches
BranchTrueFalseTotal
if ($bgp eq 'b') { } 0 2 2
elsif ($bgp eq 'g') { } 2 0 2
2328 0 $type = 1;
2329 } elsif ($bgp eq 'g') {
2330 2 $type = 2;
2331 } else {
2332 0 $type = 3;
2333 }
2334  
2335 2 ▶ if ($$hr{format} =~ /raw/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hr->{'format'} =~ /raw/i) { } 1 1 2
elsif (not $hr->{'format'} =~ /ascii/i) { } 0 1 1
2336 1 $type += 3;
2337 } elsif ($$hr{format} !~ /ascii/i) {
2338 0 return undef;
2339 }
2340  
2341 2 $$hr{type} = $type;
2342 } else {
2343 0 return undef;
2344 }
2345  
2346 14 ▶ if(defined($$hr{comments}) and length($$hr{comments})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hr->{"comments"} and length $hr->{"comments"}) 11 3 14
Condition66%2/3
defined $hr->{"comments"} and length $hr->{"comments"}
!ll&&!rl&&r
3011
MC/DC50%1/2
defined $hr->{"comments"} and length $hr->{"comments"}
defined $hr->{"comments"} length $hr->{"comments"}
Truth table66%2/3
A and B
A= defined $hr->{"comments"}B= length $hr->{"comments"}
ABresult
0X0
100
111
2347 11 $com = $$hr{comments};
2348 11 $com =~ s/^/#/gm;
2349 11 ▶ if(substr($com, -1, 1) ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($com, -1, 1) ne "\n") 11 0 11
2350 11 $com .= "\n";
2351 };
2352 }
2353  
2354 }
2355  
2356 16 ▶ if($w < 1 or $h < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w < 1 or $h < 1) 0 16 16
Condition33%1/3
$w < 1 or $h < 1
l!l&&r!l&&!r
0016
MC/DC0%0/2
$w < 1 or $h < 1
$w < 1 $h < 1
Truth table33%1/3
A or B
A= $w < 1B= $h < 1
ABresult
1X1
011
000
2357 0 return undef;
2358 }
2359  
2360 16 $head = "P$type\n$com";
2361 16 $head .= "$w $h\n";
2362  
2363 16 ▶ if($type != 1 and $type != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type != 1 and $type != 4) 14 2 16
Condition66%2/3
$type != 1 and $type != 4
!ll&&!rl&&r
2014
MC/DC50%1/2
$type != 1 and $type != 4
$type != 1 $type != 4
Truth table66%2/3
A and B
A= $type != 1B= $type != 4
ABresult
0X0
100
111
2364 14 ▶ if(!defined($max) or $max < 1 or $max > 65535) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $max or $max < 1 or $max > 65535) 0 14 14
Condition33%2/6
not defined $max or $max < 1 or $max > 65535
l!l&&r!l&&!r
0014
not defined $max or $max < 1
l!l&&r!l&&!r
0014
MC/DC0%0/3
not defined $max or $max < 1 or $max > 65535
not defined $max $max < 1 $max > 65535
Truth table25%1/4
not(A) or B or C
A= not defined $maxB= $max < 1C= $max > 65535
ABCresult
1XX1
01X1
0011
0000
2365 0 return undef;
2366 }
2367 14 $head .= "$max\n";
2368 14 ▶ if($setmax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setmax) 1 13 14
2369 1 $$hr{max} = $max;
2370 }
2371 }
2372  
2373 16 return $head;
2374 } # end &makepnmheader
2375  
2376 ##################################################################
2377  
2378  
2379 =head2 $block = encodepixels($format, $max, \@pixels);
2380  
2381 Encodes pixels into 'raw' or 'ascii' PBM/PGM/PPM format. The
2382 supplied pixels can be decimal, hex, or floating point values.
2383 Decimal and hex values greater than $max will be clipped to $max.
2384 A $max of 1 will encode a PBM file, otherwise the first pixel
2385 will be examined to determine if it is PGM or PPM data.
2386  
2387 The array of pixels can be one, two, or three dimensional. A
2388 two dimensional array is prefered and will be considered to
2389 be same format C<readpnmfile()> and C<readpnmpixels()> uses.
2390 There, the @pixels structure is an array of rows, each row
2391 being an array of pixel strings. This function will expect
2392 every row to have the same number of pixels as the first. If
2393 subsequent rows have different amounts, the results can be
2394 unpredictable. Missing values will be assumed to be 0 if it
2395 it tries to read past the end of the array.
2396  
2397 A three dimensional @pixels structure is considered to be an
2398 array of rows, each row being an array of PPM pixel values.
2399  
2400 A one dimensional @pixels structure is an array of pixel strings
2401 with no hint of row and column structure.
2402 With a one dimensional array, raw PBM files will be
2403 misencoded if number of columns is not a multiple of 8 and the data
2404 represents more than one row: each row is supposed to be padded to
2405 a multiple of 8 bits.
2406  
2407 Returns undef if $encoding is not recognized, $max is out of bounds
2408 (1 to 65535, inclusive), or @pixels cannot be understood.
2409  
2410 =cut
2411  
2412 # $block = encodepixels($encoding, $max, \@pixels);
2413 21 ▶sub encodepixels {
Subroutine encodepixels: called CC=3 SCAR=11.8
Pod documented
2414 21 my $fmt = shift;
2415 21 my $max = shift;
2416 21 my $p_r = shift;
2417 21 my $i;
2418  
2419 21 $i = int_prelim_inspect($fmt, $max, $p_r);
2420  
2421 21 ▶ if(exists($$i{error}) and length($$i{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $i->{"error"} and length $i->{"error"}) 0 21 21
Condition33%1/3
exists $i->{"error"} and length $i->{"error"}
!ll&&!rl&&r
0210
MC/DC0%0/2
exists $i->{"error"} and length $i->{"error"}
exists $i->{"error"} length $i->{"error"}
Truth table33%1/3
A and B
A= exists $i->{"error"}B= length $i->{"error"}
ABresult
0X0
100
111
2422 # we don't return a meaningful error
2423 0 return undef;
2424 }
2425  
2426 21 return int_encodepixels($$i{type}, $p_r, $$i{deep}, $$i{encode}, $max);
2427 } # end &encodepixels
2428  
2429  
2430 ##################################################################
2431  
2432  
2433 =head2 $return = writepnmfile(\*PNM, \%info, \@pixels);
2434  
2435 Writes an entire PNM image to a given filehandle. Sometimes more
2436 memory efficient than a C<makepnmheader()> C<encodepixels()> pair
2437 (by encoding row by row when possible). Does not do an C<inspectpixels()>.
2438  
2439 Writes are done using C<syswrite()> so see that the documentation for
2440 that function for warnings about mixing with other file operations.
2441  
2442 Returns undef if $encoding is not recognized, $max is out of bounds
2443 (1 to 65535, inclusive), or @pixels cannot be understood. Returns
2444 number of bytes written with positive values for complete success,
2445 0 for no bytes successfully written, and -1 * bytes written for
2446 a partial success (eg, ran out of disk space).
2447  
2448 =cut
2449  
2450 # $return = writepnmfile(\*PNM, \%info, \@pixels);
2451 5 ▶sub writepnmfile {
Subroutine writepnmfile: called CC=14 SCAR=28.4
Pod documented
2452 5 my $f_r = shift; # file
2453 5 my $i_r = shift; # image info
2454 5 my $p_r = shift; # array of pixels
2455 5 my $header;
2456 my $inspect;
2457 5 my $fmt;
2458 5 my $max;
2459 5 my $encode;
2460 5 my $deep;
2461 5 my $type;
2462 5 my $bytes;
2463 5 my $rc;
2464 5 my $row;
2465 5 my $pixels;
2466  
2467 5 ▶ if((ref($f_r) ne 'GLOB') or (ref($i_r) ne 'HASH') or (ref($p_r) ne 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $f_r ne "GLOB" or ref $i_r ne "HASH" or ref $p_r ne "ARRAY") 0 5 5
Condition33%2/6
ref $f_r ne "GLOB" or ref $i_r ne "HASH" or ref $p_r ne "ARRAY"
l!l&&r!l&&!r
005
ref $f_r ne "GLOB" or ref $i_r ne "HASH"
l!l&&r!l&&!r
005
MC/DC0%0/3
ref $f_r ne "GLOB" or ref $i_r ne "HASH" or ref $p_r ne "ARRAY"
ref $f_r ne "GLOB" ref $i_r ne "HASH" ref $p_r ne "ARRAY"
Truth table25%1/4
A or B or C
A= ref $f_r ne "GLOB"B= ref $i_r ne "HASH"C= ref $p_r ne "ARRAY"
ABCresult
1XX1
01X1
0011
0000
2468 0 return undef;
2469 }
2470  
2471 5 $header = makepnmheader($i_r);
2472 5 ▶ if(!defined($header)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $header) 0 5 5
2473 0 return undef;
2474 }
2475  
2476 5 $fmt = $$i_r{format};
2477 5 $max = $$i_r{max};
2478  
2479 5 ▶ if(!defined($fmt)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fmt) 5 0 5
2480 5 ▶ if($$i_r{type} > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_r->{'type'} > 3) { } 4 1 5
2481 4 $fmt = 'raw';
2482 } else {
2483 1 $fmt = 'ascii';
2484 }
2485 }
2486 5 $inspect = int_prelim_inspect($fmt, $max, $p_r);
2487  
2488 5 ▶ if(exists($$inspect{error}) and length($$inspect{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $inspect->{"error"} and length $inspect->{"error"}) 0 5 5
Condition33%1/3
exists $inspect->{"error"} and length $inspect->{"error"}
!ll&&!rl&&r
050
MC/DC0%0/2
exists $inspect->{"error"} and length $inspect->{"error"}
exists $inspect->{"error"} length $inspect->{"error"}
Truth table33%1/3
A and B
A= exists $inspect->{"error"}B= length $inspect->{"error"}
ABresult
0X0
100
111
2489 # last undef case
2490 0 return undef;
2491 }
2492  
2493 5 $encode = $$inspect{encode};
2494 5 $deep = $$inspect{deep};
2495 5 $type = $$inspect{type};
2496  
2497 5 $rc = syswrite($f_r, $header);
2498 5 ▶ if($rc != length($header)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc != length $header) 0 5 5
2499 0 return ($rc * -1);
2500 }
2501 5 $bytes = $rc;
2502  
2503 5 ▶ if($deep eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep eq "1d") 3 2 5
2504 # oh well, have to encode it all
2505 3 $pixels = int_encodepixels($type, $p_r, $deep, $encode, $max);
2506 3 $rc = syswrite($f_r, $pixels);
2507 3 $bytes += $rc;
2508 3 ▶ if($rc != length($pixels)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc != length $pixels) 0 3 3
2509 0 return ($bytes * -1);
2510 }
2511 3 return $bytes;
2512 }
2513  
2514 2 for $row (@$p_r) {
2515 15 $pixels = int_encodepixels($type, [ $row ], $deep, $encode, $max);
2516 15 $rc = syswrite($f_r, $pixels);
2517 15 $bytes += $rc;
2518 15 ▶ if($rc != length($pixels)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc != length $pixels) 0 15 15
2519 0 return ($bytes * -1);
2520 }
2521 }
2522  
2523 2 return $bytes;
2524 } # end &writepnmfile
2525  
2526 ##################################################################
2527  
2528  
2529 =head2 inspectpixels($format, $max, \@pixels, \%report );
2530  
2531 Performs all of the argument checks of C<encodepixels()>, and
2532 if no errors are found it does a thorough inspection all pixels
2533 looking for inconsitencies.
2534  
2535 Returns undef if there was an error, and the number of pixels
2536 if it succeeded. (An image with no pixels is considered an error.)
2537 The report hash will contain information gleaned from the inspection.
2538  
2539 =over
2540  
2541 =item *
2542  
2543 $report{error}
2544  
2545 Set if there is an error with a description of the problem.
2546  
2547 =item *
2548  
2549 $report{where}
2550  
2551 Set if there is an error with the array coordinates of the problem.
2552  
2553 =item *
2554  
2555 $report{deep}
2556  
2557 Set to '1d', '2d', or '3d' to describe the pixel array.
2558  
2559 =item *
2560  
2561 $report{width}
2562  
2563 Width of the pixel array (if not '1d' deep).
2564  
2565 =item *
2566  
2567 $report{height}
2568  
2569 Height of the pixel array (if not '1d' deep).
2570  
2571 =item *
2572  
2573 $report{pixels}
2574  
2575 Expected number pixels.
2576  
2577 =item *
2578  
2579 $report{bytes}
2580  
2581 Number of bytes needed to encode each pixel, if in raw. Will be 1
2582 for PBM files.
2583  
2584 =item *
2585  
2586 $report{encode}
2587  
2588 The 'float', 'dec', or 'hex' encoding of the first pixel. All others
2589 are expected to match this.
2590  
2591 =item *
2592  
2593 $report{first}
2594  
2595 First pixel found.
2596  
2597 =item *
2598  
2599 $report{type}
2600  
2601 The numerical type of the format. Might be wrong if B<$report{first}>
2602 is unset. Will contain one of "1" for ASCII PBM, "2" for ASCII PGM, "3" for
2603 ASCII PPM, "4" for raw PBM, "5" for raw PGM, or "6" for raw PPM.
2604  
2605 =item *
2606  
2607 $report{checked}
2608  
2609 Number of pixels checked.
2610  
2611 =back
2612  
2613 =cut
2614  
2615 10 ▶sub inspectpixels {
Subroutine inspectpixels: called CC=28 SCAR=34.8
Pod documented
2616 10 my $fmt = shift;
2617 10 my $max = shift;
2618 10 my $p_r = shift;
2619 10 my $i_r = shift;
2620  
2621 # int_prelim_inspect returns a hash ref
2622 10 %$i_r = %{int_prelim_inspect($fmt, $max, $p_r)};
2623  
2624 10 ▶ if(exists($$i_r{error}) and length($$i_r{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $i_r->{"error"} and length $i_r->{"error"}) 2 8 10
Condition66%2/3
exists $i_r->{"error"} and length $i_r->{"error"}
!ll&&!rl&&r
082
MC/DC50%1/2
exists $i_r->{"error"} and length $i_r->{"error"}
exists $i_r->{"error"} length $i_r->{"error"}
Truth table66%2/3
A and B
A= exists $i_r->{"error"}B= length $i_r->{"error"}
ABresult
0X0
100
111
2625 # the inspection report error explains the problem
2626 2 return undef;
2627 }
2628  
2629 8 my $w = 0;
2630 8 my $h = 0;
2631 8 my $checked = 0;
2632 8 my $cur;
2633 my @rgb;
2634  
2635 8 ▶ if($$i_r{deep} eq '1d') { $cur = $$p_r[$w]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i_r->{'deep'} eq '1d') { } 2 6 8
2636 6 else { $cur = $$p_r[$h][$w]; }
2637  
2638 CHECK_ALL:
2639 8 ▶ while(defined($cur)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cur) 32 1 33
2640  
2641 32 ▶ if($$i_r{deep} eq '3d') {
Branch: 4 branches
BranchTrueFalseTotal
if ($i_r->{'deep'} eq '3d') { } 11 21 32
elsif (ref $cur ne '') { } 0 21 21
elsif ($i_r->{'type'} == 6 or $i_r->{'type'} == 3) { } 8 13 21
elsif ($i_r->{'type'} == 5 or $i_r->{'type'} == 2) { } 13 0 13
2642 11 ▶ if(ref($cur) ne 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $cur ne 'ARRAY') { } 1 10 11
elsif ($#{$cur;} != 2) { } 0 10 10
elsif (not checkval($cur->[0], $i_r->{'encode'}) && checkval($cur->[1], $i_r->{'encode'}) && checkval($cur->[2], $i_r->{'encode'})) { } 1 9 10
2643 1 $$i_r{error} = 'rgb pixel not array';
2644  
2645 10 } elsif ($#{$cur} != 2) {
2646 0 $$i_r{error} = 'rgb pixel array wrong size';
2647  
2648 ▶ } elsif (!checkval($$cur[0], $$i_r{encode}) or
Condition50%3/6
checkval($cur->[0], $i_r->{'encode'}) && checkval($cur->[1], $i_r->{'encode'}) && checkval($cur->[2], $i_r->{'encode'})
!ll&&!rl&&r
019
checkval($cur->[0], $i_r->{"encode"}) and checkval($cur->[1], $i_r->{"encode"})
!ll&&!rl&&r
0010
MC/DC33%1/3
checkval($cur->[0], $i_r->{'encode'}) && checkval($cur->[1], $i_r->{'encode'}) && checkval($cur->[2], $i_r->{'encode'})
checkval($cur->[0], $i_r->{"encode"}) checkval($cur->[1], $i_r->{"encode"}) checkval($cur->[2], $i_r->{'encode'})
Truth table50%2/4
A and B && C
A= checkval($cur->[0], $i_r->{"encode"})B= checkval($cur->[1], $i_r->{"encode"})C= checkval($cur->[2], $i_r->{'encode'})
ABCresult
0XX0
10X0
1100
1111
2649 !checkval($$cur[1], $$i_r{encode}) or
2650 !checkval($$cur[2], $$i_r{encode})) {
2651 1 $$i_r{error} = 'rgb pixel array encoded wrong';
2652  
2653 }
2654 } # 3d
2655  
2656 elsif(ref($cur) ne '') {
2657 0 $$i_r{error} = 'pixel not scalar';
2658 }
2659  
2660 ▶ elsif(($$i_r{type} == 6) or ($$i_r{type} == 3)) { # pixmap
Condition66%2/3
$i_r->{"type"} == 6 or $i_r->{"type"} == 3
l!l&&r!l&&!r
8013
MC/DC50%1/2
$i_r->{"type"} == 6 or $i_r->{"type"} == 3
$i_r->{"type"} == 6 $i_r->{"type"} == 3
Truth table66%2/3
A or B
A= $i_r->{"type"} == 6B= $i_r->{"type"} == 3
ABresult
1X1
011
000
2661 8 @rgb = explodetriple($cur);
2662  
2663 8 ▶ if ($#rgb != 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($#rgb != 2) { } 0 8 8
elsif (not checkval($rgb[0], $i_r->{'encode'}) && checkval($rgb[1], $i_r->{'encode'}) && checkval($rgb[2], $i_r->{'encode'})) { } 1 7 8
2664 0 $$i_r{error} = 'rgb pixel not a triple';
2665  
2666 ▶ } elsif (!checkval($rgb[0], $$i_r{encode}) or
Condition66%4/6
checkval($rgb[0], $i_r->{'encode'}) && checkval($rgb[1], $i_r->{'encode'}) && checkval($rgb[2], $i_r->{'encode'})
!ll&&!rl&&r
107
checkval($rgb[0], $i_r->{"encode"}) and checkval($rgb[1], $i_r->{"encode"})
!ll&&!rl&&r
107
MC/DC33%1/3
checkval($rgb[0], $i_r->{'encode'}) && checkval($rgb[1], $i_r->{'encode'}) && checkval($rgb[2], $i_r->{'encode'})
checkval($rgb[0], $i_r->{"encode"}) checkval($rgb[1], $i_r->{"encode"}) checkval($rgb[2], $i_r->{'encode'})
Truth table50%2/4
A and B && C
A= checkval($rgb[0], $i_r->{"encode"})B= checkval($rgb[1], $i_r->{"encode"})C= checkval($rgb[2], $i_r->{'encode'})
ABCresult
0XX0
10X0
1100
1111
2667 !checkval($rgb[1], $$i_r{encode}) or
2668 !checkval($rgb[2], $$i_r{encode})) {
2669 1 $$i_r{error} = 'rgb pixel encoded wrong';
2670 }
2671 } # pixmap
2672  
2673 ▶ elsif(($$i_r{type} == 5) or ($$i_r{type} == 2)) { # graymap
Condition66%2/3
$i_r->{"type"} == 5 or $i_r->{"type"} == 2
l!l&&r!l&&!r
1120
MC/DC0%0/2
$i_r->{"type"} == 5 or $i_r->{"type"} == 2
$i_r->{"type"} == 5 $i_r->{"type"} == 2
Truth table66%2/3
A or B
A= $i_r->{"type"} == 5B= $i_r->{"type"} == 2
ABresult
1X1
011
000
2674 13 ▶ if (!checkval($cur, $$i_r{encode})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (checkval($cur, $i_r->{"encode"})) 1 12 13
2675 1 $$i_r{error} = 'gray pixel encoded wrong';
2676 }
2677 } # graymap
2678  
2679 32 ▶ if(length($$i_r{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $i_r->{"error"}) 4 28 32
2680 4 $$i_r{checked} = $checked;
2681 4 $$i_r{where} = "$h,$w";
2682 4 return undef;
2683 }
2684  
2685 # that pixel works out okay
2686 28 $checked ++;
2687  
2688 28 ▶ if($checked == $$i_r{pixels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checked == $i_r->{"pixels"}) 3 25 28
2689 3 last CHECK_ALL;
2690 }
2691  
2692 25 ▶ if($$i_r{deep} eq '1d') {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_r->{'deep'} eq '1d') { } 8 17 25
2693 8 $w ++;
2694 8 $cur = $$p_r[$w];
2695 } else {
2696 17 $w ++;
2697 17 ▶ if($w > ($$i_r{width} - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w > $i_r->{"width"} - 1) 5 12 17
2698 5 ▶ if(exists($$p_r[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p_r->[$h][$w]) { } 0 5 5
2699 0 $$i_r{error} = 'row too wide';
2700 0 last CHECK_ALL;
2701 } else {
2702 5 $w = 0;
2703 5 $h ++;
2704 }
2705 }
2706 17 ▶ if (!exists($$p_r[$h][$w])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $p_r->[$h][$w]) 0 17 17
2707 0 $$i_r{error} = 'row not wide enough';
2708 0 last CHECK_ALL;
2709 }
2710 17 $cur = $$p_r[$h][$w];
2711 }
2712 } # while CHECK_ALL
2713  
2714 4 $$i_r{checked} = $checked;
2715  
2716 4 ▶ if($checked != $$i_r{pixels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checked != $i_r->{"pixels"}) 1 3 4
2717 1 $$i_r{error} = 'pixel undef';
2718 1 $$i_r{where} = "$h,$w";
2719 1 return undef;
2720 }
2721  
2722 3 return $$i_r{pixels};
2723 } # end &inspectpixels
2724  
2725  
2726 ##################################################################
2727  
2728  
2729 =head2 checkval($value, $encode);
2730  
2731 Checks that a value (not an RGB triple) conforms to an encoding of
2732 'float', 'dec', or 'hex'. Returns undef if there was an error, and a
2733 positive value otherwise.
2734  
2735 =cut
2736  
2737 65 ▶sub checkval {
Subroutine checkval: called CC=9 SCAR=23.2
Pod documented
2738 65 my $v = shift;
2739 65 my $enc = shift;
2740  
2741 65 ▶ if(!defined($v) or !defined($enc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v and defined $enc) 0 65 65
Condition33%1/3
defined $v and defined $enc
!ll&&!rl&&r
0065
MC/DC0%0/2
defined $v and defined $enc
defined $v defined $enc
Truth table33%1/3
A and B
A= defined $vB= defined $enc
ABresult
0X0
100
111
2742 0 return undef;
2743 }
2744  
2745 65 ▶ if($enc eq 'float') {
Branch: 3 branches
BranchTrueFalseTotal
if ($enc eq 'float') { } 7 58 65
elsif ($enc eq 'dec') { } 34 24 58
elsif ($enc eq 'hex') { } 24 0 24
2746 7 ▶ if($v =~ /^[.\d]+,$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^[.\d]+,$/) 7 0 7
2747 7 return 1;
2748 }
2749 } elsif($enc eq 'dec') {
2750 34 ▶ if($v =~ /^[\d]+:$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^[\d]+:$/) 31 3 34
2751 31 return 1;
2752 }
2753 } elsif($enc eq 'hex') {
2754 24 ▶ if($v =~ m:^[\da-fA-F]+/$:) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ m[^[\da-fA-F]+/$]) 24 0 24
2755 24 return 1;
2756 }
2757 }
2758  
2759 3 return undef;
2760 } # sub &checkval
2761  
2762 ##################################################################
2763  
2764  
2765  
2766  
2767 =head1 PORTABILITY
2768  
2769 This code is pure perl for maximum portability, as befitting the
2770 PBM/PGM/PPM philosophy.
2771  
2772 =head1 CHANGES
2773  
2774 2.0 is a nearly complete rewrite fixing the bugs that arose from
2775 not taking the max value into account. Only the code to read an
2776 image header is taken from 1.x. None of the function names are the
2777 same and most of the interface has changed.
2778  
2779 1.05 fixes two comment related bugs (thanks Ladislav Sladecek!) and
2780 some error reporting bugs with bad filehandles.
2781  
2782 =head1 BUGS
2783  
2784 No attempt is made to deal with comments after the header in ASCII
2785 formatted files.
2786  
2787 No attempt is made to handle the PAM format.
2788  
2789 Pure perl code makes this slower than it could be.
2790  
2791 Not all PBM/PGM/PPM tools are safe for images from untrusted sources
2792 but this one should be. Be careful what you use this with. This
2793 software can create raw files with multibyte (max over 255) values, but
2794 some older PBM/PGM/PPM tools can only handle ASCII files for large
2795 max values (or cannot handle it at all).
2796  
2797 =head1 SEE ALSO
2798  
2799 The manual pages for B<pbm>(5), B<pgm>(5), and B<ppm>(5) define the
2800 various file formats. The netpbm and pbmplus packages include a host
2801 of interesting PNM tools.
2802  
2803 =head1 COPYRIGHT
2804  
2805 Copyright 2012, 2003 Benjamin Elijah Griffin / Eli the Bearded
2806 E<lt>elijah@cpan.orgE<gt>
2807  
2808 This library is free software; you can redistribute it and/or modify it
2809 under the same terms as Perl itself.
2810  
2811 =cut
2812  
2813 14 1;
2814  
2815 __END__
↑ Summary