/**
  * Executes imageconvolution() filter
  *
  * @param WideImage_Image $image
  * @param array $matrix
  * @param numeric $div
  * @param numeric $offset
  * @return WideImage_Image
  */
 function execute($image, $matrix, $div, $offset)
 {
     $new = $image->asTrueColor();
     if (!imageconvolution($new->getHandle(), $matrix, $div, $offset)) {
         throw new WideImage_GDFunctionResultException("imageconvolution() returned false");
     }
     return $new;
 }
 /**
  * Returns a greyscale copy of an image
  *
  * @param WideImage_Image $image
  * @return WideImage_Image
  */
 function execute($image)
 {
     $new = $image->asTrueColor();
     if (!imagefilter($new->getHandle(), IMG_FILTER_GRAYSCALE)) {
         throw new WideImage_GDFunctionResultException("imagefilter() returned false");
     }
     if (!$image->isTrueColor()) {
         $new = $new->asPalette();
     }
     return $new;
 }
 /**
  * Applies a mask on the copy of source image
  *
  * @param WideImage_Image $image
  * @param WideImage_Image $mask
  * @param smart_coordinate $left
  * @param smart_coordinate $top
  * @return WideImage_Image
  */
 function execute($image, $mask, $left = 0, $top = 0)
 {
     $left = WideImage_Coordinate::fix($left, $image->getWidth(), $mask->getWidth());
     $top = WideImage_Coordinate::fix($top, $image->getHeight(), $mask->getHeight());
     $width = $image->getWidth();
     $mask_width = $mask->getWidth();
     $height = $image->getHeight();
     $mask_height = $mask->getHeight();
     $result = $image->asTrueColor();
     $result->alphaBlending(false);
     $result->saveAlpha(true);
     $srcTransparentColor = $result->getTransparentColor();
     if ($srcTransparentColor >= 0) {
         # this was here. works without.
         #$trgb = $image->getColorRGB($srcTransparentColor);
         #$trgb['alpha'] = 127;
         #$destTransparentColor = $result->allocateColorAlpha($trgb);
         #$result->setTransparentColor($destTransparentColor);
         $destTransparentColor = $srcTransparentColor;
     } else {
         $destTransparentColor = $result->allocateColorAlpha(255, 255, 255, 127);
     }
     for ($x = 0; $x < $width; $x++) {
         for ($y = 0; $y < $height; $y++) {
             $mx = $x - $left;
             $my = $y - $top;
             if ($mx >= 0 && $mx < $mask_width && $my >= 0 && $my < $mask_height) {
                 $srcColor = $image->getColorAt($x, $y);
                 if ($srcColor == $srcTransparentColor) {
                     $destColor = $destTransparentColor;
                 } else {
                     $maskRGB = $mask->getRGBAt($mx, $my);
                     if ($maskRGB['red'] == 0) {
                         $destColor = $destTransparentColor;
                     } elseif ($srcColor >= 0) {
                         $imageRGB = $image->getRGBAt($x, $y);
                         $level = $maskRGB['red'] / 255 * (1 - $imageRGB['alpha'] / 127);
                         $imageRGB['alpha'] = 127 - round($level * 127);
                         if ($imageRGB['alpha'] == 127) {
                             $destColor = $destTransparentColor;
                         } else {
                             $destColor = $result->allocateColorAlpha($imageRGB);
                         }
                     } else {
                         $destColor = $destTransparentColor;
                     }
                 }
                 $result->setColorAt($x, $y, $destColor);
             }
         }
     }
     return $result;
 }
 /**
  * Returns image with noise added
  *
  * @param WideImage_Image $image
  * @param float $amount
  * @param const $type
  * @param float $threshold
  * @return WideImage_Image
  */
 function execute($image, $amount, $type)
 {
     switch ($type) {
         case 'salt&pepper':
             $fun = 'saltPepperNoise_fun';
             break;
         case 'color':
             $fun = 'colorNoise_fun';
             break;
         default:
             $fun = 'monoNoise_fun';
             break;
     }
     return self::filter($image->asTrueColor(), $fun, $amount);
 }
 /**
  * Executes imagefilter
  *
  * @param WideImage_Image $image
  * @param int $filter 
  * @param numeric $arg1
  * @param numeric $arg2
  * @param numeric $arg3
  * @return WideImage_TrueColorImage
  */
 function execute($image, $filter, $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null)
 {
     $new = $image->asTrueColor();
     if (in_array($filter, self::$one_arg_filters)) {
         $res = imagefilter($new->getHandle(), $filter, $arg1);
     } elseif (defined('IMG_FILTER_PIXELATE') && $filter == IMG_FILTER_PIXELATE) {
         $res = imagefilter($new->getHandle(), $filter, $arg1, $arg2);
     } elseif ($filter == IMG_FILTER_COLORIZE) {
         $res = imagefilter($new->getHandle(), $filter, $arg1, $arg2, $arg3, $arg4);
     } else {
         $res = imagefilter($new->getHandle(), $filter);
     }
     if (!$res) {
         throw new WideImage_GDFunctionResultException("imagefilter() returned false");
     }
     return $new;
 }
 /**
  * Returns a merged image
  *
  * @param WideImage_Image $base
  * @param WideImage_Image $overlay
  * @param smart_coordinate $left
  * @param smart_coordinate $top
  * @param numeric $pct
  * @return WideImage_Image
  */
 function execute($base, $overlay, $left, $top, $pct)
 {
     $x = WideImage_Coordinate::fix($left, $base->getWidth(), $overlay->getWidth());
     $y = WideImage_Coordinate::fix($top, $base->getHeight(), $overlay->getHeight());
     $result = $base->asTrueColor();
     $result->alphaBlending(true);
     $result->saveAlpha(true);
     if ($pct <= 0) {
         return $result;
     }
     if ($pct < 100) {
         if (!imagecopymerge($result->getHandle(), $overlay->getHandle(), $x, $y, 0, 0, $overlay->getWidth(), $overlay->getHeight(), $pct)) {
             throw new WideImage_GDFunctionResultException("imagecopymerge() returned false");
         }
     } else {
         if (!imagecopy($result->getHandle(), $overlay->getHandle(), $x, $y, 0, 0, $overlay->getWidth(), $overlay->getHeight())) {
             throw new WideImage_GDFunctionResultException("imagecopy() returned false");
         }
     }
     return $result;
 }
 /**
  * Returns a greyscale copy of an image
  *
  * @param WideImage_Image $image
  * @return WideImage_Image
  */
 function execute($image)
 {
     $palette = !$image->isTrueColor();
     $transparent = $image->isTransparent();
     if ($palette && $transparent) {
         $tcrgb = $image->getTransparentColorRGB();
     }
     $new = $image->asTrueColor();
     if (!imagefilter($new->getHandle(), IMG_FILTER_NEGATE)) {
         throw new WideImage_GDFunctionResultException("imagefilter() returned false");
     }
     if ($palette) {
         $new = $new->asPalette();
         if ($transparent) {
             $irgb = array('red' => 255 - $tcrgb['red'], 'green' => 255 - $tcrgb['green'], 'blue' => 255 - $tcrgb['blue'], 'alpha' => 127);
             // needs imagecolorexactalpha instead of imagecolorexact, otherwise doesn't work on some transparent GIF images
             $new_tci = imagecolorexactalpha($new->getHandle(), $irgb['red'], $irgb['green'], $irgb['blue'], 127);
             $new->setTransparentColor($new_tci);
         }
     }
     return $new;
 }
 /**
  * @param WideImage_Image $image
  * @param int $radius
  * @param int $color
  * @param int $smoothness
  * @return WideImage_Image
  */
 function execute($image, $radius, $color, $smoothness, $corners)
 {
     if ($smoothness < 1) {
         $sample_ratio = 1;
     } elseif ($smoothness > 16) {
         $sample_ratio = 16;
     } else {
         $sample_ratio = $smoothness;
     }
     $corner = WideImage::createTrueColorImage($radius * $sample_ratio, $radius * $sample_ratio);
     if ($color === null) {
         imagepalettecopy($corner->getHandle(), $image->getHandle());
         $bg_color = $corner->allocateColor(0, 0, 0);
         $corner->fill(0, 0, $bg_color);
         $fg_color = $corner->allocateColor(255, 255, 255);
         $corner->getCanvas()->filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color);
         $corner = $corner->resize($radius, $radius);
         $result = $image->asTrueColor();
         $tc = $result->getTransparentColor();
         if ($tc == -1) {
             $tc = $result->allocateColorAlpha(255, 255, 255, 127);
             imagecolortransparent($result->getHandle(), $tc);
             $result->setTransparentColor($tc);
         }
         if ($corners & WideImage::SIDE_TOP_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_TOP) {
             $result = $result->applyMask($corner, -1, -1);
         }
         $corner = $corner->rotate(90);
         if ($corners & WideImage::SIDE_TOP_RIGHT || $corners & WideImage::SIDE_TOP || $corners & WideImage::SIDE_RIGHT) {
             $result = $result->applyMask($corner, $result->getWidth() - $corner->getWidth() + 1, -1, 100);
         }
         $corner = $corner->rotate(90);
         if ($corners & WideImage::SIDE_BOTTOM_RIGHT || $corners & WideImage::SIDE_RIGHT || $corners & WideImage::SIDE_BOTTOM) {
             $result = $result->applyMask($corner, $result->getWidth() - $corner->getWidth() + 1, $result->getHeight() - $corner->getHeight() + 1, 100);
         }
         $corner = $corner->rotate(90);
         if ($corners & WideImage::SIDE_BOTTOM_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_BOTTOM) {
             $result = $result->applyMask($corner, -1, $result->getHeight() - $corner->getHeight() + 1, 100);
         }
         return $result;
     } else {
         $bg_color = $color;
         $corner->fill(0, 0, $bg_color);
         $fg_color = $corner->allocateColorAlpha(127, 127, 127, 127);
         $corner->getCanvas()->filledEllipse($radius * $sample_ratio, $radius * $sample_ratio, $radius * 2 * $sample_ratio, $radius * 2 * $sample_ratio, $fg_color);
         $corner = $corner->resize($radius, $radius);
         $result = $image->copy();
         if ($corners & WideImage::SIDE_TOP_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_TOP) {
             $result = $result->merge($corner, -1, -1, 100);
         }
         $corner = $corner->rotate(90);
         if ($corners & WideImage::SIDE_TOP_RIGHT || $corners & WideImage::SIDE_TOP || $corners & WideImage::SIDE_RIGHT) {
             $result = $result->merge($corner, $result->getWidth() - $corner->getWidth() + 1, -1, 100);
         }
         $corner = $corner->rotate(90);
         if ($corners & WideImage::SIDE_BOTTOM_RIGHT || $corners & WideImage::SIDE_RIGHT || $corners & WideImage::SIDE_BOTTOM) {
             $result = $result->merge($corner, $result->getWidth() - $corner->getWidth() + 1, $result->getHeight() - $corner->getHeight() + 1, 100);
         }
         $corner = $corner->rotate(90);
         if ($corners & WideImage::SIDE_BOTTOM_LEFT || $corners & WideImage::SIDE_LEFT || $corners & WideImage::SIDE_BOTTOM) {
             $result = $result->merge($corner, -1, $result->getHeight() - $corner->getHeight() + 1, 100);
         }
         return $result;
     }
 }