RGBA.pm untested

filter: Statementstmt 0.0% 0 / 140 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 240 CC 20 SCAR 34.9
CC
20
Cov
0%
_valid_pixel_values
90.0
_sample
30.0
_simple
30.0
CRAP 32.8
1 package Image::RGBA;
2  
3 =head1 NAME
4  
5 Image::RGBA - Functions for sampling simple RGBA images
6  
7 =head1 SYNOPSIS
8  
9 'simple', 'bilinear' and 'bicubic' image sampling.
10  
11 =head1 DESCRIPTION
12  
13 Hides some of the ugly stuff involved when sampling individual pixel
14 values from images. A good range of quality levels are provided,
15 currently; simple, linear and spline16.
16  
17 For an explanation of what is going on, see:
18  
19 http://www.fh-furtwangen.de/~dersch/interpolator/interpolator.html
20  
21 An RGBA image file is very simple, just each channel stored one after
22 the other with no delimiters for each pixel in turn. There is no header
23 data, so you have to know the image dimensions to reconstruct an RGBA
24 image.
25  
26 =head1 USAGE
27  
28 You can start by creating an Image::Magick object:
29  
30 my $input = new Image::Magick;
31 $input->Read ('input.jpg');
32  
33 =cut
34  
35 use strict;
36 use warnings;
37  
38 use Image::Magick;
39  
40 our $VERSION = '0.04';
41  
42 =pod
43  
44 Use an Image::Magick object as the basis of an Image::RGBA
45 object:
46  
47 my $rgba = new Image::RGBA (sample => 'linear',
48 image => $input);
49  
50 =cut
51  
52 sub new
53 {
54 my $class = shift;
55 $class = ref $class || $class;
56  
57 my $params = {@_};
58  
59 my $imagemagick = $params->{image};
60  
61 my $self;
62  
63 # we can get the width and height from the source image
64  
65 $self->{height} = $imagemagick->Get('height');
66 $self->{width} = $imagemagick->Get('width');
67  
68 # this is the raw rgba data
69  
70 $self->{blob} = _imagetoblob ($imagemagick);
71  
72 # a sensible default sampling level
73  
74 $self->{sample} = $params->{sample} || 'linear';
75  
76 bless $self, $class;
77  
78 return $self;
79 }
80  
81 =pod
82  
83 Now you can retrieve a string representing the RGBA pixel values
84 of any point in the original image:
85  
86 $values = $rgba->Pixel (20.2354, 839.6556);
87  
88 Additionally, you can write RGBA pixel values directly to an image by appending
89 the values that need to be written:
90  
91 $rgba->Pixel (22, 845, $values);
92  
93 Note that locations for writing need to be integer values.
94  
95 =cut
96  
97 sub Pixel
98 {
99 my $self = shift;
100  
101 my $m = shift;
102 my $n = shift;
103  
104 if (scalar @_)
105 {
106 my $rgba = shift;
107  
108 my $pixel_offset = (int ($m) + ($self->{width} * (int ($n) - $self->{height})));
109  
110 $self->_set_offset ($pixel_offset, $rgba);
111  
112 return;
113 };
114  
115 my ($r, $g, $b, $a) = $self->_sample ($m, $n);
116  
117 return $self->_pack ($r, $g, $b, $a);
118 }
119  
120 =pod
121  
122 You can access the image as an Image::Magick object at any time using the Image
123 method:
124  
125 $rgba->Image->Write ('filename.jpg');
126  
127 =cut
128  
129 sub Image
130 {
131 my $self = shift;
132  
133 $self->_blobtoimage;
134 }
135  
136 =pod
137  
138 =head1 OPTIONS
139  
140 =head2 SAMPLING TYPES
141  
142 =cut
143  
144 sub _sample
145 {
146 my $self = shift;
147  
148 my $m = shift;
149 my $n = shift;
150  
151 =pod
152  
153 Note that trying to sample values physically outside of the source image will
154 return a black/transparent pixel value consisting of null bytes.
155  
156 =cut
157  
158 return (0, 0, 0, 0)
159 if ($m < 0 or $m > $self->{width} or $n < 0 or $n > $self->{height});
160  
161 my ($r, $g, $b, $a);
162  
163 if ($self->{sample} eq 'simple')
164 { ($r, $g, $b, $a) = $self->_simple ($m, $n) }
165  
166 elsif ($self->{sample} eq 'linear')
167 { ($r, $g, $b, $a) = $self->_linear ($m, $n) }
168  
169 elsif ($self->{sample} eq 'spline16')
170 { ($r, $g, $b, $a) = $self->_spline16 ($m, $n) }
171  
172 return ($r, $g, $b, $a);
173 }
174  
175 =pod
176  
177 'simple' sampling is crude non-interpolated pixel sampling, equivalent
178 to the Image::Magick::Get ("pixel[$x,$y]") method. Use this when speed
179 rather than quality is required.
180  
181 =cut
182  
183 sub _simple
184 {
185 my $self = shift;
186  
187 my $m = shift;
188 my $n = shift;
189  
190 # find the nearest pixel if it's over the edge of the source image
191  
192 $m = 0 if ($m < 0);
193 $n = 0 if ($n < 0);
194 $m = $self->{width} - 1 if ($m > $self->{width} - 1);
195 $n = $self->{height} - 1 if ($n > $self->{height} - 1);
196  
197 # get raw rgba value corresponding to $m and $n
198  
199 my $pixel_offset = (int ($m) + ($self->{width} * (int ($n) - $self->{height})));
200  
201 my $rgba = $self->_get_offset ($pixel_offset);
202  
203 $self->_unpack ($rgba);
204 }
205  
206 =pod
207  
208 'linear' sampling is fast general purpose pixel sampling, about 3
209 times slower than 'simple' sampling'. Pixel values are interpolated, so
210 sampling pixel (45.5, 56.6) will get different results to pixel (45,
211 56).
212  
213 =cut
214  
215 sub _linear
216 {
217 my $self = shift;
218  
219 my $m = shift;
220 my $n = shift;
221  
222 my ($r, $g, $b, $a);
223  
224 for my $v (0 .. 1)
225 {
226 for my $u (0 .. 1)
227 {
228 my ($r0, $g0, $b0, $a0) = $self->_simple ($m + $u, $n + $v);
229  
230 my $weightxy = (1 - abs ($m - int ($m) - $u)) * (1 - abs ($n - int ($n) - $v));
231  
232 $r += $r0 * $weightxy;
233 $g += $g0 * $weightxy;
234 $b += $b0 * $weightxy;
235 $a += $a0 * $weightxy;
236 }
237 }
238  
239 return ($r, $g, $b, $a);
240 }
241  
242 =pod
243  
244 'spline16' sampling is slow high-quality sampling, about 15 times
245 slower than 'simple' sampling. Interpolated pixel values are just a
246 little bit higher quality than 'linear'.
247  
248 =cut
249  
250 sub _spline16
251 {
252 my $self = shift;
253  
254 my $m = shift;
255 my $n = shift;
256  
257 my ($r, $g, $b, $a);
258  
259 for my $v (-1 .. 2)
260 {
261 for my $u (-1 .. 2)
262 {
263 my ($r0, $g0, $b0, $a0) = $self->_simple ($m + $u, $n + $v);
264  
265 my $x = abs ($m - int ($m) - $u);
266 my $y = abs ($n - int ($n) - $v);
267  
268 my ($weightx, $weighty);
269
270 if ($x >= 1 && $x <= 2)
271 { $weightx = ((((12 - (5 * ($x - 1))) / 15) * ($x - 1)) - (7/15)) * ($x - 1) }
272  
273 else
274 { $weightx = (((($x - 1.8) * $x) - 0.2) * $x) + 1 }
275  
276 if ($y >= 1 && $y <= 2)
277 { $weighty = ((((12 - (5 * ($y - 1))) / 15) * ($y - 1)) - (7/15)) * ($y - 1) }
278  
279 else
280 { $weighty = (((($y - 1.8) * $y) - 0.2) * $y) + 1 }
281  
282 my $weightxy = $weightx * $weighty;
283  
284 $r += $r0 * $weightxy;
285 $g += $g0 * $weightxy;
286 $b += $b0 * $weightxy;
287 $a += $a0 * $weightxy;
288 }
289 }
290  
291 # fix minor floating point errors, yes this is ugly.
292
293 $r += 0.001;
294 $g += 0.001;
295 $b += 0.001;
296 $a += 0.001;
297  
298 ($r, $g, $b, $a) = $self->_valid_pixel_values ($r, $g, $b, $a);
299  
300 return ($r, $g, $b, $a);
301 }
302  
303 # various operations, curve fitting and brightness correction, can create
304 # non valid <0 >255 values, fix'em.
305 #
306 # FIXME should support other than 1 byte per channel
307  
308 sub _valid_pixel_values
309 {
310 my $self = shift;
311  
312 my ($r, $g, $b, $a) = @_;
313  
314 $r = 255 if ($r > 255);
315 $g = 255 if ($g > 255);
316 $b = 255 if ($b > 255);
317 $a = 255 if ($a > 255);
318  
319 $r = 0 if ($r < 0);
320 $g = 0 if ($g < 0);
321 $b = 0 if ($b < 0);
322 $a = 0 if ($a < 0);
323  
324 return ($r, $g, $b, $a);
325 }
326  
327 # take an array of pixel values and return packed bytes
328 #
329 # FIXME should support other than 1 byte per pixel
330
331 sub _pack
332 {
333 my $self = shift;
334  
335 my ($r, $g, $b, $a) = @_;
336  
337 pack ("CCCC", int ($r), int ($g), int ($b), int ($a));
338 }
339  
340 # take packed bytes and return an array of pixel values
341 #
342 # FIXME should support other than 1 byte per channel
343  
344 sub _unpack
345 {
346 my $self = shift;
347  
348 my $rgba = shift;
349
350 map ord (substr $rgba, $_), (0, 1, 2, 3);
351 }
352  
353 # retrieve raw bytes for a particular offset
354 #
355 # FIXME should support other than 1 byte per channel
356  
357 sub _get_offset
358 {
359 my $self = shift;
360  
361 my $pixel_offset = shift;
362  
363 substr ${$self->{blob}}, 4 * $pixel_offset, 4;
364 }
365  
366 # sets and retrieves raw bytes for a particular offset
367 #
368 # FIXME should support other than 1 byte per channel
369  
370 sub _set_offset
371 {
372 my $self = shift;
373  
374 my $pixel_offset = shift;
375 my $rgba = shift;
376  
377 substr ${$self->{blob}}, 4 * $pixel_offset, 4, $rgba;
378 }
379  
380 # only used in new(). converts from imagemagick to a simpler format
381 #
382 # FIXME should support other than 1 byte per pixel
383  
384 sub _imagetoblob
385 {
386 my $imagemagick = shift;
387  
388 $imagemagick->Set (magick => 'RGBA', depth => '8');
389 \$imagemagick->ImageToBlob;
390 }
391  
392 # used when we have an Image::RGBA object but we really need an
393 # Image::Magick object
394 #
395 # FIXME should support other than 1 byte per pixel
396  
397 sub _blobtoimage
398 {
399 my $self = shift;
400  
401 my $imagemagick = new Image::Magick (magick => 'RGBA',
402 depth => '8',
403 size => $self->{width} ."x". $self->{height});
404  
405 $imagemagick->BlobToImage (${$self->{blob}});
406  
407 return $imagemagick;
408 }
409  
410 =pod
411  
412 =head1 COPYRIGHT
413  
414 Copyright (c) 2002 Bruno Postle <bruno@postle.net>. All Rights Reserved.
415 This module is Free Software. It may be used, redistributed and/or
416 modified under the same terms as Perl itself.
417  
418 =cut
419  
420 1;
421