You are here

protected function ImageFocusEntropy::calculateHistogram in Image Focus Crop 6

Same name and namespace in other branches
  1. 7 image_focus.entropy.inc \ImageFocusEntropy::calculateHistogram()

Calculates the histogram of the zone from ($x, $y) with a width of $dx and height of $dy.

1 call to ImageFocusEntropy::calculateHistogram()
ImageFocusEntropy::calculateEntropy in ./image_focus.entropy.inc
Calculates the entropy of an image zone, defined as -sum(p.*log2(p)) where p is the histogram counts of a grayscale image.

File

./image_focus.entropy.inc, line 26
Image entropy calculation.

Class

ImageFocusEntropy
@file Image entropy calculation.

Code

protected function calculateHistogram($x, $y, $dx, $dy) {
  $maxx = min(imagesx($this->image), $x + $dx);
  $maxy = min(imagesy($this->image), $y + $dy);
  $histogram = array_fill(0, 768, 0);
  for ($i = $x; $i < $maxx; $i++) {
    for ($j = $y; $j < $maxy; $j++) {
      $rgb = imagecolorat($this->image, $i, $j);
      $r = $rgb >> 16 & 0xff;
      $g = $rgb >> 8 & 0xff;
      $b = $rgb & 0xff;
      $histogram[$r]++;
      $histogram[$g + 256]++;
      $histogram[$b + 512]++;
    }
  }
  return $histogram;
}