You are here

function SmartcropImagickTestCase::testEntropy in Imagick 7

Verify the entropy calculation with a known image.

File

imagick_smartcrop/imagick_smartcrop.test, line 28
Tests for the smartcrop module.

Class

SmartcropImagickTestCase
@file Tests for the smartcrop module.

Code

function testEntropy() {

  // Create a test image with 3 red, 3 green, and 3 blue pixels.
  $im = new Imagick();
  $im
    ->newImage(3, 1, '#ff0000');
  $im
    ->newImage(3, 1, '#00ff00');
  $im
    ->newImage(3, 1, '#0000ff');
  $im
    ->resetIterator();
  $image = new stdClass();
  $image->resource = $im
    ->appendImages(true);

  // Calculate the expected values.
  // There are 9 bins in the histogram, 3 colors * 3 channels.
  $expected_entroy = (1 / 3 * log(1 / 9, 2) + 2 / 3 * log(2 / 9, 2)) * -1;
  $image_entropy = _smartcrop_imagick_entropy_slice($image, 0, 0, 3, 3);
  $this
    ->assertTrue($image_entropy - $expected_entroy < 0.001, t('Entropy value is correct.'));
}