Gaussian blur (memory optimized, uses more CPU)

@param int $distance Blur distance.
@return Image
439871
public function useBlur($distance) {
        if($this->useBooster || $this->width * $this->height < 100000) {
            return $this->useFastBlur($distance);
        }
        $cache = array(array());
        
        for ($x = 0; $x < $this->width; ++$x) {
            $maxX = $this->width < $x + $distance ? $this->width : $x + $distance;
            $k1 = $x < $distance ? 0 : $x - $distance;
            
            for ($y = 0; $y < $this->height; ++$y) {
                $maxY = $this->height < $y + $distance ? $this->height : $y + $distance;
                $l1 = $y < $distance ? 0 : $y - $distance;
                $r = $g = $b = 0;
                
                for($k = $k1; $k < $maxX; ++$k) {
                    for ($l = $l1; $l < $maxY; ++$l) {
                        if(isset($cache[$k][$l])) {
                            $color = $cache[$k][$l];
                        } else {
                            $color = imagecolorat($this->image, $k, $l);// $columnK[$l];
                            $cache[$k][$l] = $color;
                        }
                        
                        $r += ($color >> 16) & 0xff;
                        $g += ($color >> 8) & 0xff;
                        $b += $color & 0xff;
                    }

                    if($k % 32 === 0 && $l % 16 === 0) {
                        $cache = array(array());
                    }
                }
                $size = ($maxX - $k1) * ($maxY - $l1);               
                
                $r /= $size;
                $g /= $size;
                $b /= $size;
                imagesetpixel($this->image, $x, $y, $r << 16 | $g << 8 | $b);
            }
        }
        return $this;
    }
46179
public function sloppy($phrase, $distance)
    {
        if ($distance <= 0) {
            throw new InvalidArgumentException('Slop distance has to be greater than 0.');
        }

        if (\strpos($phrase, ' ') === false) {
            throw new InvalidArgumentException('Sloppy phrase must consist of multiple terms, separated with spaces.');
        }

        $this->predicates[] = $this->processValue($phrase) . '~' . $distance;

        return $this;
    }
349748
def gauss_blur( sigma, max_error = 1.0 / 0x100 )
      filter_type = DFLOAT.align typecode
      filter = Sequence[ *Array.gauss_blur_filter( sigma, max_error / dimension ) ].
        to_type filter_type
      ( dimension - 1 ).downto( 0 ).inject self do |retval,i|
        retval.convolve filter
      end
    end
