Grafika\Imagick\Filter\Dither::ordered PHP Method

ordered() private method

Dither by applying a threshold map.
private ordered ( Image $image ) : Image
$image Grafika\Imagick\Image
return Grafika\Imagick\Image
    private function ordered($image)
    {
        // Localize vars
        $width = $image->getWidth();
        $height = $image->getHeight();
        $thresholdMap = array(array(15, 135, 45, 165), array(195, 75, 225, 105), array(60, 180, 30, 150), array(240, 120, 210, 90));
        // Loop using image1
        $pixelIterator = $image->getCore()->getPixelIterator();
        foreach ($pixelIterator as $y => $rows) {
            /* Loop through pixel rows */
            foreach ($rows as $x => $px) {
                /* Loop through the pixels in the row (columns) */
                /**
                 * @var $px \ImagickPixel */
                $rgba = $px->getColor();
                $gray = round($rgba['r'] * 0.3 + $rgba['g'] * 0.59 + $rgba['b'] * 0.11);
                $threshold = $thresholdMap[$x % 4][$y % 4];
                $oldPixel = ($gray + $threshold) / 2;
                if ($oldPixel <= 127) {
                    // Determine if black or white. Also has the benefit of clipping excess value
                    $newPixel = 0;
                } else {
                    $newPixel = 255;
                }
                // Current pixel
                $px->setColor("rgb({$newPixel},{$newPixel},{$newPixel})");
            }
            $pixelIterator->syncIterator();
            /* Sync the iterator, this is important to do on each iteration */
        }
        $type = $image->getType();
        $file = $image->getImageFile();
        $image = $image->getCore();
        return new Image($image, $file, $width, $height, $type);
        // Create new image with updated core
    }