Advertisement

Help with Bitmaps, file reading messes up...

Started by August 15, 2002 08:38 AM
-1 comments, last by Sand_Hawk 22 years, 3 months ago
[EDIT] I uploaded all my code to http://home.wanadoo.nl/vdwillik/help/. It messes up the structs when I read it, dunno why. [/EDIT] Hi all, I'm having trouble with my bitmap loading class. Current problem is that it only supports 24-bit bitmaps wich are converted to 16-bit. I also want to be able to convert 24-bits to 32-bit and load 16/32 bit bitmaps. But that's not the problem right now, problem is that my bitmap has the wrong height/width. When I try to create offscreen surface it crashes on that it can't create the offscreen plain. I first thought it was caused by my C++ file I/O but when using C functions it still doesn't work. This is my code:
    
//////////////////////////////////////////////////////////////////////////////////////////

// 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);
}
    
When I read a 640*480*24bpp bitmap the log file shows me this:

****** 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]
----------------(Inspired by Pouya)

This topic is closed to new replies.

Advertisement