开发者

Split an image into 64x64 chunks

I'm j开发者_如何学编程ust wondering how I would go about splitting a pixel array (R,G,B,A,R,G,B,A,R,G,B,A,etc.) into 64x64 chunks. I've tried several methods myself but they all seem really over-complex and a bit too messy.

I have the following variables (obviously filled with information):

int nWidth, nHeight;
unsigned char *pImage;

And basically for each chunk I want to call:

void ProcessChunk(int x, int y, int width, int height)

You may be wondering why there is a width and height argument for the processing function, but if the image is not exactly divisible by 64, then there will be smaller chunks at the edge of the image (right-hand side and the bottom). See this image for a better understanding what I mean (red chunks are the chunks that will have <64 width or height arguments).

Split an image into 64x64 chunks

Thanks in advance.


Just define a MIN() macro to determine the minimum of two expressions, and then it's easy:

void ProcessChunk(unsigned char *pImage, int x, int y, int width, int height);

#define MIN(a, b) ((a) < (b) ? (a) : (b))    
#define CHUNKSIZE 64

void ProcessImage(unsigned char *pImage, int nWidth, int nHeight)
{
    int x, y;

    for (x = 0; x < nWidth; x += CHUNKSIZE)
        for (y = 0; y < nHeight; y += CHUNKSIZE)
            ProcessChunk(pImage, x, y, MIN(nWidth - x, CHUNKSIZE), MIN(nHeight - y, CHUNKSIZE));
}


I will assume that the chunks are unsigned char *chunk[64*64*4], in a similar way to the image itself.

int chunkOffset = 0;
int bufferOffset = y*nWidth*4 + x*4;
memset(chunk, 0, 64*64*4*sizeof(unsigned char));
for (int line = 0; line < height; line++)
{
    memcpy(chunk+chunkOffset, buffer+bufferOffset, width*4*sizeof(unsigned char));
    chunkOffset += 64*4;
    bufferOffset += nWidth*4;
}

In a real code I would replace the "4"s with sizeof(PIXEL) and the "64"s with CHUNK_WIDTH & CHUNK_HEIGHT, but you get the idea. I would also check width & height for accuracy, and basically you don't really need them (you can easily calculate them inside the function).

Also note that while the "sizeof(unsigned char)" isn't really needed, I like putting it there for clarification. It doesn't effect the runtime since it's evaluated in compilation time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