function RGBtoHSV($r, $g, $b) {
	$r = $r/255.; 
	$g = $g/255.;
	$b = $b/255.;
	$cols = array( => $r,  => $g,  => $b);
	asort($cols, SORT_NUMERIC);
	$min = key(array_slice($cols, 1)); 
	$max = key(array_slice($cols, -1)); 

	
	if($cols[$min] == $cols[$max]) {
		$h = 0;
	} else {
		if($max == ) {
			$h = 60. * ( 0 + ( ($cols[]-$cols[]) / ($cols[$max]-$cols[$min]) ) );
		} elseif ($max == ) {
			$h = 60. * ( 2 + ( ($cols[]-$cols[]) / ($cols[$max]-$cols[$min]) ) );
		} elseif ($max == ) {
			$h = 60. * ( 4 + ( ($cols[]-$cols[]) / ($cols[$max]-$cols[$min]) ) );
		}
		if($h < 0) {
			$h += 360;
		}
	}

	
	if($cols[$max] == 0) {
		$s = 0;
	} else {
		$s = ( ($cols[$max]-$cols[$min])/$cols[$max] );
		$s = $s * 255;
	}

	
	$v = $cols[$max];
	$v = $v * 255;

	return(array($h, $s, $v));
}

$filename = ;
$dimensions = getimagesize($filename);
$w = $dimensions[0]; 
$h = $dimensions[1]; 

$im = imagecreatefrompng($filename);

for($hi=0; $hi < $h; $hi++) {

	for($wi=0; $wi < $w; $wi++) {
		$rgb = imagecolorat($im, $wi, $hi);

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

		
		$brgb = imagecolorat($im, $wi, $hi+1);
		$br = ($brgb >> 16) & 0xFF;
		$bg = ($brgb >> 8) & 0xFF;
		$bb = $brgb & 0xFF;
		$bhsv = RGBtoHSV($br, $bg, $bb);

		
		if($hsv[2]-$bhsv[2] > 20) { 
                    imagesetpixel($im, $wi, $hi, imagecolorallocate($im, 255, 0, 0));
		} 
                else {
		    imagesetpixel($im, $wi, $hi, imagecolorallocate($im, 0, 0, 0));
		}

        }

}

header('Content-Type: image/jpeg');
imagepng($im);
imagedestroy($im);