Bug #14820 Updated: GD: ImageColorLeastUsed
| From: | sniper@php.net | Date: | Fri, 28 Jun 2002 09:43:00 +0000 |
| Subject: | Bug #14820 Updated: GD: ImageColorLeastUsed | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-12384@lists.php.net to get a copy of this message | ||
ID: 14820
Updated by: sniper@php.net
-Summary: Feature Request: ImageColorLeastUsed
Reported By: ceo@l-i-e.com
Status: Open
-Bug Type: GD related
+Bug Type: Feature/Change Request
Operating System: Any
PHP Version: 4.1.1
New Comment:
reclassified.
Previous Comments:
------------------------------------------------------------------------
[2002-01-03 04:05:32] ceo@l-i-e.com
ImageColorResolve et al are really cool, but SOMETIMES you
*need* the color you want, and they just won't cut it.
So you want to deallocate the least-used color, and then
allocate the one you really want.
Alas, the only way to do that (as far as I can figger) is
this:
$colorcount = array();
for ($x = 0; $x < $width; $x++){
for ($y = 0; $y < $height; $y++){
$colorindex = imagecolorat($jpg, $x, $y);
if (!isset($colorcount[$colorindex])){
$colorcount[$colorindex] = 1;
}
else{
$colorcount[$colorindex]++;
}
}
}
asort($colorcount);
reset($colorcount);
$black = imagecolorexact($jpg, 0, 0, 0);
if ($black == -1){
$goner = key($colorcount);
$rgb = imagecolorsforindex($jpg, $goner);
#error_log("Need black: About to kill $goner ($rgb[red],
$rgb[green], $rgb[blue]) which was only used in
$colorcount[$goner] pixels", 0);
unset($colorcount[$goner]);
imagecolordeallocate($jpg, $goner);
$black = imagecolorallocate($jpg, 0, 0, 0);
}
if ($black == -1){
$black = imagecolorresolve($jpg, 0, 0, 0);
#error_log("Damn! STILL couldn't allocate the color!",
0);
}
Ugly, ain't it :-)
Anyway, if GD has any internal structures of a histogram
nature, that one could use for a function matching this
documentation:
int ImageColorLeastUsed(int img)
Returns a color index of the least-used color in the image.
Suitable for ImageDeallocate() when you really need a color
that is not in the image.
Also handy would be something like:
array ImageColorMostContrasty(int img)
Returns an a array of 'red', 'green', 'blue' at a maxium
distance from all other colors currently in the image.
I have no clue which color-distance algorithm I would
actually want here. But it would be nice for adding
overlays to existing images that come from a random source.
If you want some idea of what I'm talking about:
http://chatmusic.com/maplocater.htm
The latitude/longitude lines and the red circles are being
pulled from a database and overlaid in real-time. However,
somebody might upload a map with lots of red and black in
it, and then I've got un-usable images.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=14820&edit=1