//////////////////////////////////////////////////////////////////////////////////////////
// L O A D
//
// The name says it all. It opens the file and reads the bitmap from it.
//////////////////////////////////////////////////////////////////////////////////////////
int Bitmap::Load(char File[1024], DirectDraw *pDDraw)
{
DDSURFACEDESC2 ddsd;
BITMAP_FILEHEADER FileHeader;
BITMAP_INFOHEADER InfoHeader;
WORD *BitmapData;
WORD *BitmapFlipped;
BYTE Red, Green, Blue;
FILE *BitmapFile;
// ifstream BitmapFile(File, ios::binary);
CDebug Log("C:\\bitmap.log");
char Buffer[1024];
Log.Write("****** LOG FILE ******\n");
BitmapFile = fopen("C:\\test.bmp", "rb");
// Read the file header from the file
fread(&FileHeader, sizeof(BITMAP_FILEHEADER), 1, BitmapFile);
// BitmapFile.read((char *)&FileHeader, sizeof(BITMAP_FILEHEADER));
// Read the InfoHeader from the file
fread(&InfoHeader, sizeof(BITMAP_INFOHEADER), 1, BitmapFile);
// BitmapFile.read((char *)&InfoHeader, sizeof(BITMAP_INFOHEADER));
sprintf(Buffer, "Infoheader.biWidth = %d\nInfoHeader.biHeight = %d", InfoHeader.biWidth, InfoHeader.biHeight);
Log.Write(Buffer);
// Create a new surface for the bitmap to be copied to
BitmapSurface = pDDraw->CreateSurface(InfoHeader.biWidth, InfoHeader.biHeight, 0);
if (BitmapSurface == NULL)
return (FAILED_INIT_OFFSCREEN_PLAIN);
// Lock the offscreen surface for access
DDRAW_INIT_STRUCT(ddsd);
if (FAILED(BitmapSurface->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL)))
{
BitmapSurface->Release();
return (FAILED_SURFACE_LOCK);
}
// Reserve data for the bitmap. BitmapData is for the bitmap read from
// the file, BitmapFlipped is the mem for the correct bitmap
BitmapData = new WORD[InfoHeader.biWidth * InfoHeader.biHeight];
BitmapFlipped = (WORD *) ddsd.lpSurface;
// Read the bitmap bit for bit from the file, convert it to 16bit and save it
for (int Y = 0; Y < InfoHeader.biHeight; ++Y)
{
for (int X = 0; X < InfoHeader.biWidth; ++X)
{
// Read the 3 pixels from file
fread(&Blue, sizeof(BYTE), 1, BitmapFile);
fread(&Green, sizeof(BYTE), 1, BitmapFile);
fread(&Red, sizeof(BYTE), 1, BitmapFile);
// BitmapFile.read((char *)&Blue , sizeof(BYTE));
// BitmapFile.read((char *)&Green, sizeof(BYTE));
// BitmapFile.read((char *)&Red , sizeof(BYTE));
// Convert them to 16-bit and them inside the mem
BitmapData[Y * InfoHeader.biWidth + X] = RGB24TO16(Red, Green, Blue);
}
}
int HeightIndex = 0;
// Flip the bitmap.
for (Y = InfoHeader.biHeight - 1; Y >= 0; --Y)
{
for (int X = 0; X < InfoHeader.biWidth; ++X)
BitmapFlipped[HeightIndex * (ddsd.lPitch / 2) + X] = BitmapData[Y * InfoHeader.biWidth + X];
++HeightIndex;
}
// Close the file
//BitmapFile.close();
fclose(BitmapFile);
// Clean up the old bitmap data
delete BitmapData;
// Unlock the surface
BitmapSurface->Unlock(NULL);
return (SUCCESS);
}
****** LOG FILE ******
Infoheader.biWidth = 31457280
InfoHeader.biHeight = 65536
Why isn't the function reading correctly?
Sand Hawk
----------------
-Earth is 98% full. Please delete anybody you can.My Site [edited by - sand_hawk on August 15, 2002 12:13:17 PM]