<?php


abstract class phImaginator_Filter extends phImaginator_Universal implements phImaginator_Module {

	protected static $runCollected = false;


	// (c) mail at kavisiegel dot com
	public static function hue($r, $g, $b, $saturation = 100) {
		$image_data =& parent::getCurrentResource();

		$transparency = parent::calculateTransparency($saturation);

		$rgb = $r + $g + $b;

		$rgb = ($rgb > 0) ? $rgb : 1;

		$col = array($r / $rgb, $b / $rgb, $g / $rgb);

		$width = $image_data['width'];
		$height = $image_data['height'];

		for ($x = 0; $x < $width; ++$x) {
			for ($y = 0; $y < $height; ++$y) {
				$rgb = imagecolorat($image_data['resource'], $x, $y);

				$r = ($rgb >> 16) & 0xFF;
				$g = ($rgb >> 8) & 0xFF;
				$b = $rgb & 0xFF;

				$newR = $r * $col[0] + $g * $col[1] + $b * $col[2];
				$newG = $r * $col[2] + $g * $col[0] + $b * $col[1];
				$newB = $r * $col[1] + $g * $col[2] + $b * $col[0];

				imagesetpixel($image_data['resource'], $x, $y, imagecolorallocatealpha($image_data['resource'], $newR, $newG, $newB, $transparency));
			}
		}
	}


	public static function sepia() {
		$image_data =& parent::getCurrentResource();

		imagetruecolortopalette($image_data['resource'], true, 256);

		$colors = imagecolorstotal($image_data['resource']);

		for ($i = 0; $i < $colors; ++$i) {
			$pindex = imagecolorsforindex($image_data['resource'], $i);

			$r = ($pindex['red'] * 0.393 + $pindex['green'] * 0.769 + $pindex['blue'] * 0.189 ) / 1.351;
			$g = ($pindex['red'] * 0.349 + $pindex['green'] * 0.686 + $pindex['blue'] * 0.168) / 1.203;
			$b = ($pindex['red'] * 0.372 + $pindex['green'] * 0.534 + $pindex['blue'] * 0.131) / 2.140;

			imagecolorset($image_data['resource'], $i, $r, $g, $b);
		}
	}


	public static function common($filterType, $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null) {
		$image_data =& parent::getCurrentResource();

		$putArguments = array($image_data['resource'], $filterType);

		for ($arg = 1; $arg <= 4; ++$arg) {
			if (isset(${'arg' . $arg})) {
				$putArguments[] = ${'arg' . $arg};
			}
		}

		call_user_func_array('imagefilter', $putArguments);
	}


	public static function reduceColors($percentage = 100) {
		$image_data =& parent::getCurrentResource();

		$transparency = parent::calculateTransparency($percentage);

		for ($x = 0; $x < $image_data['width']; ++$x) {
			for ($y = 0; $y < $image_data['height']; ++$y) {
				$rgb = imagecolorat($image_data['resource'], $x, $y);

				$r = ($rgb >> 16) & 0xFF;
				$g = ($rgb >> 8) & 0xFF;
				$b = $rgb & 0xFF;

				$c = ($r + $g + $b) / 3;

				imagesetpixel($image_data['resource'], $x, $y, imagecolorallocatealpha($image_data['resource'], $c, $c, $c, $transparency));
			}
		}
	}

}







?>