Jump to content

  • Log In with Google      Sign In   
  • Create Account


Getting Average Color of DXTCompressed Texture


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 hdlopesrocha   Members   -  Reputation: 145

Like
0Likes
Like

Posted 23 June 2012 - 09:58 PM

Hello again.

I am trying to Implement a Minimap for my game and for each Texture I want the avg color to paint the minimap.

so far I've this:
		public Color GetAvgColor(Texture2D tex)
		{
			Color[] data = new Color[tex.Width*tex.Height];
			Color sum = Color.Black;
			int amount = 0;
			tex.GetData<Color>(data);
			foreach (Color c in data)
			{
				sum.R += c.R;
				sum.G += c.G;
				sum.B += c.B;
				++amount;
			}
			return new Color(sum.R / amount, sum.G / amount, sum.B / amount); ;
		}

I didn't tested with Colored Textures because my terrain textures are DXTCompressed and MipMapped.

The problem is with GetData:
{"The type you are using for T in this method is an invalid size for this resource."}

Is that possible to Get the Average Color of a DXTCompressed Texture?

Thanks ;-)

Sponsor:

#2 Tarika   Members   -  Reputation: 481

Like
1Likes
Like

Posted 24 June 2012 - 02:30 AM

Another excuse to use System.Linq Posted Image

public Color GetAvgColor(Texture2D tex, Rectangle rect, int mipLevel = 0)
{
	Color[] data = new Color[rect.Width * rect.Height];
	if (data.Length == 0) return Color.White;
	tex.GetData<Color>(mipLevel, rect, data, 0, data.Length);
	int r = data.Sum(t => t.R) / data.Length;
	int g = data.Sum(t => t.G) / data.Length;
	int b = data.Sum(t => t.B) / data.Length;
	int a = data.Sum(t => t.A) / data.Length;
	return new Color(r, g, b, a);
}

edit with a bit of explanation: the 3rd overload of the GetData allows you to specify a rectangle to retrieve colours from, so you simply have to make sure your data array is the same size as what your after returning.

Edited by AmzBee, 24 June 2012 - 02:32 AM.

We are now on Tumblr, so please come and check out our site! 

 

http://xpod-games.com


#3 mhagain   Crossbones+   -  Reputation: 7564

Like
1Likes
Like

Posted 24 June 2012 - 05:51 AM

Why not just sample the textures from the smallest mip level? That will give you an average colour and won't require anything CPU-side.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#4 hdlopesrocha   Members   -  Reputation: 145

Like
0Likes
Like

Posted 26 June 2012 - 02:50 AM

Another excuse to use System.Linq Posted Image

public Color GetAvgColor(Texture2D tex, Rectangle rect, int mipLevel = 0)
{
	Color[] data = new Color[rect.Width * rect.Height];
	if (data.Length == 0) return Color.White;
	tex.GetData<Color>(mipLevel, rect, data, 0, data.Length);
	int r = data.Sum(t => t.R) / data.Length;
	int g = data.Sum(t => t.G) / data.Length;
	int b = data.Sum(t => t.B) / data.Length;
	int a = data.Sum(t => t.A) / data.Length;
	return new Color(r, g, b, a);
}

edit with a bit of explanation: the 3rd overload of the GetData allows you to specify a rectangle to retrieve colours from, so you simply have to make sure your data array is the same size as what your after returning.


It's not working... The problem is the DXTcompression.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS