Ver Mensaje Individual
  #3 (permalink)  
Antiguo 21/07/2011, 02:33
KatonSP
 
Fecha de Ingreso: abril-2009
Mensajes: 63
Antigüedad: 15 años
Puntos: 0
Respuesta: Mostrar imagen bmp con C

Siento no ser más explicito postee con prisas y bueno...

He solucionado el fallo anterior y el problema que tengo es que cuando llama a la función abrir_imagen no pinta los pixeles y da error.

Código C:
Ver original
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <X11/X.h>
  6. #include <X11/Xlib.h>
  7. #include <X11/Xutil.h>
  8.  
  9. void nombre_archivo(char texto[]);
  10. void abrir_imagen();
  11. void mostrar_imagen();
  12.  
  13. Display *display;
  14. Window win1;
  15. XSetWindowAttributes attributes;
  16. XFontStruct *fontinfo;
  17. GC gr_context1;
  18. XArc arcs[10];
  19. Pixmap pixmap;
  20. Visual *visual;
  21. int screen;
  22. int depth;
  23. int i;
  24. unsigned char grey,r,g,b;
  25.  
  26. FILE *fimg,*ftext;
  27.  
  28. char nombre[20]="imagen.txt";
  29. char ruta[100]="/home/albert/Downloads/imagen2.bmp";
  30.  
  31. typedef struct {
  32.  
  33.     unsigned char r,g,b,grey;
  34.  
  35. } COLOURINDEX;
  36.  
  37. typedef struct bmpFileHeader
  38. {
  39.     unsigned short int type;                 /* Magic identifier            */
  40.     unsigned int size;                       /* File size in bytes          */
  41.     unsigned short int reserved1, reserved2;
  42.     unsigned int offset;                     /* Offset to image data, bytes */
  43. } bmpFileHeader;
  44.  
  45. typedef struct bmpInfoHeader
  46. {
  47.     unsigned int size;               /* Header size in bytes      */
  48.     int width,height;                /* Width and height of image */
  49.     unsigned short int planes;       /* Number of colour planes   */
  50.     unsigned short int bpp;         /* Bits per pixel            */
  51.     unsigned int compression;        /* Compression type          */
  52.     unsigned int imagesize;          /* Image size in bytes       */
  53.     int xresolution,yresolution;     /* Pixels per meter          */
  54.     unsigned int colors;           /* Number of colours         */
  55.     unsigned int importantcolours;   /* Important colours         */
  56. } bmpInfoHeader;
  57.  
  58. bmpFileHeader header;
  59. bmpInfoHeader bInfoHeader;
  60.  
  61. int main(){
  62.  
  63.     fimg=fopen (ruta, "rb");
  64.     if (!fimg){
  65.         printf("no hay imagen");      /* Si no podemos leer, no hay imagen*/
  66.     }
  67.  
  68.     /* Leemos la cabecera de fichero completa */
  69.     fread(&header.type, sizeof(header.type), 1, fimg);
  70.     fread(&header.size, sizeof(header.size), 1, fimg);
  71.     fread(&header.reserved1, sizeof(header.reserved1), 1, fimg);
  72.     fread(&header.reserved2, sizeof(header.reserved2), 1, fimg);
  73.     fread(&header.offset, sizeof(header.offset), 1, fimg);
  74.  
  75.     fread(&bInfoHeader,sizeof(bmpFileHeader),1,fimg);
  76.  
  77.     ftext=fopen(nombre,"wb");
  78.  
  79.     fwrite(&header,sizeof(bmpFileHeader)-2,1,ftext);
  80.     fwrite(&bInfoHeader,sizeof(bmpInfoHeader),1,ftext);
  81.  
  82.     mostrar_imagen();
  83.  
  84.     return 0;
  85. }
  86.  
  87. void mostrar_imagen(){
  88.  
  89.     XGCValues gr_values;
  90.     XEvent event;
  91.  
  92.     setbuf(stdout, NULL);
  93.     setbuf(stderr, NULL);
  94.     display = XOpenDisplay(NULL);
  95.     screen = DefaultScreen(display);
  96.     visual = DefaultVisual(display,screen);
  97.     depth = DefaultDepth(display,screen);
  98.  
  99.     win1 = XCreateWindow(display, XRootWindow(display, screen), 200, 200,
  100.             bInfoHeader.width,bInfoHeader.height, 5, depth, InputOutput, visual,
  101.             CWBackPixel | CWBorderPixel | CWOverrideRedirect, &attributes);
  102.  
  103.     XSelectInput(display, win1, ExposureMask | ButtonPressMask | KeyPressMask);
  104.     pixmap = XCreatePixmap(display, win1, 200, 100, depth);
  105.     fontinfo = XLoadQueryFont(display, "6x10");
  106.     gr_values.font = fontinfo->fid;
  107.     gr_values.function = GXcopy;
  108.     gr_values.plane_mask = AllPlanes;
  109.     gr_values.foreground = BlackPixel(display,screen);
  110.     gr_values.background = WhitePixel(display,screen);
  111.     gr_context1 = XCreateGC(display, win1, GCFont | GCFunction | GCPlaneMask
  112.             | GCForeground | GCBackground, &gr_values);
  113.  
  114.  
  115.     XMapWindow(display, win1);
  116.  
  117.     abrir_imagen();
  118.  
  119.     XDrawString(display, win1, gr_context1, 10, 32, "Now press a key to exit",23);
  120.     XFlush(display);
  121.  
  122.     do {
  123.         XNextEvent(display, &event);
  124.     } while (event.type != KeyPress);
  125.  
  126.     printf("closing display\n");
  127.     XCloseDisplay(display);
  128.  
  129. }
  130.  
  131. void abrir_imagen(){
  132.  
  133.     int gotindex=1;
  134.  
  135.     COLOURINDEX colourindex[256];
  136.  
  137.     unsigned char r,g,b,grey;
  138.  
  139.     int i,j;
  140.     long rgb;
  141.  
  142.     /* Nos desplazamos el valor de offset hasta llegar a la zona de los datos */
  143.     fseek(fimg, header.offset, SEEK_SET);
  144.  
  145.     /* Hay que invertir los indices porque la imagen se muestra al reves*/
  146.     for(j=(bInfoHeader.height) -1;j>=0;j--) {
  147.  
  148.         for(i=(bInfoHeader.width) -1;j>=0;i--) {
  149.  
  150.             switch(bInfoHeader.bpp) {
  151.  
  152.             case 1:
  153.                 break;
  154.             case 4:
  155.                 break;
  156.             case 8:
  157.                 if (fread(&grey, sizeof(unsigned char), 1, fimg) != 1) {
  158.                     fprintf(stderr, "Image read failed\n");
  159.                     exit(-1);
  160.                 }
  161.                 if (gotindex) {
  162.                     putchar(r);
  163.                     putchar(g);
  164.                     putchar(b);
  165.                 } else {
  166.                     putchar(grey);
  167.                 }
  168.                 break;
  169.  
  170.             case 24:
  171.  
  172.                 if (fread(&b, sizeof(unsigned char), 1, fimg) != 1) {
  173.                     fprintf(stderr, "Image read failed 1\n");
  174.                     exit(-1);
  175.                 }
  176.                 if (fread(&g, sizeof(unsigned char), 1, fimg) != 1) {
  177.                     fprintf(stderr, "Image read failed 2\n");
  178.                     exit(-1);
  179.                 }
  180.                 if (fread(&r, sizeof(unsigned char), 1, fimg) != 1) {
  181.                     fprintf(stderr, "Image read failed 3\n");
  182.                     exit(-1);
  183.                 }
  184.  
  185.                 /*Obtenemos el valor del color a partir del RGB*/
  186.                 rgb=65536 * r + 256 * g + b;
  187.                 /*Marcamos el color obtenido*/
  188.                 XSetForeground(display, gr_context1, rgb);
  189.                 /*Dibujamos el punto correspondiente*/
  190.                 XDrawPoint(display, pixmap, gr_context1, i, j);
  191.                 break;
  192.             }
  193.  
  194.         }//i
  195.         printf("\n");
  196.     }//j
  197.     fclose(fimg);
  198.     fclose(ftext);
  199. }

Última edición por KatonSP; 21/07/2011 a las 04:41 Razón: corregir