File Coverage

blib/lib/Image/Similar.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Image::Similar;
2 5     5   59655 use warnings;
  5         8  
  5         193  
3 5     5   21 use strict;
  5         5  
  5         100  
4 5     5   18 use base 'Exporter';
  5         10  
  5         544  
5             our @EXPORT_OK = qw/
6             load_image
7             load_signature
8             /;
9             our %EXPORT_TAGS = (
10             all => \@EXPORT_OK,
11             );
12              
13 5     5   3365 use Image::PNG::Libpng;
  0            
  0            
14             use Image::PNG::Const ':all';
15             use Scalar::Util 'looks_like_number';
16             use Carp;
17              
18             our $VERSION = '0.05';
19             require XSLoader;
20             XSLoader::load ('Image::Similar', $VERSION);
21              
22             use constant {
23             # Constants used for combining red, green, and blue values. These
24             # values are taken from the L source code.
25             red => 0.222,
26             green => 0.707,
27             blue => 0.071,
28             # png bit depth
29             png_bit_depth => 8,
30             # bytes per pixel for rgb
31             rgb_bytes => 3,
32             # bytes per pixel for rgba
33             rgba_bytes => 4,
34             # Maximum possible grey pixel
35             maxgreypixel => 255,
36             # For "round".
37             half => 0.5,
38             };
39              
40              
41             sub round
42             {
43             my ($float) = @_;
44             return int ($float + half);
45             }
46              
47             sub new
48             {
49             my ($class, %options) = @_;
50             my $is = {};
51             for my $field (qw/height width/) {
52             if ($options{$field}) {
53             if (! looks_like_number ($options{$field})) {
54             carp "$field value doesn't look like a number";
55             }
56             $is->{$field} = $options{$field};
57             }
58             else {
59             carp "Missing option $field";
60             return;
61             }
62             }
63             # print "$is->{height} $is->{width}\n";
64             $is->{image} = Image::Similar::Image::isnew ($is->{width}, $is->{height});
65             # print "Finished isnew with $is->{image}\n";
66             bless $is, $class;
67             return $is;
68             }
69              
70             sub fill_grid
71             {
72             my ($s) = @_;
73             $s->{image}->fill_grid ();
74             return;
75             }
76              
77             # Load an image assuming it's from GD.
78              
79             sub load_image_gd
80             {
81             my ($gd, %options) = @_;
82             my ($width, $height) = $gd->getBounds ();
83             my $is = Image::Similar->new (height => $height, width => $width);
84             my $image = $is->{image};
85             for my $y (0..$height - 1) {
86             for my $x (0..$width - 1) {
87             my $index = $gd->getPixel ($x, $y);
88             my ($r, $g, $b) = $gd->rgb ($index);
89             my $greypixel = round (red * $r + green * $g + blue * $b);
90             # print "$x $y $r $g $b $greypixel\n";
91             $image->set_pixel ($x, $y, $greypixel);
92             }
93             }
94             return $is;
95             }
96              
97             # Load an image assuming it's from Imager.
98              
99             sub load_image_imager
100             {
101             my ($imager, %options) = @_;
102             my $grey = $imager->convert (preset => 'gray');
103             if ($options{make_grey_png}) {
104             $grey->write (file => $options{make_grey_png});
105             }
106             my $height = $grey->getheight ();
107             my $width = $grey->getwidth ();
108             my $is = Image::Similar->new (height => $height, width => $width);
109             for my $y (0..$height - 1) {
110             # print "$y\n";
111             my @scanline = $grey->getscanline (y => $y);
112             for my $x (0..$width - 1) {
113             # Dunno a better way to do this, please shout if you do.
114             my ($greypixel, undef, undef, undef) = $scanline[$x]->rgba ();
115             if ($greypixel < 0 || $greypixel > maxgreypixel) {
116             carp "Pixel value $greypixel at $x, $y is not allowed, need 0-255 here";
117             next;
118             }
119             # print "x, y, grey = $x $y $greypixel\n";
120             $is->{image}->set_pixel ($x, $y, $greypixel);
121             }
122             }
123             return $is;
124             }
125              
126             # # C<$libpng_ok> is set to a true value if Image::PNG::Libpng has
127             # # already successfully been loaded.
128              
129             # my $libpng_ok;
130              
131             # # Load Image::PNG::Libpng.
132              
133             # sub load_libpng
134             # {
135             # if ($libpng_ok) {
136             # return 1;
137             # }
138             # my $use_ok = eval "use Image::PNG::Libpng;";
139             # if (! $use_ok || $@) {
140             # carp "Error loading Image::PNG::Libpng: $@";
141             # return;
142             # }
143             # $libpng_ok = 1;
144             # return 1;
145             # }
146              
147             sub rgb_to_grey
148             {
149             my ($r, $g, $b) = @_;
150             my $grey = red * $r + green * $g + blue * $b;
151             $grey = round ($grey);
152             return $grey;
153             }
154              
155             sub load_image_libpng
156             {
157             my ($image) = @_;
158             # load_libpng () or return;
159             my $ihdr = $image->get_IHDR ();
160             my $height = $ihdr->{height};
161             my $width = $ihdr->{width};
162             my $is = Image::Similar->new (height => $height,
163             width => $width);
164             my $rows = $image->get_rows ();
165             if ($ihdr->{bit_depth} != png_bit_depth) {
166             carp "Cannot handle PNG images of bit depth $ihdr->{bit_depth}";
167             return undef;
168             }
169             if ($ihdr->{color_type} == PNG_COLOR_TYPE_GRAY) {
170             # GRAY
171             for my $y (0..$height-1) {
172             for my $x (0..$width-1) {
173             my $grey = ord (substr ($rows->[$y], $x, 1));
174             $is->{image}->set_pixel ($x, $y, $grey);
175             }
176             }
177             }
178             elsif ($ihdr->{color_type} == PNG_COLOR_TYPE_GRAY_ALPHA) {
179             # GRAY_ALPHA
180             carp 'Discarding alpha channel and ignoring background';
181             for my $y (0..$height-1) {
182             for my $x (0..$width-1) {
183             my $grey = ord (substr ($rows->[$y], $x * 2, 1));
184             $is->{image}->set_pixel ($x, $y, $grey);
185             }
186             }
187             }
188             elsif ($ihdr->{color_type} == PNG_COLOR_TYPE_RGB ||
189             $ihdr->{color_type} == PNG_COLOR_TYPE_RGB_ALPHA) {
190             # RGB or RGBA
191              
192             # $offset is the number of bytes per pixel.
193             my $offset = rgb_bytes;
194             if ($ihdr->{color_type} == PNG_COLOR_TYPE_RGB_ALPHA) {
195             $offset = rgba_bytes;
196             # We should try to use the alpha channel to blend in a
197             # background colour here, but we don't.
198             carp 'Discarding alpha channel and ignoring background';
199             }
200             for my $y (0..$height-1) {
201             for my $x (0..$width-1) {
202             my $r = ord (substr ($rows->[$y], $x * $offset, 1));
203             my $g = ord (substr ($rows->[$y], $x * $offset + 1, 1));
204             my $b = ord (substr ($rows->[$y], $x * $offset + 2, 1));
205             # https://metacpan.org/pod/distribution/Imager/lib/Imager/Transformations.pod
206             my $grey = rgb_to_grey ($r, $g, $b);
207             $is->{image}->set_pixel ($x, $y, $grey);
208             }
209             }
210             }
211             elsif ($ihdr->{color_type} == PNG_COLOR_TYPE_PALETTE) {
212             my $palette = $image->get_PLTE ();
213             my @grey;
214             my $i = 0;
215             for my $colour (@{$palette}) {
216             my $r = $colour->{red};
217             my $g = $colour->{green};
218             my $b = $colour->{blue};
219             $grey[$i] = rgb_to_grey ($r, $g, $b);
220             $i++;
221             }
222             for my $y (0..$height-1) {
223             for my $x (0..$width-1) {
224             my $grey = $grey [ord (substr ($rows->[$y], $x, 1))];
225             $is->{image}->set_pixel ($x, $y, $grey);
226             }
227             }
228             }
229             else {
230             carp "Cannot handle image of colour type $ihdr->{color_type}";
231             return undef;
232             }
233             return $is;
234             }
235              
236             sub load_image
237             {
238             my ($image) = @_;
239             my $is;
240             my $imtype = ref $image;
241             if ($imtype eq 'Imager') {
242             $is = load_image_imager ($image);
243             }
244             elsif ($imtype eq 'Image::PNG::Libpng') {
245             $is = load_image_libpng ($image);
246             }
247             elsif ($imtype eq 'GD::Image') {
248             $is = load_image_gd ($image);
249             }
250             else {
251             carp "Unknown object type $imtype, cannot load this image";
252             return undef;
253             }
254             $is->fill_grid ();
255             return $is;
256             }
257              
258             sub load_signature
259             {
260             my ($sig) = @_;
261             my $is = bless {}, 'Image::Similar';
262             $is->{image} = Image::Similar::Image::fill_from_sig ($sig);
263             return $is;
264             }
265              
266             sub sig_diff
267             {
268             my ($is, $sig) = @_;
269             # Get the signature out of the image
270             my $image1 = $is->{image};
271             my $image2 = Image::Similar::Image::fill_from_sig ($sig);
272             # Compare the two signatures and put the result in "diff".
273             my $diff = Image::Similar::Image::image_diff ($image1, $image2);
274             return $diff;
275             }
276              
277             sub Image::Similar::write_png
278             {
279             my ($is, $filename) = @_;
280             if ($is->{image}->valid_image ()) {
281             # load_libpng () or return;
282             my $png = Image::PNG::Libpng::create_write_struct ();
283             $png->set_IHDR ({
284             height => $is->{height},
285             width => $is->{width},
286             bit_depth => 8,
287             color_type => 0, # Image::PNG::Const::PNG_COLOR_TYPE_GRAY,
288             });
289             my $rows = $is->{image}->get_rows ();
290             if (scalar (@{$rows}) != $is->{height}) {
291             die "Error: bad numbers: $is->{height} != " . scalar (@{$rows});
292             }
293             $png->set_rows ($rows);
294             $png->write_png_file ($filename);
295             }
296             else {
297             carp 'This object does not contain valid image data';
298             }
299             return;
300             }
301              
302             sub diff
303             {
304             my ($s1, $s2) = @_;
305             return $s1->{image}->image_diff ($s2->{image});
306             }
307              
308             sub signature
309             {
310             my ($s) = @_;
311             return $s->{image}->signature ();
312             }
313              
314             1;