I currently have the following code for a sprite struct (c-style for use in DLL):
typedef struct LABglSpriteTAG
{
unsigned int glTexture;
int w;
int h;
LABglColour *data;
bool compiled;
}*LABglSpriteHANDLE;
And the following function:
void LABglSpriteConvolute(LABglSpriteHANDLE spr, int w, int h, int *kernel)
{
size_t dim=spr->w*spr->h;
LABglColour *out=new LABglColour[dim];
for (size_t i=0; i<dim; ++i)
out[i]=0;
int centerx=w/2;
int centery=h/2;
for (int x=0; x<spr->w; ++x)
{
for (int y=0; y<spr->h; ++i)
{
for (int kx=0; kx<w; ++kx)
{
int xx=w-1-kx;
for (int ky=0; ky<h; ++ky)
{
int yy=h-1-ky;
int rx=x+(kx-centerx);
int ry=y+(ky-centery);
if (rx>=0&&rx<spr->w&&ry>=0&&ry<spr->h)
out[y*spr->w+x]+=spr->data[ry*spr->w+rx]*(1.0/kernel[ky*w+kx]);
}
}
}
}
delete[]spr->data;
spr->data=out;
#warning SLOW 2012-03-27-12.34: Unknown solution
}
The problem is that this will run in O(nk) time (n being array size, k being kernel size) and takes up a lot of space. Is there any way to optimize this function?