Advertisement

Cannt Read BMP (my function)

Started by May 18, 2002 06:16 PM
5 comments, last by Nukem 22 years, 9 months ago
I am reading threw the NeHe lessons and converting them to GLUT. Since I dont have glaux.h and there isn't much in it, so I made my own BMP reading function. It has been reading BMP files fine till I came to the Lesson 17 Font.bmp. It won't read it for some reason and I cannt figure out why. I resaved it using GIMP(a Linux art program) and it still won't work. Can someone please help me out? thx nuke
        
struct PICTURE
{
  unsigned long x;
  unsigned long y;
  char* data;
};
  
      
  //0 failed

  //1 everything is kool

  int LoadBMP(char *filename, PICTURE* pic)
  {
    FILE* file;
    unsigned long size;
    unsigned long a;
    unsigned short int planes;
    unsigned short int bpp;
    char temp;

    file = fopen(filename, "rb");

    if(file == NULL)
    {
      fclose(file);
      return 0;
    }

    fseek(file, 18, SEEK_CUR);

    a = fread(&pic->x, 4, 1, file);

    if(a != 1)
    {
      fclose(file);
      return 0;
    }

    a = fread(&pic->y, 4, 1, file);

    if(a != 1)
    {
      fclose(file);
      return 0;
    }

    size = pic->x * pic->y * 3;

    if(fread(&planes, 2, 1, file) != 1)
    {
      fclose(file);
      return 0;
    }

    if(planes != 1)
    {
      fclose(file);
      return 0;
    }

    a = fread(&bpp, 2, 1, file);

    if(a != 1)
    {
      fclose(file);
      return 0;
    }

    if(bpp != 24)
    {
      fclose(file);
      return 0;
    }

    fseek(file, 24, SEEK_CUR);

    pic->data = (char *) malloc(size);

    if(pic->data == NULL)
    {
      fclose(file);
      return 0;
    }

    a = fread(pic->data, size, 1, file);

    if(a != 1)
    {
      fclose(file);
      return 0;
    }

    for(a = 0; a < size; a += 3)
    {
      temp = pic->data[a];
      pic->data[a] = pic->data[a + 2];
      pic->data[a + 2] = temp;
    }

     return 1;
  }
  
      
  int LoadFontTexture(char* FileName)
  {
    PICTURE* pic;

    pic = (PICTURE *)malloc(sizeof(PICTURE));

    if(pic == NULL)
    {
      return 0;
    }

    if(LoadBMP(FileName, pic) == 0)
    {
 //error happens here

      cout << "couldnt load bmp!\n";
      return 0;
    }

     glGenTextures(1, &FontTexture[0]);

    // Build All The Textures

    glBindTexture(GL_TEXTURE_2D, FontTexture[0]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, pic->x, pic->y, 0, GL_RGB, GL_UNSIGNED_BYTE, pic->data);

    delete pic;

    return 1;
  }
      
[edited by - Nukem on May 18, 2002 7:21:33 PM]
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
It''s your code. Try narrowing down the problem to a few lines.
---visit #directxdev on afternet <- not just for directx, despite the name
Advertisement
Its in the Function LoadBMP (shown above). It cannt find the bpp.


  a = fread(&bpp, 2, 1, file);if(a != 1){	fclose(file);	return 0;}if(bpp != 24){	fclose(file);	return 0;}  
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
I figured out what was wrong. My function only supports RBG (color) bmps while the Font.bmp was a B&W (black and white) bmp. Ill have to figure out how to support B&w bmps.

Thx anyway!
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind
if it helps any, b/w is just a byte for each pixel value. instead of a series of 3 bytes for each color channel.

a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
Ready4Dis had a nice 8-bit BMP loader in this thread:
http://www.gamedev.net/community/forums/topic.asp?topic_id=92325

G''luck.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

[twitter]warrenm[/twitter]

Advertisement
thx!
--------------------------Nukemmsn: nukem996@hotmail.comaim: nukem996open source open mind

This topic is closed to new replies.

Advertisement