API3/Compat/API2/Resource/XObject/Image/PNM.pm

filter: Statementstmt 17.3% 21 / 121 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 63.6% 7 / 11 Podpod 50.0% 2 / 4 totaltotal 14.9% 30 / 201 CC 27 SCAR 48.6
CC
27
Cov
12%
readppmheader
240.0
read_pnm
110.0
new
20.0
CRAP 129.4
« API3/Compat/API2/Basic/TTF/Coverage.pm ↑ Summary API3/Compat/API2/Basic/TTF/GPOS.pm »
1 #=======================================================================
2 # ____ ____ _____ _ ____ ___ ____
3 # | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \
4 # | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) |
5 # | __/| |_| | _| _ _ / ___ \| __/| | / __/
6 # |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____|
7 #
8 # A Perl Module Chain to faciliate the Creation and Modification
9 # of High-Quality "Portable Document Format (PDF)" Files.
10 #
11 # Copyright 1999-2005 Alfred Reibenschuh <areibens@cpan.org>.
12 #
13 #=======================================================================
14 #
15 # This library is free software; you can redistribute it and/or
16 # modify it under the terms of the GNU Lesser General Public
17 # License as published by the Free Software Foundation; either
18 # version 2 of the License, or (at your option) any later version.
19 #
20 # This library is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 # Lesser General Public License for more details.
24 #
25 # You should have received a copy of the GNU Lesser General Public
26 # License along with this library; if not, write to the
27 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 # Boston, MA 02111-1307, USA.
29 #
30 # $Id: PNM.pm,v 2.1 2007/05/24 19:29:46 areibens Exp $
31 #
32 #=======================================================================
33 package PDF::API3::Compat::API2::Resource::XObject::Image::PNM;
34
35 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
36
37 1 ▶ use PDF::API3::Compat::API2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶ use PDF::API3::Compat::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶ use PDF::API3::Compat::API2::Resource::XObject::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
40
41 1 ▶ use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
42
43 1 ▶ use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 @ISA = qw( PDF::API3::Compat::API2::Resource::XObject::Image );
45 1 ( $VERSION ) = sprintf '%i.%03i', split(/\./,('$Revision: 2.1 $' =~ /Revision: (\S+)\s/)[0]); # $Date: 2007/05/24 19:29:46 $
46 }
47 1 ▶no warnings qw[ deprecated recursion uninitialized ];
Subroutine BEGIN: called CC=1 SCAR=0.0
48
49 =item $res = PDF::API3::Compat::API2::Resource::XObject::Image::PNM->new $pdf, $file [, $name]
50
51 Returns a pnm-image object.
52
53 =cut
54
55 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
56 0 my ($class,$pdf,$file,$name) = @_;
57 0 my $self;
58 0 my $fh = IO::File->new;
59
60 0 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
61
62 0 ▶ $self=$class->SUPER::new($pdf,$name || 'Nx'.pdfkey());
Condition0%0/2
$name || 'Nx' . pdfkey()
l!l
00
MC/DC0%0/1
$name || 'Nx' . pdfkey()
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
63 0 ▶ $pdf->new_obj($self) unless($self->is_obj($pdf));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_obj($pdf) 0 0 0
64
65 0 $self->{' apipdf'}=$pdf;
66
67 0 $self->read_pnm($pdf,$file);
68
69 0 return($self);
70 }
71
72 =item $res = PDF::API3::Compat::API2::Resource::XObject::Image::PNM->new_api $api, $file [, $name]
73
74 Returns a pnm-image object. This method is different from 'new' that
75 it needs an PDF::API3::Compat::API2-object rather than a Text::PDF::File-object.
76
77 =cut
78
79 0 ▶sub new_api {
Subroutine new_api: not called CC=1 SCAR=6.9
Pod documented
80 0 my ($class,$api,@opts)=@_;
81
82 0 my $obj=$class->new($api->{pdf},@opts);
83 0 $obj->{' api'}=$api;
84
85 0 return($obj);
86 }
87
88 # READPPMHEADER
89 # taken from Image::PBMLib
90 # Copyright by Benjamin Elijah Griffin (28 Feb 2003)
91 #
92 0 ▶sub readppmheader($) {
Subroutine readppmheader: not called CC=15 SCAR=54.8
Pod undocumented
93 0 my $gr = shift; # input file glob ref
94 0 my $in = '';
95 0 my $no_comments;
96 my %info;
97 0 my $rc;
98 0 $info{error} = undef;
99
100 0 $rc = read($gr, $in, 3);
101
102 0 ▶ if (!defined($rc) or $rc != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rc or $rc != 3) 0 0 0
Condition0%0/3
not defined $rc or $rc != 3
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $rc or $rc != 3
not defined $rc $rc != 3
Truth table0%0/3
not(A) or B
A= not defined $rcB= $rc != 3
ABresult
1X1
011
000
103 0 $info{error} = 'Read error or EOF';
104 0 return \%info;
105 }
106
107 0 ▶ if ($in =~ /^P([123456])\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /^P([123456])\s/) { } 0 0 0
108 0 $info{type} = $1;
109 0 ▶ if ($info{type} > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info{'type'} > 3) { } 0 0 0
110 0 $info{raw} = 1;
111 } else {
112 0 $info{raw} = 0;
113 }
114
115 0 ▶ if ($info{type} == 1 or $info{type} == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info{'type'} == 1 or $info{'type'} == 4) { } 0 0 0
elsif ($info{'type'} == 2 or $info{'type'} == 5) { } 0 0 0
Condition0%0/3
$info{"type"} == 1 or $info{"type"} == 4
l!l&&r!l&&!r
000
MC/DC0%0/2
$info{"type"} == 1 or $info{"type"} == 4
$info{"type"} == 1 $info{"type"} == 4
Truth table0%0/3
A or B
A= $info{"type"} == 1B= $info{"type"} == 4
ABresult
1X1
011
000
116 0 $info{max} = 1;
117 0 $info{bgp} = 'b';
118 ▶ } elsif ($info{type} == 2 or $info{type} == 5) {
Condition0%0/3
$info{"type"} == 2 or $info{"type"} == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$info{"type"} == 2 or $info{"type"} == 5
$info{"type"} == 2 $info{"type"} == 5
Truth table0%0/3
A or B
A= $info{"type"} == 2B= $info{"type"} == 5
ABresult
1X1
011
000
119 0 $info{bgp} = 'g';
120 } else {
121 0 $info{bgp} = 'p';
122 }
123
124 0 while(1) {
125 0 $rc = read($gr, $in, 1, length($in));
126 0 ▶ if (!defined($rc) or $rc != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rc or $rc != 1) 0 0 0
Condition0%0/3
not defined $rc or $rc != 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $rc or $rc != 1
not defined $rc $rc != 1
Truth table0%0/3
not(A) or B
A= not defined $rcB= $rc != 1
ABresult
1X1
011
000
127 0 $info{error} = 'Read error or EOF';
128 0 return \%info;
129 }
130
131 0 $no_comments = $in;
132 0 $info{comments} = '';
133 0 ▶ while ($no_comments =~ /#.*\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_comments =~ /#.*\n/) 0 0 0
134 0 $no_comments =~ s/#(.*\n)/ /;
135 0 $info{comments} .= $1;
136 }
137
138 0 ▶ if ($info{bgp} eq 'b') {
Branch: 1 branch
BranchTrueFalseTotal
if ($info{'bgp'} eq 'b') { } 0 0 0
139 0 ▶ if ($no_comments =~ /^P\d\s+(\d+)\s+(\d+)\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_comments =~ /^P\d\s+(\d+)\s+(\d+)\s/) 0 0 0
140 0 $info{width} = $1;
141 0 $info{height} = $2;
142 0 last;
143 }
144 } else {
145 0 ▶ 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/) 0 0 0
146 0 $info{width} = $1;
147 0 $info{height} = $2;
148 0 $info{max} = $3;
149 0 last;
150 }
151 }
152 } # while reading header
153
154 0 $info{fullheader} = $in;
155
156 } else {
157 0 $info{error} = 'Wrong magic number';
158 }
159
160 0 return \%info;
161 }
162
163 0 ▶sub read_pnm {
Subroutine read_pnm: not called CC=10 SCAR=47.0
Pod undocumented
164 0 my $self = shift @_;
165 0 my $pdf = shift @_;
166 0 my $file = shift @_;
167
168 0 my ($buf,$t,$s,$line);
169 0 my ($w,$h,$bpc,$cs,$img,@img)=(0,0,'','','');
170 0 open(INF,$file);
171 0 binmode(INF,':raw');
172 0 my $info=readppmheader(INF);
173 0 ▶ if($info->{type} == 4) {
Branch: 3 branches
BranchTrueFalseTotal
if ($info->{'type'} == 4) { } 0 0 0
elsif ($info->{'type'} == 5) { } 0 0 0
elsif ($info->{'type'} == 6) { } 0 0 0
174 0 $bpc=1;
175 0 read(INF,$self->{' stream'},($info->{width}*$info->{height}/8));
176 0 $cs='DeviceGray';
177 0 $self->{Decode}=PDFArray(PDFNum(1),PDFNum(0));
178 } elsif($info->{type} == 5) {
179 0 $buf.=<INF>;
180 0 ▶ if($info->{max}==255){
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'max'} == 255) { } 0 0 0
181 0 $s=0;
182 } else {
183 0 $s=255/$info->{max};
184 }
185 0 $bpc=8;
186 0 ▶ if($s>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s > 0) { } 0 0 0
187 0 ▶ for($line=($info->{width}*$info->{height});$line>0;$line--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line > 0) 0 0 0
188 0 read(INF,$buf,1);
189 0 $self->{' stream'}.=pack('C',(unpack('C',$buf)*$s));
190 }
191 } else {
192 0 read(INF,$self->{' stream'},$info->{width}*$info->{height});
193 }
194 0 $cs='DeviceGray';
195 } elsif($info->{type} == 6) {
196 0 ▶ if($info->{max}==255){
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'max'} == 255) { } 0 0 0
197 0 $s=0;
198 } else {
199 0 $s=255/$info->{max};
200 }
201 0 $bpc=8;
202 0 ▶ if($s>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s > 0) { } 0 0 0
203 0 ▶ for($line=($info->{width}*$info->{height});$line>0;$line--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line > 0) 0 0 0
204 0 read(INF,$buf,1);
205 0 $self->{' stream'}.=pack('C',(unpack('C',$buf)*$s));
206 0 read(INF,$buf,1);
207 0 $self->{' stream'}.=pack('C',(unpack('C',$buf)*$s));
208 0 read(INF,$buf,1);
209 0 $self->{' stream'}.=pack('C',(unpack('C',$buf)*$s));
210 }
211 } else {
212 0 read(INF,$self->{' stream'},$info->{width}*$info->{height}*3);
213 }
214 0 $cs='DeviceRGB';
215 }
216 0 close(INF);
217
218 0 $self->width($info->{width});
219 0 $self->height($info->{height});
220
221 0 $self->bpc($bpc);
222
223 0 $self->filters('FlateDecode');
224
225 0 $self->colorspace($cs);
226
227 0 return($self);
228 }
229
230 1 1;
231
232 __END__
« API3/Compat/API2/Basic/TTF/Coverage.pm ↑ Summary API3/Compat/API2/Basic/TTF/GPOS.pm »