| 1 | package Image::Photo; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Image::Photo - Functions for sampling simple Photographic images | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | Photographic lens correction. | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | An extension of the Image::RGBA suitable for sampling photographic | ||
| 14 | images | ||
| 15 | |||
| 16 | Provided is optional radial luminance correction - Suitable for sampling | ||
| 17 | photographs where there is a known light falloff from the centre of the | ||
| 18 | image to the edges. | ||
| 19 | |||
| 20 | Also radial lens distortion can be corrected at the same time. | ||
| 21 | |||
| 22 | =head1 USAGE | ||
| 23 | |||
| 24 | You can start by creating an Image::Magick object: | ||
| 25 | |||
| 26 | my $input = new Image::Magick; | ||
| 27 | $input->Read ('input.jpg'); | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | use strict; | ||
| 32 | use warnings; | ||
| 33 | |||
| 34 | use Image::RGBA; | ||
| 35 | |||
| 36 | use vars qw /@ISA/; | ||
| 37 | @ISA = qw /Image::RGBA/; | ||
| 38 | |||
| 39 | our $VERSION = '0.01'; | ||
| 40 | |||
| 41 | =pod | ||
| 42 | |||
| 43 | Use an Image::Magick object as the basis of an Image::Photo | ||
| 44 | object: | ||
| 45 | |||
| 46 | my $rgba = new Image::Photo (sample => 'linear', | ||
| 47 | radlum => 0, | ||
| 48 | image => $input, | ||
| 49 | a => 0.0, | ||
| 50 | b => -0.2, | ||
| 51 | c => 0.0); | ||
| 52 | |||
| 53 | The parameters 'sample', 'radlum', 'a', 'b' and 'c' are quality settings | ||
| 54 | (see descriptions below). | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub new | ||
| 59 | { | ||
| 60 | my $class = shift; | ||
| 61 | $class = ref $class || $class; | ||
| 62 | |||
| 63 | my $params = {@_}; | ||
| 64 | |||
| 65 | #my $self = new Image::RGBA (%$params); | ||
| 66 | |||
| 67 | my $self = $class->SUPER::new (%$params); | ||
| 68 | |||
| 69 | # various photo calculations reuse values derived from the width | ||
| 70 | # and height. May as well calculate them at the start. | ||
| 71 | |||
| 72 | $self->{w2} = $self->{width} / 2; | ||
| 73 | $self->{h2} = $self->{height} / 2; | ||
| 74 | |||
| 75 | if ($self->{width} < $self->{height}) { $self->{diameter} = $self->{width} } | ||
| 76 | else { $self->{diameter} = $self->{height} } | ||
| 77 | |||
| 78 | $self->{radius} = $self->{diameter} / 2; | ||
| 79 | |||
| 80 | # attributes specific to photos | ||
| 81 | |||
| 82 | $self->{radlum} = $params->{radlum} || 0; | ||
| 83 | |||
| 84 | $self->{a} = $params->{a} || 0; | ||
| 85 | $self->{b} = $params->{b} || 0; | ||
| 86 | $self->{c} = $params->{c} || 0; | ||
| 87 | |||
| 88 | # bless $self, $class; | ||
| 89 | |||
| 90 | return $self; | ||
| 91 | } | ||
| 92 | |||
| 93 | =pod | ||
| 94 | |||
| 95 | Now you can retrieve a string representing the RGBA pixel values | ||
| 96 | of any point in the original image: | ||
| 97 | |||
| 98 | $values = $rgba->Pixel (20.2354, 839.6556); | ||
| 99 | |||
| 100 | Additionally, you can write RGBA pixel values directly to an image by appending | ||
| 101 | the values that need to be written: | ||
| 102 | |||
| 103 | $rgba->Pixel (22, 845, $values); | ||
| 104 | |||
| 105 | Note that locations for writing need to be integer values. | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub Pixel | ||
| 110 | { | ||
| 111 | my $self = shift; | ||
| 112 | |||
| 113 | my $m = shift; | ||
| 114 | my $n = shift; | ||
| 115 | |||
| 116 | if (scalar @_) | ||
| 117 | { | ||
| 118 | my $rgba = shift; | ||
| 119 | my $pixel_offset = (int ($m) + ($self->{width} * (int ($n) - $self->{height}))); | ||
| 120 | $self->_set_offset ($pixel_offset, $rgba); | ||
| 121 | return; | ||
| 122 | }; | ||
| 123 | |||
| 124 | # lens correction is expensive, so only do it if necessary. | ||
| 125 | |||
| 126 | ($m, $n) = $self->Correct ($m, $n) | ||
| 127 | unless ($self->{a} eq 0 && $self->{b} eq 0 && $self->{c} eq 0); | ||
| 128 | |||
| 129 | # do the actual sampling | ||
| 130 | |||
| 131 | my ($r, $g, $b, $a) = $self->_sample ($m, $n); | ||
| 132 | |||
| 133 | # radial luminance correction is expensive, so only do it if necessary. | ||
| 134 | |||
| 135 | ($r, $g, $b, $a) = $self->_adjust_luminance ($m, $n, $r, $g, $b, $a) | ||
| 136 | if ($self->{radlum}); | ||
| 137 | |||
| 138 | return $self->_pack ($r, $g, $b, $a); | ||
| 139 | } | ||
| 140 | |||
| 141 | =pod | ||
| 142 | |||
| 143 | =head1 OPTIONS | ||
| 144 | |||
| 145 | =head2 LENS CORRECTION | ||
| 146 | |||
| 147 | 'a', 'b' and 'c' parameters relate to lens correction of images. For an | ||
| 148 | explanation, see: | ||
| 149 | |||
| 150 | http://www.fh-furtwangen.de/~dersch/barrel/barrel.html | ||
| 151 | |||
| 152 | The default values are all '0', which equates to no lens correction. | ||
| 153 | |||
| 154 | In addition, the mathematical distortion can be queried directly using the | ||
| 155 | Correct method: | ||
| 156 | |||
| 157 | ($p, $q) = $self->Correct ($m, $n) | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub Correct | ||
| 162 | { | ||
| 163 | my $self = shift; | ||
| 164 | |||
| 165 | my $m = shift; | ||
| 166 | my $n = shift; | ||
| 167 | |||
| 168 | my $rd = sqrt (($m - $self->{w2}) * ($m - $self->{w2}) + ($n - $self->{h2}) * ($n - $self->{h2})) | ||
| 169 | / $self->{radius}; | ||
| 170 | |||
| 171 | my $foo = $self->{a} * $rd * $rd * $rd | ||
| 172 | + $self->{b} * $rd * $rd | ||
| 173 | + $self->{c} * $rd | ||
| 174 | + 1 - $self->{a} - $self->{b} - $self->{c}; | ||
| 175 | |||
| 176 | $m = (($m - $self->{w2}) * $foo) + $self->{w2}; | ||
| 177 | $n = (($n - $self->{h2}) * $foo) + $self->{h2}; | ||
| 178 | |||
| 179 | return ($m, $n); | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | =pod | ||
| 184 | |||
| 185 | =head2 RADIAL LUMINANCE | ||
| 186 | |||
| 187 | The 'radlum' value can be used to fix radial luminance variation in the | ||
| 188 | source image. Typically a photograph will be brighter in the centre | ||
| 189 | than at the edges - A small positive number, eg. '10', will try to | ||
| 190 | correct for this. | ||
| 191 | |||
| 192 | The number represents the difference in luminance between the centre and the | ||
| 193 | nearest edge, the units assume a range of 256 between black and white. | ||
| 194 | |||
| 195 | The default is '0', no radial luminance correction. | ||
| 196 | |||
| 197 | Radial luminance correction is loosely based on that provided by the | ||
| 198 | Panorama Tools Correct plugin, with a couple of variations that should | ||
| 199 | make it more suitable for batch processing images. | ||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | sub _adjust_luminance | ||
| 204 | { | ||
| 205 | my $self = shift; | ||
| 206 | |||
| 207 | my ($m, $n, $r, $g, $b, $a) = @_; | ||
| 208 | |||
| 209 | my $factor = $self->_calc_luminance ($m, $n); | ||
| 210 | |||
| 211 | $r *= $factor; $g *= $factor; $b *= $factor; | ||
| 212 | |||
| 213 | # adjusting luminance may send some pixels out-of-range | ||
| 214 | |||
| 215 | $self->_valid_pixel_values ($r, $g, $b, $a); | ||
| 216 | } | ||
| 217 | |||
| 218 | sub _calc_luminance | ||
| 219 | { | ||
| 220 | my $self = shift; | ||
| 221 | |||
| 222 | my $m = shift; | ||
| 223 | my $n = shift; | ||
| 224 | |||
| 225 | # The first bit is the same method as ptools 'correct' | ||
| 226 | |||
| 227 | $m = $m - $self->{w2}; | ||
| 228 | $n = $n - $self->{h2}; | ||
| 229 | |||
| 230 | my $k = ($self->{radlum} / 2) | ||
| 231 | - ((($m * $m) + ($n * $n)) * ($self->{radlum} / ($self->{radius} * $self->{radius}))); | ||
| 232 | |||
| 233 | # ptools just subtracts $k from the colour values, shifting the | ||
| 234 | # brightness | ||
| 235 | # $r -= $k; $g -= $k; $b -= $k; | ||
| 236 | # alternative method scales rather than shifts values | ||
| 237 | |||
| 238 | return 1 - ($k / 127); | ||
| 239 | } | ||
| 240 | |||
| 241 | =pod | ||
| 242 | |||
| 243 | =head1 COPYRIGHT | ||
| 244 | |||
| 245 | Copyright (c) 2002 Bruno Postle <bruno@postle.net>. All Rights Reserved. | ||
| 246 | This module is Free Software. It may be used, redistributed and/or | ||
| 247 | modified under the same terms as Perl itself. | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | 1; | ||
| 252 |