$max) return $max; return $value; } function rAt($img,$x,$y) { return (imagecolorat($img,$x,$y) >> 16) & 0xFF; } function gAt($img,$x,$y) { return (imagecolorat($img,$x,$y) >> 8) & 0xFF; } function bAt($img,$x,$y) { return imagecolorat($img,$x,$y) & 0xFF; } function pixelAt($img,$x,$y) { // returns the average of r,g,b for the given pixel return round((rAt($img,$x,$y)+gAt($img,$x,$y)+bAt($img,$x,$y))/3); } function pixelAt3($img,$x,$y,$w,$h) { // returns the pixelAt value, averaged for the 3x3 pixels around x,y $count=0; $sum=0; if ($x>0 && $y>0) { $sum+=pixelAt($img, $x-1, $y-1);$count++; } if ( $y>0) { $sum+=pixelAt($img, $x , $y-1);$count++; } if ($x<$w && $y>0) { $sum+=pixelAt($img, $x+1, $y-1);$count++; } if ($x>0 ) { $sum+=pixelAt($img, $x-1, $y );$count++; } if (1 ) { $sum+=pixelAt($img, $x , $y );$count++; } if ($x<$w ) { $sum+=pixelAt($img, $x+1, $y );$count++; } if ($x>0 && $y<$h) { $sum+=pixelAt($img, $x-1, $y+1);$count++; } if ( $y<$h) { $sum+=pixelAt($img, $x , $y+1);$count++; } if ($x<$w && $y<$h) { $sum+=pixelAt($img, $x+1, $y+1);$count++; } return $sum/$count; } $gaussian5=array(array(1, 4, 7, 4, 1), array(4, 20, 33, 20, 4), array(7, 33, 55, 33, 7), array(4, 20, 33, 20, 4), array(1, 4, 7, 4, 1)); $sharpen5=array(array(-1, -1, -1, -1, -1), array(-1, 2, 2, 2, -1), array(-1, 2, 8, 2, -1), array(-1, 2, 2, 2, -1), array(-1, -1, -1, -1, -1)); function pixelAverageAt($img, $x, $y, $s, $w, $h) { // averages the pixels around x,y (by convoluting with a (2s+1)x(2s+1) matrix global $sharpen5; $count=0; $sum=0.0; for ($i=-$s; $i<=$s; $i++) { for ($j=-$s; $j<=$s; $j++) { $px=$x+$i; $py=$y+$j; if ($px >= 0 && $px<$w && $py>=0 && $py<$h) { $sum+=pixelAt($img,$px,$py);// * $sharpen5[$i+$s][$j+$s]; } else { $sum+=255;// * $sharpen[$i+$s][$j+$s]; // pixels outside of the picture are white } $count++; } } return $sum/$count; } function S2P($x,$y,$params) // convert x,y from "sinusoidal space" to picture space // params[0]: angle of rotation (theta) // params[1]: scaling factor along X // params[2]: scaling factor along Y // params[3]: translation along X // params[4]: translation along Y { $c=cos($params[0]);$s=sin($params[0]); $sx=$params[1]; $sy=$params[2]; $tx=$params[3]; $ty=$params[4]; $xp=$x*$sx*$c - $y*$sy*$s + $sx*$c*$tx - $sy*$s*$ty; $yp=$x*$sx*$s + $y*$sy*$c + $sx*$s*$tx + $sy*$c*$ty; return array($xp,$yp); } function P2S($x,$y,$params) // convert x,y from picture space to "sinusoidal space" // params[0]: angle of rotation (theta) // params[1]: scaling factor along X // params[2]: scaling factor along Y // params[3]: translation along X // params[4]: translation along Y { $c=cos(-$params[0]);$s=sin(-$params[0]); $sx=1.0/$params[1]; $sy=1.0/$params[2]; $tx=-$params[3]; $ty=-$params[4]; $xp=$x*$sx*$c - $y*$sx*$s + $tx; $yp=$x*$sy*$s + $y*$sy*$c + $ty; ## FIXME: is this the inverse matrix of S2P return array($xp,$yp); } ?>