Sign in to follow this  

Getting Average Color of DXTCompressed Texture

This topic is 2031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
[CODE]
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); ;
}
[/CODE]

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

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

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

Thanks ;-)

Share this post


Link to post
Share on other sites
Another excuse to use System.Linq [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

[CODE]
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);
}
[/CODE]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='AmzBee' timestamp='1340526610' post='4952256']
Another excuse to use System.Linq [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

[CODE]
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);
}
[/CODE]

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.
[/quote]

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

Share this post


Link to post
Share on other sites
Sign in to follow this