Getting Average Color of DXTCompressed Texture

Started by
2 comments, last by HenriqueRocha 11 years, 10 months ago
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 ;-)
Advertisement
Another excuse to use System.Linq biggrin.png


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.

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

http://xpod-games.com

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


Another excuse to use System.Linq biggrin.png


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.

This topic is closed to new replies.

Advertisement