-
-
Save redanium/e70426fd7742e434d81e to your computer and use it in GitHub Desktop.
1-bit Atkinson dither in PHP with GD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$img = imagecreatefromjpeg('./delayclose.jpg'); | |
imagefilter($img, IMG_FILTER_GRAYSCALE); | |
$width = imagesx($img); | |
$height = imagesy($img); | |
$img_arr = array(); | |
// Parse image (can be combined with dither stage, but combining them is slower.) | |
for($y=0; $y < $height; $y++){ | |
for($x=0; $x < $width; $x++){ | |
$img_arr[$x][$y] = imagecolorat($img, $x, $y); | |
} | |
} | |
// make a b/w output image. | |
$output = imagecreate($width, $height); | |
$black = imagecolorallocate($output, 0, 0, 0); //background color. | |
$white = imagecolorallocate($output, 0xff, 0xff, 0xff); | |
// Dither image with Atkinson dither | |
/* Atkinson Error Diffusion Kernel: | |
1/8 is 1/8 * quantization error. | |
+-------+-------+-------+-------+ | |
| | Curr. | 1/8 | 1/8 | | |
+-------|-------|-------|-------| | |
| 1/8 | 1/8 | 1/8 | | | |
+-------|-------|-------|-------| | |
| | 1/8 | | | | |
+-------+-------+-------+-------+ | |
*/ | |
for($y=0; $y < $height; $y++){ | |
for($x=0; $x < $width; $x++){ | |
$old = $img_arr[$x][$y]; | |
if($old > 0xffffff*.5){ // This is the b/w threshold. Currently @ halfway between white and black. | |
$new = 0xffffff; | |
imagesetpixel($output, $x, $y, $white); // Only setting white pixels, because the image is already black. | |
}else{ | |
$new = 0x000000; | |
} | |
$quant_error = $old-$new; | |
$error_diffusion = (1/8)*$quant_error; //I can do this because this dither uses 1 value for the applied error diffusion. | |
//dithering here. | |
$img_arr[$x+1][$y] += $error_diffusion; | |
$img_arr[$x+2][$y] += $error_diffusion; | |
$img_arr[$x-1][$y+1] += $error_diffusion; | |
$img_arr[$x][$y+1] += $error_diffusion; | |
$img_arr[$x+1][$y+1] += $error_diffusion; | |
$img_arr[$x][$y+2] += $error_diffusion; | |
} | |
} | |
// plop out a png of the dithered image. | |
Header("Content-type: image/png"); | |
imagepng($output, NULL, 9); //to print to screen | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment