Skip to main content
gamedev.net
🔒 Locked

bitmap padded data help????

Started by baddrizz Dec 5, 2001 at 5:14 PM 2 replies 200+ views
Original Post
baddrizz
baddrizz
this is a dumb question but what do you do with the padded bitmap data cause i can load bitmap''s that are divisible by 4 but others that aren''t the color''s all messed up i''m guessing cause of the padding
Zeke
Zeke
I do something like this.
  
for(int x=0;x<Height;x++)
{
for(int y=0;y<Width;y++)
{
// copy i row of bitmap into buffer

ZeroMemory(Dest,LengthofRow+padding);
memcpy(Dest,Source,LengthofRow)
// Length of row is the width of the bitmap

Dest+=LengthofRow+padding;
// Source+=LengthofRow

}
}


Its something along those lines (cant remember it exactly). You basically have to read in a line of data into a buffer that has room for the padding (with the padding bits set to 0).

Im not really explaining this very well am i?

Tell you what post your bitmap loading code, or send it to Ezekiel0000000@aol.com and ill have a look at it and figure it out.
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
baddrizz
baddrizz
here it is it''s not the best but it works
here''s the stuct i use to hold varibles
struct
{
LPDIRECTDRAW7 lpdd;
LPDIRECTDRAWSURFACE7 primary;
LPDIRECTDRAWSURFACE7 back;
LPDIRECTDRAWCLIPPER clip;

HWND hwnd;
}gdx;

and i use this to convert from 24bit to 16bit color
#define _24to16bit( r, g, b) (((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3))

all the other code is pretty simple
  
bool DRAWING::LoadBitmap( LPDDRAWSURF7& surf, char *filename, bool _16or24bit)
{
FILE *file;
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
int width, height, bpp, size, x, y;
byte *picbuffer;
byte r, g, b;
char errorholder[20];

if(!(file = fopen( filename, "rb")))
{
wsprintf( errorholder, "Could Load %s", filename);
MessageBox( gdx.hwnd, errorholder, "ERROR", MB_OK | MB_ICONERROR);
return false;
}

fread( &bmfh, sizeof( BITMAPFILEHEADER), 1, file);
fread( &bmih, sizeof( BITMAPINFOHEADER), 1, file);
fseek( file, bmfh.bfOffBits, SEEK_SET);

if( bmfh.bfType != 0x4D42)
{
MessageBox( gdx.hwnd, "Not A Bitmap", "ERROR", MB_OK | MB_ICONERROR);
return false;
}

width = bmih.biWidth; height = bmih.biHeight; bpp = bmih.biBitCount;

// unsigned int bytesUsed = bmp_width * bmp_channels;

// unsigned int padding = bmp_stride - bytesUsed;

// bmp_stride = bmp_width * bmp_channels;


wsprintf( errorholder, "Could Load %d", bmih.biWidth);
MessageBox( gdx.hwnd, errorholder, "ERROR", MB_OK | MB_ICONERROR);

size = width;
while((size % 4) != 0)
size++;
size = (size * (bpp>>3)) * height;

wsprintf( errorholder, "Could Load %d", size);
MessageBox( gdx.hwnd, errorholder, "ERROR", MB_OK | MB_ICONERROR);
if(!(picbuffer = (byte*)malloc(size)))
{
MessageBox( gdx.hwnd, "Could Allocate Memory", "ERROR", MB_OK | MB_ICONERROR);
return false;
}

fread( picbuffer, 1, size, file);

ClearDDSD();

surf->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL);

USHORT* buffer = (USHORT*)ddsd.lpSurface;
int pitch = ddsd.lPitch >> 1;

for(y = 0; y < height; y++)
{
for(x = 0; x < width; x++)
{
int line = ((y*width)+x)*3;
r = picbuffer[line+2];
g = picbuffer[line+1];
b = picbuffer[line];

buffer[y*pitch+x] = _24to16bit( r, g, b);
}
}

surf->Unlock( NULL);

return true;
}

a person
a person
if you read the docs about bmps, (msdn, wotsit.org, anywhere they explain the bmp file format) you would know that each scanline of a bmp must be padded to a DWORD (32 bits). a simple macro that gives you the width DWORD aligned

#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)

usage WIDTHBYTES(widthOfBitmap*colorDepthInBits)

24bit colordepth width of 320.

WIDTHBYTES(320*24);

you then use that value as your stride instead of width*numberOfBytesPerPixel like you normally do.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.