Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (degenerate_762, AbrahamR, AndrewAMD, ozgur), 667 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Pixel color percentages #226743
09/11/08 01:47
09/11/08 01:47
Joined: Sep 2006
Posts: 74
Northwestern Oregon, United St...
openfire2691 Offline OP
Junior Member
openfire2691  Offline OP
Junior Member

Joined: Sep 2006
Posts: 74
Northwestern Oregon, United St...
I've got a game I'm making which has a kind of unique gameplay mechanic. At the end of each level, the engine needs to be able to calculate the percentage of the screen that is each individual color. I have a script that (I think) works just fine, but it takes way, way too long. I'm talking over an hour or so. I guess what I'm asking is, is there a way to calculate the pixel color percentages without doing what I did? Here's my code:


Code:
function calculate_colors()
{
	var temp_pixel;
	var format;
	var h = 0;
	var w = 0;
	wait(1);
	bmap_for_screen(tempy, 1, 0);
	wait(1);
	format = bmap_lock(tempy, 0);
	while(h < bmap_height(tempy))
	{
		while(w < bmap_width(tempy))
		{
			temp_pixel = pixel_to_vec(temp, NULL, format, pixel_for_bmap(tempy, w, h));
			if(temp_pixel.red == 255)
			{
				if(temp_pixel.green == 255)
				{
					if(temp_pixel.blue == 255)
					{
						colorpercents[0] += 1; //white
					}
					else
					{
						colorpercents[3] += 1; //yellow
					}
				}
				else
				{
					if(temp_pixel.blue == 255)
					{
						colorpercents[6] += 1; //purple
					}
					else
					{
						if(temp_pixel.green == 75)
						{
							colorpercents[2] += 1; //orange
						}
						else
						{
							colorpercents[1] += 1; //red
						}
					}
				}
			}
			else
			{
				if(temp_pixel.green == 255)
				{
					colorpercents[4] += 1; //green
				}
				else
				{
					if(temp_pixel.blue == 255)
					{
						colorpercents[5] += 1; //blue
					}
				}
			}
			w += 1;
			wait(1);
		}
		w = 0;
		h += 1;
		wait(1);
	}
}



When your script isn't working right, usually it's because it's doing exactly what you told it to. -An Unknown Programmer
Re: Pixel color percentages [Re: openfire2691] #226840
09/11/08 17:18
09/11/08 17:18
Joined: Apr 2008
Posts: 17
S
SlackNHash Offline
Newbie
SlackNHash  Offline
Newbie
S

Joined: Apr 2008
Posts: 17
How are the pixels getting set in the first place? The best way is to keep track then, if possible.

Code:
drawShape (12);
for ... {
  colorCount[red] += colorInShape[12].red;
  colorCount[green] += colorInShape[12].green;
...
}


If you can calculate anything ahead of time, like the color content of each graphic, that's much better than doing it runtime.

"if" statements are slow, because the computer tries to guess what's next and work ahead, and when it's wrong there's a lag, about the worst lag you get in programming.

Code:
colorPercent[(temp_pixel.blue == 255) * 3] +=1;


Looks more messy than if/else, but should run faster because there's no jump just a compare.

The first example would be best if possible, with just basic additions which are almost free. Comparisons (blue == 255) are slower but ifs are *much* slower, unless repetitive which these probably aren't.

Code:
colorPercent[6] += (green == 75);
colorPercent[(red == 255) + (green == 255) * 2 + (blue == 255) * 4] += 1;


That puts the array in a different order, and has an extra entry for any other colors - if they're never used you could subtract 1 from the array index.

That last way the order is:
Other, Red, Green, Yellow, Blue, Purple, Orange, White
Orange goes over unused cyan.

Also if any of these are still too slow but only by 10x or so, is per-pixel accuracy needed? A power of 2 size change, like 1/4 by 1/4 would speed it up 16x and the downsampling isn't too slow with a basic antialiasing filter to keep it precise. A 5% margin of error for a 16x speedup is often worth it.

Hope any or all of the above help,

Dave Heinemann

Re: Pixel color percentages [Re: SlackNHash] #226905
09/11/08 22:04
09/11/08 22:04
Joined: Sep 2006
Posts: 74
Northwestern Oregon, United St...
openfire2691 Offline OP
Junior Member
openfire2691  Offline OP
Junior Member

Joined: Sep 2006
Posts: 74
Northwestern Oregon, United St...
Thanks Dave, that's perfect! Unfortunately I can't simply add up the content of each bmap, since there's going to be a lot of overlapping. I'm going for a color count for a screenshot, not just a panel. Your other code works brilliantly, though.


When your script isn't working right, usually it's because it's doing exactly what you told it to. -An Unknown Programmer
Re: Pixel color percentages [Re: openfire2691] #226907
09/11/08 22:11
09/11/08 22:11
Joined: Oct 2005
Posts: 4,771
Bay City, MI
lostclimate Offline
Expert
lostclimate  Offline
Expert

Joined: Oct 2005
Posts: 4,771
Bay City, MI
I'd say instead of count every pixel, do a sample (random or ordered) or just use a lower res version of the screen.


Moderated by  HeelX, rvL_eXile 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1