Archived

This topic is now archived and is closed to further replies.

Colin Jeanne

Padding in Bitmaps?

Recommended Posts

Colin Jeanne    1114
I'm trying to work with a 24bpp bitmap that I created using CreateDIBSection. If I understand the MSDN correctly the width of each scanline is aligned to DWORD (NT) or WORD (Win9x) boundries but I must be missing something because when I account for the extra padding the image is still messed up. This is how I am creating the bitmap:
BITMAPINFOHEADER bmi;
bmi.biSize = sizeof(BITMAPINFOHEADER);
bmi.biWidth = crp->size;
bmi.biHeight = -crp->size;
bmi.biPlanes = 1;
bmi.biBitCount = 24;
bmi.biCompression = BI_RGB;
bmi.biClrImportant = 0;
bmi.biSizeImage = 0;

HDC hdc = GetDC(hwnd);
crp->hbm = CreateDIBSection(hdc, reinterpret_cast<BITMAPINFO *>(&bmi), DIB_RGB_COLORS, reinterpret_cast<LPVOID *>(&crp->bits), 0, 0);
This is how I am trying to set each pixel:
//size is the width of the image in pixels

void SetPixel(char *bits, unsigned long x, unsigned long y, int size, COLORREF c) {
	unsigned long padding = sizeof(DWORD) - (3 * size % sizeof(DWORD));

	if (padding == sizeof(DWORD))
		padding = 0;

	unsigned long linear_pixel = (x + (size + padding) * y) * 3;
	
	bits[linear_pixel] = GetBValue(c);
	bits[linear_pixel + 1] = GetGValue(c);
	bits[linear_pixel + 2] = GetRValue(c);
}
If the width is DWORD aligned then the image paints perfectly. Is there something I'm not taking into account?
Thanks Salsa!Colin Jeanne | Invader's Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud [edited by - Invader X on March 21, 2004 8:25:36 PM]

Share this post


Link to post
Share on other sites
Melekor    379
I think it's your padding formula that's messed up.

try this:

int width = size*3;
pad = (4-(width&3)) & (~4);
linear_pixel = (width+pad)*y + x*3;

EDIT: *3, of course.

[edited by - Melekor on March 21, 2004 9:27:18 PM]

Share this post


Link to post
Share on other sites