PDA

View Full Version : Bitmap File



Mukund
03-18-2010, 07:37 PM
Hello everyone.Im new to OpenGL..
I tried opening a bitmap image(.bmp) in my program and then displaying it using the OpenGL primitives..
I just used GL_POINTS and then put the pixel info onto the screen..

But this is not working for all bitmaps!!

Here is the code:
************************************************** ************

#include<gl/glut.h>
#include<stdio.h>
#include<stdlib.h>


typedef struct {
unsigned short type;
unsigned long size;
unsigned short reserved1;
unsigned short reserved2;
unsigned long offsetbits;
} BITMAPFILEHEADER1;

typedef struct {
unsigned long size;
unsigned long width;
unsigned long height;
unsigned short planes;
unsigned short bitcount;
unsigned long compression;
unsigned long sizeimage;
long xpelspermeter;
long ypelspermeter;
unsigned long colorsused;
unsigned long colorsimportant;
} BITMAPINFOHEADER1;

typedef struct {
unsigned char blue;
unsigned char green;
unsigned char red;
} SINGLE_PIXEL1;



void display()
{

FILE *fp;unsigned char p;
int x=0,y=0,c=0;
float r,g,b;

BITMAPINFOHEADER1 bitm;
BITMAPFILEHEADER1 bitmp;

glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);

fp = fopen("****","rb");//Filename is given

fread(&amp;bitmp,14,1,fp);
fread(&amp;bitm,40,1,fp);

gluOrtho2D(0.0,bitm.width,0.0,bitm.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glViewport(0,0,bitm.width,bitm.height);


glBegin(GL_POINTS);
while(!feof(fp))
{
fread(&amp;p,1,1,fp);
b = p/255;
fread(&amp;p,1,1,fp);
g = p/255;
fread(&amp;p,1,1,fp);
r = p/255;



glColor3f(r,g,b);
glVertex2i(x++,y);

if(x == bitm.width)
{
x = 0;
y++;
}
}
glEnd();
glFlush();
fclose(fp);
}


int main(int argc, char* argv[])
{

glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);

glutInitWindowPosition(100,150);
glutCreateWindow("BITMAP");

glutDisplayFunc(display);
glutMainLoop();

return 0;
}

ZbuffeR
03-19-2010, 01:13 AM
"not working for all bitmaps" is pretty vague. Care to explain ?

Mukund
03-19-2010, 04:48 AM
I created a few bitmap images using MS Paint and opened them..I was able to open those..But then i tried opening some other bitmaps..I tried opening some images given in Windows by default..but could not open them!

Is the code all right or is there any modification that im required to do?? Pls help..

ZbuffeR
03-19-2010, 05:28 AM
"could not open" : an error occured, or a crash, or no textured showed up, or ?

When creatig BMP, be sure to not create the compressed version, which is more complex to read.

Mukund
03-19-2010, 10:46 AM
No,what was happening was, i got a black texture..The file was actually a complex shade of brown.It was just a patch of Black.

And i made sure that it was not a compressed version..I checked the compression bit..It was ZERO..

Is there anything wrong with the code?

ZbuffeR
03-19-2010, 02:46 PM
I think I found something :


fread(&amp;p,1,1,fp);
b = p/255;
fread(&amp;p,1,1,fp);
g = p/255;
fread(&amp;p,1,1,fp);
r = p/255;
glColor3f(r,g,b);


1) p/255 is an integer division in C. That mean either 0 or 1 as a result ...
2) instead, you can use directly the unsigned byte :




unsigned char r,g,b;
fread(&amp;b,1,1,fp);
fread(&amp;g,1,1,fp);
fread(&amp;r,1,1,fp);
glColor3ub(r,g,b);

Mukund
03-20-2010, 09:55 AM
Thanks a lot for helping..but its still not working.. :( :(


I changed the code as u said.I used printf and tried to debug..It is generating the same value for r,g,b throughout!! so basically im getting a shade of one color..!!