Builder/Resource/XObject/Image/JPEG.pm

filter: Statementstmt 92.7% 77 / 83 Branchbran 62.5% 25 / 40 Conditioncond 53.8% 14 / 26 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 9 / 9 Podpod 50.0% 1 / 2 totaltotal 73.5% 128 / 174 CC 30 SCAR 28.8
CC
30
Cov
78%
read_jpeg
26.6
new
14.9
BEGIN
1.0
CRAP 17.9
« Builder/Resource/BaseFont.pm ↑ Summary Builder/Resource/XObject/Form/BarCode/code128.pm »
1 package PDF::Builder::Resource::XObject::Image::JPEG;
2  
3 2 ▶use base 'PDF::Builder::Resource::XObject::Image';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '3.028'; # VERSION
9 2 our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
10  
11 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use PDF::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use PDF::Builder::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 PDF::Builder::Resource::XObject::Image::JPEG - Support routines for JPEG image library
19  
20 Inherits from L<PDF::Builder::Resource::XObject::Image>
21  
22 =head1 METHODS
23  
24 =head2 new
25  
26 $res = PDF::Builder::Resource::XObject::Image::JPEG->new($pdf, $file, %opts)
27  
28 =over
29  
30 Options:
31  
32 =over
33  
34 =item 'name' => 'string'
35  
36 This is the name you can give for the JPEG image object. The default is Jxnnnn.
37  
38 =back
39  
40 =back
41  
42 =cut
43  
44 3 ▶sub new {
Subroutine new: called CC=13 SCAR=27.0
Pod documented
45 3 my ($class, $pdf, $file, %opts) = @_;
46 # copy dashed option names to preferred undashed names
47 0 ▶ if (defined $opts{'-name'} && !defined $opts{'name'}) { $opts{'name'} = delete($opts{'-name'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-name"} and not defined $opts{"name"}) 0 3 3
Condition33%1/3
defined $opts{"-name"} and not defined $opts{"name"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $opts{"-name"} and not defined $opts{"name"}
defined $opts{"-name"} not defined $opts{"name"}
Truth table33%1/3
A and not(B)
A= defined $opts{"-name"}B= not defined $opts{"name"}
ABresult
0X0
100
111
48 0 ▶ if (defined $opts{'-compress'} && !defined $opts{'compress'}) { $opts{'compress'} = delete($opts{'-compress'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-compress"} and not defined $opts{"compress"}) 0 3 3
Condition33%1/3
defined $opts{"-compress"} and not defined $opts{"compress"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $opts{"-compress"} and not defined $opts{"compress"}
defined $opts{"-compress"} not defined $opts{"compress"}
Truth table33%1/3
A and not(B)
A= defined $opts{"-compress"}B= not defined $opts{"compress"}
ABresult
0X0
100
111
49  
50 3 my ($name, $compress);
51 0 ▶ if (exists $opts{'name'}) { $name = $opts{'name'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"name"}) 0 3 3
52 #if (exists $opts{'compress'}) { $compress = $opts{'compress'}; }
53  
54 3 my $fh = IO::File->new();
55  
56 3 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 3 3
57  
58 3 ▶ my $self = $class->SUPER::new($pdf, $name || 'Jx' . pdfkey());
Condition50%1/2
$name || 'Jx' . pdfkey()
l!l
03
MC/DC0%0/1
$name || 'Jx' . pdfkey()
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
59 3 ▶ $pdf->new_obj($self) unless $self->is_obj($pdf);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_obj($pdf) 0 3 3
60  
61 3 $self->{' apipdf'} = $pdf;
62 3 weaken $self->{' apipdf'};
63  
64 3 ▶ if (ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 1 2 3
65 1 $fh = $file;
66 } else {
67 2 ▶ open $fh, "<", $file or die "$!: $file";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or die "$!: $file" 1 1 2
68 }
69 2 binmode($fh, ':raw');
70  
71 2 $self->read_jpeg($fh);
72  
73 2 ▶ if (ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 1 1 2
74 1 seek($fh, 0, 0);
75 1 $self->{' stream'} = '';
76 1 my $buf = '';
77 1 ▶ while (!eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 2 1 3
78 2 read($fh, $buf, 512);
79 2 $self->{' stream'} .= $buf;
80 }
81 1 $self->{'Length'} = PDFNum(length $self->{' stream'});
82 } else {
83 1 $self->{'Length'} = PDFNum(-s $file);
84 1 $self->{' streamfile'} = $file;
85 }
86  
87 2 $self->filters('DCTDecode');
88 2 $self->{' nofilt'} = 1;
89  
90 2 return $self;
91 }
92  
93 2 ▶sub read_jpeg {
Subroutine read_jpeg: called CC=18 SCAR=32.8
Pod undocumented
94 2 my ($self, $fh) = @_;
95  
96 2 my ($buf, $p, $h, $w, $c, $ff, $mark, $len);
97  
98 2 $fh->seek(0,0);
99 2 $fh->read($buf,2);
100 2 while (1) {
101 10 $fh->read($buf, 4);
102 10 my ($ff, $mark, $len) = unpack('CCn', $buf);
103 10 ▶ last if $ff != 0xFF;
Branch: 1 branch
BranchTrueFalseTotal
if $ff != 255 0 10 10
104 10 ▶ last if $mark == 0xDA || $mark == 0xD9; # SOS/EOI
Branch: 1 branch
BranchTrueFalseTotal
if $mark == 218 or $mark == 217 0 10 10
Condition33%1/3
$mark == 218 or $mark == 217
l!l&&r!l&&!r
0010
MC/DC0%0/2
$mark == 218 or $mark == 217
$mark == 218 $mark == 217
Truth table33%1/3
A or B
A= $mark == 218B= $mark == 217
ABresult
1X1
011
000
105 10 ▶ last if $len < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 2 0 10 10
106 10 ▶ last if $fh->eof();
Branch: 1 branch
BranchTrueFalseTotal
if $fh->eof 0 10 10
107 10 $fh->read($buf, $len - 2);
108 10 ▶ next if $mark == 0xFE;
Branch: 1 branch
BranchTrueFalseTotal
if $mark == 254 2 8 10
109 8 ▶ next if $mark >= 0xE0 && $mark <= 0xEF;
Branch: 1 branch
BranchTrueFalseTotal
if $mark >= 224 and $mark <= 239 2 6 8
Condition66%2/3
$mark >= 224 and $mark <= 239
!ll&&!rl&&r
602
MC/DC50%1/2
$mark >= 224 and $mark <= 239
$mark >= 224 $mark <= 239
Truth table66%2/3
A and B
A= $mark >= 224B= $mark <= 239
ABresult
0X0
100
111
110 6 ▶ if ($mark >= 0xC0 && $mark <= 0xCF && $mark != 0xC4 && $mark != 0xC8 && $mark != 0xCC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mark >= 192 and $mark <= 207 and $mark != 196 and $mark != 200 and $mark != 204) 2 4 6
Condition66%8/12
$mark >= 192 and $mark <= 207 and $mark != 196 and $mark != 200 and $mark != 204
!ll&&!rl&&r
402
$mark >= 192 and $mark <= 207 and $mark != 196 and $mark != 200
!ll&&!rl&&r
402
$mark >= 192 and $mark <= 207 and $mark != 196
!ll&&!rl&&r
402
$mark >= 192 and $mark <= 207
!ll&&!rl&&r
042
MC/DC20%1/5
$mark >= 192 and $mark <= 207 and $mark != 196 and $mark != 200 and $mark != 204
$mark >= 192 $mark <= 207 $mark != 196 $mark != 200 $mark != 204
Truth table33%2/6
A and B and C and D and E
A= $mark >= 192B= $mark <= 207C= $mark != 196D= $mark != 200E= $mark != 204
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
111 2 ($p, $h, $w, $c) = unpack('CnnC', substr($buf, 0, 6));
112 2 last;
113 }
114 }
115  
116 2 $self->width($w);
117 2 $self->height($h);
118 2 $self->bits_per_component($p);
119  
120 0 ▶ if (!defined $c) { return $self; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c) 0 2 2
121 2 ▶ if ($c == 3) {
Branch: 3 branches
BranchTrueFalseTotal
if ($c == 3) { } 2 0 2
elsif ($c == 4) { } 0 0 0
elsif ($c == 1) { } 0 0 0
122 2 $self->colorspace('DeviceRGB');
123 } elsif ($c == 4) {
124 0 $self->colorspace('DeviceCMYK');
125 } elsif ($c == 1) {
126 0 $self->colorspace('DeviceGray');
127 }
128  
129 2 return $self;
130 }
131  
132 2 1;
« Builder/Resource/BaseFont.pm ↑ Summary Builder/Resource/XObject/Form/BarCode/code128.pm »