You are here

function SmartcropTestCase::testEntropy in Smart Crop 7

Same name and namespace in other branches
  1. 6 tests/smartcrop.test \SmartcropTestCase::testEntropy()

Verify the entropy calculation with a known image.

File

tests/smartcrop.test, line 31
Tests for the smartcrop module.

Class

SmartcropTestCase
@file Tests for the smartcrop module.

Code

function testEntropy() {

  // Create a test image with 3 red, 3 green, and 3 blue pixels.
  $image = imagecreatetruecolor(3, 3);
  imagefilledrectangle($image, 0, 0, 2, 0, imagecolorallocate($image, 255, 0, 0));
  imagefilledrectangle($image, 0, 1, 2, 1, imagecolorallocate($image, 0, 255, 0));
  imagefilledrectangle($image, 0, 2, 2, 2, imagecolorallocate($image, 0, 0, 255));

  // 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_gd_entropy($image);
  $this
    ->assertTrue($image_entropy - $expected_entroy < 0.001, t('Entropy value is correct.'));
}