You are here

protected static function GdGaussianBlur::applyCoeffsLine in Image Effects 8

Same name and namespace in other branches
  1. 8.3 src/Component/GdGaussianBlur.php \Drupal\image_effects\Component\GdGaussianBlur::applyCoeffsLine()
  2. 8.2 src/Component/GdGaussianBlur.php \Drupal\image_effects\Component\GdGaussianBlur::applyCoeffsLine()

Applies the Gaussian coefficients to a line of the destination image.

Parameters

resource $src: The source image resource.

resource $dst: The destination image resource.

int $line: The image's line to be processed.

int $linelen: The line's length in pixels.

float[] $coeffs: The array of coefficients to use for the blur.

int $radius: The radius of the blur.

string $axis: The direction of the blur.

1 call to GdGaussianBlur::applyCoeffsLine()
GdGaussianBlur::applyCoeffs in src/Component/GdGaussianBlur.php
Applies the Gaussian coefficients to the destination image.

File

src/Component/GdGaussianBlur.php, line 101

Class

GdGaussianBlur
Gaussian Blur helper methods for GD.

Namespace

Drupal\image_effects\Component

Code

protected static function applyCoeffsLine($src, $dst, $line, $linelen, array $coeffs, $radius, $axis) {

  // Preloads line's pixels colors to minimize calls to imagexxx functions.
  $pixels = [];
  for ($ndx = 0; $ndx < $linelen; $ndx++) {
    $src_idx = $axis === 'HORIZONTAL' ? imagecolorat($src, $ndx, $line) : imagecolorat($src, $line, $ndx);
    $pixels[$ndx] = imagecolorsforindex($src, $src_idx);
  }

  // Loops through all pixels on the line.
  for ($ndx = 0; $ndx < $linelen; $ndx++) {
    $r = $g = $b = $a = 0;

    // Loops through all pixels in the radius.
    for ($cndx = -$radius; $cndx <= $radius; $cndx++) {
      $coeff = $coeffs[$cndx + $radius];
      $rndx = static::reflect($linelen, $ndx + $cndx);
      $r += $coeff * $pixels[$rndx]['red'];
      $g += $coeff * $pixels[$rndx]['green'];
      $b += $coeff * $pixels[$rndx]['blue'];
      $a += $coeff * $pixels[$rndx]['alpha'];
    }

    // Set resulting pixel color on the destination resource.
    $dst_color = imagecolorallocatealpha($dst, static::ucharClamp($r, 0xff), static::ucharClamp($g, 0xff), static::ucharClamp($b, 0xff), static::ucharClamp($a, 0x7f));
    if ($axis === 'HORIZONTAL') {
      imagesetpixel($dst, $ndx, $line, $dst_color);
    }
    else {
      imagesetpixel($dst, $line, $ndx, $dst_color);
    }
  }
}