I have a large image, starting at a known address. The image is a grid of
icons, 10 icons wide by 10 high, all 72 pixels x 72 pixels square.
I need to copy each icon to a different area of memory. I just don’t know
how to do it.
I cannot use SDL for this (if I could, I would).
A bit strange to say that on the SDL list, but oh well…
Andreas Schiffler explained the memory layout pretty well. So what you
need to know are the following:
- memory address of the upper left corner of the source image
- memory address of the destination image
- bytes per pixel
- the pitch of the source image (size of “(width * bytes per pixel) + filler”)
- the pitch of the destination image
The amount that you want to copy is known, 72x72 pixels, so it’s
pretty trivial to calculate where to start and stop within the image.
This is untested, half-way between pseudo-code and C (and C++ will
find issues with it, I’m sure):
/* The icon_x/icon_y are zero-based, so the third icon of the first
line would be 2, 0. bpp is in BYTES, not in BITS, unlike many cases
where you might see “bpp” (but it’s easy to convert!). /
void CopyIcon(void src_img, void* dst_img, size_t bpp, size_t
src_pitch, size_t dst_pitch, unsigned int icon_x, unsigned int icon_y)
{
/* skip the right number of rows in the source image */
src_img += icon_y * 72 * src_pitch;
/* shift ourselves so that we point at the first pixel of the icon */
src_img += icon_y * 72 * bpp;
int i = 0;
for (i = 0; i < 72; ++i) {
/* copy a row of the icon */
memcpy(dst_img, src_img, 72 * bpp);
src_img += src_pitch;
dst_img += dst_pitch;
}
}
If there is no filling/padding in the source image, the src_pitch
would be a constant “bpp * 72 * 10”, but if this is video memory, you
have to find out (it can depend on the video mode). This function
could be written tighter, but I optimized for legibility and
understanbility. :-)On Wed, Jan 14, 2009 at 10:44 AM, Edward Byard <e_byard at yahoo.co.uk> wrote:
–
http://pphaneuf.livejournal.com/