| 1 | package Image::Similar; | ||
| 2 | 6 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 6 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 6 | ▶ | use base 'Exporter'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | our @EXPORT_OK = qw/ | ||
| 6 | load_image | ||
| 7 | load_signature | ||
| 8 | /; | ||
| 9 | our %EXPORT_TAGS = ( | ||
| 10 | all => \@EXPORT_OK, | ||
| 11 | ); | ||
| 12 | |||
| 13 | 0 | ▶ | use Image::PNG::Libpng; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use Image::PNG::Const ':all'; | ||
| 15 | use Scalar::Util 'looks_like_number'; | ||
| 16 | use Carp; | ||
| 17 | |||
| 18 | our $VERSION = '0.07'; | ||
| 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<Imager> 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 Image::Imlib2. | ||
| 98 | |||
| 99 | sub load_image_imlib2 | ||
| 100 | { | ||
| 101 | my ($imlib2, %options) = @_; | ||
| 102 | my $height = $imlib2->height (); | ||
| 103 | my $width = $imlib2->width (); | ||
| 104 | my $is = Image::Similar->new (height => $height, width => $width); | ||
| 105 | my $image = $is->{image}; | ||
| 106 | for my $y (0..$height - 1) { | ||
| 107 | for my $x (0..$width - 1) { | ||
| 108 | my ($r, $g, $b, $a) = $imlib2->query_pixel ($x, $y); | ||
| 109 | my $greypixel = round (red * $r + green * $g + blue * $b); | ||
| 110 | $image->set_pixel ($x, $y, $greypixel); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | return $is; | ||
| 114 | } | ||
| 115 | |||
| 116 | # Load an image assuming it's from Imager. | ||
| 117 | |||
| 118 | sub load_image_imager | ||
| 119 | { | ||
| 120 | my ($imager, %options) = @_; | ||
| 121 | my $grey = $imager->convert (preset => 'gray'); | ||
| 122 | if ($options{make_grey_png}) { | ||
| 123 | $grey->write (file => $options{make_grey_png}); | ||
| 124 | } | ||
| 125 | my $height = $grey->getheight (); | ||
| 126 | my $width = $grey->getwidth (); | ||
| 127 | my $is = Image::Similar->new (height => $height, width => $width); | ||
| 128 | for my $y (0..$height - 1) { | ||
| 129 | # print "$y\n"; | ||
| 130 | my @scanline = $grey->getscanline (y => $y); | ||
| 131 | for my $x (0..$width - 1) { | ||
| 132 | # Dunno a better way to do this, please shout if you do. | ||
| 133 | my ($greypixel, undef, undef, undef) = $scanline[$x]->rgba (); | ||
| 134 | if ($greypixel < 0 || $greypixel > maxgreypixel) { | ||
| 135 | carp "Pixel value $greypixel at $x, $y is not allowed, need 0-255 here"; | ||
| 136 | next; | ||
| 137 | } | ||
| 138 | # print "x, y, grey = $x $y $greypixel\n"; | ||
| 139 | $is->{image}->set_pixel ($x, $y, $greypixel); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | return $is; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub rgb_to_grey | ||
| 146 | { | ||
| 147 | my ($r, $g, $b) = @_; | ||
| 148 | my $grey = red * $r + green * $g + blue * $b; | ||
| 149 | $grey = round ($grey); | ||
| 150 | return $grey; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub load_image_libpng | ||
| 154 | { | ||
| 155 | my ($image) = @_; | ||
| 156 | my $ihdr = $image->get_IHDR (); | ||
| 157 | my $height = $ihdr->{height}; | ||
| 158 | my $width = $ihdr->{width}; | ||
| 159 | my $is = Image::Similar->new (height => $height, | ||
| 160 | width => $width); | ||
| 161 | my $rows = $image->get_rows (); | ||
| 162 | if ($ihdr->{bit_depth} != png_bit_depth) { | ||
| 163 | carp "Cannot handle PNG images of bit depth $ihdr->{bit_depth}"; | ||
| 164 | return undef; | ||
| 165 | } | ||
| 166 | if ($ihdr->{color_type} == PNG_COLOR_TYPE_GRAY) { | ||
| 167 | # GRAY | ||
| 168 | for my $y (0..$height-1) { | ||
| 169 | for my $x (0..$width-1) { | ||
| 170 | my $grey = ord (substr ($rows->[$y], $x, 1)); | ||
| 171 | $is->{image}->set_pixel ($x, $y, $grey); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | } | ||
| 175 | elsif ($ihdr->{color_type} == PNG_COLOR_TYPE_GRAY_ALPHA) { | ||
| 176 | # GRAY_ALPHA | ||
| 177 | # carp 'Discarding alpha channel and ignoring background'; | ||
| 178 | for my $y (0..$height-1) { | ||
| 179 | for my $x (0..$width-1) { | ||
| 180 | my $grey = ord (substr ($rows->[$y], $x * 2, 1)); | ||
| 181 | $is->{image}->set_pixel ($x, $y, $grey); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
| 185 | elsif ($ihdr->{color_type} == PNG_COLOR_TYPE_RGB || | ||
| 186 | $ihdr->{color_type} == PNG_COLOR_TYPE_RGB_ALPHA) { | ||
| 187 | # RGB or RGBA | ||
| 188 | |||
| 189 | # $offset is the number of bytes per pixel. | ||
| 190 | my $offset = rgb_bytes; | ||
| 191 | if ($ihdr->{color_type} == PNG_COLOR_TYPE_RGB_ALPHA) { | ||
| 192 | $offset = rgba_bytes; | ||
| 193 | # We should try to use the alpha channel to blend in a | ||
| 194 | # background colour here, but we don't. | ||
| 195 | # carp 'Discarding alpha channel and ignoring background'; | ||
| 196 | } | ||
| 197 | for my $y (0..$height-1) { | ||
| 198 | for my $x (0..$width-1) { | ||
| 199 | my $r = ord (substr ($rows->[$y], $x * $offset, 1)); | ||
| 200 | my $g = ord (substr ($rows->[$y], $x * $offset + 1, 1)); | ||
| 201 | my $b = ord (substr ($rows->[$y], $x * $offset + 2, 1)); | ||
| 202 | # https://metacpan.org/pod/distribution/Imager/lib/Imager/Transformations.pod | ||
| 203 | my $grey = rgb_to_grey ($r, $g, $b); | ||
| 204 | $is->{image}->set_pixel ($x, $y, $grey); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | elsif ($ihdr->{color_type} == PNG_COLOR_TYPE_PALETTE) { | ||
| 209 | my $palette = $image->get_PLTE (); | ||
| 210 | my @grey; | ||
| 211 | my $i = 0; | ||
| 212 | for my $colour (@{$palette}) { | ||
| 213 | my $r = $colour->{red}; | ||
| 214 | my $g = $colour->{green}; | ||
| 215 | my $b = $colour->{blue}; | ||
| 216 | $grey[$i] = rgb_to_grey ($r, $g, $b); | ||
| 217 | $i++; | ||
| 218 | } | ||
| 219 | for my $y (0..$height-1) { | ||
| 220 | for my $x (0..$width-1) { | ||
| 221 | my $grey = $grey [ord (substr ($rows->[$y], $x, 1))]; | ||
| 222 | $is->{image}->set_pixel ($x, $y, $grey); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | else { | ||
| 227 | carp "Cannot handle image of colour type $ihdr->{color_type}"; | ||
| 228 | return undef; | ||
| 229 | } | ||
| 230 | return $is; | ||
| 231 | } | ||
| 232 | |||
| 233 | sub load_image | ||
| 234 | { | ||
| 235 | my ($image) = @_; | ||
| 236 | my $is; | ||
| 237 | my $imtype = ref $image; | ||
| 238 | if ($imtype eq 'Imager') { | ||
| 239 | $is = load_image_imager ($image); | ||
| 240 | } | ||
| 241 | elsif ($imtype eq 'Image::PNG::Libpng') { | ||
| 242 | $is = load_image_libpng ($image); | ||
| 243 | } | ||
| 244 | elsif ($imtype eq 'GD::Image') { | ||
| 245 | $is = load_image_gd ($image); | ||
| 246 | } | ||
| 247 | elsif ($imtype eq 'Image::Imlib2') { | ||
| 248 | $is = load_image_imlib2 ($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; | ||