summaryrefslogtreecommitdiff
path: root/lib/PDF/Builder/Resource/XObject/Image.pm
blob: 5428f0864e32c81cd64413c19762be3367707d01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package PDF::Builder::Resource::XObject::Image;

use base 'PDF::Builder::Resource::XObject';

use strict;
use warnings;

our $VERSION = '3.026'; # VERSION
our $LAST_UPDATE = '3.026'; # manually update whenever code is changed

use PDF::Builder::Basic::PDF::Utils;

=head1 NAME

PDF::Builder::Resource::XObject::Image - Base class for external raster image objects

=head1 METHODS

=head2 new

    $image = PDF::Builder::Resource::XObject::Image->new($pdf, $name)

=over

Returns an image resource object.

=back

=cut

sub new {
    my ($class, $pdf, $name) = @_;

    my $self = $class->SUPER::new($pdf, $name);

    $self->subtype('Image');

    return $self;
}

=head2 width

    $width = $image->width()

=over

Get the width (in points) of the image object.

B<Note> that this function also has the ability to I<set> the width,
by giving the new width (in points), but it appears that it never 
worked correctly. The I<set> capability has been B<deprecated>, and 
is scheduled to be removed some time after October, 2025. If you are
using the C<width()> method in some manner to I<set> the image width,
please let us know, so we can plan to keep it enabled!

=back

=cut

sub width {
    my $self = shift;

    $self->{'Width'} = PDFNum(shift()) if scalar @_;
    return $self->{'Width'}->val();
}

=head2 height

    $height = $image->height()

=over

Get the height (in points) of the image object.

B<Note> that this function also has the ability to I<set> the height,
by giving the new height (in points), but it appears that it never 
worked correctly. The I<set> capability has been B<deprecated>, and 
is scheduled to be removed some time after October, 2025. If you are
using the C<height()> method in some manner to I<set> the image height,
please let us know, so we can plan to keep it enabled!

=back

=cut

sub height {
    my $self = shift;

    $self->{'Height'} = PDFNum(shift()) if scalar @_;
    return $self->{'Height'}->val();
}

## probably not useful, so do not add, for now
#=head2 bbox
#
#    ($x1,$x2, $w,$h) = $image->bbox()
#
#=over
#
#Get the image dimensions similarly to a form's I<bounding box>. 
#Note that the C<$x1> and C<$x2> values will always be 0.
#
#This method is offered as an alternative to the C<width> and C<height> methods.
#
#=back
#
#=cut
#
#sub bbox {
#    my $self = shift();
#    my @bb = (0,0, $self->width(),$self->height());
#    return @bb;
#}

=head2 smask

    $image->smask($xobject)

=over

Set the soft-mask image object.

=back

=cut

sub smask {
    my $self = shift;
    $self->{'SMask'} = shift;

    return $self;
}

=head2 mask

    $image->mask(@color_range)

    $image->mask($xobject)

=over

Set the mask to an image mask XObject or an array containing a range
of colors to be applied as a color key mask.

=back

=cut

sub mask {
    my $self = shift();

    if (ref($_[0])) {
        $self->{'Mask'} = shift();
    } else {
        $self->{'Mask'} = PDFArray(map { PDFNum($_) } @_);
    }

    return $self;
}

# imask() functionality rolled into mask()

=head2 colorspace

    $image->colorspace($name)

    $image->colorspace($array)

=over

Set the color space used by the image. Depending on the color space,
this will either be just the name of the color space, or it will be an
array containing the color space and any required parameters.

If passing an array, parameters must already be encoded as PDF
objects. The array itself may also be a PDF object. If not, one will
be created.

=back

=cut

sub colorspace {
    my ($self, @values) = @_;

    if      (scalar @values == 1 and ref($values[0])) {
        $self->{'ColorSpace'} = $values[0];
    } elsif (scalar @values == 1) {
        $self->{'ColorSpace'} = PDFName($values[0]);
    } else {
        $self->{'ColorSpace'} = PDFArray(@values);
    }

    return $self;
}

=head2 bits_per_component

    $image->bits_per_component($integer)

=over

Set the number of bits used to represent each color component.

=back

=cut

sub bits_per_component {
    my $self = shift;

    $self->{'BitsPerComponent'} = PDFNum(shift());

    return $self;
}

# bpc() renamed to bits_per_component()

1;