File Coverage

blib/lib/PDF/Builder/Resource/XObject/Image/GD.pm
Criterion Covered Total %
statement 18 56 32.1
branch 0 16 0.0
condition 0 18 0.0
subroutine 6 8 75.0
pod 1 2 50.0
total 25 100 25.0


line stmt bran cond sub pod time code
1             package PDF::Builder::Resource::XObject::Image::GD;
2              
3 1     1   996 use base 'PDF::Builder::Resource::XObject::Image';
  1         2  
  1         97  
4              
5 1     1   5 use strict;
  1         2  
  1         16  
6 1     1   3 use warnings;
  1         1  
  1         104  
7              
8             our $VERSION = '3.028'; # VERSION
9             our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
10              
11 1     1   4 use PDF::Builder::Util;
  1         2  
  1         172  
12 1     1   5 use PDF::Builder::Basic::PDF::Utils;
  1         2  
  1         58  
13 1     1   4 use Scalar::Util qw(weaken);
  1         1  
  1         503  
14              
15             =head1 NAME
16              
17             PDF::Builder::Resource::XObject::Image::GD - Support routines for Graphics Development image library
18              
19             Inherits from L<PDF::Builder::Resource::XObject::Image>
20              
21             =head1 METHODS
22              
23             =head2 new
24              
25             $res = PDF::Builder::Resource::XObject::Image::GD->new($pdf, $file, %opts)
26              
27             =over
28              
29             Options:
30              
31             =over
32              
33             =item 'name' => 'string'
34              
35             This is the name you can give for the GD image object. The default is Dxnnnn.
36              
37             =item 'lossless' => 1
38              
39             Use lossless compression.
40              
41             =back
42              
43             An image object is created from GD input. Note that this should be invoked
44             from Builder.pm's method.
45              
46             =back
47              
48             =cut
49              
50             sub new {
51 0     0 1   my ($class, $pdf, $obj, %opts) = @_;
52             # copy dashed option names to preferred undashed names
53 0 0 0       if (defined $opts{'-name'} && !defined $opts{'name'}) { $opts{'name'} = delete($opts{'-name'}); }
  0            
54 0 0 0       if (defined $opts{'-compress'} && !defined $opts{'compress'}) { $opts{'compress'} = delete($opts{'-compress'}); }
  0            
55 0 0 0       if (defined $opts{'-lossless'} && !defined $opts{'lossless'}) { $opts{'lossless'} = delete($opts{'-lossless'}); }
  0            
56              
57 0           my ($name, $compress);
58 0 0         if (exists $opts{'name'}) { $name = $opts{'name'}; }
  0            
59             #if (exists $opts{'compress'}) { $compress = $opts{'compress'}; }
60              
61 0           my $self;
62              
63 0 0         $class = ref($class) if ref($class);
64              
65 0   0       $self = $class->SUPER::new($pdf, $name || 'Dx'.pdfkey());
66 0 0         $pdf->new_obj($self) unless $self->is_obj($pdf);
67              
68 0           $self->{' apipdf'} = $pdf;
69 0           weaken $self->{' apipdf'};
70              
71 0           $self->read_gd($obj, %opts);
72              
73 0           return $self;
74             }
75              
76             sub read_gd {
77 0     0 0   my ($self, $gd, %opts) = @_;
78              
79 0           my ($w,$h) = $gd->getBounds();
80 0           my $c = $gd->colorsTotal();
81              
82 0           $self->width($w);
83 0           $self->height($h);
84              
85 0           $self->bits_per_component(8);
86 0           $self->colorspace('DeviceRGB');
87              
88 0 0 0       if ($gd->can('jpeg') && ($c > 256) && !$opts{'lossless'}) {
    0 0        
89              
90 0           $self->filters('DCTDecode');
91 0           $self->{' nofilt'} = 1;
92 0           $self->{' stream'} = $gd->jpeg(75);
93              
94             } elsif ($gd->can('raw')) {
95              
96 0           $self->filters('FlateDecode');
97 0           $self->{' stream'} = $gd->raw();
98              
99             } else {
100              
101 0           $self->filters('FlateDecode');
102 0           for(my $y=0; $y<$h; $y++) {
103 0           for(my $x=0; $x<$w; $x++) {
104 0           my $index = $gd->getPixel($x,$y);
105 0           my @rgb = $gd->rgb($index);
106 0           $self->{' stream'} .= pack('CCC', @rgb);
107             }
108             }
109              
110             }
111              
112 0           return $self;
113             }
114              
115             1;