imagecolorallocate accuracy problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gth759k
Forum Commoner
Posts: 76
Joined: Mon Jun 15, 2009 3:04 am

imagecolorallocate accuracy problem

Post by gth759k »

I've been experimenting with different methods of storing photo data in MySQL. So, not the location to a photo, but the actual data. I have one script that takes in a photo and puts the data in the database by looping through each pixel and using imagecolorat and converting the rgb to html, and another script that can read the stored data and create an image by converting the html back to rgb and using imagecolorallocate and imagesetpixel. The data stored in the database looks like this:

(condensed)

0=263435&1=1c2524&2=141613&3=e0f09&4=171a13&5=282d27&6=36413b&7=3a4b45&8=32424f&9=253a3f ...

(exploded)

0=263435
1=1c2524
2=141613
3=e0f09
4=171a13
5=282d27
6=36413b
7=3a4b45
8=32424f
9=253a3f
...

where the first number is an index in the image, starting at the top left corner and going down. So, if the image has a height of 137 pixels then an index value of 159 would correspond to (x, y) = (1, 22) by these equations $x = floor($index/$height); $y = $index - ($x*$height); (first row and column are zero, so its actually the 23rd row and 2nd column).

Anyways, it sort of works, but I can't figure out why the image I piece back together has artifacts.

Here is the original image:
Image

Here is the generated image:
Image

I've already checked the database values for some of the funky pixels and the right value was stored, but it doesn't set the right color to the image. Also, it doesn't matter how many times I run the scripts, the artifacts end up in the same spots every time. Would this be a memory problem?

Any help would be appreciated. Thanks.
gth759k
Forum Commoner
Posts: 76
Joined: Mon Jun 15, 2009 3:04 am

Re: imagecolorallocate accuracy problem

Post by gth759k »

lol, I figured it out.

wrong:

Code: Select all

        $rgb = imagecolorat($image, $x, $y);
        $html = dechex($rgb);
right:

Code: Select all

        $rgb = imagecolorat($image, $x, $y);
	$r = ($rgb >> 16) & 0xFF; 
	$g = ($rgb >> 8) & 0xFF; 
	$b = $rgb & 0xFF;
			
        $r2 = dechex($r<0?0:($r>255?255:$r));
	$g2 = dechex($g<0?0:($g>255?255:$g));
	$b2 = dechex($b<0?0:($b>255?255:$b));
		
	$html = (strlen($r2) < 2?'0':'').$r2;
	$html .= (strlen($g2) < 2?'0':'').$g2;
	$html .= (strlen($b2) < 2?'0':'').$b2;
gth759k
Forum Commoner
Posts: 76
Joined: Mon Jun 15, 2009 3:04 am

Re: imagecolorallocate accuracy problem

Post by gth759k »

Wow, it actually compressed the image really well!

Original: 76,888 bytes
Image

Generated: 10,188 bytes
Image
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: imagecolorallocate accuracy problem

Post by Benjamin »

I am completely bewildered why you would want to not only store the image in the database, rather than just a path, but also, why in the world you want want to store a RGB value for each pixel?!?!

You can use a blob field for binary data, just so you know..
gth759k
Forum Commoner
Posts: 76
Joined: Mon Jun 15, 2009 3:04 am

Re: imagecolorallocate accuracy problem

Post by gth759k »

Benjamin wrote:I am completely bewildered why you would want to not only store the image in the database, rather than just a path, but also, why in the world you want want to store a RGB value for each pixel?!?!

You can use a blob field for binary data, just so you know..
I know its weird, but the application of the storage and retrieval process is for a collaborative ajax image editing/paint tool. Where when one user changes the color of some pixels, the change is sent to the database and via ajax all instances of the image corresponding to a painting session are updated. I tried several other storage schemes, but the application must know the index and color information for each pixel to update the image's "state" like a multi-player game. Its just an experiment anyways.
Post Reply