Skip to main content
gamedev.net
🔒 Locked

Help wanted in Bitmap loading

Started by arunvb 2 replies 300+ views
Original Post
arunvb
arunvb
I have a function to load a bitmap and blt it to the backbuffer. My resolution in 800 * 600 and color mode is 8 bit palettized. If the bitmap has a width of 600 ( equal to the width of the screen ) then I do not have any problems. But if the bitmap is smaller (eg 100 * 100) then it doesnt show up correctly. The image appears highly distorted. I have also taken care of the Pitch in the bitmap loader function. What could be the problem ? Thanks in advance for your help. Arun
arunvb
arunvb
The code to extract the bitmap from the file is as below:

bool BitmapSurface::LoadBmpData()
{
HANDLE fileHandle;
DWORD bytesRead;
DDSURFACEDESC2 ddsd;
LPDIRECTDRAWPALETTE lpddpal = NULL;
UCHAR* bmpData;

// Open the file
fileHandle = CreateFile (bmpfile, GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL / FILE_FLAG_SEQUENTIAL_SCAN,
NULL);

if (fileHandle == NULL)
return 0;

// Set the cursor to start of the data
SetFilePointer (fileHandle,-(int)(byteSize), NULL, FILE_END);

// Read the image the data into the buffer
if (!surface7)
return FALSE;

// Lock the surface to get the image data
if (!(Lock(&ddsd, NULL)))
{
TraceToFile ("Lock of the bmpsurface failed");
return FALSE;
}

ZeroMemory (ddsd.lpSurface, byteSize);

// Allocate memory for the memory pointer
bmpData = (UCHAR*) malloc(byteSize);

// Read the image data from the file
// if (!(ReadFile(fileHandle, (void*) ddsd.lpSurface, byteSize, &bytesRead, NULL)))
if (!(ReadFile(fileHandle, (void*) bmpData, byteSize, &bytesRead, NULL)))
{
TraceToFile ("ReadFile failed", GetLastError ());
return FALSE;

}

// Load the bmpData into the surface
// memory is non-linear, copy line by line
UCHAR* surfacePtr = (UCHAR*) ddsd.lpSurface;
for (int y=0; y < height; y++)
{
// copy line
memcpy((void *)surfacePtr, (void *)bmpData, width);

// advance pointers to next line
surfacePtr += ddsd.lPitch;
bmpData += width;
} // end for

// Unlock the surface
UnLock (NULL);

// Close the file
CloseHandle (fileHandle);

return 1;

} /* BitmapSurface::LoadBmpData */
cyberben
cyberben
If you want I have written my own blit routine which will blit from any sized locked surface to any other sized locked surface using the following passed in variables.

Source,Dest,SourcePitch,DestPitch,SourceRect,DestX,DestY,bpp

Where Source is the returned address after locking the source surface and dest is the address of the locked destination surface, SourcePitch is the Pitch of the source surface and DestPitch is the Pitch of the destination surface, Source Rect is a RECT filled in with the source rectangle you want to copy. DestX & Y are the destination co-ordinats (Warning this does no out of bounds testing, so you have to make sure the blit is within the surface) and bpp is bits per pixel.
Now there''s two things about this, it''s only been tested at 16bpp, and it copies by DWORD for speed so 16bpp bitmaps had to be multiples of 2 pixels wide, for an 8 bit picture I''d guess it has to be multiples of 4 pixels. Last but not least I wrote it in assembler as my game will have to do several hundred blits per frame. So the idea is that you locked your TileSet or source surface, and the dest (usually backbuffer) and then blit everything before unlocking. This is even faster than using BltFast w/ DirectDraw.
So you may want a small bit of understanding of assembler.
So if you want me to send it to you email me. (Click my profile icon to get it, I don''t like including for i-bots)
See ya,
Ben
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
Geradian
Geradian
After each line the bitmap has a couple of extra bytes. You can get how many these bytes are by taking:

WidthInBytes % 4;

You should change this line:
bmpData += width;

to
bmpData += (width + (width % 4));

Here I suppose that the resolution on screen is the smae as in the bitmap.

Topic Locked

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

Sign in to reply to this topic.