Bitmap displays in wrong direction

Loading a bitmap manually displays it in the wrong direction

Page 1 of 1

1 Replies - 896 Views - Last Post: 30 January 2010 - 06:19 AM Rate Topic: -----

#1 Jim147  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-January 10

Bitmap displays in wrong direction

Post icon  Posted 29 January 2010 - 06:58 PM

I'm trying to load a bitmap manually, which works, but when I display it on my opengl win32 application, it looks as if the bitmap has been rotated 90 degrees clockwise and then flipped horizontally. I have never tried loading a bitmap manually, so I'm completely new to that. Here's the code for the class that loads the bitmap for use in opengl:

#include <iostream>

//Bitmap Class
class bmp
{
 public:
  //Functions
  BYTE &pixel(int,int,int);

  GLuint loadBitmap(const char*);

  void generateTexture(GLuint*);

  //Variables
  BYTE *data;
  int iHeight,iWidth;
};

BYTE &bmp::pixel(int a,int b,int c)
{
 return data[(b*iWidth+a)*3+c];
}

GLuint bmp::loadBitmap(const char *fileName)
{
 BYTE colorTable[1024],tempData;
 char cHeader[54];
 FILE *file = fopen(fileName,"rb");
 GLuint glTexture = 0;
 int a,b,c,d,iBits,iOffset;

 if(!file)
 {
  MessageBox(0,"Could not load bitmap: file does not exist.","Error",MB_OK);
  fclose(file);
  return 0;
 }
 fread(cHeader,54,1,file);
 if(cHeader[0] != 'B' || cHeader[1] != 'M')
 {
  MessageBox(0,"Could not load bitmap: incorrect file type.","Error",MB_OK);
  fclose(file);
  return 0;
 }
 iHeight = *(int*)(cHeader+22);
 iOffset = *(unsigned int*)(cHeader+10);
 iWidth = *(int*)(cHeader+18);
 delete[] data;
 data = new BYTE[iHeight*iWidth*3];
 iBits = int(cHeader[28]);

 switch(iBits)
 {
  case 1:
   fread(colorTable,8,1,file);
   fseek(file,iOffset,SEEK_SET);
   for(a = 0;a < iHeight;++a)
   {
	for(b = 0;b < iWidth;++b)
	{
	 fread(&tempData,1,1,file);
	 for(c = 0;c < 8;++c)
	 {
	  for(d = 0;d < 3;++d)
	  {
	   pixel(b+c,a,d) = colorTable[((tempData >> (7-c))&1)*4+2-d];
	  }
	 }
	}
   }
  break;
  case 4:
   fread(colorTable,64,1,file);
   fseek(file,iOffset,SEEK_SET);
   for(a = 0;a < 256;++a)
   {
	for(b = 0;b < 256;++b)
	{
	 fread(&tempData,1,1,file);
	 for(c = 0;c < 3;++c)
	 {
	  pixel(b,a,c) = colorTable[tempData/16*4+2-c];
	 }
	 for(c = 0;c < 3;++c)
	 {
	  pixel(b+1,a,c) = colorTable[tempData%16*4+2-c];
	 }
	}
   }
  break;
  case 8:
   fread(colorTable,1024,1,file);
   fseek(file,iOffset,SEEK_SET);
   for(a = 0;a < iHeight;++a)
   {
	for(b = 0;b < iWidth;++b)
	{
	 fread(&tempData,1,1,file);
	 for(c = 0;c < 3;++c)
	 {
	  pixel(b,a,c) = colorTable[tempData*4+2-c];
	 }
	}
   }
  break;
  case 24:
   fseek(file,iOffset,SEEK_SET);
   fread(data,iHeight*iWidth*3,1,file);
   for(a = 0;a < iHeight*iWidth*3;a += 3)
   {
	tempData = data[a];
	data[a] = data[a+2];
	data[a+2] = tempData;
   }
  break;
  default:
   fclose(file);
   MessageBox(0,"Could not load bitmap: unknown file format.","Error",MB_OK);
 }
 generateTexture(&glTexture);
 fclose(file);
 return glTexture;
}

void bmp::generateTexture(GLuint *glTexture)
{
 glGenTextures(1,glTexture);
 glPixelStorei(GL_UNPACK_ALIGNMENT,4);
 glBindTexture(GL_TEXTURE_2D,*glTexture);
 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,iWidth,iHeight,0,GL_RGB,GL_UNSIGNED_BYTE,data);
 glBindTexture(GL_TEXTURE_2D,0);
}



I just can't figure out why it's being rotated like that. The image does display, but as I said, it looks like it's being rotated 90 degrees clockwise and then flipped horizontally. Oh and, the image I'm trying to load is an 8 bit bitmap.

Any help is greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Bitmap displays in wrong direction

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,628
  • Joined: 16-October 07

Re: Bitmap displays in wrong direction

Posted 30 January 2010 - 06:19 AM

Interesting code. You're actually trying to deconstruct the format? Well, at least it's the easiest. :P

Your problem probably is, you're gonna love this, that's the way it's stored!

Quote

Bitmap data

This block of bytes describes the image, pixel by pixel. Pixels are stored "upside-down" with respect to normal image raster scan order, starting in the lower left corner, going from left to right, and then row by row from the bottom to the top of the image.

-- http://en.wikipedia....BMP_file_format

You may change your code accordingly. I'd also make a better names than a,b,c,d and consider using a struct for the header. The code that you currently have may only work at 8bit ( certainly not the 1 or 4). Also consider loading the whole mess into memory and then sifting through the bits.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1