Thread: bmp loader prob

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    55
    Rep Power
    12

    bmp loader prob


    I'm trying to load a bitmap so I can paste it to a square, but I don't think that the fopen(file, "rb"); function is getting the file name. Do I have to write something like -

    file=" C:/Documents and Settings/User/My Documents/My Videos/aaliyah.bmp"

    before fopen?

    #define BITMAP_ID 0x4D42
    BITMAPINFOHEADER bitmapInfoHeader;
    unsigned char *bitmapData;
    unsigned int texture;
    bool InitializeGL();

    unsigned char *LoadBitmapFile(char *file, BITMAPINFOHEADER *bitmapInfoHeader)
    {

    char image[] = { "C:/Documents and Settings/User/My Documents/My Videos/aaliyah.bmp" };

    // Bitmaps are made up of 3 parts, the file header, info header, and
    // the image. Once we load in the file and info headers we can then
    // load the image into our unsigned char variable called bitmapData.

    FILE *pFile; // Need this to open a file.
    BITMAPFILEHEADER header; // This will hold the bitmap header information.

    unsigned char *textureData; // This will hold the bitmap image itself.

    // This will be used to swap the image colors from BGR to RGB.
    unsigned char textureColors;
    //bitmapData = LoadBitmapFile(image, &bitmapInfoHeader);

    pFile = fopen(file, "rb"); // Open the file.

    if(pFile == NULL) return NULL;// Check and make sure there are no errors.

    // Read in the bitmap header info into the BITMAPFILEHEADER variable.
    fread(&header, sizeof(BITMAPFILEHEADER), 1, pFile);

    // Make sure this is a real bitmap by checking the ID.
    if(header.bfType != BITMAP_ID)
    {
    fclose(pFile);
    return NULL;
    }

    // Read in the second header info into the BITMAPINFOHEADER variable.
    fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, pFile);

    // Place the pointer in front of where the image data starts.
    fseek(pFile, header.bfOffBits, SEEK_SET);

    // Dynamically create enough memory for the image.
    textureData = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

    // Error checking. Make sure the memory was allocated.
    if(!textureData)
    {
    free(textureData);
    fclose(pFile);
    return NULL;
    }

    // Read in the image.
    fread(textureData, 1, bitmapInfoHeader->biSizeImage, pFile);

    // Error checking. Make sure an image was loaded.
    if(textureData == NULL)
    {
    fclose(pFile);
    return NULL;
    }

    // Bitmaps are saved in BGR format so we will make the image RGB by...
    for(int index = 0; index < bitmapInfoHeader->biSizeImage; index+=3)
    {
    textureColors = textureData[index];
    textureData[index] = textureData[index + 2];
    textureData[index + 2] = textureColors;
    }

    fclose(pFile); // We are done with the file so close it.
    return textureData; // Send the image to the function that called this.
    }
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    8
    Rep Power
    0
    Hey
    I had the problem a few days ago, just check load a bitmap post.
    insted of using fopen, try the loadimage() mothod, it returns a handle to your bitmap. then you can draw the bitmap by bltbit().
    but as i said check the posts
    if you still have problems just post again and i will try to find some code for you
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    55
    Rep Power
    12
    When u say check ur posts,do u mean the debug column? If so, how do u get that up and running, cos every time I open it, it's just blank. Thanks for your help, ben
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    55
    Rep Power
    12
    I think the problem is with loading the file name into the image variable. Can anyone see why this won't load? (ie what I have to change about char image[] = { "C:/Documents and Settings/User/My Documents/My Pictures/aaliya.bmp" };
    -

    #define BITMAP_ID 0x4D42
    BITMAPINFOHEADER bitmapInfoHeader;
    unsigned char *bitmapData;
    unsigned int texture;
    unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader);

    bool InitializeGL()
    {
    // Add to the status report that we are starting initialization. If
    // the program crashed here we would know this was the function that
    // caused a problem.
    cout << "Starting Initialization...\n\n";

    int PopUp;

    // I decided to place the texture image name in a variable. This
    // way if there was a problem with it (this is only useful if
    // there was more than one) the name of the texture will be printed
    // to the console screen along with a error message. I printed to
    // the console screen instead of using MessageBoxes because I think
    // its cooler.
    char image[] = { "C:/Documents and Settings/User/My Documents/My Pictures/aaliya.bmp" };
    // Enable texture mapping by sending GL_TEXTURE_2D to glEnable().
    glEnable(GL_TEXTURE_2D);

    // Now we load the image into memory.
    // The line below shows why I decided to place the texture image name in a variable.
    cout << "Loading image file " << image << " ...\n";

    // Load the bitmap image into our global variable bitmapData.
    bitmapData = LoadBitmapFile(image, &bitmapInfoHeader);

    // If there was any errors then print this error message to the console screen.
    if(!bitmapData)
    {
    cout << "Loading image " << image << " failed (check if file exist)...\n\n";
    free(bitmapData);
    return false;
    }

    // Next you must generate the texture in OpenGL.
    glGenTextures(1, &texture);

    // Lastly you bind the texture to the variable texture.
    glBindTexture(GL_TEXTURE_2D, texture);
  8. #5
  9. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    >>C:/Documents and Settings/User/My Documents/My Pictures/aaliya.bmp

    using forward slashes where you need backslashes

    try

    C:\\Documents and Settings\\User\\My Documents\\My Pictures\\aaliya.bmp

    need to use two backslashes to get one backslash.
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa

IMN logo majestic logo threadwatch logo seochat tools logo