Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: m@o_614 en 20 Noviembre 2015, 17:53 pm



Título: guardar una imagen bmp
Publicado por: m@o_614 en 20 Noviembre 2015, 17:53 pm
Saludos

Tengo el siguiente código en C++ que tiene una función llamada Bresenham(), que nos va a dibujar una(s) linea(s) simple en la ventana. Una vez que se dibujó la línea, el usuario debe de dar clic derecho en la ventana para que las líneas se guarden como una imagen bmp. Para esto tengo que crear dos estructuras: bmpInfoHeader y bmpFileHeader. El problema que tengo es que en la estructura de bmpFileHeader no se cómo calcular los campos de altura y anchura, y al momento de abrir la imagen no me aparece nada

Código
  1. #include<windows.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<stdint.h>
  5. #include<gl/glut.h>
  6.  
  7. typedef struct
  8. {
  9.    uint32_t tamanio;
  10.    uint16_t campoReservado;
  11.    uint32_t offset;
  12. }bmpFileHeader;
  13.  
  14. typedef struct
  15. {
  16.    uint32_t tamanioEncabezado;
  17.    uint32_t anchura;
  18.    uint32_t altura;
  19.    uint16_t planos;
  20.    uint16_t profundidadModeloColor;
  21.    uint32_t compresion;
  22.    uint32_t tamanioImagen;
  23.    uint32_t bmpx;
  24.    uint32_t bmpy;
  25.    uint32_t colores;
  26.    uint32_t impColores;
  27. }bmpInfoHeader;
  28.  
  29. float a[90000];
  30. int x0=0,y0=0,xf=0,yf=0;
  31. int puedeImprimir=0;
  32. FILE *bmp = NULL;
  33. unsigned char *imagen;
  34. bmpInfoHeader infoEncabezado;
  35. bmpFileHeader encabezadoArchivo;
  36.  
  37. void init(void);
  38. void putpixel(int x,int y);
  39. void Bresenham(int x0,int y0,int x1,int y1);
  40. void display(void);
  41. void onMotion(int x,int y);
  42. void onMouse(int button, int e, int x, int y);
  43. void onPassive(int x,int y);
  44. void crearEncabezadoInformacion(bmpInfoHeader *infoEncabezado);
  45. void crearEncabezadoArchivo(bmpFileHeader *encabezadoArchivo);
  46. void Guardar(bmpFileHeader *encabezadoArchivo,bmpInfoHeader *infoEncabezado);
  47.  
  48. int main()
  49. {
  50.    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
  51.    glutInitWindowSize(300, 300);
  52.    glutInitWindowPosition(100, 100);
  53.    glutCreateWindow("Practica 2. Trazo de lineas con mouse");
  54.    init();
  55.    glutDisplayFunc(display);
  56.    glutMotionFunc(onMotion);
  57.    glutMouseFunc(onMouse);
  58.    glutPassiveMotionFunc(onPassive);
  59.    glutMainLoop();
  60. }
  61.  
  62. void init(void)
  63. {
  64.   glClearColor(1.0, 1.0, 1.0, 0.0);
  65.   glMatrixMode(GL_PROJECTION);
  66.   gluOrtho2D(0.0, 300.0, 0.0,300.0);
  67. }
  68.  
  69. void putpixel(int x,int y)
  70. {
  71.    glColor3f(0.0, 0.0,0.0);
  72.    glBegin(GL_POINTS);
  73.    glVertex2i(x,y);
  74.    glEnd();
  75. }
  76.  
  77. void Bresenham(int x0,int y0,int x1,int y1)
  78. {
  79.    int dx,dy,p,x,y,pasox = 1,pasoy = 1,dosDymenosDx,dosDy,i;
  80.    glColor3f(0.0,0.0,1.0);
  81.    dx = x1-x0;
  82. dy = y1-y0;
  83.  
  84.    if(dx < 0)
  85.       dx = dx*-1;
  86.    if(dy < 0)
  87.       dy = dy*-1;
  88.    if(x1 < x0)
  89.       pasox = -1;
  90.    if(y1 < y0)
  91.       pasoy = -1;
  92.  
  93.    x = x0;
  94.    y = y0;
  95. if( dx > dy )
  96.    {
  97.        putpixel(x,y);
  98.        p = 2 * dy - dx;
  99.        dosDymenosDx = 2 * ( dy - dx );
  100.        dosDy = 2 * dy;
  101.        for( i = 0; i < dx; i++ )
  102.        {
  103.             if( p >= 0 )
  104.             {
  105.                 y += pasoy;
  106.                 p += dosDymenosDx;
  107.             }
  108.             else
  109.                p += dosDy;
  110.             x += pasox;
  111.             putpixel(x,y);
  112.        }
  113.   }
  114.   else
  115.   {
  116.       putpixel(x,y);
  117.       p = 2*dx - dy;
  118.       dosDymenosDx = 2 * ( dx - dy );
  119.       dosDy = 2*dx;
  120.       for( i = 0; i < dy; i++ )
  121.       {
  122.           if( p >= 0 )
  123.           {
  124.               x += pasox;
  125.               p += dosDymenosDx;
  126.           }
  127.           else
  128.              p += dosDy;
  129.           y += pasoy;
  130.           putpixel(x,y);
  131.       }
  132.   }
  133.   glFlush();
  134. }
  135.  
  136. void display(void)
  137. {
  138.    glClear(GL_COLOR_BUFFER_BIT);
  139.    if(puedeImprimir==1)
  140.       glDrawPixels(300,300,GL_RGB,GL_UNSIGNED_BYTE,a);
  141.    Bresenham(x0,y0,xf,yf);
  142.    glFlush();
  143. }
  144.  
  145. void onMotion(int x,int y)
  146. {
  147.    xf = x;
  148.    yf = 300-y;
  149.    glutPostRedisplay();
  150. }
  151.  
  152. void onMouse(int button, int e, int x, int y)
  153. {
  154.    unsigned char media;
  155.    if((button == GLUT_LEFT_BUTTON) && (e == GLUT_DOWN))
  156.    {
  157.        puedeImprimir = 1;
  158.        x0 = xf = x;
  159.        y0 = yf = abs(300-y);
  160.    }
  161.    else if((button == GLUT_LEFT_BUTTON) && (e == GLUT_UP))
  162.       puedeImprimir = 0;
  163.    else if((button == GLUT_RIGHT_BUTTON) && (e == GLUT_UP))
  164.    {
  165.        //Abrir();
  166.        crearEncabezadoInformacion(&infoEncabezado);
  167.        Guardar(&encabezadoArchivo,&infoEncabezado);
  168.    }
  169. }
  170.  
  171. void crearEncabezadoInformacion(bmpInfoHeader *infoEncabezado)
  172. {
  173.    infoEncabezado = (bmpInfoHeader*)malloc(sizeof(bmpInfoHeader));
  174.  
  175.    infoEncabezado->tamanioEncabezado = sizeof(bmpInfoHeader);
  176.    infoEncabezado->anchura = ????;
  177.    infoEncabezado->altura = ????;
  178.    infoEncabezado->planos = 1;
  179.    infoEncabezado->profundidadModeloColor = 24;
  180.    infoEncabezado->compresion = BI_RGB;
  181.    infoEncabezado->tamanioImagen = ?????;
  182.    infoEncabezado->bmpx = 0;
  183.    infoEncabezado->bmpy = 0;
  184.    infoEncabezado->colores = 0;
  185.    infoEncabezado->impColores = 0;
  186. }
  187.  
  188. void Guardar(bmpFileHeader *encabezadoArchivo,unsigned char *imagen,bmpInfoHeader *info)
  189. {
  190.    uint16_t firma;
  191.    if((bmp = fopen("practica no. 7.bmp","wt"))!= NULL)
  192.    {
  193.        firma = 0x4D42;
  194.        encabezadoArchivo->campoReservado = 0;
  195.        encabezadoArchivo->offset = sizeof(bmpFileHeader)+sizeof(bmpInfoHeader)+2;
  196.        encabezadoArchivo->tamanio = info->tamanioImagen+sizeof(bmpFileHeader)+sizeof(bmpInfoHeader);
  197.        fwrite(&firma,sizeof(firma),1,bmp);
  198.        fwrite(encabezadoArchivo,sizeof(bmpFileHeader),1,bmp);
  199.        fwrite(info,sizeof(bmpInfoHeader),1,bmp);
  200.    }
  201.    else
  202.       printf("No se pudo crear fichero");
  203. }
  204.  
  205. void onPassive(int x,int y)
  206. {
  207.    glReadPixels(0.0,0.0,300.0,300.0,GL_RGB,GL_UNSIGNED_BYTE,a);
  208.    Bresenham(x0,y0,xf,yf);
  209. }
  210.  
  211.  

alguna idea de cómo arreglarlo?