255 ) { $arrLUT[ $i ] = 255; } else { $arrLUT[ $i ] = $intVal; } } for ( $y = 0; $y < $intHeight; $y++ ) { for ( $x = 0; $x < $intWidth; $x++ ) { $rgbCol = imageColorAt( $srcImage, $x, $y ); $intR = ( $rgbCol >> 16 ) & 0xFF; $intG = ( $rgbCol >> 8 ) & 0xFF; $intB = $rgbCol & 0xFF; $gray = ( 77 * $intR + 150 * $intG + 29 * $intB ) >> 8; $value = $arrLUT[ 255 + $gray - $arrDither[ $x % $intSize ][ $y % $intSize ] ]; imageSetPixel( $srcImage, $x, $y, ( $value << 16 ) | ( $value << 8 ) | $value ); } } } function imageDitherPrintColor( &$srcImage, $intSize = 8, $intBlur = 6 ) { $intWidth = imageSX( $srcImage ); $intHeight = imageSY( $srcImage ); $intSize *= 15; $arrDitherR = imageCalcSpot( $intSize, 15.0, - 15.0 ); // cyan: 75 degrees $arrDitherG = imageCalcSpot( $intSize, 15.0, 15.0 ); // magenta: 15 degrees // optimized spot size for yellow... if ( $intSize == 90 ) { $arrDitherB = imageCalcSpot( $intSize, 9.0, 45.0 ); // yellow: 45 degrees } else if ( $intSize == 120 ) { $arrDitherB = imageCalcSpot( $intSize, 12.0, 45.0 ); // yellow: 45 degrees } else if ( $intSize == 180 ) { $arrDitherB = imageCalcSpot( $intSize, 12.0, 45.0 ); // yellow: 45 degrees } else { $arrDitherB = imageCalcSpot( $intSize, 15.0, 45.0 ); // yellow: 45 degrees } $intOffset = 255 - ( 255 / $intBlur ) / 2; for ( $i = 0; $i < 512; $i++ ) { $intVal = round( $intBlur * ( $i - $intOffset ) ); if ( $intVal < 0 ) { $arrLUT[ $i ] = 0; } else if ( $intVal > 255 ) { $arrLUT[ $i ] = 255; } else { $arrLUT[ $i ] = $intVal; } } for ( $y = 0; $y < $intHeight; $y++ ) { for ( $x = 0; $x < $intWidth; $x++ ) { $rgbCol = imageColorAt( $srcImage, $x, $y ); $intR = ( $rgbCol >> 16 ) & 0xFF; $intG = ( $rgbCol >> 8 ) & 0xFF; $intB = $rgbCol & 0xFF; $intR = 255 + $intR - $arrDitherR[ $x % $intSize ][ $y % $intSize ]; $intG = 255 + $intG - $arrDitherG[ $x % $intSize ][ $y % $intSize ]; $intB = 255 + $intB - $arrDitherB[ $x % $intSize ][ $y % $intSize ]; imageSetPixel( $srcImage, $x, $y, ( $arrLUT[ $intR ] << 16 ) | ( $arrLUT[ $intG ] << 8 ) | $arrLUT[ $intB ] ); } } } function imageDitherSimple( &$srcImage ) { $intWidth = imageSX( $srcImage ); $intHeight = imageSY( $srcImage ); $arrDither[ 0 ] = array( 0, 8, 2, 10 ); $arrDither[ 1 ] = array( 12, 4, 14, 6 ); $arrDither[ 2 ] = array( 3, 11, 1, 9 ); $arrDither[ 3 ] = array( 15, 7, 13, 5 ); for ( $y = 0; $y < $intHeight; $y++ ) { for ( $x = 0; $x < $intWidth; $x++ ) { $rgbCol = imageColorAt( $srcImage, $x, $y ); $rgbR = ( $rgbCol >> 16 ) & 0xFF; $rgbG = ( $rgbCol >> 8 ) & 0xFF; $rgbB = $rgbCol & 0xFF; $newR = ( $rgbR > $arrDither[ $x % 4 ][ $y % 4 ] ) ? 255 : 0; $newG = ( $rgbG > $arrDither[ $x % 4 ][ $y % 4 ] ) ? 255 : 0; $newB = ( $rgbB > $arrDither[ $x % 4 ][ $y % 4 ] ) ? 255 : 0; imageSetPixel( $srcImage, $x, $y, ( $newR << 16 ) | ( $newG << 8 ) | $newB ); } } } function imageDitherBayer( &$srcImage, $intSteps = 4 ) { $fltLevel = 255 / $intSteps; for ( $i = 0; $i < 256; $i++ ) { $arrSteps[ $i ] = round( round( $i / $fltLevel ) * $fltLevel ); } $intWidth = imageSX( $srcImage ); $intHeight = imageSY( $srcImage ); $arrDither[ 0 ] = array( 0, 128, 32, 160, 8, 136, 40, 168 ); $arrDither[ 1 ] = array( 192, 64, 224, 96, 200, 72, 232, 104 ); $arrDither[ 2 ] = array( 48, 176, 16, 144, 56, 184, 24, 152 ); $arrDither[ 3 ] = array( 240, 112, 208, 80, 248, 120, 216, 88 ); $arrDither[ 4 ] = array( 12, 140, 44, 172, 4, 132, 36, 164 ); $arrDither[ 5 ] = array( 204, 76, 236, 108, 196, 68, 228, 100 ); $arrDither[ 6 ] = array( 60, 188, 28, 156, 52, 180, 20, 148 ); $arrDither[ 7 ] = array( 252, 124, 220, 92, 244, 116, 212, 84 ); for ( $y = 0; $y < $intHeight; $y++ ) { for ( $x = 0; $x < $intWidth; $x++ ) { $rgbCol = imageColorAt( $srcImage, $x, $y ); $rgbR = ( $rgbCol >> 16 ) & 0xFF; $rgbG = ( $rgbCol >> 8 ) & 0xFF; $rgbB = $rgbCol & 0xFF; $newR = ( $rgbR > $arrDither[ $x % 8 ][ $y % 8 ] ) ? 255 : 0; $newG = ( $rgbG > $arrDither[ $x % 8 ][ $y % 8 ] ) ? 255 : 0; $newB = ( $rgbB > $arrDither[ $x % 8 ][ $y % 8 ] ) ? 255 : 0; imageSetPixel( $srcImage, $x, $y, ( $newR << 16 ) | ( $newG << 8 ) | $newB ); } } } function imageDitherPrint( &$srcImage ) { $intWidth = imageSX( $srcImage ); $intHeight = imageSY( $srcImage ); $arrDither[ 0 ] = array( 7, 111, 183, 239, 231, 191, 71, 15 ); $arrDither[ 1 ] = array( 87, 47, 135, 175, 199, 159, 55, 127 ); $arrDither[ 2 ] = array( 215, 143, 39, 79, 119, 63, 151, 207 ); $arrDither[ 3 ] = array( 247, 223, 103, 31, 23, 95, 167, 255 ); $arrDither[ 4 ] = array( 231, 191, 71, 15, 7, 111, 183, 239 ); $arrDither[ 5 ] = array( 199, 159, 55, 127, 87, 47, 135, 175 ); $arrDither[ 6 ] = array( 119, 63, 151, 207, 215, 143, 39, 79 ); $arrDither[ 7 ] = array( 23, 95, 167, 255, 247, 223, 103, 31 ); for ( $y = 0; $y < $intHeight; $y++ ) { for ( $x = 0; $x < $intWidth; $x++ ) { $rgbCol = imageColorAt( $srcImage, $x, $y ); $rgbR = ( $rgbCol >> 16 ) & 0xFF; $rgbG = ( $rgbCol >> 8 ) & 0xFF; $rgbB = $rgbCol & 0xFF; $newR = ( $rgbR > $arrDither[ $x % 8 ][ $y % 8 ] ) ? 255 : 0; $newG = ( $rgbG > $arrDither[ $x % 8 ][ $y % 8 ] ) ? 255 : 0; $newB = ( $rgbB > $arrDither[ $x % 8 ][ $y % 8 ] ) ? 255 : 0; imageSetPixel( $srcImage, $x, $y, ( $newR << 16 ) | ( $newG << 8 ) | $newB ); } } } function imageDitherSketch( &$srcImage, $intCell = 8 ) { $intWidth = imageSX( $srcImage ); $intHeight = imageSY( $srcImage ); $kc = 256 / $intCell; $jc = $kc / $intCell; for ( $i = 0; $i < $intCell; $i++ ) { for ( $j = 0; $j < $intCell; $j++ ) { $k1 = abs( ( $j + $i ) % $intCell ); $k2 = abs( ( $j - $i ) % $intCell ); $arrDitherR[ $i ][ $j ] = round( abs( 2 * $k1 * $kc + $j * $jc - 254 ) ); $arrDitherG[ $i ][ $j ] = round( abs( 2 * $k2 * $kc + $j * $jc - 254 ) ); $arrDitherB[ $i ][ $j ] = round( abs( 2 * $i * $kc + $j * $jc - 254 ) ); } } for ( $y = 0; $y < $intHeight; $y++ ) { for ( $x = 0; $x < $intWidth; $x++ ) { $rgbCol = imageColorAt( $srcImage, $x, $y ); $rgbR = ( $rgbCol >> 16 ) & 0xFF; $rgbG = ( $rgbCol >> 8 ) & 0xFF; $rgbB = $rgbCol & 0xFF; $newR = ( $rgbR > $arrDitherR[ $x % $intCell ][ $y % $intCell ] ) ? 255 : 0; $newG = ( $rgbG > $arrDitherG[ $x % $intCell ][ $y % $intCell ] ) ? 255 : 0; $newB = ( $rgbB > $arrDitherB[ $x % $intCell ][ $y % $intCell ] ) ? 255 : 0; imageSetPixel( $srcImage, $x, $y, ( $newR << 16 ) | ( $newG << 8 ) | $newB ); } } } function imageDitherSierra( &$srcImage, $intSteps = 4 ) { $fltLevel = 255 / $intSteps; for ( $i = 0; $i < 256; $i++ ) { $arrSteps[ $i ] = round( round( $i / $fltLevel ) * $fltLevel ); } for ( $i = 0; $i < 512; $i++ ) { $arrLUT[ $i ] = ( $i < 255 ) ? $i : 255; } $intWidth = imageSX( $srcImage ); $intHeight = imageSY( $srcImage ); for ( $y = 0; $y < $intHeight; $y++ ) { for ( $x = 0; $x < $intWidth; $x++ ) { $rgbCol = imageColorAt( $srcImage, $x, $y ); $rgbR = ( $rgbCol >> 16 ) & 0xFF; $rgbG = ( $rgbCol >> 8 ) & 0xFF; $rgbB = $rgbCol & 0xFF; $newR = $arrSteps[ $rgbR ]; $newG = $arrSteps[ $rgbG ]; $newB = $arrSteps[ $rgbB ]; imageSetPixel( $srcImage, $x, $y, ( $newR << 16 ) | ( $newG << 8 ) | $newB ); $deltaR = $rgbR - $newR; $deltaG = $rgbG - $newG; $deltaB = $rgbB - $newB; // $rgbCol = imageColorAt( $srcImage, $x + 1, $y ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( $deltaR >> 1 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( $deltaG >> 1 ); $newB = ( $rgbCol & 0xFF ) + ( $deltaB >> 1 ); imageSetPixel( $srcImage, $x + 1, $y, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); // $rgbCol = imageColorAt( $srcImage, $x - 1, $y + 1 ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( $deltaR >> 2 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( $deltaG >> 2 ); $newB = ( $rgbCol & 0xFF ) + ( $deltaB >> 2 ); imageSetPixel( $srcImage, $x - 1, $y + 1, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); // $rgbCol = imageColorAt( $srcImage, $x, $y + 1 ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( $deltaR >> 2 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( $deltaG >> 2 ); $newB = ( $rgbCol & 0xFF ) + ( $deltaB >> 2 ); imageSetPixel( $srcImage, $x, $y + 1, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); } } } function imageDitherFloyd( &$srcImage, $intSteps = 4 ) { $fltLevel = 255 / $intSteps; for ( $i = 0; $i < 256; $i++ ) { $arrSteps[ $i ] = round( round( $i / $fltLevel ) * $fltLevel ); } for ( $i = 0; $i < 512; $i++ ) { $arrLUT[ $i ] = ( $i < 255 ) ? $i : 255; } $intWidth = imageSX( $srcImage ); $intHeight = imageSY( $srcImage ); for ( $y = 0; $y < $intHeight; $y++ ) { for ( $x = 0; $x < $intWidth; $x++ ) { $rgbCol = imageColorAt( $srcImage, $x, $y ); $rgbR = ( $rgbCol >> 16 ) & 0xFF; $rgbG = ( $rgbCol >> 8 ) & 0xFF; $rgbB = $rgbCol & 0xFF; $newR = $arrSteps[ $rgbR ]; $newG = $arrSteps[ $rgbG ]; $newB = $arrSteps[ $rgbB ]; imageSetPixel( $srcImage, $x, $y, ( $newR << 16 ) | ( $newG << 8 ) | $newB ); $deltaR = $rgbR - $newR; $deltaG = $rgbG - $newG; $deltaB = $rgbB - $newB; // $rgbCol = imageColorAt( $srcImage, $x + 1, $y ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( ( 7 * $deltaR ) >> 4 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( ( 7 * $deltaG ) >> 4 ); $newB = ( $rgbCol & 0xFF ) + ( ( 7 * $deltaB ) >> 4 ); imageSetPixel( $srcImage, $x + 1, $y, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); // $rgbCol = imageColorAt( $srcImage, $x - 1, $y + 1 ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( ( 3 * $deltaR ) >> 4 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( ( 3 * $deltaG ) >> 4 ); $newB = ( $rgbCol & 0xFF ) + ( ( 3 * $deltaB ) >> 4 ); imageSetPixel( $srcImage, $x - 1, $y + 1, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); // $rgbCol = imageColorAt( $srcImage, $x, $y + 1 ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( ( 5 * $deltaR ) >> 4 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( ( 5 * $deltaG ) >> 4 ); $newB = ( $rgbCol & 0xFF ) + ( ( 5 * $deltaB ) >> 4 ); imageSetPixel( $srcImage, $x, $y + 1, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); // $rgbCol = imageColorAt( $srcImage, $x + 1, $y + 1 ); $newR = ( ( $rgbCol >> 16 ) & 0xFF ) + ( ( 1 * $deltaR ) >> 4 ); $newG = ( ( $rgbCol >> 8 ) & 0xFF ) + ( ( 1 * $deltaG ) >> 4 ); $newB = ( $rgbCol & 0xFF ) + ( ( 1 * $deltaB ) >> 4 ); imageSetPixel( $srcImage, $x + 1, $y + 1, ( $arrLUT[ $newR ] << 16 ) | ( $arrLUT[ $newG ] << 8 ) | $arrLUT[ $newB ] ); } } } function imageCalcSpot( $intSize, $fltFreq, $fltAngle ) { $fltAng = tan( deg2rad( $fltAngle ) ); $fltRad = 2.0 * PI(); $fltFactor = $intSize / $fltFreq; $arrSpot = array(); for ( $y = 0; $y < $intSize; $y++ ) { for ( $x = 0; $x < $intSize; $x++ ) { $value = cos( $fltRad * ( $x + $fltAng * $y ) / $fltFactor ) + cos( $fltRad * ( $y - $fltAng * $x ) / $fltFactor ); $arrSpot[ $y ][ $x ] = 128 + 63 * $value; } } return $arrSpot; } ?>