note 79910 added to function.imagewbmp

From: Date: Wed, 19 Dec 2007 13:31:43 +0000
Subject: note 79910 added to function.imagewbmp
Groups: php.notes 
Request: Send a blank email to php-notes+get-133777@lists.php.net to get a copy of this message
In the previous note by -> lukeross at sys3175 dot co dot uk <- there is a coding error in the for() loops. After correcting the error, I found that this function did a great job of converting color images to 2 colors. The following is the corrected function code: function ImageColorFloydSteinberg($dst_img, $src_img) { ImageColorAllocate($dst_img, 0,0,0); ImageColorAllocate($dst_img, 255,255,255); $isx = ImageSX($src_img); $isy = ImageSY($src_img); $grey_img = ImageCreate($isx, $isy); for ($a = 0; $a <= 255; $a++) ImageColorAllocate($grey_img, $a,$a,$a); $isx -= 1; // To correct pixel count in source image width starting from 0 $isy -= 1; // Correcting pixel count in source image height starting from 0 for($x = 0; $x <= $isx; $x++) { for($y = 0; $y <= $isy; $y++) { $color = ImageColorsForIndex($src_img, ImageColorAt($src_img, $x, $y)); $greyscale = .299 * $color["red"] + .587 * $color["green"] + .114 * $color["blue"]; ImageSetPixel($grey_img, $x, $y, ImageColorClosest($grey_img, $greyscale, $greyscale, $greyscale)); } } for($x = 0; $x <= $isx; $x++) { for($y = 0; $y <= $isy; $y++) { $color = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x, $y)); if ($color["red"] > 128) { ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img,255,255,255)); $err = $color["red"] - 255; } else { ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img,0,0,0)); $err = $color["red"]; } if ($x != $isx) { $color2 = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x+1, $y)); $newgrey = $color2["red"] + $err * 7 / 16; ImageSetPixel($grey_img, $x+1, $y, ImageColorClosest($grey_img,$newgrey, $newgrey, $newgrey)); } if ($x != 0) { $color2 = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x-1, $y)); $newgrey = $color2["red"] + $err * 3 / 16; ImageSetPixel($grey_img, $x-1, $y, ImageColorClosest($grey_img,$newgrey, $newgrey, $newgrey)); } if ($y != $isy) { $color2 = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x, $y+1)); $newgrey = $color2["red"] + $err * 5 / 16; ImageSetPixel($grey_img, $x, $y+1, ImageColorClosest($grey_img,$newgrey, $newgrey, $newgrey)); } if ($x != $isx && $y != $isy) { $color2 = ImageColorsForIndex($grey_img, ImageColorAt($grey_img, $x+1, $y+1)); $newgrey = $color2["red"] + $err / 16; ImageSetPixel($grey_img, $x+1, $y+1, ImageColorClosest($grey_img,$newgrey, $newgrey, $newgrey)); } } } imagedestroy($grey_img); } ---- Server IP: 69.147.83.197 Probable Submitter: 99.240.194.65 ---- Manual Page -- http://www.php.net/manual/en/function.imagewbmp.php Edit -- https://master.php.net/note/edit/79910 Del: integrated -- https://master.php.net/note/delete/79910/integrated Del: useless -- https://master.php.net/note/delete/79910/useless Del: bad code -- https://master.php.net/note/delete/79910/bad+code Del: spam -- https://master.php.net/note/delete/79910/spam Del: non-english -- https://master.php.net/note/delete/79910/non-english Del: in docs -- https://master.php.net/note/delete/79910/in+docs Del: other reasons-- https://master.php.net/note/delete/79910 Reject -- https://master.php.net/note/reject/79910 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#133777) next »