/*
Apply effects on an image

Parameters
resource $resource	: image resource
integer $h			: hue
integer $s			: saturation
integer $v			: value
integer $a			: alpha

Return
resource
188897
public function apply($resource){
		// Extract arguments
		@list(,$h,$s,$v,$a)=func_get_args();
		$h=(int)$h;
		$s=(int)$s;
		$v=(int)$v;
		$a=(int)$a;
		// Convert
		list($r,$g,$b)=hsv2rgb($h,$s,$v);
		// Normalize
		if($a<0)        $a=0;
		elseif($a>100)  $a=100;
		if($v<0)        $v=0;
		elseif($v>100)  $v=100;
		$a=(100-$a)*1.27;
		$v=($v*2-100)*2.55;
		// Compute hue ratio
		$delta=($r+$g+$b);
		if($delta!=0){
			$ratio1=$r/$delta;
			$ratio2=$g/$delta;
			$ratio3=$b/$delta;
		}
		// récupération de la résolution
		$width=imagesx($resource);
		$height=imagesy($resource);
		if(!$width || !$height){
			throw new Exception("An error was encountered while getting image resolution");
		}
		// Apply effect
		$x=0;
		do{
			$y=0;
			do{
				// Get current pixel color
				list($r,$g,$b)=$this->_getColorAt($resource,$x,$y);
				// Define new color
				$new_r=$r*$ratio1+$g*$ratio2+$b*$ratio3+$v;
				$new_g=$r*$ratio3+$g*$ratio1+$b*$ratio2+$v;
				$new_b=$r*$ratio2+$g*$ratio3+$b*$ratio1+$v;
				// Format
				if($new_r<0)        $new_r=0;
				elseif($new_r>255)  $new_r=255;
				if($new_g<0)        $new_g=0;
				elseif($new_g>255)  $new_g=255;
				if($new_b<0)        $new_b=0;
				elseif($new_b>255)  $new_b=255;
				// Define pixel
				imagesetpixel($resource,$x,$y,imagecolorallocatealpha($resource,$new_r,$new_g,$new_b,$a));
			}
			while(++$y<$height);
		}
		while(++$x<$width);
		return $resource;
	}
1304376
public function apply(Picture $picture)
    {
        $resource = $picture->getResource();

        if($picture->isGd())
        {
            imagesavealpha($resource, TRUE);
            imagealphablending($resource, FALSE);

            $w = imagesx($resource);
            $h = imagesy($resource);

            // FIND SMALLEST ALPHA VALUE
            $minimalAlpha = 127;

            for($x = 0; $x < $w; $x++) {

                for($y = 0; $y < $h; $y++)
                {
                    $alpha = (imagecolorat($resource, $x, $y) >> 24 ) & 0xFF;

                    if($alpha < $minimalAlpha)
                    {
                        $minimalAlpha = $alpha;
                    }
                }
            }

            // MODIFY IMAGE PIXELS
            for($x = 0; $x < $w; $x++)
            {
                for($y = 0; $y < $h; $y++)
                {
                    $colorXY = imagecolorat($resource, $x, $y);
                    $alpha = ($colorXY >> 24) & 0xFF;

                    if($minimalAlpha !== 127)
                    {
                        $alpha = 127 + 127 * $this->opacity * ($alpha - 127) / (127 - $minimalAlpha);
                    }
                    else
                    {
                        $alpha += 127 * $this->opacity;
                    }

                    $alphaColorXY = imagecolorallocatealpha(

                        $resource,
                        ($colorXY >> 16) & 0xFF,
                        ($colorXY >> 8) & 0xFF,
                        $colorXY & 0xFF,
                        $alpha
                    );

                    // MODIFY SINGLE PIXEL VALUE
                    imagesetpixel($resource, $x, $y, $alphaColorXY);
                }
            }

            $picture->setResource($resource);
        }
        else
        {
            if(!$resource->getImageAlphaChannel())
            {
                $resource->setImageAlphaChannel(Imagick::ALPHACHANNEL_ACTIVATE);
            }

            $resource->evaluateImage(Imagick::EVALUATE_MULTIPLY, $this->opacity, Imagick::CHANNEL_ALPHA);
        }
    }
950518
public void setImage(Resource resource)
    {
        ImageIcon image = null;
        if (resource != null && resource.exists())
        {
            try
            {
                image = new ImageIcon(resource.getURL());
            }
            catch (IOException e)
            {
                logger.warn("Error reading resource: " + resource);
                throw new RuntimeException("Error reading resource " + resource, e);
            }
        }
        setImage(image);
    }
293543
public function apply( $image ) {

        imagefilter($image->getCore(), IMG_FILTER_COLORIZE, $this->red, $this->green, $this->blue);
        return $image;
    }
993809
public function cropTransparentImage($imageResource)
    {
        /** @var int $imageTransparency Transparent color */
        $imageTransparency = imagecolorallocatealpha($imageResource, 255, 255, 255, 127);
        /** @var resource $croppedImage Cropped image resource */
        $croppedImage = imagecreatetruecolor($_POST['crop_width'], $_POST['crop_height']);
        // Fill new image with transparent color
        imagefill($croppedImage, 0, 0, $imageTransparency);
        // Save Alpha chanel
        imagesavealpha($croppedImage, true);
        /** @var resource $rotatedImage Rotated image resource */
        $rotatedImage = imagerotate($imageResource, -($_POST['rotate']), $imageTransparency);
        // Copy rotated image to cropped one
        imagecopy(
            $croppedImage,
            $rotatedImage,
            0,
            0,
            $_POST['crop_x'],
            $_POST['crop_y'],
            $_POST['crop_width'],
            $_POST['crop_height']
        );
        // Delete temp image resource
        imagedestroy($rotatedImage);
        // Return result image
        return $croppedImage;
    }
