1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| <?php
$begin = microtime(true);
$image = imagecreatefrompng("8.png");
$white = imagecolorallocate($image, 255, 255, 255);
$lightGrey = imagecolorallocate($image, 160, 160, 160);
$darkGrey = imagecolorallocate($image, 80, 80, 80);
list($width, $height) = getimagesize('8.png');
$limit = 700;
for ($i=1; $i<$width; $i++) {
for ($j=1; $j<$height; $j++) {
if (imagecolorat($image, $i, $j) == $white)
{
$stack = array(
array($i, $j)
);
$toFill = array();
while (count($stack) > 0) {
$current = array_pop($stack);
array_push($toFill, $current);
// gros bug à cause de cette condition
if (count($toFill) > $limit)
break;
imagesetpixel($image, $current[0], $current[1], $lightGrey);
$north = array($current[0], $current[1]-1);
$south = array($current[0], $current[1]+1);
$west = array($current[0]-1, $current[1]);
$east = array($current[0]+1, $current[1]);
if ($north[1] > 0 && imagecolorat($image, $north[0], $north[1]) == $white)
array_push($stack, $north);
if ($south[1] < $height && imagecolorat($image, $south[0], $south[1]) == $white)
array_push($stack, $south);
if ($west[0] > 0 && imagecolorat($image, $west[0], $west[1]) == $white)
array_push($stack, $west);
if ($east[0] < $width && imagecolorat($image, $east[0], $east[1]) == $white)
array_push($stack, $east);
}
if (count($toFill) < $limit) {
while (count($toFill) > 0) {
$current = array_pop($toFill);
imagesetpixel($image, $current[0], $current[1], $darkGrey);
}
}
unset($stack);
unset($toFill);
}
}
}
imagepng($image, 'result.png');
imagedestroy($image);
$end = microtime(true);
echo ($end - $begin);
?> |
Partager