You are here

function _imageapi_gd_pixel_color_walk in ImageAPI 6

Same name and namespace in other branches
  1. 5 imagefilter.inc \_imageapi_gd_pixel_color_walk()

walk each pixel in an image applying $callback to it.

1 call to _imageapi_gd_pixel_color_walk()
imagefilter in ./imagefilter.inc

File

./imagefilter.inc, line 26

Code

function _imageapi_gd_pixel_color_walk(&$im, $callback, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) {
  $max_y = imagesy($im);
  $max_x = imagesx($im);
  for ($y = 0; $y < $max_y; ++$y) {
    for ($x = 0; $x < $max_x; ++$x) {
      $rgb = imagecolorat($im, $x, $y);
      $r = $rgb >> 16 & 0xff;
      $g = $rgb >> 8 & 0xff;
      $b = $rgb & 0xff;
      $a = $rgb >> 24;
      $callback($r, $g, $b, $a, $arg1, $arg2, $arg3, $arg4);

      // sanitize rgb values.
      $r = $r > 255 ? 255 : ($r < 0 ? 0 : $r);
      $g = $g > 255 ? 255 : ($g < 0 ? 0 : $g);
      $b = $b > 255 ? 255 : ($b < 0 ? 0 : $b);
      $a = $a > 127 ? 127 : ($a < 0 ? 0 : $a);
      if (!($color = imagecolorallocatealpha($im, $r, $g, $b, $a))) {
        $color = imagecolorclosestalpha($im, $r, $g, $b, $a);
      }
      imagesetpixel($im, $x, $y, $color);
    }
  }
}