File Coverage

blib/lib/PDF/Builder/Resource/XObject/Image.pm
Criterion Covered Total %
statement 30 40 75.0
branch 6 10 60.0
condition 2 3 66.6
subroutine 9 11 81.8
pod 7 7 100.0
total 54 71 76.0


line stmt bran cond sub pod time code
1             package PDF::Builder::Resource::XObject::Image;
2              
3 6     6   2753 use base 'PDF::Builder::Resource::XObject';
  6         14  
  6         874  
4              
5 6     6   35 use strict;
  6         14  
  6         142  
6 6     6   21 use warnings;
  6         13  
  6         591  
7              
8             our $VERSION = '3.028'; # VERSION
9             our $LAST_UPDATE = '3.028'; # manually update whenever code is changed
10              
11 6     6   60 use PDF::Builder::Basic::PDF::Utils;
  6         14  
  6         3886  
12              
13             =head1 NAME
14              
15             PDF::Builder::Resource::XObject::Image - Base class for external raster image objects
16              
17             Inherits from L<PDF::Builder::Resource::XObject>
18              
19             =head1 METHODS
20              
21             =head2 new
22              
23             $image = PDF::Builder::Resource::XObject::Image->new($pdf, $name)
24              
25             =over
26              
27             Returns an image resource object.
28              
29             =back
30              
31             =cut
32              
33             sub new {
34 17     17 1 47 my ($class, $pdf, $name) = @_;
35              
36 17         96 my $self = $class->SUPER::new($pdf, $name);
37              
38 17         76 $self->subtype('Image');
39              
40 17         44 return $self;
41             }
42              
43             =head2 width
44              
45             $width = $image->width()
46              
47             =over
48              
49             Get the width (in points) of the image object.
50              
51             B<Note> that this function also has the ability to I<set> the width,
52             by giving the new width (in points), but it appears that it never
53             worked correctly. The I<set> capability has been B<deprecated>, and
54             may be removed soon, unless it is found to be needed for an E<lt>img>
55             tag in C<column()>. If you are
56             using the C<width()> method in some manner to I<set> the image width,
57             please let us know, so we can plan to keep it enabled!
58              
59             =back
60              
61             =cut
62              
63             sub width {
64 22     22 1 5508 my $self = shift;
65              
66 22 100       130 $self->{'Width'} = PDFNum(shift()) if scalar @_;
67 22         75 return $self->{'Width'}->val();
68             }
69              
70             =head2 height
71              
72             $height = $image->height()
73              
74             =over
75              
76             Get the height (in points) of the image object.
77              
78             B<Note> that this function also has the ability to I<set> the height,
79             by giving the new height (in points), but it appears that it never
80             worked correctly. The I<set> capability has been B<deprecated>, and
81             may be removed soon, unless it is found to be needed for an E<lt>img>
82             tag in C<column()>. If you are
83             using the C<height()> method in some manner to I<set> the image height,
84             please let us know, so we can plan to keep it enabled!
85              
86             =back
87              
88             =cut
89              
90             sub height {
91 13     13 1 22 my $self = shift;
92              
93 13 50       44 $self->{'Height'} = PDFNum(shift()) if scalar @_;
94 13         49 return $self->{'Height'}->val();
95             }
96              
97             ## probably not useful, so do not add, for now
98             #=head2 bbox
99             #
100             # ($x1,$x2, $w,$h) = $image->bbox()
101             #
102             #=over
103             #
104             #Get the image dimensions similarly to a form's I<bounding box>.
105             #Note that the C<$x1> and C<$x2> values will always be 0.
106             #
107             #This method is offered as an alternative to the C<width> and C<height> methods.
108             #
109             #=back
110             #
111             #=cut
112             #
113             #sub bbox {
114             # my $self = shift();
115             # my @bb = (0,0, $self->width(),$self->height());
116             # return @bb;
117             #}
118              
119             =head2 smask
120              
121             $image->smask($xobject)
122              
123             =over
124              
125             Set the soft-mask image object.
126              
127             =back
128              
129             =cut
130              
131             sub smask {
132 0     0 1 0 my $self = shift;
133 0         0 $self->{'SMask'} = shift;
134              
135 0         0 return $self;
136             }
137              
138             =head2 mask
139              
140             $image->mask(@color_range)
141              
142             $image->mask($xobject)
143              
144             =over
145              
146             Set the mask to an image mask XObject or an array containing a range
147             of colors to be applied as a color key mask.
148              
149             =back
150              
151             =cut
152              
153             sub mask {
154 0     0 1 0 my $self = shift();
155              
156 0 0       0 if (ref($_[0])) {
157 0         0 $self->{'Mask'} = shift();
158             } else {
159 0         0 $self->{'Mask'} = PDFArray(map { PDFNum($_) } @_);
  0         0  
160             }
161              
162 0         0 return $self;
163             }
164              
165             # imask() functionality rolled into mask()
166              
167             =head2 colorspace
168              
169             $image->colorspace($name)
170              
171             $image->colorspace($array)
172              
173             =over
174              
175             Set the color space used by the image. Depending on the color space,
176             this will either be just the name of the color space, or it will be an
177             array containing the color space and any required parameters.
178              
179             If passing an array, parameters must already be encoded as PDF
180             objects. The array itself may also be a PDF object. If not, one will
181             be created.
182              
183             =back
184              
185             =cut
186              
187             sub colorspace {
188 13     13 1 37 my ($self, @values) = @_;
189              
190 13 100 66     86 if (scalar @values == 1 and ref($values[0])) {
    50          
191 4         13 $self->{'ColorSpace'} = $values[0];
192             } elsif (scalar @values == 1) {
193 9         29 $self->{'ColorSpace'} = PDFName($values[0]);
194             } else {
195 0         0 $self->{'ColorSpace'} = PDFArray(@values);
196             }
197              
198 13         34 return $self;
199             }
200              
201             =head2 bits_per_component
202              
203             $image->bits_per_component($integer)
204              
205             =over
206              
207             Set the number of bits used to represent each color component.
208              
209             =back
210              
211             =cut
212              
213             sub bits_per_component {
214 13     13 1 23 my $self = shift;
215              
216 13         34 $self->{'BitsPerComponent'} = PDFNum(shift());
217              
218 13         27 return $self;
219             }
220              
221             # bpc() renamed to bits_per_component()
222              
223             1;