985963
public function apply($resource){
		// Extract arguments
		@list(,$rplus,$gplus,$bplus)=func_get_args();
		$rplus=(int)$rplus;
		$gplus=(int)$gplus;
		$bplus=(int)$bplus;
		// Get resolution
		$width=imagesx($resource);
		$height=imagesy($resource);
		if(!$width || !$height){
			throw new Exception("An error was encountered while getting image resolution");
		}
		// Apply effect
		$x=0;
		do{
			$y=0;
			do{
				// Get current pixel color
				list($r,$g,$b,$a)=$this->_getColorAt($resource,$x,$y);
				// Define new color
				$b=$r+$bplus;
				$g=$r+$gplus;
				$r+=$rplus;
				// Format
				if($r<0)		$r=0;
				elseif($r>255)	$r=255;
				if($g<0)		$g=0;
				elseif($g>255)	$g=255;
				if($b<0)		$b=0;
				elseif($b>255)	$b=255;
				// Define pixel
				imagesetpixel($resource,$x,$y,imagecolorallocatealpha($resource,$r,$g,$b,$a));
			}
			while(++$y<$height);
		}
		while(++$x<$width);
		return $resource;
	}
910628
public function apply($image)
    {

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

        $pixels = array();
        $new    = imagecreatetruecolor($width, $height);
        for ($y = 0; $y < $height; $y++) {
            for ($x = 0; $x < $width; $x++) {
                // row 0
                if ($x > 0 and $y > 0) {
                    $matrix[0][0] = $this->getColor($old, $pixels,$x - 1, $y - 1);
                } else {
                    $matrix[0][0] = $this->getColor($old, $pixels, $x, $y);
                }

                if ($y > 0) {
                    $matrix[1][0] = $this->getColor($old, $pixels, $x, $y - 1);
                } else {
                    $matrix[1][0] = $this->getColor($old, $pixels, $x, $y);
                }

                if ($x + 1 < $width and $y > 0) {
                    $matrix[2][0] = $this->getColor($old, $pixels, $x + 1, $y - 1);
                } else {
                    $matrix[2][0] = $this->getColor($old, $pixels, $x, $y);
                }

                // row 1
                if ($x > 0) {
                    $matrix[0][1] = $this->getColor($old, $pixels, $x - 1, $y);
                } else {
                    $matrix[0][1] = $this->getColor($old, $pixels, $x, $y);
                }

                if ($x + 1 < $width) {
                    $matrix[2][1] = $this->getColor($old, $pixels, $x + 1, $y);
                } else {
                    $matrix[2][1] = $this->getColor($old, $pixels, $x, $y);
                }

                // row 1
                if ($x > 0 and $y + 1 < $height) {
                    $matrix[0][2] = $this->getColor($old, $pixels, $x - 1, $y + 1);
                } else {
                    $matrix[0][2] = $this->getColor($old, $pixels, $x, $y);
                }

                if ($y + 1 < $height) {
                    $matrix[1][2] = $this->getColor($old, $pixels, $x, $y + 1);
                } else {
                    $matrix[1][2] = $this->getColor($old, $pixels, $x, $y);
                }

                if ($x + 1 < $width and $y + 1 < $height) {
                    $matrix[2][2] = $this->getColor($old, $pixels, $x + 1, $y + 1);
                } else {
                    $matrix[2][2] = $this->getColor($old, $pixels, $x, $y);
                }

                $edge = $this->convolve($matrix);
                $edge = intval($edge / 2);
                if ($edge > 255) {
                    $edge = 255;
                }
                $color = imagecolorallocate($new, $edge, $edge, $edge);
                imagesetpixel($new, $x, $y, $color);

            }
        }
        imagedestroy($old); // Free resource
        // Create and return new image with updated core
        return new Image(
            $new,
            $image->getImageFile(),
            $width,
            $height,
            $image->getType()
        );
    }
