| 1 | package Image::PHash; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.008; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | no warnings "portable"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Config; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Math::DCT 'dct2d'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | our $VERSION = '0.1'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | Image::PHash - Fast perceptual image hashing (DCT-based pHash) | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | use Image::PHash; | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # Load an image and prepare to hash. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # Will try to find an image library to load and resize the image to 32x32, ready for the DCT step | ||||||||||||||||||||||||||||||||||||||||||
| 24 | my $iph = Image::PHash->new($image_file); | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | # Calculate the perceptual hash (top 8x8 of the DCT) - 64 bits, 16 hex chars | ||||||||||||||||||||||||||||||||||||||||||
| 27 | my $p = $iph->pHash(); # implies settings geometry => '8x8', method => 'average' | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | # Alternative, better performing, 64 bit hash (64 upper-left most DCT values) | ||||||||||||||||||||||||||||||||||||||||||
| 30 | my $p = $iph->pHash(geometry => 64); # this is actually recommended over the default hash | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | # Alternative method for creating the bitmask with lower false-negative rate | ||||||||||||||||||||||||||||||||||||||||||
| 33 | my $p = $iph->pHash(geometry => 64, method => 'log'); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | # Calculate a pHash with the upper left half of the upper-left-most 7x7 of the DCT - 27 bits, 7 hex chars | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # Use for indexing or reducing full phash false negatives - significant false negative rate by itself | ||||||||||||||||||||||||||||||||||||||||||
| 37 | my $p7 = $iph->pHash(geometry => '7x7', reduce => 1); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # or shortcut function | ||||||||||||||||||||||||||||||||||||||||||
| 39 | $p7 = $iph->pHash7(); | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # Calculate a pHash with the upper left half of the upper-left-most 6x6 of the DCT - 20 bits, 5 hex chars | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # Use for indexing or reducing full phash false negatives - very significant false negative rate by itself | ||||||||||||||||||||||||||||||||||||||||||
| 43 | my $p6 = $iph->pHash(geometry => '6x6', reduce => 1, method => 'median'); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # or shortcut function | ||||||||||||||||||||||||||||||||||||||||||
| 45 | $p6 = $iph->pHash6(); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | # Calculate the difference (Hamming distance) between two hex hashes | ||||||||||||||||||||||||||||||||||||||||||
| 48 | my $diff = Image::PHash::diff($p1, $p2); | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | Image::PHash allows you to calculate the (DCT-based) perceptual hash (pHash) of an image. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | The constructor and general structure is based on L<Image::Hash> - keeping usage quite | ||||||||||||||||||||||||||||||||||||||||||
| 55 | similar, but the pHash algorithm is rewritten from scratch as the L<Image::Hash> | ||||||||||||||||||||||||||||||||||||||||||
| 56 | implementation was flawed (and slow). Apart from fixes/tweaks for GD, Imager, ImageMagick | ||||||||||||||||||||||||||||||||||||||||||
| 57 | resizing, L<Image::Imlib2> support is added along with some unique features like | ||||||||||||||||||||||||||||||||||||||||||
| 58 | reduced hashes for indexing, options to deal with image mirroring, alternative | ||||||||||||||||||||||||||||||||||||||||||
| 59 | bitmap methods. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | A fast DCT XS module made specifically to serve this hashing module is used: L<Math::DCT>. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | Depending on your setup, it should be 15x or so faster than pHash.org | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =head1 CONSTRUCTOR METHOD | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | =head2 C<new> | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | my $iph = Image::PHash->new($image_file , $library?, \%settings?); | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | The first argument is an image filename when the Imlib2 library is used, but it | ||||||||||||||||||||||||||||||||||||||||||
| 71 | can also by a variable with the image data for the other libraries. | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | The second (optional) argument is the image library. Valid options are C<Imlib2>, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | C<GD>, C<ImageMagick>, C<Imager> and if not specified the module will try to load | ||||||||||||||||||||||||||||||||||||||||||
| 75 | them in that order. Using a different library (or even library version) will most | ||||||||||||||||||||||||||||||||||||||||||
| 76 | likely result in different hashes being returned, so make sure you hash your entire | ||||||||||||||||||||||||||||||||||||||||||
| 77 | image set using the same image library. Also, the module will probably not work with | ||||||||||||||||||||||||||||||||||||||||||
| 78 | very old versions of the image libraries. See Notes for comparison of libraries. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | The third (optional) argument can take a reference to a settings hash. Currently | ||||||||||||||||||||||||||||||||||||||||||
| 81 | supported settings: | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | =item * C<resize> : Expects an integer to specify the size of the image resize before | ||||||||||||||||||||||||||||||||||||||||||
| 86 | applying the DCT transformation. By default it is C<32>, which resizes to a C<32x32> image. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | You may want to explore different sizes which have different hashing behaviour e.g. | ||||||||||||||||||||||||||||||||||||||||||
| 88 | increasing resize to C<64> seems to offer some benefit for the reduced/index hashes | ||||||||||||||||||||||||||||||||||||||||||
| 89 | (at a performance penalty of course). | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =item * C<magick_filter> : The C<filter> parameter for L<ImageMagick> which controls | ||||||||||||||||||||||||||||||||||||||||||
| 92 | the scaling filter. The default is C<Cubic> which is about as good as L<ImageMagick>'s | ||||||||||||||||||||||||||||||||||||||||||
| 93 | own default C<Lanczos>, but significantly faster. You can look up the L<ImageMagick> | ||||||||||||||||||||||||||||||||||||||||||
| 94 | documentation for alternatives (e.g. C<Lanczos>, C<Mitchell>, C<Triangle>, C<Gaussian>...) | ||||||||||||||||||||||||||||||||||||||||||
| 95 | if you want a different balance of speed/quality. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | =item * C<imager_qtype> : The C<qtype> parameter for L<Imager> which defines the | ||||||||||||||||||||||||||||||||||||||||||
| 98 | quality of scaling performed. The type C<mixing> is used by default as it seems to | ||||||||||||||||||||||||||||||||||||||||||
| 99 | behave for most cases while being about twice as fast as Imager's internal default | ||||||||||||||||||||||||||||||||||||||||||
| 100 | qtype of C<normal>, but you can still manually specify that if you prefer. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | The constructor will C<croak> if there is an error loading an image library or an | ||||||||||||||||||||||||||||||||||||||||||
| 103 | incorrect/missing argument. It will only C<carp> and return C<undef> if an image library | ||||||||||||||||||||||||||||||||||||||||||
| 104 | returns after failing to load an image. | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | =head2 C<pHash> | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | my $p = $iph->pHash( | ||||||||||||||||||||||||||||||||||||||||||
| 113 | geometry => '8x8', # Size of the matrix to keep from the DCT. 8x8 is the standard 64 bit pHash. | ||||||||||||||||||||||||||||||||||||||||||
| 114 | reduce => 0, # If enabled removes lower right half or matrix and 0,0 position. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | method => 'average', # Method with which to convert the dct to bitmask. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | mirror => 0, # If enabled will return the hash of the mirror image (horizontal flip). | ||||||||||||||||||||||||||||||||||||||||||
| 117 | mirrorproof => 0, # If enabled will return a hash type that resists image mirroring. | ||||||||||||||||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | my @bits = $iph->pHash(); | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | Generates a pHash, returns it as a hex string (or array of 1s and 0s in array | ||||||||||||||||||||||||||||||||||||||||||
| 123 | context). | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | The pHash process consists of resizing the image to 32x32 (unless different C<resize>) | ||||||||||||||||||||||||||||||||||||||||||
| 126 | was specified with the constructor, converting color to luminance, DCT on resized image's | ||||||||||||||||||||||||||||||||||||||||||
| 127 | luminance, conversion to bit values based on the selected method and dropping the high | ||||||||||||||||||||||||||||||||||||||||||
| 128 | frequency part of the matrix, following the C<geometry> and C<reduce> settings. | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | The parameters to pass: | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | =item * C<geometry> : A string with the desired square dimensions C<NxN> of the bit | ||||||||||||||||||||||||||||||||||||||||||
| 135 | matrix taken from the upper left of the full (by default 32x32) processed DCT to be | ||||||||||||||||||||||||||||||||||||||||||
| 136 | used in the hash. By default it is C<'8x8'>, which produces the typical 64bit pHash. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | Alternatively, specify a simple integer for the number of low frequency bits to take | ||||||||||||||||||||||||||||||||||||||||||
| 139 | from the matrix going through the matrix in increasing diagonals. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | E.g. a value of C<64> will return 64 bits just like the default pHash, but they will | ||||||||||||||||||||||||||||||||||||||||||
| 141 | be taken from the upper left half of the 11x11 top-left part of the DCT matrix. The | ||||||||||||||||||||||||||||||||||||||||||
| 142 | effect is that the resulting hash will have improved characteristics, especially | ||||||||||||||||||||||||||||||||||||||||||
| 143 | in the average distance of non-similar images. | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | =item * C<reduce> : The option will return only the bits that are on, or to the upper | ||||||||||||||||||||||||||||||||||||||||||
| 146 | left of the 0,N->N,0 diagonal of the selected NxN matrix, except the first bit (which | ||||||||||||||||||||||||||||||||||||||||||
| 147 | is always 1). This way you get the (N-1)*(2+N)/2 most significant (recording the largest | ||||||||||||||||||||||||||||||||||||||||||
| 148 | changes) bits. | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | For example, a 8x8 reduced hash has 35 bits, which is less than a 6x6 matrix, and yet | ||||||||||||||||||||||||||||||||||||||||||
| 151 | may outperform a full 7x7 49 bit matrix. | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | The option only applies to square C<geometry>. If you have specified bits instead, | ||||||||||||||||||||||||||||||||||||||||||
| 154 | you already get an effect similar to C<reduce>. | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =item * C<method> : Specifies which method to use for converting the DCT result to | ||||||||||||||||||||||||||||||||||||||||||
| 157 | a bitmask. By default it is C<average>. Supported methods: | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =item * C<average> : The default method which compares each DCT value with the arithmetic | ||||||||||||||||||||||||||||||||||||||||||
| 162 | mean. It usually a bit better at recognising similarity than C<median> (the average | ||||||||||||||||||||||||||||||||||||||||||
| 163 | difference of similar images will be lower), albeit with an increase in false positives. | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | =item * C<median> : The median of the DCT values is used as the threshold. Some | ||||||||||||||||||||||||||||||||||||||||||
| 166 | implementations prefer it over C<average> due to lower false positives/collision rate, | ||||||||||||||||||||||||||||||||||||||||||
| 167 | which can be good for reduced size hashes, but it increases false negatives so it is | ||||||||||||||||||||||||||||||||||||||||||
| 168 | not the recommended method for many scenarios. | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | =item * C<average_x> : Applies only to reduced hashes - it will calculate the average using | ||||||||||||||||||||||||||||||||||||||||||
| 171 | the entire NxN matrix for C<geometry='NxN'>, or use the next X lowest frequency DCT coefficients | ||||||||||||||||||||||||||||||||||||||||||
| 172 | (total 2*X) to calculate the average for C<geometry=X>. Almost as low collision rate as C<log>, | ||||||||||||||||||||||||||||||||||||||||||
| 173 | perhaps a bit better (lower) false negatives. | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | =item * C<log> : A special logarithmic average is calculated as the threshold, giving | ||||||||||||||||||||||||||||||||||||||||||
| 176 | the lowest collision rate (a tie with <median> or better). It will usually have a bit | ||||||||||||||||||||||||||||||||||||||||||
| 177 | increased false negative chance compared to C<average> or perhaps even C<diff> - but | ||||||||||||||||||||||||||||||||||||||||||
| 178 | should still be lower than C<median>. Quite close to C<average_x>, but also applies to | ||||||||||||||||||||||||||||||||||||||||||
| 179 | non-reduced hashes. | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | =item * C<diff> : The difference between each DCT value is taken as the bitmask. It seems | ||||||||||||||||||||||||||||||||||||||||||
| 182 | to fall between C<average> and C<median> in most tests, both in false negative/collision | ||||||||||||||||||||||||||||||||||||||||||
| 183 | rate and at similarity recognition. | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | =item * C<mirror> : Returns the pHash of the mirror image. Note that this is a function | ||||||||||||||||||||||||||||||||||||||||||
| 188 | applied after the DCT, so if you call C<pHash> once with C<mirror> and once without | ||||||||||||||||||||||||||||||||||||||||||
| 189 | you can get both hashes without a processing overhead. Not compatible with C<mirrorproof>. | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | =item * C<mirrorproof> : Will return a pHash that is impervious to mirroring (flipping | ||||||||||||||||||||||||||||||||||||||||||
| 192 | images horizontally). This means two mirrored images will have the same/similar pHash, | ||||||||||||||||||||||||||||||||||||||||||
| 193 | so it is good for declaring such images as "similar" with a single pHash comparison, but | ||||||||||||||||||||||||||||||||||||||||||
| 194 | if you want to know they were mirrored the C<mirror> option is more appropriate. | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | Caveat: The option sacrifices about 2 bits or so of entropy, so the resulting | ||||||||||||||||||||||||||||||||||||||||||
| 197 | pHash is less effective. Thus, it should not be preferred if there is no specific | ||||||||||||||||||||||||||||||||||||||||||
| 198 | reason, especially when the C<mirror> option is available. | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | =head1 INDEXING SHORTCUT METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | The special reduced hashes below, named B<pHash6> and B<pHash7> are specially chosen | ||||||||||||||||||||||||||||||||||||||||||
| 205 | to be useful in two scenarios: | ||||||||||||||||||||||||||||||||||||||||||
| 206 | * Extra verification to reduce false positives that the full has produces. Depending | ||||||||||||||||||||||||||||||||||||||||||
| 207 | on the scenario, tests have shown each of the reduced hash being able to reduce false | ||||||||||||||||||||||||||||||||||||||||||
| 208 | positives by 60-90% with the right threshold (example 2, 3 for pHash6, pHash7 resp.), | ||||||||||||||||||||||||||||||||||||||||||
| 209 | and if both are used over 95% reduction of false positives is possible. | ||||||||||||||||||||||||||||||||||||||||||
| 210 | * For scenarios where we want to use a simple index of a database (e.g. MySQL) and only | ||||||||||||||||||||||||||||||||||||||||||
| 211 | very simple manipulations are required to be matched (e.g. resize) or there is a higher | ||||||||||||||||||||||||||||||||||||||||||
| 212 | tolerance for false negatives, pHash6 & pHash7 at diff 0 can be used to retrieve most | ||||||||||||||||||||||||||||||||||||||||||
| 213 | matches. For example, storing pHash6, pHash7 (as indices) along with pHash in a MySQL db, | ||||||||||||||||||||||||||||||||||||||||||
| 214 | we can retrieve most matches with something like: | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | SELECT * | ||||||||||||||||||||||||||||||||||||||||||
| 217 | FROM hash_table | ||||||||||||||||||||||||||||||||||||||||||
| 218 | WHERE (phash7 = @phash7 | ||||||||||||||||||||||||||||||||||||||||||
| 219 | OR phash6 = @phash6) | ||||||||||||||||||||||||||||||||||||||||||
| 220 | AND BIT_COUNT(CAST(CONV(phash, 16, 10) AS UNSIGNED) ^ CAST(CONV(@phash, 16, 10) AS UNSIGNED)) < 8; | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | =head2 C<pHash7> | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | my $p7 = $iph->pHash7(); | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | Equivalent to C<$iph-E<gt>pHash(geometry =E<gt> '7x7', reduce =E<gt> 1)>. It is useful for | ||||||||||||||||||||||||||||||||||||||||||
| 228 | relational databases that don't support indexing with differences, so using the reduced | ||||||||||||||||||||||||||||||||||||||||||
| 229 | pHash will return many of the close full pHash matches. For simple resize/compression manipulations | ||||||||||||||||||||||||||||||||||||||||||
| 230 | expect matches in the region of 98% or so to be returned. It can also be used to verify | ||||||||||||||||||||||||||||||||||||||||||
| 231 | the match in some cases where a specific pattern might make different photos match in | ||||||||||||||||||||||||||||||||||||||||||
| 232 | the full phash but not the limited. It takes the same parameters as C<pHash>. | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | =head2 C<pHash6> | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | my $p6 = $iph->pHash6(); | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | Equivalent to C<$iph-E<gt>pHash(geometry =E<gt> '6x6', reduce =E<gt> 1, method =E<gt> 'median')>. Similar | ||||||||||||||||||||||||||||||||||||||||||
| 239 | to pHash7, but will produce more matches when used as an index, along with many more false | ||||||||||||||||||||||||||||||||||||||||||
| 240 | positives. | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | Note that neither reduced version is appropriate to use as an image comparison hash by itself | ||||||||||||||||||||||||||||||||||||||||||
| 243 | (too many false positives), and they are chosen to be complimentary, so when used in conjunction | ||||||||||||||||||||||||||||||||||||||||||
| 244 | for either indexing or verification, their performance increases considerably. | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | =head1 HELPER METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | =head2 C<reducedimage> | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | my $img = $iph->reducedimage(); | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | Returns the reduced (rescaled) image that will be used for the DCT. | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | =head2 C<dctdump> | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | my $dct = $iph->dctdump(); | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | Will return the full 32x32 DCT as an arrayref of floats. | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | =head2 C<printbitmatrix> | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | $iph->printbitmatrix( | ||||||||||||||||||||||||||||||||||||||||||
| 263 | %phash_opt, # Any pHash method option applies | ||||||||||||||||||||||||||||||||||||||||||
| 264 | separator => '', # Separator for horizontal values | ||||||||||||||||||||||||||||||||||||||||||
| 265 | filler => ' ' # For reduced results, filler for the missing positions | ||||||||||||||||||||||||||||||||||||||||||
| 266 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | Will return a print-friendly reduced size bitmask matrix as a string. Basically a | ||||||||||||||||||||||||||||||||||||||||||
| 269 | string with rows/columns of the 1s and 0s you would get from calling C<$iph-E<gt>pHash()> | ||||||||||||||||||||||||||||||||||||||||||
| 270 | with the same parameters. | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | =head1 HELPER FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | =head2 C<b2h> | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | my $hash = Image::PHash::b2h(join('', @bits)); | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | Will convert a bit value string to a hex string. | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | =head2 C<diff> | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | my $diff = Image::PHash::diff($phash1, $phash2); | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | Will calculate the bit difference of two hex string hashes (their Hamming distance | ||||||||||||||||||||||||||||||||||||||||||
| 285 | of their bit stream form). On 64 bit systems (checking C<$Config{ivsize}>) it will | ||||||||||||||||||||||||||||||||||||||||||
| 286 | actually call C<_diff64> which can calculate the difference of up to 64bit hashes in | ||||||||||||||||||||||||||||||||||||||||||
| 287 | a single operation (using C<%064b>). | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | =head1 NOTES | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | =head2 Performance | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | The hashing performance of the module is enough to make the actual pHash generation | ||||||||||||||||||||||||||||||||||||||||||
| 294 | from the final 32x32 mono image a trivial part of the process. For a general idea, | ||||||||||||||||||||||||||||||||||||||||||
| 295 | on a single core of a 2015 Macbook Pro, over 18000 hashes/sec can be processed thanks | ||||||||||||||||||||||||||||||||||||||||||
| 296 | in part to the fast L<Math::DCT> XS module (developed specifically for Image::PHash). | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | So, most of the processing time is spent on loading the image, resizing, extracting | ||||||||||||||||||||||||||||||||||||||||||
| 299 | pixel values, removing color, all of which depend on the specific image module. On an | ||||||||||||||||||||||||||||||||||||||||||
| 300 | Apple M1, hashing 800x600 jpg images was measured at 131 h/s with L<Image::Magick>, | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 208 h/s with L<Imager>, 241 h/s with L<GD>, 547 h/s with L<Image::Imlib2>. | ||||||||||||||||||||||||||||||||||||||||||
| 302 | Higher resolutions make the process slower as you could expect. Since all images will | ||||||||||||||||||||||||||||||||||||||||||
| 303 | be resized to 32x32 in the end, the fastest hashing performance would be if you loaded | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 32x32 thumbnails. In that case, the performance of the libraries in the same order for | ||||||||||||||||||||||||||||||||||||||||||
| 305 | the resized imageset were: 659 h/s, 664 h/s, 1883 h/s, 2296 h/s. It is clear that | ||||||||||||||||||||||||||||||||||||||||||
| 306 | L<Image::Imlib2> should be preferred when hashing performance is desired, as it offers | ||||||||||||||||||||||||||||||||||||||||||
| 307 | dramatically better performance (unless you are hashing 32x32 images in which case GD | ||||||||||||||||||||||||||||||||||||||||||
| 308 | also fast). It should be noted that the resulting hashes don't have exactly the same | ||||||||||||||||||||||||||||||||||||||||||
| 309 | behaviour/metrics, due to the different resizing algorithms used, but the differences | ||||||||||||||||||||||||||||||||||||||||||
| 310 | seem to be very small. You are encouraged to test on your own data set. | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | Remember, never mix image libraries (or settings), the hashes will most likely not | ||||||||||||||||||||||||||||||||||||||||||
| 313 | be compatible. | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | Finally, if you are curious about the performance of this module compared to the | ||||||||||||||||||||||||||||||||||||||||||
| 316 | C++ pHash.org implementation, pHash.org could achieve 33 h/s with the test setup as | ||||||||||||||||||||||||||||||||||||||||||
| 317 | above, making L<Image::PHash> over 16x faster with Imlib2. With pre-sized 32x32 | ||||||||||||||||||||||||||||||||||||||||||
| 318 | images, pHash.org ran at 101 h/s. | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | =head2 Compatibility of hashes | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | As already mentioned, if you produce hashes with different settings, different image | ||||||||||||||||||||||||||||||||||||||||||
| 323 | libraries etc, the hashes might not be compatible. It is advisable to even freeze | ||||||||||||||||||||||||||||||||||||||||||
| 324 | the version of this module and the image library in a production environment to | ||||||||||||||||||||||||||||||||||||||||||
| 325 | avoid any degraded performance. | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | =head2 Calculation caching | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | Calculating pHashes with different dct/reduce/median/mirror arguments for the same | ||||||||||||||||||||||||||||||||||||||||||
| 330 | image is very fast (when the same object is used), as the resize and DCT transform | ||||||||||||||||||||||||||||||||||||||||||
| 331 | will only happen on the very first pHash calculation and are cached for any subsequent | ||||||||||||||||||||||||||||||||||||||||||
| 332 | call. You can essentially get the extra phash6/phash7/mirror etc "for free" after | ||||||||||||||||||||||||||||||||||||||||||
| 333 | the initial pHash calculation. | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | =head2 L<Image::PHash> vs L<Image::Hash> | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | While L<Image::Hash> may still be useful for the aHash and dHash functionality, its | ||||||||||||||||||||||||||||||||||||||||||
| 338 | pHash implementation is seriously flawed. It does not actually do a full DCT, using | ||||||||||||||||||||||||||||||||||||||||||
| 339 | instead a shortcut that seems to result to hashes with lots of zeros and thus a high | ||||||||||||||||||||||||||||||||||||||||||
| 340 | rate of collisions (~2% chance for identical hash on dissimilar images making it useless | ||||||||||||||||||||||||||||||||||||||||||
| 341 | for my large data set), which is the reason the hashing was implemented from scratch. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | Despite it not doing a full DCT it was really slow (over 80x slower than the XS L<Math::DCT>), | ||||||||||||||||||||||||||||||||||||||||||
| 343 | so switching to L<Image::PHash> will give you "correct" hashes at a significant speed | ||||||||||||||||||||||||||||||||||||||||||
| 344 | increase, along with several extra features. | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | =head2 L<Image::PHash> vs L<pHash.org> | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | Apart from the significant speed advantage of Image::PHash noted above, there are a couple | ||||||||||||||||||||||||||||||||||||||||||
| 349 | of important differences, in that pHash.org will apply a 7x7 mean filter to the image | ||||||||||||||||||||||||||||||||||||||||||
| 350 | before the resize and the conversion to bits is always done with the median method. This | ||||||||||||||||||||||||||||||||||||||||||
| 351 | seems to keep false positives quite low, but its false negatives are higher. Since with | ||||||||||||||||||||||||||||||||||||||||||
| 352 | Image::PHash you can get even better hashes with, for example, C<geometry=64> and you can | ||||||||||||||||||||||||||||||||||||||||||
| 353 | combine them with C<method='average_x'> or C<method='log'>, you will get even lower false | ||||||||||||||||||||||||||||||||||||||||||
| 354 | positive rate than pHash.org, but with less false negatives as well. Feel free to share | ||||||||||||||||||||||||||||||||||||||||||
| 355 | your own comparisons with the author if in doubt. | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | Note that the differences you are to use as a threshold for Image::PHash and pHash.org are | ||||||||||||||||||||||||||||||||||||||||||
| 358 | quite different - pHash.org will give about 50% greater diffs on average (e.g. where I would | ||||||||||||||||||||||||||||||||||||||||||
| 359 | use 7 for the former, 11 would be the equivalent for the latter). | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | =head2 Selecting a C<diff> threshold | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | The appropriate C<diff> threshold for declaring images as "similar" is not a precise | ||||||||||||||||||||||||||||||||||||||||||
| 364 | art and will depend on the application (type of images, tolerance for false positives | ||||||||||||||||||||||||||||||||||||||||||
| 365 | etc.). The exact application is very important too, if you have 2 images and want to | ||||||||||||||||||||||||||||||||||||||||||
| 366 | check whether they are similar, a false positive rate of even over 1% is fine, in which | ||||||||||||||||||||||||||||||||||||||||||
| 367 | case the diff can be chosen to be probably over 10, whereas having a big collection of | ||||||||||||||||||||||||||||||||||||||||||
| 368 | photos in which you want to check whether a duplicate exists, requires a very low false | ||||||||||||||||||||||||||||||||||||||||||
| 369 | positive rate. Example diff ranges for a full pHash are 3-7 if you want to keep false | ||||||||||||||||||||||||||||||||||||||||||
| 370 | positives close to 0%. For the small pHash7 and pHash6 probably not more than 3 and 2 | ||||||||||||||||||||||||||||||||||||||||||
| 371 | respectively are useful for lookups (and still with lots of false positives as noted above). | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | sub pHash7 { | ||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | ▶ | my ($self, %opt) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pHash7: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | return $self->pHash(%opt, geometry => '7x7', reduce => 1); | |||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | sub pHash6 { | ||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | my ($self, %opt) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pHash6: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | return $self->pHash(%opt, geometry => '6x6', reduce => 1, method => 'median'); | |||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 386 | 3 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=30 SCAR=49.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | 3 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||
| 389 | 3 | bless($self, $class); | |||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | 3 | ▶ | $self->{image} = shift || croak("Image file or data expected."); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || croak('Image file or data expected.')
MC/DC0%0/2
shift() || croak('Image file or data expected.')
shift()
croak('Image file or data expected.')
Truth table33%1/3
A || B
A= shift()B= croak('Image file or data expected.')
| |||||||||||||||||||||||||||||||||||||||||||
| 392 | 3 | $self->{module} = shift; | |||||||||||||||||||||||||||||||||||||||||
| 393 | 3 | ▶ | $self->{settings} = shift || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | 3 | ▶ | croak("Hashref expected for settings argument") unless ref($self->{settings}) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 396 | 3 | ▶ | $self->{dct_size} = int($self->{settings}->{resize} || 32); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'settings'}{'resize'} || 32
MC/DC0%0/1
$self->{'settings'}{'resize'} || 32
$self->{'settings'}{'resize'}
Truth table50%1/2
A
A= $self->{'settings'}{'resize'}
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | 3 | ▶ | $self->{imager_qtype} = $self->{settings}->{imager_qtype} || 'mixing'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'settings'}{'imager_qtype'} || 'mixing'
MC/DC0%0/1
$self->{'settings'}{'imager_qtype'} || 'mixing'
$self->{'settings'}{'imager_qtype'}
Truth table50%1/2
A
A= $self->{'settings'}{'imager_qtype'}
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 3 | ▶ | $self->{magick_filter} = $self->{settings}->{magick_filter} || 'Cubic'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'settings'}{'magick_filter'} || 'Cubic'
MC/DC0%0/1
$self->{'settings'}{'magick_filter'} || 'Cubic'
$self->{'settings'}{'magick_filter'}
Truth table50%1/2
A
A= $self->{'settings'}{'magick_filter'}
| |||||||||||||||||||||||||||||||||||||||||||
| 399 | 3 | ▶ | croak("resize > 5 expected.") unless $self->{dct_size} > 5; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | 3 | my %libs = ( # Library, reduce/resize function, pixels function, support for passing data variable | |||||||||||||||||||||||||||||||||||||||||
| 402 | Imlib2 => ['Image::Imlib2', \&_reduce_Imlib2, \&_pixels_Imlib2], | ||||||||||||||||||||||||||||||||||||||||||
| 403 | GD => ['GD', \&_reduce_GD, \&_pixels_GD, 1], | ||||||||||||||||||||||||||||||||||||||||||
| 404 | ImageMagick => ['Image::Magick', \&_reduce_Magick, \&_pixels_Magick, 1], | ||||||||||||||||||||||||||||||||||||||||||
| 405 | Imager => ['Imager', \&_reduce_Imager, \&_pixels_Imager, 1], | ||||||||||||||||||||||||||||||||||||||||||
| 406 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | 3 | my $file = -f $self->{image}; | |||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | 3 | ▶ | if ($self->{module}) { # User specified image module | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | $libs{$libs{$_}->[0]} = $libs{$_} for keys %libs; # Allow synonyms | |||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | my $lib = $libs{$self->{module}}->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | croak("Unknown image library specified: '$self->{module}'. " | ||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | ▶ | . "Choose from: ".join(', ', map {/:/ ? () : $_} keys %libs) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 416 | ) unless $lib; | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | ▶ | eval "require $lib" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | or croak("Specified image library '$self->{module}' could not be loaded."); | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | $self->{module} = $lib; # Normalize | |||||||||||||||||||||||||||||||||||||||||
| 422 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 423 | # Try to load Imlib2, GD, ImageMagic or Imager in that order | ||||||||||||||||||||||||||||||||||||||||||
| 424 | 3 | foreach my $lib (qw/Imlib2 GD Magick Imager/) { | |||||||||||||||||||||||||||||||||||||||||
| 425 | 12 | ▶ | if (eval "require $libs{$lib}->[0]" && ($file || $libs{$lib}->[3])) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
eval "require $libs{$lib}[0]" and $file || $libs{$lib}[3]
$file or $libs{$lib}[3]
MC/DC0%0/3
eval "require $libs{$lib}[0]" and $file || $libs{$lib}[3]
eval "require $libs{$lib}[0]"
$file
$libs{$lib}[3]
Truth table25%1/4
A and B or C
A= eval "require $libs{$lib}[0]"B= $fileC= $libs{$lib}[3]
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | 3 | $self->{module} = $lib; | |||||||||||||||||||||||||||||||||||||||||
| 427 | 3 | last; | |||||||||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||||||
| 430 | croak("None of the supported image libraries could be loaded. " | ||||||||||||||||||||||||||||||||||||||||||
| 431 | . " Tried loading: ".join(', ', keys %libs) | ||||||||||||||||||||||||||||||||||||||||||
| 432 | 3 | ▶ | ) unless $self->{module}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||||||||||||
| 434 | croak("No file at $self->{image}") | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 3 | ▶ | unless $file || (length($self->{image}) > 255 && $libs{$self->{module}}->[3]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
$file or length $self->{"image"} > 255 and $libs{$self->{"module"}}[3]
length $self->{"image"} > 255 and $libs{$self->{"module"}}[3]
MC/DC0%0/3
$file or length $self->{"image"} > 255 and $libs{$self->{"module"}}[3]
$file
length $self->{"image"} > 255
$libs{$self->{"module"}}[3]
Truth table25%1/4
A or B and C
A= $fileB= length $self->{"image"} > 255C= $libs{$self->{"module"}}[3]
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 3 | my $error = ''; | |||||||||||||||||||||||||||||||||||||||||
| 438 | 3 | ▶ | if ($libs{$self->{module}}->[0] eq 'Image::Imlib2') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | $self->{im} = Image::Imlib2->load($self->{image}); | |||||||||||||||||||||||||||||||||||||||||
| 440 | } elsif ($libs{$self->{module}}->[0] eq 'GD') { | ||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | GD::Image->trueColor(1); | |||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | $self->{im} = GD::Image->new($self->{image}); | |||||||||||||||||||||||||||||||||||||||||
| 443 | } elsif ($libs{$self->{module}}->[0] eq 'Image::Magick') { | ||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | $self->{im} = Image::Magick->new(); | |||||||||||||||||||||||||||||||||||||||||
| 445 | $error = | ||||||||||||||||||||||||||||||||||||||||||
| 446 | ($file) | ||||||||||||||||||||||||||||||||||||||||||
| 447 | ? $self->{im}->Read($self->{image}) | ||||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | ▶ | : $self->{im}->BlobToImage($self->{image}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | ▶ | $self->{im} = undef if $error; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 451 | 3 | ▶ | my $type = $file ? 'file' : 'data'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 452 | 3 | $self->{im} = Imager->new($type => $self->{image}); | |||||||||||||||||||||||||||||||||||||||||
| 453 | 3 | ▶ | $error = Imager->errstr() || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
'Imager'->errstr || ''
MC/DC0%0/1
'Imager'->errstr || ''
'Imager'->errstr
Truth table50%1/2
A
A= 'Imager'->errstr
| |||||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | 3 | ▶ | unless ($self->{im}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 457 | 3 | ▶ | carp("Cannot load ".($file ? $self->{image} : 'data')." with $self->{module}. $error"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 458 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | $self->{reduced} = $libs{$self->{module}}->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | $self->{pixels} = $libs{$self->{module}}->[2]; | |||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | $self->{methods} = { | ||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | average => \&_apply_average, | |||||||||||||||||||||||||||||||||||||||||
| 466 | average_x => \&_apply_average, | ||||||||||||||||||||||||||||||||||||||||||
| 467 | median => \&_apply_median, | ||||||||||||||||||||||||||||||||||||||||||
| 468 | diff => \&_apply_diff, | ||||||||||||||||||||||||||||||||||||||||||
| 469 | log => \&_apply_log_average, | ||||||||||||||||||||||||||||||||||||||||||
| 470 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | # Helper function: | ||||||||||||||||||||||||||||||||||||||||||
| 476 | # Convert from binary to hexadecimal | ||||||||||||||||||||||||||||||||||||||||||
| 477 | # | ||||||||||||||||||||||||||||||||||||||||||
| 478 | # Borrowed from http://www.perlmonks.org/index.pl?node_id=644225 | ||||||||||||||||||||||||||||||||||||||||||
| 479 | sub b2h { | ||||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | ▶ | my $num = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
b2h: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 481 | 0 | my $WIDTH = 4; | |||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | my $index = length($num) - $WIDTH; | |||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | my $hex = ''; | |||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 485 | 0 | my $width = $WIDTH; | |||||||||||||||||||||||||||||||||||||||||
| 486 | 0 | ▶ | if ($index < 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | $width += $index; | |||||||||||||||||||||||||||||||||||||||||
| 488 | 0 | $index = 0; | |||||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||||||
| 490 | 0 | my $cut_string = substr($num, $index, $width); | |||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | $hex = sprintf('%X', oct("0b$cut_string")) . $hex; | |||||||||||||||||||||||||||||||||||||||||
| 492 | 0 | $index -= $WIDTH; | |||||||||||||||||||||||||||||||||||||||||
| 493 | } while ($index > (-1 * $WIDTH)); | ||||||||||||||||||||||||||||||||||||||||||
| 494 | 0 | return $hex; | |||||||||||||||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||
| 497 | # Difference in bits between two hex strings | ||||||||||||||||||||||||||||||||||||||||||
| 498 | # About 30% slower than using %064b directly, but this is portable to 32 bits | ||||||||||||||||||||||||||||||||||||||||||
| 499 | sub diff { | ||||||||||||||||||||||||||||||||||||||||||
| 500 | 2 | ▶ | return _diff64(@_) if length($_[0]) <= 16 && $Config{ivsize} >= 8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=4 SCAR=13.9
Pod
documented
Branch: 1 branch
Condition66%2/3
length $_[0] <= 16 and $Image::PHash::Config{"ivsize"} >= 8
MC/DC50%1/2
length $_[0] <= 16 and $Image::PHash::Config{"ivsize"} >= 8
length $_[0] <= 16
$Image::PHash::Config{"ivsize"} >= 8
Truth table66%2/3
A and B
A= length $_[0] <= 16B= $Image::PHash::Config{"ivsize"} >= 8
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 1 | my $diff; | |||||||||||||||||||||||||||||||||||||||||
| 502 | 1 | ▶ | for (my $i = 0; $i < length($_[0]); $i += 8) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 3 | my $d = | |||||||||||||||||||||||||||||||||||||||||
| 504 | sprintf("%032b", hex(substr($_[0], $i, 8))) ^ | ||||||||||||||||||||||||||||||||||||||||||
| 505 | sprintf("%032b", hex(substr($_[1], $i, 8))); | ||||||||||||||||||||||||||||||||||||||||||
| 506 | 3 | $diff += $d =~ tr/\0//c; | |||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||
| 508 | 1 | return $diff; | |||||||||||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | sub _diff64 { | ||||||||||||||||||||||||||||||||||||||||||
| 512 | 1 | ▶ | my $k = sprintf("%064b", hex($_[0])); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_diff64: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 513 | 1 | my $l = sprintf("%064b", hex($_[1])); | |||||||||||||||||||||||||||||||||||||||||
| 514 | 1 | my $diff = $k ^ $l; | |||||||||||||||||||||||||||||||||||||||||
| 515 | 1 | my $num_mismatch = $diff =~ tr/\0//c; | |||||||||||||||||||||||||||||||||||||||||
| 516 | 1 | return $num_mismatch; | |||||||||||||||||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | # Reduce the size of an image using Imlib2 | ||||||||||||||||||||||||||||||||||||||||||
| 520 | sub _reduce_Imlib2 { | ||||||||||||||||||||||||||||||||||||||||||
| 521 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reduce_Imlib2: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 522 | return $self->{im_scaled} = $self->{im} | ||||||||||||||||||||||||||||||||||||||||||
| 523 | if $self->{im}->width == $self->{dct_size} | ||||||||||||||||||||||||||||||||||||||||||
| 524 | 0 | ▶ | && $self->{im}->height == $self->{dct_size}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"im"}->width == $self->{"dct_size"} and $self->{"im"}->height == $self->{"dct_size"}
MC/DC0%0/2
$self->{"im"}->width == $self->{"dct_size"} and $self->{"im"}->height == $self->{"dct_size"}
$self->{"im"}->width == $self->{"dct_size"}
$self->{"im"}->height == $self->{"dct_size"}
Truth table0%0/3
A and B
A= $self->{"im"}->width == $self->{"dct_size"}B= $self->{"im"}->height == $self->{"dct_size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | $self->{im_scaled} = | ||||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | $self->{im}->create_scaled_image($self->{dct_size}, $self->{dct_size}); | |||||||||||||||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||
| 530 | # Reduce the size of an image using GD | ||||||||||||||||||||||||||||||||||||||||||
| 531 | sub _reduce_GD { | ||||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reduce_GD: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | $self->{im_scaled} = $self->{im}; | |||||||||||||||||||||||||||||||||||||||||
| 534 | return | ||||||||||||||||||||||||||||||||||||||||||
| 535 | if $self->{im}->width == $self->{dct_size} | ||||||||||||||||||||||||||||||||||||||||||
| 536 | 0 | ▶ | && $self->{im}->height == $self->{dct_size}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"im"}->width == $self->{"dct_size"} and $self->{"im"}->height == $self->{"dct_size"}
MC/DC0%0/2
$self->{"im"}->width == $self->{"dct_size"} and $self->{"im"}->height == $self->{"dct_size"}
$self->{"im"}->width == $self->{"dct_size"}
$self->{"im"}->height == $self->{"dct_size"}
Truth table0%0/3
A and B
A= $self->{"im"}->width == $self->{"dct_size"}B= $self->{"im"}->height == $self->{"dct_size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | 0 | my $dest = GD::Image->new($self->{dct_size}, $self->{dct_size}); | |||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | $dest->copyResampled( | ||||||||||||||||||||||||||||||||||||||||||
| 541 | $self->{im_scaled}, 0, 0, # (srcimg, dstX, dstY) | ||||||||||||||||||||||||||||||||||||||||||
| 542 | 0, 0, $self->{dct_size}, $self->{dct_size}, # (srcX, srxY, destX, destY) | ||||||||||||||||||||||||||||||||||||||||||
| 543 | $self->{im_scaled}->width, $self->{im_scaled}->height | ||||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 545 | 0 | $self->{im_scaled} = $dest; | |||||||||||||||||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | # Reduce the size of an image using Image::Magick | ||||||||||||||||||||||||||||||||||||||||||
| 549 | sub _reduce_Magick { | ||||||||||||||||||||||||||||||||||||||||||
| 550 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reduce_Magick: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 551 | 0 | $self->{im_scaled} = $self->{im}; | |||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||
| 553 | 0 | my ($w, $h) = $self->{im}->Get('width', 'height'); | |||||||||||||||||||||||||||||||||||||||||
| 554 | 0 | ▶ | return if $w == $self->{dct_size} && $h == $self->{dct_size}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$w == $self->{"dct_size"} and $h == $self->{"dct_size"}
MC/DC0%0/2
$w == $self->{"dct_size"} and $h == $self->{"dct_size"}
$w == $self->{"dct_size"}
$h == $self->{"dct_size"}
Truth table0%0/3
A and B
A= $w == $self->{"dct_size"}B= $h == $self->{"dct_size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | 0 | $self->{im_scaled}->Set(antialias => 'True'); | |||||||||||||||||||||||||||||||||||||||||
| 557 | $self->{im_scaled}->Resize( | ||||||||||||||||||||||||||||||||||||||||||
| 558 | width => $self->{dct_size}, | ||||||||||||||||||||||||||||||||||||||||||
| 559 | height => $self->{dct_size}, | ||||||||||||||||||||||||||||||||||||||||||
| 560 | filter => $self->{magick_filter} | ||||||||||||||||||||||||||||||||||||||||||
| 561 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||
| 564 | # Reduce the size of an image using Imager | ||||||||||||||||||||||||||||||||||||||||||
| 565 | sub _reduce_Imager { | ||||||||||||||||||||||||||||||||||||||||||
| 566 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reduce_Imager: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 567 | return $self->{im_scaled} = $self->{im} | ||||||||||||||||||||||||||||||||||||||||||
| 568 | if $self->{im}->getwidth() == $self->{dct_size} | ||||||||||||||||||||||||||||||||||||||||||
| 569 | 0 | ▶ | && $self->{im}->getheight() == $self->{dct_size}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"im"}->getwidth == $self->{"dct_size"} and $self->{"im"}->getheight == $self->{"dct_size"}
MC/DC0%0/2
$self->{"im"}->getwidth == $self->{"dct_size"} and $self->{"im"}->getheight == $self->{"dct_size"}
$self->{"im"}->getwidth == $self->{"dct_size"}
$self->{"im"}->getheight == $self->{"dct_size"}
Truth table0%0/3
A and B
A= $self->{"im"}->getwidth == $self->{"dct_size"}B= $self->{"im"}->getheight == $self->{"dct_size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | $self->{im_scaled} = $self->{im}->scale( | ||||||||||||||||||||||||||||||||||||||||||
| 572 | xpixels => $self->{dct_size}, | ||||||||||||||||||||||||||||||||||||||||||
| 573 | ypixels => $self->{dct_size}, | ||||||||||||||||||||||||||||||||||||||||||
| 574 | qtype => $self->{imager_qtype}, | ||||||||||||||||||||||||||||||||||||||||||
| 575 | 0 | type => "nonprop" | |||||||||||||||||||||||||||||||||||||||||
| 576 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||
| 579 | # Return the pixel values for an image when using Imlib2 | ||||||||||||||||||||||||||||||||||||||||||
| 580 | sub _pixels_Imlib2 { | ||||||||||||||||||||||||||||||||||||||||||
| 581 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pixels_Imlib2: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 582 | 0 | my @pixels; | |||||||||||||||||||||||||||||||||||||||||
| 583 | 0 | ▶ | for (my $y = 0; $y < $self->{dct_size}; $y++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 584 | 0 | ▶ | for (my $x = 0; $x < $self->{dct_size}; $x++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | my ($red, $green, $blue, $a) = $self->{im_scaled}->query_pixel($x, $y); | |||||||||||||||||||||||||||||||||||||||||
| 587 | 0 | my $grey = $red * 0.3 + $green * 0.59 + $blue * 0.11; | |||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | push(@pixels, $grey); | |||||||||||||||||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||
| 592 | 0 | return \@pixels; | |||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | # Return the pixel values for an image when using GD | ||||||||||||||||||||||||||||||||||||||||||
| 596 | sub _pixels_GD { | ||||||||||||||||||||||||||||||||||||||||||
| 597 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pixels_GD: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 598 | 0 | my @pixels; | |||||||||||||||||||||||||||||||||||||||||
| 599 | 0 | ▶ | for (my $y = 0; $y < $self->{dct_size}; $y++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 600 | 0 | ▶ | for (my $x = 0; $x < $self->{dct_size}; $x++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||
| 602 | 0 | my $color = $self->{im_scaled}->getPixel($x, $y); | |||||||||||||||||||||||||||||||||||||||||
| 603 | 0 | my ($red, $green, $blue) = $self->{im_scaled}->rgb($color); | |||||||||||||||||||||||||||||||||||||||||
| 604 | 0 | my $grey = $red * 0.3 + $green * 0.59 + $blue * 0.11; | |||||||||||||||||||||||||||||||||||||||||
| 605 | 0 | push(@pixels, $grey); | |||||||||||||||||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||
| 609 | 0 | return \@pixels; | |||||||||||||||||||||||||||||||||||||||||
| 610 | } | ||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||
| 612 | # Return the pixel values for an image when using Image::Magick | ||||||||||||||||||||||||||||||||||||||||||
| 613 | sub _pixels_Magick { | ||||||||||||||||||||||||||||||||||||||||||
| 614 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pixels_Magick: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 615 | 0 | my @pixels; | |||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | ▶ | for (my $y = 0; $y < $self->{dct_size}; $y++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 617 | 0 | ▶ | for (my $x = 0; $x < $self->{dct_size}; $x++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 618 | my @pixel = $self->{im_scaled}->GetPixel( | ||||||||||||||||||||||||||||||||||||||||||
| 619 | 0 | x => $x, | |||||||||||||||||||||||||||||||||||||||||
| 620 | y => $y, | ||||||||||||||||||||||||||||||||||||||||||
| 621 | normalize => 0 | ||||||||||||||||||||||||||||||||||||||||||
| 622 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 623 | 0 | my $grey = $pixel[0] * 0.3 + $pixel[1] * 0.59 + $pixel[2] * 0.11; | |||||||||||||||||||||||||||||||||||||||||
| 624 | 0 | push(@pixels, $grey); | |||||||||||||||||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||
| 628 | 0 | ▶ | for (my $i = 0; $i <= $#pixels; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 629 | 0 | $pixels[$i] = $pixels[$i] / 256; | |||||||||||||||||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||
| 632 | 0 | return \@pixels; | |||||||||||||||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||
| 635 | # Return the pixel values for an image when using Imager | ||||||||||||||||||||||||||||||||||||||||||
| 636 | sub _pixels_Imager { | ||||||||||||||||||||||||||||||||||||||||||
| 637 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pixels_Imager: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 638 | 0 | my @pixels; | |||||||||||||||||||||||||||||||||||||||||
| 639 | 0 | ▶ | for (my $y = 0; $y < $self->{dct_size}; $y++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 640 | 0 | ▶ | for (my $x = 0; $x < $self->{dct_size}; $x++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 641 | my $c = $self->{im_scaled}->getpixel( | ||||||||||||||||||||||||||||||||||||||||||
| 642 | 0 | x => $x, | |||||||||||||||||||||||||||||||||||||||||
| 643 | y => $y | ||||||||||||||||||||||||||||||||||||||||||
| 644 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 645 | 0 | my ($red, $green, $blue, $alpha) = $c->rgba(); | |||||||||||||||||||||||||||||||||||||||||
| 646 | 0 | my $grey = $red * 0.3 + $green * 0.59 + $blue * 0.11; | |||||||||||||||||||||||||||||||||||||||||
| 647 | 0 | push(@pixels, $grey); | |||||||||||||||||||||||||||||||||||||||||
| 648 | } | ||||||||||||||||||||||||||||||||||||||||||
| 649 | } | ||||||||||||||||||||||||||||||||||||||||||
| 650 | 0 | return \@pixels; | |||||||||||||||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||||||||||
| 653 | sub reducedimage { | ||||||||||||||||||||||||||||||||||||||||||
| 654 | 0 | ▶ | my ($self, %opt) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reducedimage: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||||||||
| 656 | 0 | ▶ | $self->{reduced}->($self, %opt) unless $self->{im_scaled}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 657 | 0 | return $self->{im_scaled}; | |||||||||||||||||||||||||||||||||||||||||
| 658 | } | ||||||||||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||||||||||
| 660 | sub dctdump { | ||||||||||||||||||||||||||||||||||||||||||
| 661 | 0 | ▶ | my ($self, %opt) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dctdump: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||||
| 663 | 0 | ▶ | $self->{reduced}->($self, %opt) unless $self->{im_scaled}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 664 | 0 | ▶ | $self->{dct} ||= dct2d($self->{pixels}->(($self, %opt))); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"dct"} ||= dct2d($self->{"pixels"}->(($self, %opt)))
MC/DC0%0/2
$self->{"dct"} ||= dct2d($self->{"pixels"}->(($self, %opt)))
$self->{"dct"}
dct2d($self->{"pixels"}->(($self, %opt)))
Truth table0%0/3
A ||= B
A= $self->{"dct"}B= dct2d($self->{"pixels"}->(($self, %opt)))
| |||||||||||||||||||||||||||||||||||||||||||
| 665 | 0 | my $dctv = $self->_mirroring(%opt); | |||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||
| 667 | 0 | return $self->{$dctv}; | |||||||||||||||||||||||||||||||||||||||||
| 668 | } | ||||||||||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||||||||
| 670 | sub printbitmatrix { | ||||||||||||||||||||||||||||||||||||||||||
| 671 | 0 | ▶ | my ($self, %opt) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
printbitmatrix: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||||||
| 673 | 0 | my @array = $self->pHash(%opt); | |||||||||||||||||||||||||||||||||||||||||
| 674 | 0 | ▶ | my $sep = $opt{separator} || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt{'separator'} || ''
MC/DC0%0/1
$opt{'separator'} || ''
$opt{'separator'}
Truth table0%0/2
A
A= $opt{'separator'}
| |||||||||||||||||||||||||||||||||||||||||||
| 675 | 0 | ▶ | my $fill = $opt{filler} || ' '; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt{'filler'} || ' '
MC/DC0%0/1
$opt{'filler'} || ' '
$opt{'filler'}
Truth table0%0/2
A
A= $opt{'filler'}
| |||||||||||||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||||||||||||
| 677 | 0 | ▶ | my ($xs, $ys) = split(/x/, $opt{geometry} || '8x8'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt{'geometry'} || '8x8'
MC/DC0%0/1
$opt{'geometry'} || '8x8'
$opt{'geometry'}
Truth table0%0/2
A
A= $opt{'geometry'}
| |||||||||||||||||||||||||||||||||||||||||||
| 678 | 0 | my $str = ''; | |||||||||||||||||||||||||||||||||||||||||
| 679 | 0 | ▶ | if ($ys) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 680 | 0 | ▶ | for (my $i = 0; $i < $xs; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 681 | $str .= $opt{reduce} && ($i+$_==0 || $i+$_ >= ($xs+$ys)/2) ? "$fill$sep" : shift(@array).$sep | ||||||||||||||||||||||||||||||||||||||||||
| 682 | 0 | ▶ | for (0..$ys-1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$opt{"reduce"} and $i + $_ == 0 || $i + $_ >= ($xs + $ys) / 2
$i + $_ == 0 or $i + $_ >= ($xs + $ys) / 2
MC/DC0%0/3
$opt{"reduce"} and $i + $_ == 0 || $i + $_ >= ($xs + $ys) / 2
$opt{"reduce"}
$i + $_ == 0
$i + $_ >= ($xs + $ys) / 2
Truth table0%0/4
A and B or C
A= $opt{"reduce"}B= $i + $_ == 0C= $i + $_ >= ($xs + $ys) / 2
| |||||||||||||||||||||||||||||||||||||||||||
| 683 | 0 | $str .= "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||||||||||||||||
| 685 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 686 | 0 | my @matrix = ($fill); | |||||||||||||||||||||||||||||||||||||||||
| 687 | OUTER: | ||||||||||||||||||||||||||||||||||||||||||
| 688 | 0 | ▶ | for (my $i = 1; $i < $self->{dct_size}; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 689 | 0 | ▶ | for (my $j = 0; $j <= $i; $j++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 690 | 0 | ▶ | $matrix[$j] //= ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$matrix[$j] //= ""
MC/DC0%0/1
$matrix[$j] //= ""
$matrix[$j]
Truth table0%0/2
A
A= $matrix[$j]
| |||||||||||||||||||||||||||||||||||||||||||
| 691 | 0 | $matrix[$j] .= shift(@array).$sep; | |||||||||||||||||||||||||||||||||||||||||
| 692 | 0 | ▶ | last OUTER unless @array; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 693 | } | ||||||||||||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||||||||||||
| 695 | 0 | $str .= "$_\n" for @matrix; | |||||||||||||||||||||||||||||||||||||||||
| 696 | } | ||||||||||||||||||||||||||||||||||||||||||
| 697 | 0 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 698 | } | ||||||||||||||||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||||||||||||||||
| 700 | sub _mirroring { | ||||||||||||||||||||||||||||||||||||||||||
| 701 | 0 | ▶ | my ($self, %opt) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mirroring: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||||||||||
| 703 | 0 | my $dctv = 'dct'; | |||||||||||||||||||||||||||||||||||||||||
| 704 | 0 | ▶ | if ($opt{mirrorproof}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 705 | 0 | $dctv = 'mirrorproof'; | |||||||||||||||||||||||||||||||||||||||||
| 706 | 0 | ▶ | $self->{$dctv} ||= _dct_mirrorproof($self->{dct}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{$dctv} ||= _dct_mirrorproof($self->{"dct"})
MC/DC0%0/2
$self->{$dctv} ||= _dct_mirrorproof($self->{"dct"})
$self->{$dctv}
_dct_mirrorproof($self->{"dct"})
Truth table0%0/3
A ||= B
A= $self->{$dctv}B= _dct_mirrorproof($self->{"dct"})
| |||||||||||||||||||||||||||||||||||||||||||
| 707 | 0 | ▶ | croak("Options 'mirror' and 'mirrorproof' are mutually exclusive") if $opt{mirror}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 708 | } elsif ($opt{mirror}) { | ||||||||||||||||||||||||||||||||||||||||||
| 709 | 0 | $dctv = 'mirror'; | |||||||||||||||||||||||||||||||||||||||||
| 710 | 0 | ▶ | $self->{$dctv} ||= _dct_mirror($self->{dct}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{$dctv} ||= _dct_mirror($self->{"dct"})
MC/DC0%0/2
$self->{$dctv} ||= _dct_mirror($self->{"dct"})
$self->{$dctv}
_dct_mirror($self->{"dct"})
Truth table0%0/3
A ||= B
A= $self->{$dctv}B= _dct_mirror($self->{"dct"})
| |||||||||||||||||||||||||||||||||||||||||||
| 711 | } | ||||||||||||||||||||||||||||||||||||||||||
| 712 | 0 | return $dctv; | |||||||||||||||||||||||||||||||||||||||||
| 713 | } | ||||||||||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||||||||||
| 715 | sub _validate_options { | ||||||||||||||||||||||||||||||||||||||||||
| 716 | 0 | ▶ | my ($self, %opt) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_options: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||
| 717 | 0 | my %valid = ( | |||||||||||||||||||||||||||||||||||||||||
| 718 | geometry => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 719 | method => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 720 | mirror => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 721 | mirrorproof => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 722 | reduce => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 723 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||||||||||||
| 725 | 0 | foreach (keys %opt) { | |||||||||||||||||||||||||||||||||||||||||
| 726 | 0 | ▶ | carp("Unknown option '$_'") unless $valid{$_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 727 | } | ||||||||||||||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||||||||||||
| 729 | 0 | ▶ | $opt{geometry} ||= '8x8'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt{"geometry"} ||= "8x8"
MC/DC0%0/1
$opt{"geometry"} ||= "8x8"
$opt{"geometry"}
Truth table0%0/2
A
A= $opt{"geometry"}
| |||||||||||||||||||||||||||||||||||||||||||
| 730 | 0 | ▶ | $opt{method} ||= 'average'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opt{"method"} ||= "average"
MC/DC0%0/1
$opt{"method"} ||= "average"
$opt{"method"}
Truth table0%0/2
A
A= $opt{"method"}
| |||||||||||||||||||||||||||||||||||||||||||
| 731 | 0 | ▶ | if ($opt{geometry} =~ /^(\d+)(x\1)?$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 732 | croak("geometry cannot be greater than the resize value ($self->{dct_size})") | ||||||||||||||||||||||||||||||||||||||||||
| 733 | 0 | ▶ | if $2 && $1 > $self->{dct_size}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$2 and $1 > $self->{"dct_size"}
MC/DC0%0/2
$2 and $1 > $self->{"dct_size"}
$2
$1 > $self->{"dct_size"}
Truth table0%0/3
A and B
A= $2B= $1 > $self->{"dct_size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 734 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 735 | 0 | croak("geometry expected to be either 'NxN' for square matrix, or number of bits"); | |||||||||||||||||||||||||||||||||||||||||
| 736 | } | ||||||||||||||||||||||||||||||||||||||||||
| 737 | 0 | croak("Unsupported method. Choose from: ".keys(%{$self->{methods}})) | |||||||||||||||||||||||||||||||||||||||||
| 738 | 0 | ▶ | unless $self->{methods}->{$opt{method}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||
| 740 | 0 | return %opt; | |||||||||||||||||||||||||||||||||||||||||
| 741 | } | ||||||||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||||||||
| 743 | sub pHash { | ||||||||||||||||||||||||||||||||||||||||||
| 744 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pHash: not called CC=24 SCAR=64.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 745 | 0 | my %opt = $self->_validate_options(@_); | |||||||||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||||||||||
| 747 | 0 | ▶ | $self->{reduced}->($self, %opt) unless $self->{im_scaled}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 748 | 0 | ▶ | $self->{dct} ||= dct2d($self->{pixels}->($self, %opt)); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"dct"} ||= dct2d($self->{"pixels"}->($self, %opt))
MC/DC0%0/2
$self->{"dct"} ||= dct2d($self->{"pixels"}->($self, %opt))
$self->{"dct"}
dct2d($self->{"pixels"}->($self, %opt))
Truth table0%0/3
A ||= B
A= $self->{"dct"}B= dct2d($self->{"pixels"}->($self, %opt))
| |||||||||||||||||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||||||||||||||||
| 750 | 0 | my $dctv = $self->_mirroring(%opt); | |||||||||||||||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||||||||||||||||
| 752 | 0 | my (@array, @extra); | |||||||||||||||||||||||||||||||||||||||||
| 753 | 0 | ▶ | my $x_mult = ($opt{method} eq 'median' || $opt{method} eq 'average_x') ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opt{"method"} eq "median" or $opt{"method"} eq "average_x"
MC/DC0%0/2
$opt{"method"} eq "median" or $opt{"method"} eq "average_x"
$opt{"method"} eq "median"
$opt{"method"} eq "average_x"
Truth table0%0/3
A or B
A= $opt{"method"} eq "median"B= $opt{"method"} eq "average_x"
| |||||||||||||||||||||||||||||||||||||||||||
| 754 | 0 | ▶ | if ($opt{geometry} =~ /x/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 755 | 0 | my ($xs, $ys) = split(/x/, $opt{geometry}); | |||||||||||||||||||||||||||||||||||||||||
| 756 | 0 | ▶ | for (my $i = 0; $i < $xs; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 757 | 0 | ▶ | for (my $j = 0; $j < $ys; $j++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 758 | 0 | ▶ | next if $opt{reduce} && $i + $j == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opt{"reduce"} and $i + $j == 0
MC/DC0%0/2
$opt{"reduce"} and $i + $j == 0
$opt{"reduce"}
$i + $j == 0
Truth table0%0/3
A and B
A= $opt{"reduce"}B= $i + $j == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 759 | 0 | ▶ | if (!$opt{reduce} || ($i + $j < ($xs + $ys) / 2)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
not $opt{"reduce"} or $i + $j < ($xs + $ys) / 2
MC/DC0%0/2
not $opt{"reduce"} or $i + $j < ($xs + $ys) / 2
not $opt{"reduce"}
$i + $j < ($xs + $ys) / 2
Truth table0%0/3
not(A) or B
A= not $opt{"reduce"}B= $i + $j < ($xs + $ys) / 2
| |||||||||||||||||||||||||||||||||||||||||||
| 760 | 0 | push @array, $self->{$dctv}->[$i * $self->{dct_size} + $j]; | |||||||||||||||||||||||||||||||||||||||||
| 761 | } elsif ($x_mult) { | ||||||||||||||||||||||||||||||||||||||||||
| 762 | 0 | push @extra, $self->{$dctv}->[$i * $self->{dct_size} + $j]; | |||||||||||||||||||||||||||||||||||||||||
| 763 | } | ||||||||||||||||||||||||||||||||||||||||||
| 764 | } | ||||||||||||||||||||||||||||||||||||||||||
| 765 | } | ||||||||||||||||||||||||||||||||||||||||||
| 766 | # Let's allow diff=1 for median, even for NxN matrices | ||||||||||||||||||||||||||||||||||||||||||
| 767 | push(@extra, $self->{$dctv}->[$xs+1], $self->{$dctv}->[($xs+1) * $self->{dct_size}]) | ||||||||||||||||||||||||||||||||||||||||||
| 768 | 0 | ▶ | if ($x_mult && !$opt{reduce} && $xs < $self->{dct_size}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$x_mult and not $opt{"reduce"} and $xs < $self->{"dct_size"}
$x_mult and not $opt{"reduce"}
MC/DC0%0/3
$x_mult and not $opt{"reduce"} and $xs < $self->{"dct_size"}
$x_mult
not $opt{"reduce"}
$xs < $self->{"dct_size"}
Truth table0%0/4
A and not(B) and C
A= $x_multB= not $opt{"reduce"}C= $xs < $self->{"dct_size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 769 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 770 | OUTER: | ||||||||||||||||||||||||||||||||||||||||||
| 771 | 0 | ▶ | for (my $i = 1; $i < $self->{dct_size}; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 772 | 0 | ▶ | for (my $j = 0; $j <= $i; $j++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 773 | 0 | ▶ | if (scalar @array < $opt{geometry}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 774 | 0 | push(@array, $self->{$dctv}->[($i-$j)*$self->{dct_size}+$j]); | |||||||||||||||||||||||||||||||||||||||||
| 775 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 776 | $x_mult && scalar(@extra) < $x_mult * $opt{geometry} | ||||||||||||||||||||||||||||||||||||||||||
| 777 | 0 | ▶ | ? push(@extra, $self->{$dctv}->[($i - $j) * $self->{dct_size} + $j]) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$x_mult and scalar @extra < $x_mult * $opt{"geometry"}
MC/DC0%0/2
$x_mult and scalar @extra < $x_mult * $opt{"geometry"}
$x_mult
scalar @extra < $x_mult * $opt{"geometry"}
Truth table0%0/3
A and B
A= $x_multB= scalar @extra < $x_mult * $opt{"geometry"}
| |||||||||||||||||||||||||||||||||||||||||||
| 778 | : last OUTER; | ||||||||||||||||||||||||||||||||||||||||||
| 779 | } | ||||||||||||||||||||||||||||||||||||||||||
| 780 | } | ||||||||||||||||||||||||||||||||||||||||||
| 781 | } | ||||||||||||||||||||||||||||||||||||||||||
| 782 | } | ||||||||||||||||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||||||||||||||||
| 784 | # Convert to binary using median/average threshold | ||||||||||||||||||||||||||||||||||||||||||
| 785 | 0 | ▶ | my $skip = (!$opt{reduce} && $opt{geometry} =~ /x/) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $opt{"reduce"} and $opt{"geometry"} =~ /x/
MC/DC0%0/2
not $opt{"reduce"} and $opt{"geometry"} =~ /x/
not $opt{"reduce"}
$opt{"geometry"} =~ /x/
Truth table0%0/3
not(A) and B
A= not $opt{"reduce"}B= $opt{"geometry"} =~ /x/
| |||||||||||||||||||||||||||||||||||||||||||
| 786 | 0 | $self->{methods}->{$opt{method}}->(\@array, $skip, \@extra); | |||||||||||||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||||||||||||||||
| 789 | # Return an array of binary values in array context and a hex representative in scalar context. | ||||||||||||||||||||||||||||||||||||||||||
| 790 | 0 | ▶ | if (wantarray()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 791 | 0 | return @array; | |||||||||||||||||||||||||||||||||||||||||
| 792 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 793 | 0 | return b2h(join('', @array)); | |||||||||||||||||||||||||||||||||||||||||
| 794 | } | ||||||||||||||||||||||||||||||||||||||||||
| 795 | } | ||||||||||||||||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||||||||||||||||
| 797 | sub _dct_mirror { | ||||||||||||||||||||||||||||||||||||||||||
| 798 | 0 | ▶ | my $dct = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dct_mirror: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 799 | 0 | my $t = 1; | |||||||||||||||||||||||||||||||||||||||||
| 800 | 0 | ▶ | my @mirror = map {($t ^= 1) ? -$_ : $_} @$dct; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 801 | 0 | return \@mirror; | |||||||||||||||||||||||||||||||||||||||||
| 802 | } | ||||||||||||||||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||||||||||||||||
| 804 | sub _dct_mirrorproof { | ||||||||||||||||||||||||||||||||||||||||||
| 805 | 0 | ▶ | my $dct = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dct_mirrorproof: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 806 | 0 | my $t = 1; | |||||||||||||||||||||||||||||||||||||||||
| 807 | 0 | ▶ | my @mirror = map {($t ^= 1) ? abs($_) : $_} @$dct; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 808 | 0 | return \@mirror; | |||||||||||||||||||||||||||||||||||||||||
| 809 | } | ||||||||||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||||||||||
| 811 | sub _apply_average { | ||||||||||||||||||||||||||||||||||||||||||
| 812 | 0 | ▶ | my $array = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_average: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 813 | 0 | ▶ | my $skip = shift || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 814 | 0 | ▶ | my $extra = shift || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || []
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 815 | 0 | my $average = 0; | |||||||||||||||||||||||||||||||||||||||||
| 816 | 0 | $average += $array->[$_] foreach $skip .. $#$array; | |||||||||||||||||||||||||||||||||||||||||
| 817 | 0 | $average += $_ foreach @$extra; | |||||||||||||||||||||||||||||||||||||||||
| 818 | 0 | $average /= scalar(@$array) + scalar(@$extra) - $skip; | |||||||||||||||||||||||||||||||||||||||||
| 819 | 0 | ▶ | $array->[$_] = $array->[$_] >= $average ? '1' : '0' foreach 0 .. $#$array; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 820 | } | ||||||||||||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||||||||||
| 822 | sub _apply_log_average { | ||||||||||||||||||||||||||||||||||||||||||
| 823 | 0 | ▶ | my $array = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_log_average: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 824 | 0 | ▶ | my $skip = shift || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 825 | 0 | my $exp = 1 + (scalar(@$array)**(1 / 6)); | |||||||||||||||||||||||||||||||||||||||||
| 826 | 0 | my $average = 0; | |||||||||||||||||||||||||||||||||||||||||
| 827 | 0 | foreach ($skip .. $#$array) { | |||||||||||||||||||||||||||||||||||||||||
| 828 | 0 | my $add = abs($array->[$_])**(1 / $exp); | |||||||||||||||||||||||||||||||||||||||||
| 829 | 0 | ▶ | $average += $array->[$_] < 0 ? -$add : $add; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 830 | } | ||||||||||||||||||||||||||||||||||||||||||
| 831 | 0 | $average /= scalar(@$array) - $skip; | |||||||||||||||||||||||||||||||||||||||||
| 832 | 0 | my $thres = $average**$exp; | |||||||||||||||||||||||||||||||||||||||||
| 833 | 0 | ▶ | $thres *= -1 if $average < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 834 | 0 | ▶ | $array->[$_] = $array->[$_] >= $thres ? '1' : '0' foreach 0 .. $#$array; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 835 | } | ||||||||||||||||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||||||||||||||||
| 837 | sub _apply_diff { | ||||||||||||||||||||||||||||||||||||||||||
| 838 | 0 | ▶ | my $array = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_diff: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 839 | 0 | my $prev = 0; | |||||||||||||||||||||||||||||||||||||||||
| 840 | 0 | foreach (0 .. $#$array) { | |||||||||||||||||||||||||||||||||||||||||
| 841 | 0 | ▶ | my $diff = $array->[$_] > $prev ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 842 | 0 | $prev = $array->[$_]; | |||||||||||||||||||||||||||||||||||||||||
| 843 | 0 | $array->[$_] = $diff; | |||||||||||||||||||||||||||||||||||||||||
| 844 | } | ||||||||||||||||||||||||||||||||||||||||||
| 845 | } | ||||||||||||||||||||||||||||||||||||||||||
| 846 | |||||||||||||||||||||||||||||||||||||||||||
| 847 | sub _apply_median { | ||||||||||||||||||||||||||||||||||||||||||
| 848 | 0 | ▶ | my $array = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_median: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 849 | 0 | ▶ | my $skip = shift || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 850 | 0 | ▶ | my $extra = shift || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || []
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 851 | 0 | my @vals = sort {$a <=> $b} @$array, @$extra; | |||||||||||||||||||||||||||||||||||||||||
| 852 | 0 | my $len = scalar @vals - $skip; | |||||||||||||||||||||||||||||||||||||||||
| 853 | 0 | ▶ | my $thresh = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 854 | $len % 2 | ||||||||||||||||||||||||||||||||||||||||||
| 855 | ? $vals[int($len / 2)] | ||||||||||||||||||||||||||||||||||||||||||
| 856 | : ($vals[int($len / 2) - 1] + $vals[int($len / 2)]) / 2; | ||||||||||||||||||||||||||||||||||||||||||
| 857 | 0 | ▶ | $array->[$_] = $array->[$_] >= $thresh ? '1' : '0' foreach 0 .. $#$array; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 858 | } | ||||||||||||||||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||||||||||||||||
| 860 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||||||||||||
| 862 | Initially based on L<Image::Hash>, so code to do with loading images, pixels etc | ||||||||||||||||||||||||||||||||||||||||||
| 863 | has been kept. | ||||||||||||||||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||||||||||||||||
| 865 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||||||||||
| 867 | Dimitrios Kechagias, C<< <dkechag at cpan.org> >> | ||||||||||||||||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||||||||||||||||
| 869 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||||||||||||||||
| 871 | Please report any bugs or feature requests either on GitHub, or on RT (via the email | ||||||||||||||||||||||||||||||||||||||||||
| 872 | C<bug-image-phash at rt.cpan.org> or web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Image-PHash>). | ||||||||||||||||||||||||||||||||||||||||||
| 873 | |||||||||||||||||||||||||||||||||||||||||||
| 874 | I will be notified, and then you'll be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||||||||||||||||
| 876 | =head1 GIT | ||||||||||||||||||||||||||||||||||||||||||
| 877 | |||||||||||||||||||||||||||||||||||||||||||
| 878 | L<https://github.com/dkechag/Image-PHash> | ||||||||||||||||||||||||||||||||||||||||||
| 879 | |||||||||||||||||||||||||||||||||||||||||||
| 880 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||||||||||||||||
| 882 | Copyright (C) 2022, SpareRoom & Dimitrios Kechagias. | ||||||||||||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||||||||||||
| 884 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||||||||||||||||
| 885 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||||||||||||||||
| 887 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 888 | |||||||||||||||||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||||||||||||||||
| 890 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||