0

Put a bitmap file in the same directory of this code. Name this file "a.bmp"

Code =>

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

typedef struct __attribute__((__packed__)) { 
    unsigned short type; 
    unsigned long size; 
    unsigned short reserved1; 
    unsigned short reserved2; 
    unsigned long offsetbits; 
} BITMAPFILEHEADER1;

typedef struct __attribute__((__packed__)) {
    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;    

    BITMAPFILEHEADER1 bitmp;    
    BITMAPINFOHEADER1 bitm; 

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

    fp = fopen("a.bmp","rb");//Filename is given    

    fread(&bitmp,14,1,fp);  

    fread(&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);
    int cnt=0;

    int bi,gi,ri;
    unsigned char tp1,tp2,tp3;
    while(!feof(fp))
    {
        fread(&p,1,1,fp);       
        //b = p/255.0;
        tp1=p;      
        fread(&p,1,1,fp);
        //g = p/255.0;
        tp2=p;      
        fread(&p,1,1,fp);
        //r = p/255.0;
        tp3=p;
        //glColor3f(r,g,b);
        glColor3ub(tp1,tp2,tp3);
        glVertex2i(x++,y);
        if(x == bitm.width)     
        { 
            x = 0;
            y++;
        }
    }
    glEnd();
    glFlush();
    fclose(fp);
}
int main(int argc, char* argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowSize(800,800);
    glutInitWindowPosition(100,150);
    glutCreateWindow("BITMAP");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

Put the code in a file named "bit.cpp". Then compile this "bit.cpp" file:

g++ bit.cpp -o bit -lglut -lGL -lGLU

And run:

./bit

You will see black and white(gray) image in glut window. How can i draw colored pixels? glColor3ub(tp1,tp2,tp3); this should set color for vertex. Shouldn't it? But it's not drawing colored pixels, why? How can I get colored image in glut window?

Any answer will be highly appreciated.

Thanks in advance.

cola
  • 12,198
  • 36
  • 105
  • 165
  • 2
    Not the cause of your problem (most likely) but [`while(!feof(fp))` is always wrong](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – molbdnilo May 23 '16 at 11:18
  • Are the values of tp1 tp2 and tp3 ever different? – stark May 23 '16 at 11:31
  • Have you examined the values of `tp1`, `tp2`, and `tp3` to see if they are as expected? Are you sure that the format of the picture is three eight-bit integers per pixel? Are you sure that the `fread` calls succeed? Are you sure that your hard-coded constants are correct (`sizeof` is a friend, not an enemy)? – molbdnilo May 23 '16 at 11:32
  • @stark, Yes, they are different. – cola May 23 '16 at 11:53
  • @molbdnilo, I got colored image for this = http://www.flightunit.com/wp-content/uploads/2012/08/Flightunit-happy-face.bmp – cola May 23 '16 at 11:57
  • @shibly That would suggest that the other images you've tested with don't have the format you expect. – molbdnilo May 23 '16 at 11:59
  • @molbdnilo, But all images have .bmp extension. – cola May 23 '16 at 12:00
  • @shibly Yes, but the pixels may be stored differently, and they may even be compressed. The `BITMAPINFOHEADER` structure contains this information, and its documentation is [here](https://msdn.microsoft.com/en-us/library/windows/desktop/dd183376%28v=vs.85%29.aspx). – molbdnilo May 23 '16 at 12:06

0 Answers0