[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/imageapi/ -> imagefilter.inc (source)

   1  <?php
   2  // $Id: imagefilter.inc,v 1.3.4.1 2009/03/18 08:35:59 drewish Exp $
   3  
   4  //Made by Chao Xu(Mgccl) 3/1/07
   5  //www.webdevlogs.com
   6  //V 1.0 
   7  
   8  // Drupal code style and some restructuring by dopry. http://www.darrelopry.com
   9  
  10  
  11  define('IMG_FILTER_NEGATE', 0);
  12  define('IMG_FILTER_GRAYSCALE', 1);
  13  define('IMG_FILTER_BRIGHTNESS', 2);
  14  define('IMG_FILTER_CONTRAST', 3);
  15  define('IMG_FILTER_COLORIZE', 4);
  16  define('IMG_FILTER_EDGEDETECT', 5);
  17  define('IMG_FILTER_EMBOSS', 6);
  18  define('IMG_FILTER_GAUSSIAN_BLUR', 7);
  19  define('IMG_FILTER_SELECTIVE_BLUR', 8);
  20  define('IMG_FILTER_MEAN_REMOVAL', 9);
  21  define('IMG_FILTER_SMOOTH', 10);
  22  
  23  define('IMAGEAPI_IMAGEFILTER_PHP', 1);
  24  /** 
  25   * walk each pixel in an image applying $callback to it.
  26   */
  27  function _imageapi_gd_pixel_color_walk(&$im, $callback, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) {
  28    $max_y = imagesy($im);
  29    $max_x = imagesx($im);
  30    for ($y=0; $y < $max_y; ++$y) {
  31      for ($x=0; $x < $max_x; ++$x) {
  32        $rgb = imagecolorat($im, $x, $y);
  33        $r = ($rgb >> 16) & 0xFF;
  34        $g = ($rgb >> 8) & 0xFF;
  35        $b = ($rgb & 0xFF);
  36        $a = $rgb >> 24;
  37  
  38        $callback($r, $g, $b, $a, $arg1, $arg2, $arg3, $arg4);
  39    
  40        // sanitize rgb values.
  41        $r = ($r > 255)? 255 : (($r < 0)? 0:$r);
  42        $g = ($g > 255)? 255 : (($g < 0)? 0:$g);
  43        $b = ($b > 255)? 255 : (($b < 0)? 0:$b);
  44        $a = ($a > 127)? 127 : (($a < 0)? 0:$a);
  45  
  46        if (!$color = imagecolorallocatealpha($im, $r, $g, $b, $a)) {
  47          $color = imagecolorclosestalpha($im, $r, $g, $b, $a);
  48        }
  49        imagesetpixel($im, $x, $y, $color);
  50      }
  51    }
  52  }
  53  
  54  function _imageapi_gd_pixel_negate(&$r, &$g, &$b, &$a) {
  55    $r = 255 - $r;
  56    $g = 255 - $g;
  57    $b = 255 - $b;
  58  }
  59  
  60  
  61  function _imageapi_gd_pixel_grayscale(&$r, &$g, &$b, &$a) {
  62    $r = round($r * 0.299 + $g * 0.587 + $b * 0.114);
  63    $g = $r;
  64    $b = $r;
  65  }
  66  
  67  function _imageapi_gd_pixel_brightness(&$r, &$g, &$b, &$a, $arg1) {
  68    $r += $arg1;
  69    $g += $arg1;
  70    $b += $arg1;
  71  }
  72  
  73  function _imageapi_gd_pixel_contrast(&$r, &$g, &$b, &$a, $contrast) {
  74    // normalize color value between -0.5 - 0.5 
  75    // multiply by contrast value to accentuate positive/negative value.
  76    // denormalize to 0-255 range.
  77    $r = ((($r/255 - 0.5) * $contrast) + 0.5) * 255;
  78    $g = ((($g/255 - 0.5) * $contrast) + 0.5) * 255;
  79    $b = ((($b/255 - 0.5) * $contrast) + 0.5) * 255;
  80  }
  81  
  82  function _imageapi_gd_pixel_colorize(&$r, &$g, &$b, &$a, $arg1, $arg2, $arg3, $arg4) {
  83    $r += $arg1;
  84    $g += $arg2;
  85    $b += $arg3;
  86  }
  87   
  88  function imagefilter(&$im, $var, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) {
  89  
  90    switch ($var) {
  91      case IMG_FILTER_NEGATE:
  92        _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_negate');
  93        return TRUE;
  94  
  95      case IMG_FILTER_GRAYSCALE:
  96        _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_grayscale');  
  97        return TRUE;
  98  
  99      case IMG_FILTER_BRIGHTNESS:
 100        _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_brightness', $arg1);  
 101        return TRUE;
 102  
 103      case IMG_FILTER_CONTRAST:
 104        // normalize between 0-1, square to keep positive.
 105        $contrast = pow((100-$arg1)/100, 2);
 106        _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_contrast', $contrast);  
 107        return TRUE;
 108  
 109      case IMG_FILTER_COLORIZE:
 110        $arg1 = (is_null($arg1)) ? 0 : $arg1;
 111        $arg2 = (is_null($arg2)) ? 0 : $arg2;
 112        $arg3 = (is_null($arg3)) ? 0 : $arg3;
 113        $arg4 = (is_null($arg4)) ? 0 : $arg4;
 114        _imageapi_gd_pixel_color_walk($im, '_imageapi_gd_pixel_colorize', $arg1, $arg2, $arg3, $arg4);  
 115        return TRUE;
 116  
 117      case IMG_FILTER_EDGEDETECT:
 118        return imageconvolution($im, array(array(-1, 0, -1), array(0, 4, 0), array(-1, 0, -1)), 1, 127);
 119  
 120      case IMG_FILTER_EMBOSS:
 121        return imageconvolution($im, array(array(1.5, 0, 0), array(0, 0, 0), array(0, 0, -1.5)), 1, 127);
 122  
 123      case IMG_FILTER_GAUSSIAN_BLUR:
 124        return imageconvolution($im, array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1)), 16, 0);
 125  
 126      case IMG_FILTER_SELECTIVE_BLUR:
 127        for ($y = 0; $y<$max_y; ++$y) {
 128          for ($x = 0; $x<$max_x; ++$x) {
 129            $flt_r_sum = $flt_g_sum = $flt_b_sum = 0;
 130            $cpxl = imagecolorat($im, $x, $y);
 131            for ($j=0; $j<3; ++$j) {
 132              for ($i=0; $i<3; ++$i) {
 133                if (($j == 1) && ($i == 1)) {
 134                  $flt_r[1][1] = $flt_g[1][1] = $flt_b[1][1] = 0.5;
 135                } 
 136                else {
 137                  $pxl = imagecolorat($im, $x-(3>>1)+$i, $y-(3>>1)+$j);
 138                  $new_a = $pxl >> 24;
 139                  //$r = (($pxl >> 16) & 0xFF);
 140                  //$g = (($pxl >> 8) & 0xFF);
 141                  //$b = ($pxl & 0xFF);
 142                  $new_r = abs((($cpxl >> 16) & 0xFF) - (($pxl >> 16) & 0xFF));
 143                  if ($new_r != 0) {
 144                    $flt_r[$j][$i] = 1/$new_r;
 145                  } 
 146                  else {
 147                    $flt_r[$j][$i] = 1;
 148                  }
 149  
 150                  $new_g = abs((($cpxl >> 8) & 0xFF) - (($pxl >> 8) & 0xFF));
 151                  if ($new_g != 0) {
 152                    $flt_g[$j][$i] = 1/$new_g;
 153                  } 
 154                  else {
 155                    $flt_g[$j][$i] = 1;
 156                  }
 157  
 158                  $new_b = abs(($cpxl & 0xFF) - ($pxl & 0xFF));
 159                  if ($new_b != 0) {
 160                    $flt_b[$j][$i] = 1/$new_b;
 161                  } 
 162                  else {
 163                    $flt_b[$j][$i] = 1;
 164                  }
 165                }
 166  
 167                $flt_r_sum += $flt_r[$j][$i];
 168                $flt_g_sum += $flt_g[$j][$i];
 169                $flt_b_sum += $flt_b[$j][$i];
 170              }
 171            }
 172  
 173            for ($j=0; $j<3; ++$j) {
 174              for ($i=0; $i<3; ++$i) {
 175                if ($flt_r_sum != 0) $flt_r[$j][$i] /= $flt_r_sum;
 176                if ($flt_g_sum != 0) $flt_g[$j][$i] /= $flt_g_sum;
 177                if ($flt_b_sum != 0) $flt_b[$j][$i] /= $flt_b_sum;
 178  
 179                $new_r = $new_g = $new_b = 0;
 180  
 181                for ($j=0; $j<3; ++$j) {
 182                  for ($i=0; $i<3; ++$i) {
 183                    $pxl = imagecolorat($im, $x-(3>>1)+$i, $y-(3>>1)+$j);
 184                    $new_r += (($pxl >> 16) & 0xFF) * $flt_r[$j][$i];
 185                    $new_g += (($pxl >> 8) & 0xFF) * $flt_g[$j][$i];
 186                    $new_b += ($pxl & 0xFF) * $flt_b[$j][$i];
 187                  }
 188                }
 189  
 190                $new_r = ($new_r > 255)? 255 : (($new_r < 0)? 0:$new_r);
 191                $new_g = ($new_g > 255)? 255 : (($new_g < 0)? 0:$new_g);
 192                $new_b = ($new_b > 255)? 255 : (($new_b < 0)? 0:$new_b);
 193                $new_pxl = imagecolorallocatealpha($im, (int)$new_r, (int)$new_g, (int)$new_b, $new_a);
 194                if ($new_pxl == FALSE) {
 195                  $new_pxl = imagecolorclosestalpha($im, (int)$new_r, (int)$new_g, (int)$new_b, $new_a);
 196                }
 197                imagesetpixel($im, $x, $y, $new_pxl);
 198              }
 199            }
 200          }
 201        }
 202        return TRUE;
 203  
 204      case IMG_FILTER_MEAN_REMOVAL:
 205        return imageconvolution($im, array(array(-1, -1, -1), array(-1, 9, -1), array(-1, -1, -1)), 1, 0);
 206  
 207      case IMG_FILTER_SMOOTH:
 208        return imageconvolution($im, array(array(1, 1, 1), array(1, $arg1, 1), array(1, 1, 1)), $arg1 + 8, 0);
 209    }
 210  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7