forked from jenssegers/imagehash
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding new perceptual hash implementation from @smithtek, code by @b1…
…rdex See issue jenssegers#52 Squashed commit of the following: commit 39baec8 Author: Anatoly Pashin <[email protected]> Date: Thu Jul 9 12:45:26 2020 +1000 Fix cs commit aa31683 Author: Anatoly Pashin <[email protected]> Date: Thu Jul 9 12:45:17 2020 +1000 Fix cs commit df4555d Author: Anatoly Pashin <[email protected]> Date: Thu Jul 9 12:40:40 2020 +1000 Fix typo commit b422ea4 Author: Anatoly Pashin <[email protected]> Date: Thu Jul 9 12:39:20 2020 +1000 Add PerceptualHash2
- Loading branch information
Vincent Chalnot
committed
Nov 22, 2020
1 parent
5fd4dbd
commit 3ee829e
Showing
2 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Jenssegers\ImageHash\Implementations; | ||
|
||
use Intervention\Image\Image; | ||
use Jenssegers\ImageHash\Hash; | ||
use Jenssegers\ImageHash\Implementation; | ||
use LogicException; | ||
|
||
/** | ||
* This is an optimized version of the original {@see PerceptualHash} | ||
* based on some R&D. You can read more at the issue page. | ||
* | ||
* @see https://github.com/jenssegers/imagehash/issues/52 | ||
*/ | ||
final class PerceptualHash2 implements Implementation | ||
{ | ||
const DCT_11_16 = [ | ||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], | ||
[0.99518473, 0.95694034, 0.88192126, 0.77301045, 0.63439328, 0.47139674, 0.29028468, 0.09801714, -0.09801714, -0.29028468, -0.47139674, -0.63439328, -0.77301045, -0.88192126, -0.95694034, -0.99518473], | ||
[0.98078528, 0.83146961, 0.55557023, 0.19509032, -0.19509032, -0.55557023, -0.83146961, -0.98078528, -0.98078528, -0.83146961, -0.55557023, -0.19509032, 0.19509032, 0.55557023, 0.83146961, 0.98078528], | ||
[0.95694034, 0.63439328, 0.09801714, -0.47139674, -0.88192126, -0.99518473, -0.77301045, -0.29028468, 0.29028468, 0.77301045, 0.99518473, 0.88192126, 0.47139674, -0.09801714, -0.63439328, -0.95694034], | ||
[0.92387953, 0.38268343, -0.38268343, -0.92387953, -0.92387953, -0.38268343, 0.38268343, 0.92387953, 0.92387953, 0.38268343, -0.38268343, -0.92387953, -0.92387953, -0.38268343, 0.38268343, 0.92387953], | ||
[0.88192126, 0.09801714, -0.77301045, -0.95694034, -0.29028468, 0.63439328, 0.99518473, 0.47139674, -0.47139674, -0.99518473, -0.63439328, 0.29028468, 0.95694034, 0.77301045, -0.09801714, -0.88192126], | ||
[0.83146961, -0.19509032, -0.98078528, -0.55557023, 0.55557023, 0.98078528, 0.19509032, -0.83146961, -0.83146961, 0.19509032, 0.98078528, 0.55557023, -0.55557023, -0.98078528, -0.19509032, 0.83146961], | ||
[0.77301045, -0.47139674, -0.95694034, 0.09801714, 0.99518473, 0.29028468, -0.88192126, -0.63439328, 0.63439328, 0.88192126, -0.29028468, -0.99518473, -0.09801714, 0.95694034, 0.47139674, -0.77301045], | ||
[0.70710678, -0.70710678, -0.70710678, 0.70710678, 0.70710678, -0.70710678, -0.70710678, 0.70710678, 0.70710678, -0.70710678, -0.70710678, 0.70710678, 0.70710678, -0.70710678, -0.70710678, 0.70710678], | ||
[0.63439328, -0.88192126, -0.29028468, 0.99518473, -0.09801714, -0.95694034, 0.47139674, 0.77301045, -0.77301045, -0.47139674, 0.95694034, 0.09801714, -0.99518473, 0.29028468, 0.88192126, -0.63439328], | ||
[0.55557023, -0.98078528, 0.19509032, 0.83146961, -0.83146961, -0.19509032, 0.98078528, -0.55557023, -0.55557023, 0.98078528, -0.19509032, -0.83146961, 0.83146961, 0.19509032, -0.98078528, 0.55557023], | ||
]; | ||
const DCT_11_32 = [ | ||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], | ||
[0.99879546, 0.98917651, 0.97003125, 0.94154407, 0.90398929, 0.85772861, 0.80320753, 0.74095113, 0.67155895, 0.5956993, 0.51410274, 0.42755509, 0.33688985, 0.24298018, 0.14673047, 0.04906767, -0.04906767, -0.14673047, -0.24298018, -0.33688985, -0.42755509, -0.51410274, -0.5956993, -0.67155895, -0.74095113, -0.80320753, -0.85772861, -0.90398929, -0.94154407, -0.97003125, -0.98917651, -0.99879546], | ||
[0.99518473, 0.95694034, 0.88192126, 0.77301045, 0.63439328, 0.47139674, 0.29028468, 0.09801714, -0.09801714, -0.29028468, -0.47139674, -0.63439328, -0.77301045, -0.88192126, -0.95694034, -0.99518473, -0.99518473, -0.95694034, -0.88192126, -0.77301045, -0.63439328, -0.47139674, -0.29028468, -0.09801714, 0.09801714, 0.29028468, 0.47139674, 0.63439328, 0.77301045, 0.88192126, 0.95694034, 0.99518473], | ||
[0.98917651, 0.90398929, 0.74095113, 0.51410274, 0.24298018, -0.04906767, -0.33688985, -0.5956993, -0.80320753, -0.94154407, -0.99879546, -0.97003125, -0.85772861, -0.67155895, -0.42755509, -0.14673047, 0.14673047, 0.42755509, 0.67155895, 0.85772861, 0.97003125, 0.99879546, 0.94154407, 0.80320753, 0.5956993, 0.33688985, 0.04906767, -0.24298018, -0.51410274, -0.74095113, -0.90398929, -0.98917651], | ||
[0.98078528, 0.83146961, 0.55557023, 0.19509032, -0.19509032, -0.55557023, -0.83146961, -0.98078528, -0.98078528, -0.83146961, -0.55557023, -0.19509032, 0.19509032, 0.55557023, 0.83146961, 0.98078528, 0.98078528, 0.83146961, 0.55557023, 0.19509032, -0.19509032, -0.55557023, -0.83146961, -0.98078528, -0.98078528, -0.83146961, -0.55557023, -0.19509032, 0.19509032, 0.55557023, 0.83146961, 0.98078528], | ||
[0.97003125, 0.74095113, 0.33688985, -0.14673047, -0.5956993, -0.90398929, -0.99879546, -0.85772861, -0.51410274, -0.04906767, 0.42755509, 0.80320753, 0.98917651, 0.94154407, 0.67155895, 0.24298018, -0.24298018, -0.67155895, -0.94154407, -0.98917651, -0.80320753, -0.42755509, 0.04906767, 0.51410274, 0.85772861, 0.99879546, 0.90398929, 0.5956993, 0.14673047, -0.33688985, -0.74095113, -0.97003125], | ||
[0.95694034, 0.63439328, 0.09801714, -0.47139674, -0.88192126, -0.99518473, -0.77301045, -0.29028468, 0.29028468, 0.77301045, 0.99518473, 0.88192126, 0.47139674, -0.09801714, -0.63439328, -0.95694034, -0.95694034, -0.63439328, -0.09801714, 0.47139674, 0.88192126, 0.99518473, 0.77301045, 0.29028468, -0.29028468, -0.77301045, -0.99518473, -0.88192126, -0.47139674, 0.09801714, 0.63439328, 0.95694034], | ||
[0.94154407, 0.51410274, -0.14673047, -0.74095113, -0.99879546, -0.80320753, -0.24298018, 0.42755509, 0.90398929, 0.97003125, 0.5956993, -0.04906767, -0.67155895, -0.98917651, -0.85772861, -0.33688985, 0.33688985, 0.85772861, 0.98917651, 0.67155895, 0.04906767, -0.5956993, -0.97003125, -0.90398929, -0.42755509, 0.24298018, 0.80320753, 0.99879546, 0.74095113, 0.14673047, -0.51410274, -0.94154407], | ||
[0.92387953, 0.38268343, -0.38268343, -0.92387953, -0.92387953, -0.38268343, 0.38268343, 0.92387953, 0.92387953, 0.38268343, -0.38268343, -0.92387953, -0.92387953, -0.38268343, 0.38268343, 0.92387953, 0.92387953, 0.38268343, -0.38268343, -0.92387953, -0.92387953, -0.38268343, 0.38268343, 0.92387953, 0.92387953, 0.38268343, -0.38268343, -0.92387953, -0.92387953, -0.38268343, 0.38268343, 0.92387953], | ||
[0.90398929, 0.24298018, -0.5956993, -0.99879546, -0.67155895, 0.14673047, 0.85772861, 0.94154407, 0.33688985, -0.51410274, -0.98917651, -0.74095113, 0.04906767, 0.80320753, 0.97003125, 0.42755509, -0.42755509, -0.97003125, -0.80320753, -0.04906767, 0.74095113, 0.98917651, 0.51410274, -0.33688985, -0.94154407, -0.85772861, -0.14673047, 0.67155895, 0.99879546, 0.5956993, -0.24298018, -0.90398929], | ||
[0.88192126, 0.09801714, -0.77301045, -0.95694034, -0.29028468, 0.63439328, 0.99518473, 0.47139674, -0.47139674, -0.99518473, -0.63439328, 0.29028468, 0.95694034, 0.77301045, -0.09801714, -0.88192126, -0.88192126, -0.09801714, 0.77301045, 0.95694034, 0.29028468, -0.63439328, -0.99518473, -0.47139674, 0.47139674, 0.99518473, 0.63439328, -0.29028468, -0.95694034, -0.77301045, 0.09801714, 0.88192126], | ||
]; | ||
const DCT_11_64 = [ | ||
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], | ||
[0.99969882, 0.99729046, 0.99247953, 0.98527764, 0.97570213, 0.96377607, 0.94952818, 0.9329928, 0.91420976, 0.8932243, 0.87008699, 0.84485357, 0.81758481, 0.78834643, 0.75720885, 0.72424708, 0.68954054, 0.65317284, 0.61523159, 0.57580819, 0.53499762, 0.49289819, 0.44961133, 0.40524131, 0.35989504, 0.31368174, 0.26671276, 0.21910124, 0.17096189, 0.12241068, 0.07356456, 0.02454123, -0.02454123, -0.07356456, -0.12241068, -0.17096189, -0.21910124, -0.26671276, -0.31368174, -0.35989504, -0.40524131, -0.44961133, -0.49289819, -0.53499762, -0.57580819, -0.61523159, -0.65317284, -0.68954054, -0.72424708, -0.75720885, -0.78834643, -0.81758481, -0.84485357, -0.87008699, -0.8932243, -0.91420976, -0.9329928, -0.94952818, -0.96377607, -0.97570213, -0.98527764, -0.99247953, -0.99729046, -0.99969882], | ||
[0.99879546, 0.98917651, 0.97003125, 0.94154407, 0.90398929, 0.85772861, 0.80320753, 0.74095113, 0.67155895, 0.5956993, 0.51410274, 0.42755509, 0.33688985, 0.24298018, 0.14673047, 0.04906767, -0.04906767, -0.14673047, -0.24298018, -0.33688985, -0.42755509, -0.51410274, -0.5956993, -0.67155895, -0.74095113, -0.80320753, -0.85772861, -0.90398929, -0.94154407, -0.97003125, -0.98917651, -0.99879546, -0.99879546, -0.98917651, -0.97003125, -0.94154407, -0.90398929, -0.85772861, -0.80320753, -0.74095113, -0.67155895, -0.5956993, -0.51410274, -0.42755509, -0.33688985, -0.24298018, -0.14673047, -0.04906767, 0.04906767, 0.14673047, 0.24298018, 0.33688985, 0.42755509, 0.51410274, 0.5956993, 0.67155895, 0.74095113, 0.80320753, 0.85772861, 0.90398929, 0.94154407, 0.97003125, 0.98917651, 0.99879546], | ||
[0.99729046, 0.97570213, 0.9329928, 0.87008699, 0.78834643, 0.68954054, 0.57580819, 0.44961133, 0.31368174, 0.17096189, 0.02454123, -0.12241068, -0.26671276, -0.40524131, -0.53499762, -0.65317284, -0.75720885, -0.84485357, -0.91420976, -0.96377607, -0.99247953, -0.99969882, -0.98527764, -0.94952818, -0.8932243, -0.81758481, -0.72424708, -0.61523159, -0.49289819, -0.35989504, -0.21910124, -0.07356456, 0.07356456, 0.21910124, 0.35989504, 0.49289819, 0.61523159, 0.72424708, 0.81758481, 0.8932243, 0.94952818, 0.98527764, 0.99969882, 0.99247953, 0.96377607, 0.91420976, 0.84485357, 0.75720885, 0.65317284, 0.53499762, 0.40524131, 0.26671276, 0.12241068, -0.02454123, -0.17096189, -0.31368174, -0.44961133, -0.57580819, -0.68954054, -0.78834643, -0.87008699, -0.9329928, -0.97570213, -0.99729046], | ||
[0.99518473, 0.95694034, 0.88192126, 0.77301045, 0.63439328, 0.47139674, 0.29028468, 0.09801714, -0.09801714, -0.29028468, -0.47139674, -0.63439328, -0.77301045, -0.88192126, -0.95694034, -0.99518473, -0.99518473, -0.95694034, -0.88192126, -0.77301045, -0.63439328, -0.47139674, -0.29028468, -0.09801714, 0.09801714, 0.29028468, 0.47139674, 0.63439328, 0.77301045, 0.88192126, 0.95694034, 0.99518473, 0.99518473, 0.95694034, 0.88192126, 0.77301045, 0.63439328, 0.47139674, 0.29028468, 0.09801714, -0.09801714, -0.29028468, -0.47139674, -0.63439328, -0.77301045, -0.88192126, -0.95694034, -0.99518473, -0.99518473, -0.95694034, -0.88192126, -0.77301045, -0.63439328, -0.47139674, -0.29028468, -0.09801714, 0.09801714, 0.29028468, 0.47139674, 0.63439328, 0.77301045, 0.88192126, 0.95694034, 0.99518473], | ||
[0.99247953, 0.9329928, 0.81758481, 0.65317284, 0.44961133, 0.21910124, -0.02454123, -0.26671276, -0.49289819, -0.68954054, -0.84485357, -0.94952818, -0.99729046, -0.98527764, -0.91420976, -0.78834643, -0.61523159, -0.40524131, -0.17096189, 0.07356456, 0.31368174, 0.53499762, 0.72424708, 0.87008699, 0.96377607, 0.99969882, 0.97570213, 0.8932243, 0.75720885, 0.57580819, 0.35989504, 0.12241068, -0.12241068, -0.35989504, -0.57580819, -0.75720885, -0.8932243, -0.97570213, -0.99969882, -0.96377607, -0.87008699, -0.72424708, -0.53499762, -0.31368174, -0.07356456, 0.17096189, 0.40524131, 0.61523159, 0.78834643, 0.91420976, 0.98527764, 0.99729046, 0.94952818, 0.84485357, 0.68954054, 0.49289819, 0.26671276, 0.02454123, -0.21910124, -0.44961133, -0.65317284, -0.81758481, -0.9329928, -0.99247953], | ||
[0.98917651, 0.90398929, 0.74095113, 0.51410274, 0.24298018, -0.04906767, -0.33688985, -0.5956993, -0.80320753, -0.94154407, -0.99879546, -0.97003125, -0.85772861, -0.67155895, -0.42755509, -0.14673047, 0.14673047, 0.42755509, 0.67155895, 0.85772861, 0.97003125, 0.99879546, 0.94154407, 0.80320753, 0.5956993, 0.33688985, 0.04906767, -0.24298018, -0.51410274, -0.74095113, -0.90398929, -0.98917651, -0.98917651, -0.90398929, -0.74095113, -0.51410274, -0.24298018, 0.04906767, 0.33688985, 0.5956993, 0.80320753, 0.94154407, 0.99879546, 0.97003125, 0.85772861, 0.67155895, 0.42755509, 0.14673047, -0.14673047, -0.42755509, -0.67155895, -0.85772861, -0.97003125, -0.99879546, -0.94154407, -0.80320753, -0.5956993, -0.33688985, -0.04906767, 0.24298018, 0.51410274, 0.74095113, 0.90398929, 0.98917651], | ||
[0.98527764, 0.87008699, 0.65317284, 0.35989504, 0.02454123, -0.31368174, -0.61523159, -0.84485357, -0.97570213, -0.99247953, -0.8932243, -0.68954054, -0.40524131, -0.07356456, 0.26671276, 0.57580819, 0.81758481, 0.96377607, 0.99729046, 0.91420976, 0.72424708, 0.44961133, 0.12241068, -0.21910124, -0.53499762, -0.78834643, -0.94952818, -0.99969882, -0.9329928, -0.75720885, -0.49289819, -0.17096189, 0.17096189, 0.49289819, 0.75720885, 0.9329928, 0.99969882, 0.94952818, 0.78834643, 0.53499762, 0.21910124, -0.12241068, -0.44961133, -0.72424708, -0.91420976, -0.99729046, -0.96377607, -0.81758481, -0.57580819, -0.26671276, 0.07356456, 0.40524131, 0.68954054, 0.8932243, 0.99247953, 0.97570213, 0.84485357, 0.61523159, 0.31368174, -0.02454123, -0.35989504, -0.65317284, -0.87008699, -0.98527764], | ||
[0.98078528, 0.83146961, 0.55557023, 0.19509032, -0.19509032, -0.55557023, -0.83146961, -0.98078528, -0.98078528, -0.83146961, -0.55557023, -0.19509032, 0.19509032, 0.55557023, 0.83146961, 0.98078528, 0.98078528, 0.83146961, 0.55557023, 0.19509032, -0.19509032, -0.55557023, -0.83146961, -0.98078528, -0.98078528, -0.83146961, -0.55557023, -0.19509032, 0.19509032, 0.55557023, 0.83146961, 0.98078528, 0.98078528, 0.83146961, 0.55557023, 0.19509032, -0.19509032, -0.55557023, -0.83146961, -0.98078528, -0.98078528, -0.83146961, -0.55557023, -0.19509032, 0.19509032, 0.55557023, 0.83146961, 0.98078528, 0.98078528, 0.83146961, 0.55557023, 0.19509032, -0.19509032, -0.55557023, -0.83146961, -0.98078528, -0.98078528, -0.83146961, -0.55557023, -0.19509032, 0.19509032, 0.55557023, 0.83146961, 0.98078528], | ||
[0.97570213, 0.78834643, 0.44961133, 0.02454123, -0.40524131, -0.75720885, -0.96377607, -0.98527764, -0.81758481, -0.49289819, -0.07356456, 0.35989504, 0.72424708, 0.94952818, 0.99247953, 0.84485357, 0.53499762, 0.12241068, -0.31368174, -0.68954054, -0.9329928, -0.99729046, -0.87008699, -0.57580819, -0.17096189, 0.26671276, 0.65317284, 0.91420976, 0.99969882, 0.8932243, 0.61523159, 0.21910124, -0.21910124, -0.61523159, -0.8932243, -0.99969882, -0.91420976, -0.65317284, -0.26671276, 0.17096189, 0.57580819, 0.87008699, 0.99729046, 0.9329928, 0.68954054, 0.31368174, -0.12241068, -0.53499762, -0.84485357, -0.99247953, -0.94952818, -0.72424708, -0.35989504, 0.07356456, 0.49289819, 0.81758481, 0.98527764, 0.96377607, 0.75720885, 0.40524131, -0.02454123, -0.44961133, -0.78834643, -0.97570213], | ||
[0.97003125, 0.74095113, 0.33688985, -0.14673047, -0.5956993, -0.90398929, -0.99879546, -0.85772861, -0.51410274, -0.04906767, 0.42755509, 0.80320753, 0.98917651, 0.94154407, 0.67155895, 0.24298018, -0.24298018, -0.67155895, -0.94154407, -0.98917651, -0.80320753, -0.42755509, 0.04906767, 0.51410274, 0.85772861, 0.99879546, 0.90398929, 0.5956993, 0.14673047, -0.33688985, -0.74095113, -0.97003125, -0.97003125, -0.74095113, -0.33688985, 0.14673047, 0.5956993, 0.90398929, 0.99879546, 0.85772861, 0.51410274, 0.04906767, -0.42755509, -0.80320753, -0.98917651, -0.94154407, -0.67155895, -0.24298018, 0.24298018, 0.67155895, 0.94154407, 0.98917651, 0.80320753, 0.42755509, -0.04906767, -0.51410274, -0.85772861, -0.99879546, -0.90398929, -0.5956993, -0.14673047, 0.33688985, 0.74095113, 0.97003125], | ||
]; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $size; | ||
/** | ||
* @var int[][]|float[][] | ||
*/ | ||
private $dct11; | ||
/** | ||
* @var float | ||
*/ | ||
private $sizeSqrt; | ||
|
||
public function __construct(int $size = 32) | ||
{ | ||
switch ($size) { | ||
case 16: | ||
$this->dct11 = self::DCT_11_16; | ||
break; | ||
case 32: | ||
$this->dct11 = self::DCT_11_32; | ||
break; | ||
case 64: | ||
$this->dct11 = self::DCT_11_64; | ||
break; | ||
default: | ||
throw new LogicException('$size must be 16, 32 or 64'); | ||
} | ||
|
||
$this->size = $size; | ||
$this->sizeSqrt = sqrt(2 / $size); | ||
} | ||
|
||
public function hash(Image $image): Hash | ||
{ | ||
// Resize the image. | ||
$resized = $image->resize($this->size, $this->size); | ||
|
||
$matrix = []; | ||
$row = []; | ||
$rows = []; | ||
$col = []; | ||
|
||
$matrixSize = 11; | ||
|
||
for ($y = 0; $y < $this->size; $y++) { | ||
for ($x = 0; $x < $this->size; $x++) { | ||
$rgb = $resized->pickColor($x, $y); | ||
$row[$x] = (int) floor(($rgb[0] * 0.299) + ($rgb[1] * 0.587) + ($rgb[2] * 0.114)); | ||
} | ||
$rows[$y] = $this->calculateDCT($row, $matrixSize); | ||
} | ||
|
||
$rowMatrixSize = $matrixSize; | ||
|
||
for ($x = 0; $x < $matrixSize; $x++) { | ||
for ($y = 0; $y < $this->size; $y++) { | ||
$col[$y] = $rows[$y][$x]; | ||
} | ||
$matrix[$x] = $this->calculateDCT($col, $rowMatrixSize); | ||
$rowMatrixSize--; | ||
} | ||
|
||
$pixels = $this->diagonalMatrix($matrix, $matrixSize); | ||
|
||
$pixels = array_slice($pixels, 1, 64); // discard first and cut to size | ||
|
||
$compare = $this->average($pixels); | ||
|
||
// Calculate hash. | ||
$bits = []; | ||
foreach ($pixels as $pixel) { | ||
$bits[] = (int) ($pixel > $compare); | ||
} | ||
|
||
return Hash::fromBits($bits); | ||
} | ||
|
||
/** | ||
* Perform a 1 dimension Discrete Cosine Transformation. | ||
* | ||
* @param int[]|float[] $matrix | ||
* | ||
* @return float[] | ||
*/ | ||
private function calculateDCT(array $matrix, int $partialSize): array | ||
{ | ||
$dctCos = $this->dct11; | ||
$transformed = []; | ||
|
||
for ($i = 0; $i < $partialSize; $i++) { | ||
$sum = 0; | ||
for ($j = 0; $j < $this->size; $j++) { | ||
$sum += $matrix[$j] * $dctCos[$i][$j]; | ||
} | ||
$sum *= $this->sizeSqrt; | ||
if ($i === 0) { | ||
$sum *= 0.70710678118655; | ||
} | ||
$transformed[$i] = $sum; | ||
} | ||
|
||
return $transformed; | ||
} | ||
|
||
private function diagonalMatrix(array $mat, int $size = 11, bool $half = true) | ||
{ | ||
$mode = 0; | ||
$it = 0; | ||
$lower = 0; | ||
$result = []; | ||
$max = ($half ? ceil((($size * $size) / 2) + ($size * 0.5)) : 0); | ||
for ($t = 0; $t < (2 * $size - 1); $t++) { | ||
$t1 = $t; | ||
if ($t1 >= $size) { | ||
$mode++; | ||
$t1 = $size - 1; | ||
$it--; | ||
$lower++; | ||
} else { | ||
$lower = 0; | ||
$it++; | ||
} | ||
for ($i = $t1; $i >= $lower; $i--) { | ||
if ($half && count($result) >= $max) { | ||
return $result; | ||
} | ||
if (($t1 + $mode) % 2 == 0) { | ||
$result[] = $mat[$i][$t1 + $lower - $i]; | ||
} else { | ||
$result[] = $mat[$t1 + $lower - $i][$i]; | ||
} | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Get the average of the pixel values. | ||
*/ | ||
private function average(array $pixels): float | ||
{ | ||
// Calculate the average value from top 8x8 pixels, except for the first one. | ||
$n = count($pixels) - 1; | ||
|
||
return array_sum(array_slice($pixels, 1, $n)) / $n; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters