| 1 | package Image::Compare::IMAGE; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Imager ':handy'; | ||
| 7 | use Imager::Fountain; | ||
| 8 | |||
| 9 | use base qw/Image::Compare::Comparator/; | ||
| 10 | |||
| 11 | use constant FILL_IMAGE_CACHE => {}; | ||
| 12 | use constant FILL_IMAGE_WIDTH => 1000; | ||
| 13 | use constant DEFAULT_COLOR_FOUNTAIN => Imager::Fountain->simple( | ||
| 14 | positions => [ 0.0, 0.5, 1.0], | ||
| 15 | colors => [NC(255, 0, 0), NC(0, 255, 0), NC(0, 0, 255)], | ||
| 16 | ); | ||
| 17 | use constant DEFAULT_GREYSCALE_FOUNTAIN => Imager::Fountain->simple( | ||
| 18 | positions => [ 0.0, 1.0], | ||
| 19 | colors => [NC(0, 0, 0), NC(255, 255, 255)], | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub setup { | ||
| 23 | my $self = shift; | ||
| 24 | $self->SUPER::setup(@_); | ||
| 25 | $self->{count} = 0; | ||
| 26 | $self->{img} = Imager->new( | ||
| 27 | xsize => $_[0]->getwidth(), | ||
| 28 | ysize => $_[0]->getheight(), | ||
| 29 | ); | ||
| 30 | # Now we build up our fill image; this is a 1000 x 1 image that is filled | ||
| 31 | # using an Imager fountain. We'll pull pixels from it to put colors into | ||
| 32 | # the generated image. | ||
| 33 | my $fountain; | ||
| 34 | if ($self->{args}) { | ||
| 35 | # If there's an argument, it's either a fountain, or it's telling us to use | ||
| 36 | # the default color fountain. | ||
| 37 | if (ref $self->{args} && $self->{args}->isa('Imager::Fountain')) { | ||
| 38 | $fountain = $self->{args}; | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | $fountain = DEFAULT_COLOR_FOUNTAIN(); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | # In this case, we use the default greyscale fountain. | ||
| 46 | $fountain = DEFAULT_GREYSCALE_FOUNTAIN(); | ||
| 47 | } | ||
| 48 | # Using an object as a hash key stringifies it to its memory location and | ||
| 49 | # uses that. This is suboptimal, but the Imager::Fountain class has no | ||
| 50 | # "to_string", so we make do. | ||
| 51 | unless (FILL_IMAGE_CACHE()->{$fountain}) { | ||
| 52 | my $image = Imager->new(xsize => FILL_IMAGE_WIDTH(), ysize => 1); | ||
| 53 | $image->filter( | ||
| 54 | type => 'fountain', segments => $fountain, | ||
| 55 | xa => 0, ya => 0, | ||
| 56 | xb => FILL_IMAGE_WIDTH(), yb => 0, | ||
| 57 | ); | ||
| 58 | FILL_IMAGE_CACHE()->{$fountain} = $image; | ||
| 59 | } | ||
| 60 | $self->{fill_image} = FILL_IMAGE_CACHE()->{$fountain}; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub accumulate { | ||
| 64 | my $self = shift; | ||
| 65 | my($pix1, $pix2, $x, $y) = @_; | ||
| 66 | my $diff = $self->color_distance($pix1, $pix2); | ||
| 67 | # We want to convert from our range of values for $diff (0 .. 441.7) to | ||
| 68 | # the range of values of our fill image, which is defined by a constant. | ||
| 69 | $diff *= FILL_IMAGE_WIDTH(); | ||
| 70 | $diff /= 441.7; | ||
| 71 | my $color = $self->{fill_image}->getpixel( | ||
| 72 | x => $diff, | ||
| 73 | y => 0, | ||
| 74 | ); | ||
| 75 | $self->{img}->setpixel( | ||
| 76 | x => $x, | ||
| 77 | y => $y, | ||
| 78 | color => $color, | ||
| 79 | ); | ||
| 80 | return undef; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub get_result { | ||
| 84 | my $self = shift; | ||
| 85 | return $self->{img}; | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | |||
| 90 | __END__ |