1

I have this code which has a Bresenham() function that draws a simple line and then when the user clicks the right button, the bresenham line(s) is saved as a bmp image. But the problem that I have is that I don´t know how to calculate the width and the height of the bmpInfoHeader structure. And I also don´t know how to load the image so that it can save it.

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

typedef struct
{
    uint32_t Size;
    uint16_t field;
    uint32_t offset;
}bmpFileHeader;

typedef struct
{
    uint32_t headerSize;
    uint32_t width;
    uint32_t hight;
    uint16_t planes;
    uint16_t dcm;
    uint32_t compression;
    uint32_t imageSize;
    uint32_t bmpx;
    uint32_t bmpy;
    uint32_t colors;
    uint32_t impColors;
}bmpInfoHeader;

float a[90000];
int x0=0,y0=0,xf=0,yf=0;
int print=0;
FILE *bmp = NULL;
bmpInfoHeader *infoHeader;
bmpFileHeader *fileHeader;

void init(void);
void putpixel(int x,int y);
void Bresenham(int x0,int y0,int x1,int y1);
void display(void);
void onMotion(int x,int y);
void onMouse(int button, int e, int x, int y);
void Save();
void onPassive(int x,int y);
void createFileHeader(bmpFileHeader *fileHeader);
void createInfoHeader(bmpInfoHeader *infoHeader);

void init(void)
{
   glClearColor(1.0, 1.0, 1.0, 0.0);
   glMatrixMode(GL_PROJECTION);
   gluOrtho2D(0.0, 300.0, 0.0,300.0);
}

void putpixel(int x,int y)
{
    glColor3f(0.0, 0.0,0.0);
    glBegin(GL_POINTS);
    glVertex2i(x,y);
    glEnd();
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    if(print==1)
       glDrawPixels(300,300,GL_RGB,GL_UNSIGNED_BYTE,a);
    Bresenham(x0,y0,xf,yf);
    glFlush();
}

void Bresenham(int x0,int y0,int x1,int y1)
{
    int dx,dy,p,x,y,px = 1,py = 1,twoDy_Dx,twoDy,i;
    glColor3f(0.0,0.0,1.0);
    dx = x1-x0;
    dy = y1-y0;

    if(dx < 0)
       dx = dx*-1;
    if(dy < 0)
       dy = dy*-1;
    if(x1 < x0)
       px = -1;
    if(y1 < y0)
       py = -1;

    x = x0;
    y = y0;
    if( dx > dy )
    {
        putpixel(x,y);
        p = 2 * dy - dx;
        twoDy_Dx = 2 * ( dy - dx );
        twoDy = 2 * dy;
        for( i = 0; i < dx; i++ )
        {
             if( p >= 0 )
             {
                 y += py;
                 p += twoDy_Dx;
             }
             else
                p += twoDy;
             x += px;
             putpixel(x,y);
        }
   }
   else
   {
       putpixel(x,y);
       p = 2*dx - dy;
       twoDy_Dx = 2 * ( dx - dy );
       twoDy = 2*dx;
       for( i = 0; i < dy; i++ )
       {
           if( p >= 0 )
           {
               x += px;
               p += twoDy_Dx;
           }
           else
              p += twoDy;
           y += py;
           putpixel(x,y);
       }
   }
   glFlush();
}


void onMotion(int x,int y)
{
    xf = x;
    yf = 300-y;
    glutPostRedisplay();
}

void onMouse(int button, int e, int x, int y)
{
    if((button == GLUT_LEFT_BUTTON) && (e == GLUT_DOWN))
    {
        print = 1;
        x0 = xf = x;
        y0 = yf = abs(300-y);
    }
    else if((button == GLUT_LEFT_BUTTON) && (e == GLUT_UP))
       print = 0;
    else if((button == GLUT_RIGHT_BUTTON) && (e == GLUT_UP))
    {
        createFileHeader(fileHeader);
        createInfoHeader(infoHeader);
        Save();
    }
}

void onPassive(int x,int y)
{
    glReadPixels(0.0,0.0,300.0,300.0,GL_RGB,GL_UNSIGNED_BYTE,a);
    Bresenham(x0,y0,xf,yf);
}

void createInfoHeader(bmpInfoHeader *infoHeader)
{
    infoHeader = (bmpInfoHeader*)malloc(sizeof(bmpInfoHeader));

    infoHeader->headerSize = sizeof(bmpInfoHeader);
    infoHeader->width = ???;
    infoHeader->hight = ???;
    infoHeader->planes = 1;
    infoHeader->dcm = 24;
    infoHeader->compression = BI_RGB;
    infoHeader->imageSize =;
    infoHeader->bmpx = 0;
    infoHeader->bmpy = 0;
    infoHeader->colors = 0;
    infoHeader->impColors = 0;
}

void createFileHeader(bmpFileHeader *fileHeader)
{
    fileHeader = (bmpFileHeader*)malloc(sizeof(bmpFileHeader));
    fileHeader->Size = 0;
    fileHeader->field = 0;
    fileHeader->offset = sizeof(bmpFileHeader)+sizeof(bmpInfoHeader);
}

void Save()
{
    uint16_t type;
    if((bmp = fopen("practica no. 7.bmp","wt"))!= NULL)
    {
        type = 0x4D42;
        fwrite(&type,sizeof(type),1,bmp);
        fwrite(&fileHeader,sizeof(bmpFileHeader),1,bmp);
        fwrite(&infoHeader,sizeof(bmpInfoHeader),1,bmp);
    }
    else
       printf("No se pudo crear fichero");
}

int main()
{
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(300, 300);
    glutInitWindowPosition(100, 100);
    glutCreateWindow();
    init();
    glutDisplayFunc(display);
    glutMotionFunc(onMotion);
    glutMouseFunc(onMouse);
    glutPassiveMotionFunc(onPassive);
    glutMainLoop();
}
zerocukor287
  • 555
  • 2
  • 8
  • 23
user3105533
  • 321
  • 3
  • 11

1 Answers1

0

As I can expose from your code, the image is 300 x 300. It means, your bmpInfoHeader.width and bmpInfoHeader.height should be 300, if you keep the scale. Otherwise, multiply window width with scale.

The other part of your question has answer in How to use GLUT to render to a file

zerocukor287
  • 555
  • 2
  • 8
  • 23