I'm tyring to do mip maps generation, and would like to average a 2x2 block. The current code crashes as out of bounds exception.
for (int mip = 0; mip < levelCount; mip++)
{
uint nWidth = Math.Max((width) / ((uint)1 << mip), 1);
uint nHeight = Math.Max((height) / ((uint)1 << mip), 1);
byte[] imageData = new byte[nWidth * nHeight * formatInfo.BPP];
for (int y = 0; y < nWidth; y ++)
{
for (int x = 0; x < nHeight; x ++)
{
int dst_index_x = x;
int dst_index_y = y;
int src_index_x = dst_index_x;
int src_index_y = dst_index_y ;
float filtered_1 = (data[src_index_x + src_index_y]);
float filtered_2 = (data[src_index_x + 1] + data[src_index_x + src_index_y]);
float filtered_3 = (data[src_index_x] + data[src_index_x + 1 + src_index_y ]);
float filtered_4 = (data[src_index_x + 1] + data[(src_index_x + 1) + (src_index_y + 1)]);
float filtered = (filtered_1 + filtered_2 + filtered_3 + filtered_4)*0.25f;
imageData[x + y* nWidth] = (byte)filtered;
}
}