You are here

protected function ImageFocusEntropy::calculateHistogram in Image Focus Crop 8

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 src/lib/ImageFocusEntropy.php
Calculates the entropy of an image zone.

File

src/lib/ImageFocusEntropy.php, line 48

Class

ImageFocusEntropy
Class ImageFocusEntropy.

Namespace

Drupal\image_focus\lib

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;
}