739867
public function apply(Picture $picture)
    {
        $resource = $picture->getResource();

        if($picture->isGd())
        {
            imagefilter($resource, IMG_FILTER_CONTRAST, -15);
            imagefilter($resource, IMG_FILTER_BRIGHTNESS, 8);

            $picture->setResource($resource);
        }
        else
        {
            $resource->modulateImage(108, 170, 100);
        }
    }
326593
function applyResource(resource, isMethod) {
      let root;
      let parent;
      let currentPath;
      const path = resource.path.replace(/^\//, '').replace(/\/$/, '');
      const pathParts = path.split('/');

      function applyNodeResource(node, parts, index) {
        const n = node;
        if (index === parts.length - 1) {
          n.name = resource.name;
          if (resource.resourceId) {
            n.resourceId = resource.resourceId;
            if (_.every(predefinedResourceNodes, (iter) => iter.path !== n.path)) {
              predefinedResourceNodes.push(node);
            }
          }
          if (isMethod && !node.hasMethod) {
            n.hasMethod = true;
            if (_.every(methodNodes, (iter) => iter.path !== n.path)) {
              methodNodes.push(node);
            }
          }
        }

        parent = node;
      }

      pathParts.forEach((pathPart, index) => {
        currentPath = currentPath ? `${currentPath}/${pathPart}` : pathPart;
        root = root || _.find(trees, (node) => node.path === currentPath);
        parent = parent || root;

        let node;
        if (parent) {
          if (parent.path === currentPath) {
            applyNodeResource(parent, pathParts, index);
            return;
          } else if (parent.children.length > 0) {
            node = _.find(parent.children, (n) => n.path === currentPath);
            if (node) {
              applyNodeResource(node, pathParts, index);
              return;
            }
          }
        }

        node = {
          path: currentPath,
          pathPart,
          parent,

          level: index,
          children: [],
        };

        if (parent) {
          parent.children.push(node);
        }

        if (!root) {
          root = node;
          trees.push(root);
        }

        applyNodeResource(node, pathParts, index);
      });
    }
1200312
private function colorMeanValue($resource) {
		$colorList = array();
		$colorSum = 0;
		for ($a = 0; $a<8; $a++) {
			for ($b = 0; $b<8; $b++) {
				$rgb = imagecolorat($resource, $a, $b);
				$colorList[] = $rgb & 0xFF;
				$colorSum += $rgb & 0xFF;
			}
		}
		
		return array($colorSum/64,$colorList);
	}
744229
def _saturation(color, **kwargs):
    
    s = colorsys.rgb_to_hls(*[x / 255.0 for x in color.value[:3]])[2]
    return NumberValue((s * 100, '%'))
981250
protected function applyWatermarkGD($resource, Picture $picture) {

        // SOURCE IMAGE DIMENSIONS
        $pictureWidth  = imagesx($resource);
        $pictureHeight = imagesy($resource);

        // WATERMARK DIMENSIONS
        $watermarkPicture = new Picture($this->watermark, NULL, Picture::WORKER_GD);

        $opacity = new Opacity($this->opacity);
        $opacity->apply($watermarkPicture);

        if($this->size)
        {
            $resize = new Resize(
                $pictureWidth / 100 * $this->size,
                $pictureHeight / 100 * $this->size,
                Resize::MODE_FIT
            );

            $resize->apply($watermarkPicture);
        }

        $watermark = $watermarkPicture->getResource($watermarkPicture::WORKER_GD);

        imagealphablending($watermark, TRUE);

        $watermarkWidth  = imagesx($watermark);
        $watermarkHeight = imagesx($watermark);

        // CALCULATE WATERMARK POSITION
        switch($this->position)
        {
            case 'repeat':

                for($w = 0; $w < $pictureWidth; $w += $watermarkWidth + $this->space)
                {
                    for($h = 0; $h < $pictureHeight; $h += $watermarkHeight + $this->space)
                    {
                        imagecopy($resource, $watermark, $w, $h, 0, 0, $watermarkWidth, $watermarkHeight);
                    }
                }

                return $resource;

            case 'center':

                $positionX = ($pictureWidth - $watermarkWidth) / 2 - $this->offsetX;
                $positionY = ($pictureHeight - $watermarkHeight) / 2 - $this->offsetY;
                break;

            case 'topRight':

                $positionX = $pictureWidth - $watermarkWidth - $this->offsetX;
                $positionY = $this->offsetY;
                break;

            case 'bottomRight':

                $positionX = $pictureWidth  - $watermarkWidth  - $this->offsetX;
                $positionY = $pictureHeight - $watermarkHeight - $this->offsetY;
                break;

            case 'bottomLeft':

                $positionX = $this->offsetX;
                $positionY = $pictureHeight - $watermarkHeight - $this->offsetY;
                break;

            default:

                $positionX = $this->offsetX;
                $positionY = $this->offsetY;
                break;
        }

        imagecopy($resource, $watermark, $positionX, $positionY, 0, 0, $watermarkWidth, $watermarkHeight);

        return $resource;
    }
534741
public function apply($imageRessource)
  {
    imagefilter ($imageRessource , IMG_FILTER_COLORIZE, $this->red, $this->green, $this->blue, $this->alpha);
    return $imageRessource;
  }
419293
public function apply($resource){
		return parent::apply(
			parent::apply(
				$resource,
				parent::HORIZONTAL,
				2,1,0,0,0
			),
			parent::VERTICAL,
			2,1,0,0,0
		);
	}
883919
public function apply($resource){
		// Verify support
		if(!function_exists('imagefilter')){
			throw new Exception("It seems your PHP version is not compiled with the bundled version of the GD library");
		}
		// Extract arguments
		@list(,$color,$opacity)=func_get_args();
		$rgb=html2rgb($color);
		// Normalize
		if($opacity===null)		$opacity=100;
		elseif($opacity<0)		$opacity=0;
		elseif($opacity>100)	$opacity=100;
		$a=(100-$opacity)*1.27;
		// Apply effect
		if(!imagefilter($resource,IMG_FILTER_COLORIZE,$rgb[0],$rgb[1],$rgb[2],$a)){
			throw new Exception("COLORIZE filter failed");
		}
		return $resource;
	}
709315
public function apply($imageRessource)
  {
    $width = imagesx($imageRessource);
    $height = imagesy($imageRessource);
    for ($x = 0; $x < $width; $x++) {
      for ($y = 0; $y < $height; $y++) {
        $color = imagecolorallocatealpha($imageRessource, $this->red, $this->green, $this->blue, $this->opacity);
        imagesetpixel($imageRessource, $x, $y, $color);
      }
    }
    return $imageRessource;
  }
1280251
def apply(self, resource):
        
        if not isinstance(resource, Image.Image):
            raise ValueError('Unknown resource format')

        resource_format = resource.format
        resource = resource.rotate(self.angle, expand=True)
        resource.format = resource_format

        return resource