467266
public function toHdr($threshold = 0.80) {
		if($threshold > 1 || $threshold < 0) {
			throw new Exception('Invalid threshold, should be between 0 and 1');
		}

		$emboss = $this->getCopy();
		$gd = $emboss->getGd2Handle();
		imagefilter($gd, IMG_FILTER_EMBOSS);
		imagefilter($gd, IMG_FILTER_GAUSSIAN_BLUR);
		$emboss->toGrayScale();

		//$this->image = $light;
		$this->useShader('
			$alpha = ((imagecolorat($args[0], $x, $y) & 0xff) / 255);
			if($luminance < '.$threshold.') {
				$dd = ($luminance/ '.$threshold.');
				$luminance *= 0.9 * $dd;
				$saturation *= 2 - $dd;
				if($saturation > 1) {
					$saturation = 1;
				}
			} else {
				if($alpha < 0.9) {
					$luminance *= (1 - $alpha / 8);
					$saturation *= 1.3 * (1 - $alpha / 8);
				} else {
					$dd = 1 - (($luminance - '.$threshold.') / '.(1 - $threshold).');
					$saturation *= 1.8 * $dd;
				}
			}
		', array($gd));

		//$this->image = $gd;
		return $this;
	}
769347
function scaleImage( $imageObject, $filterData, &$filterVariables, $sourceMimeData, $destinationMimeData )
    {
        $geometry = $this->calculateScaledAspectGeometry( ImageSX( $imageObject ), ImageSY( $imageObject ),
                                                          $filterData['data'][0], $filterData['data'][1], true );
        return $this->scaleImageCopy( $imageObject,
                                      $geometry,
                                      $sourceMimeData, $destinationMimeData );
    }
159208
public function run(Image $image)
    {
        $blur = $this->getBlur();

        if ($blur !== null) {
            $image->blur($blur);
        }

        return $image;
    }
325333
public static <T extends ImageBase<T>> BlurStorageFilter<T> gaussian(ImageType<T> type , double sigma , int radius ) {
		return new BlurStorageFilter<>("gaussian", type, sigma, radius);
	}
400010
public function tetrad(int $distance = self::DEFAULT_DISTANCE): array
    {
        return [
            $this->baseColor,
            $this->baseColor->adjustHue(180),
            $this->baseColor->adjustHue($distance * -1),
            $this->baseColor->adjustHue(180 + $distance),
        ];
    }
1154288
public function apply($resource){
		@list(,$input,$output)=func_get_args();
		$input=(float)$input;
		$output=(float)$output;
		if(!imagegammacorrect($resource,$input,$output)){
			throw new Exception("Gamma correction failed");
		}
		return $resource;
	}
1345288
def disparity_image_to_distance(self, dval_img):
        ""
        dist_img = dval_img / 2048.0
        dist_img = 1 / (self.opars[0]*dist_img + self.opars[1])
        return dist_img
456765
def blur(self, image, geometry, options):
        
        if options.get('blur'):
            return self._blur(image, int(options.get('blur')))
        return image
561000
private function diffusion( $image ){
        $pixel = array();

        // Localize vars
        $width = $image->getWidth();
        $height = $image->getHeight();
        $old = $image->getCore();

        $new = imagecreatetruecolor($width, $height);

        for ( $y = 0; $y < $height; $y+=1 ) {
            for ( $x = 0; $x < $width; $x+=1 ) {

                $color = imagecolorat( $old, $x, $y );
                $r = ($color >> 16) & 0xFF;
                $g = ($color >> 8) & 0xFF;
                $b = $color & 0xFF;

                $gray = round($r * 0.3 + $g * 0.59 + $b * 0.11);

                if(isset($pixel[$x][$y])){ // Add errors to color if there are
                    $gray += $pixel[$x][$y];
                }

                if ( $gray <= 127 ) { // Determine if black or white. Also has the benefit of clipping excess val due to adding the error
                    $blackOrWhite = 0;
                } else {
                    $blackOrWhite = 255;
                }

                $oldPixel = $gray;
                $newPixel = $blackOrWhite;

                // Current pixel
                imagesetpixel( $new, $x, $y,
                    imagecolorallocate( $new,
                        $newPixel,
                        $newPixel,
                        $newPixel
                    )
                );

                $qError = $oldPixel - $newPixel; // Quantization error

                // Propagate error on neighbor pixels
                if ( $x + 1 < $width ) {
                    $pixel[$x+1][$y] = (isset($pixel[$x+1][$y]) ? $pixel[$x+1][$y] : 0) + ($qError * (7 / 16));
                }

                if ( $x - 1 > 0 and $y + 1 < $height ) {
                    $pixel[$x-1][$y+1] = (isset($pixel[$x-1][$y+1]) ? $pixel[$x-1][$y+1] : 0) + ($qError * (3 / 16));
                }

                if ( $y + 1 < $height ) {
                    $pixel[$x][$y+1] = (isset($pixel[$x][$y+1]) ? $pixel[$x][$y+1] : 0) + ($qError * (5 / 16));
                }

                if ( $x + 1 < $width and $y + 1 < $height ) {
                    $pixel[$x+1][$y+1] = (isset($pixel[$x+1][$y+1]) ? $pixel[$x+1][$y+1] : 0) + ($qError * (1 / 16));
                }

            }
        }

        imagedestroy($old); // Free resource
        // Create new image with updated core
        return new Image(
            $new,
            $image->getImageFile(),
            $width,
            $height,
            $image->getType()
        );
    }
267127
def standardDeviation2d(img, ksize=5, blurred=None):
    '''
    
    '''
    if ksize not in (list, tuple):
        ksize = (ksize,ksize)

    if blurred is None:
        blurred = gaussian_filter(img, ksize)
    else:
        assert blurred.shape == img.shape
    
    std = np.empty_like(img)
    
    _calc(img, ksize[0], ksize[1], blurred, std)
    
    return std
1268438
public function getBlur()
    {
        if (!is_numeric($this->blur)) {
            return;
        }

        if ($this->blur < 0 or $this->blur > 100) {
            return;
        }

        return (int) $this->blur;
    }
505328
public function apply( $image ) {
        $amount = $this->amount;
        // build matrix
        $min = $amount >= 10 ? $amount * -0.01 : 0;
        $max = $amount * -0.025;
        $abs = ((4 * $min + 4 * $max) * -1) + 1;
        $div = 1;
        $matrix = array(
            array($min, $max, $min),
            array($max, $abs, $max),
            array($min, $max, $min)
        );
        // apply the matrix
        imageconvolution($image->getCore(), $matrix, $div, 0);
        return $image;
    }
468765
public function apply( $image ) {

        $image->getCore()->sigmoidalContrastImage($this->amount > 0, $this->amount / 4, 0);
        return $image;
    }
489828
public function adjacent(int $distance = self::DEFAULT_DISTANCE): array
    {
        return [
            $this->baseColor->adjustHue($distance * -1),
            $this->baseColor,
            $this->baseColor->adjustHue($distance),
        ];
    }