blib/lib/PDF/API2/Resource/XObject/Image/Imager.pm

filter: Statementstmt 25.9% 21 / 81 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 80.0% 8 / 10 Podpod n/a totaltotal 25.2% 29 / 115 CC 11 SCAR 33.1
CC
11
Cov
21%
read_imager
56.0
new
30.0
BEGIN
1.3
CRAP 27.5
↑ Summary
1 package PDF::API2::Resource::XObject::Image::Imager;
2  
3 # $Id: Imager.pm 2707 2025-05-17 17:03:41Z fil $
4  
5 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 our $VERSION = '0.01';
10  
11 3 ▶use PDF::API2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use PDF::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use base 'PDF::API2::Resource::XObject::Image';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub DEBUG () { 0 }
18  
19 sub new
20 {
21 0 ▶ my( $package, $pdf, $img, $name, %opts ) = @_;
Subroutine new: not called CC=5 SCAR=34.0
22
23 0 ▶ $package = ref $package if ref $package;
Branch: 1 branch
BranchTrueFalseTotal
if ref $package 0 0 0
24
25 0 ▶ $pdf = $pdf->{'pdf'} if $pdf->isa( 'PDF::API2' );
Branch: 1 branch
BranchTrueFalseTotal
if $pdf->isa("PDF::API2") 0 0 0
26  
27 0 ▶ my $self = $package->SUPER::new( $pdf, $name||'Im'.pdfkey() );
Condition0%0/2
$name || 'Im' . pdfkey()
l!l
00
MC/DC0%0/1
$name || 'Im' . pdfkey()
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
28 0 ▶ $pdf->new_obj( $self ) unless $self->is_obj( $pdf );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_obj($pdf) 0 0 0
29  
30 0 $self->{' apipdf'}=$pdf;
31 0 weaken $self->{' apipdf'};
32  
33 0 $self->read_imager( $img, %opts );
34  
35 0 return $self;
36 }
37  
38  
39 sub read_imager
40 {
41 0 ▶ my( $self, $img, %opts ) = @_;
Subroutine read_imager: not called CC=7 SCAR=40.3
42  
43 0 my $pdf = $self->{' apipdf'};
44  
45 0 my $w = $img->getwidth;
46 0 my $h = $img->getheight;
47 0 my $bpc = $img->bits;
48 0 my $type = $img->type;
49  
50 0 DEBUG and warn "# Type: $type";
51 0 DEBUG and warn "# Bits per pixel: $bpc";
52
53 0 ▶ if( $type eq 'paletted' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "paletted") 0 0 0
54 # 1-bit image w/o alpha is probably better handled by
55 # PDF::API2::Resource::XObject::Image::PNG
56 0 DEBUG and warn "# Convert to rgb8";
57 0 $img = $img->to_rgb8;
58 0 $bpc = $img->bits;
59 0 $type = $img->type;
60 0 DEBUG and warn "# Bits per pixel: $bpc";
61 }
62  
63 0 DEBUG and warn "# Width: $w";
64 0 DEBUG and warn "# Height: $h";
65  
66 0 ▶ die "Unable to process $bpc bit image" if $bpc != 8;
Branch: 1 branch
BranchTrueFalseTotal
if $bpc != 8 0 0 0
67  
68 0 $self->width( $w );
69 0 $self->height( $h );
70 0 $self->bpc( $bpc );
71  
72 0 my $ch = $img->getchannels;
73  
74 0 ▶ if( $ch == 2 || $ch == 4 ) { # grey+alpha or RGBA
Branch: 1 branch
BranchTrueFalseTotal
if ($ch == 2 or $ch == 4) 0 0 0
Condition0%0/3
$ch == 2 or $ch == 4
l!l&&r!l&&!r
000
MC/DC0%0/2
$ch == 2 or $ch == 4
$ch == 2 $ch == 4
Truth table0%0/3
A or B
A= $ch == 2B= $ch == 4
ABresult
1X1
011
000
75 0 DEBUG and warn "# Alpha channel";
76 0 my $alpha = $img->convert( preset=>'alpha' ); # save alpha channels
77 0 $img = $img->convert( preset=>'noalpha' ); # save RGB channels
78 0 $ch--;
79  
80 # Add an SMask
81 0 my $mask = PDFDict();
82 0 $pdf->new_obj( $mask );
83 0 $mask->{'Type'} = PDFName('XObject');
84 0 $mask->{'Subtype'} = PDFName('Image');
85 0 $mask->{'Width'} = PDFNum($w);
86 0 $mask->{'Height'} = PDFNum($h);
87 0 $mask->{'ColorSpace'} = PDFName('DeviceGray');
88 # $mask->{'Filter'} = PDFArray(PDFName('FlateDecode'));
89 0 $mask->{'BitsPerComponent'} = PDFNum($bpc);
90 0 $self->{'SMask'} = $mask;
91  
92 0 $self->_read_to( $mask, $alpha );
93 }
94  
95 0 my $decode = PDFDict();
96 0 $self->{'DecodeParms'} = PDFArray($decode);
97 0 $decode->{'BitsPerComponent'} = PDFNum($bpc);
98 0 $decode->{'Colors'} = PDFNum( $ch );
99 0 $decode->{'Columns'} = PDFNum( $w );
100  
101 # $self->filters('FlateDecode');
102 0 ▶ if( $ch == 1 ) { # grey
Branch: 2 branches
BranchTrueFalseTotal
if ($ch == 1) { } 0 0 0
elsif ($ch == 3) { } 0 0 0
103 0 DEBUG and warn "# Device: Gray";
104 0 $self->colorspace('DeviceGray');
105 }
106 elsif( $ch == 3 ) { # RGB
107 0 DEBUG and warn "# Device: RGB";
108 0 $self->colorspace('DeviceRGB');
109 }
110 else {
111 0 die "How do I deal with $ch channels?";
112 }
113  
114 0 $self->_read_to( $self, $img );
115  
116 0 return $self;
117 }
118  
119 sub _read_to
120 {
121 my( $self, $obj, $img ) = @_;
122 # It would be nice to LZW this data
123 # https://www.verypdf.com/document/pdf-format-reference/pg_0072.htm
124 my $data;
125 $img->write( type=>'raw', data=>\$data ) or die "Unable to create raw: ".$img->errstr;
126 if( 0 ) {
127 3 ▶ use Compress::LZW::Compressor;
Subroutine BEGIN: called CC=1 SCAR=2.6
128 my $c = Compress::LZW::Compressor->new;
129 $obj->{'Filter'} = PDFArray(PDFName('LZWDecode'));
130 $obj->{' stream'} = $c->compress( $data );
131 # use Data::Dump qw( pp );
132 warn pp $c->compress( join '', 0x45, 0x45, 0x45, 0x45, 0x45, 0x65, 0x45, 0x45, 0x45, 0x66 );
133 }
134 else {
135 $obj->{'Filter'} = PDFArray(PDFName('FlateDecode'));
136 $obj->{' stream'} = $data;
137 }
138 }
139  
140 *PDF::API2::imager = sub {
141 my( $self, $img, %opts ) = @_;
142 my $obj = PDF::API2::Resource::XObject::Image::Imager->new( $self->{'pdf'}, $img, undef, %opts );
143 $self->{'pdf'}->out_obj( $self->{'pages'} );
144 return $obj;
145 };
146  
147  
148 1;
149 __END__
↑ Summary