Sign in to follow this  
HenriqueRocha

Getting Average Color of DXTCompressed Texture

Recommended Posts

HenriqueRocha    145
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
AmzBee    485
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
HenriqueRocha    145
[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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this