Ya he conseguido, gracias por echarme una mano Instru, el problema que tenía era que después de pintar la imagen volvia a pintar toda la pantalla de negro.
Dejo el código que ya funciona.
Código C:
Ver original#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
void nombre_archivo(char texto[]);
void abrir_imagen();
void mostrar_imagen();
Display *display;
Window win1;
XSetWindowAttributes attributes;
XFontStruct *fontinfo;
GC gr_context1;
XArc arcs[10];
Pixmap pixmap;
Visual *visual;
int screen;
int depth;
int i;
unsigned char grey,r,g,b;
FILE *fimg,*ftext;
char nombre[20]="imagen.txt";
char ruta[100]="/home/albert/Downloads/imagen2.bmp";
typedef struct {
unsigned char r,g,b,grey;
} COLOURINDEX;
typedef struct bmpFileHeader
{
unsigned short int type; /* Magic identifier */
unsigned int size; /* File size in bytes */
unsigned short int reserved1, reserved2;
unsigned int offset; /* Offset to image data, bytes */
} bmpFileHeader;
typedef struct bmpInfoHeader
{
unsigned int size; /* Header size in bytes */
int width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bpp; /* Bits per pixel */
unsigned int compression; /* Compression type */
unsigned int imagesize; /* Image size in bytes */
int xresolution,yresolution; /* Pixels per meter */
unsigned int colors; /* Number of colours */
unsigned int importantcolours; /* Important colours */
} bmpInfoHeader;
bmpFileHeader header;
bmpInfoHeader bInfoHeader;
int main(){
if (!fimg){
printf("no hay imagen"); /* Si no podemos leer, no hay imagen*/ }
/* Leemos la cabecera de fichero completa */
fread(&header.
type, sizeof(header.
type), 1, fimg
); fread(&header.
size, sizeof(header.
size), 1, fimg
); fread(&header.
reserved1, sizeof(header.
reserved1), 1, fimg
); fread(&header.
reserved2, sizeof(header.
reserved2), 1, fimg
); fread(&header.
offset, sizeof(header.
offset), 1, fimg
);
fread(&bInfoHeader
,sizeof(bmpFileHeader
),1,fimg
);
ftext
=fopen(nombre
,"wb");
fwrite(&header
,sizeof(bmpFileHeader
)-2,1,ftext
); fwrite(&bInfoHeader
,sizeof(bmpInfoHeader
),1,ftext
);
mostrar_imagen();
return 0;
}
void mostrar_imagen(){
//XGCValues gr_values;
XEvent event;
display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
visual = DefaultVisual(display,screen);
depth = DefaultDepth(display,screen);
//attributes.background_pixel = XWhitePixel(display, screen);
//attributes.border_pixel = XBlackPixel(display, screen);
attributes.override_redirect = 0;
win1 = XCreateWindow(display, XRootWindow(display, screen), 200, 200, bInfoHeader.width,
bInfoHeader.height, 5, depth, InputOutput, visual, CWBackPixel | CWBorderPixel
| CWOverrideRedirect, &attributes);
XSelectInput(display, win1, ExposureMask | ButtonPressMask | KeyPressMask);
pixmap = XCreatePixmap(display, win1, bInfoHeader.width, bInfoHeader.height, depth);
/*fontinfo = XLoadQueryFont(display, "6x10");
gr_values.font = fontinfo->fid;
gr_values.function = GXcopy;
gr_values.plane_mask = AllPlanes;
gr_values.foreground = BlackPixel(display,screen);
gr_values.background = WhitePixel(display,screen);*/
gr_context1 = XCreateGC(display, win1, 0, 0);
//XDefineCursor(display, win1, XCreateFontCursor(display, XC_heart));
XMapWindow(display, win1);
/*do {
XNextEvent(display, &event);
if (event.type == Expose) {
//XCopyArea(display, win1, pixmap, gr_context1, 50, 25, bInfoHeader.width, bInfoHeader.height, 0,
0);
XSetFunction(display, gr_context1, GXinvert);
XDrawImageString(display, pixmap, gr_context1, 80, 45, "pixmap", 6);
XDrawImageString(display, pixmap, gr_context1, 90, 60, "copy", 4);
XSetFunction(display, gr_context1, GXcopy);
XSetForeground(display, gr_context1, 505);/
}
} while (event.type != KeyPress);*/
XCopyArea(display, pixmap, win1, gr_context1, 0, 0, bInfoHeader.width, bInfoHeader.height, 0, 0);
abrir_imagen();
//XDrawString(display, win1, gr_context1, 10, 32, "Now press a key to exit",23);
XFlush(display);
do {
XNextEvent(display, &event);
} while (event.type != KeyPress);
XCloseDisplay(display);
}
void abrir_imagen(){
int gotindex=1;
unsigned char r,g,b,grey;
int i,j;
long rgb;
/* Nos desplazamos el valor de offset hasta llegar a la zona de los datos */
fseek(fimg
, header.
offset, SEEK_SET
);
/* Hay que invertir los indices porque la imagen se muestra al reves*/
for(j=bInfoHeader.height-1;j>=0;j--) {
for(i=bInfoHeader.width-1;i>=0;i--) {
switch(bInfoHeader.bpp) {
case 1:
break;
case 4:
break;
case 8:
if (fread(&grey
, sizeof(unsigned char), 1, fimg
) != 1) { fprintf(stderr
, "Image read failed\n"); }
if (gotindex) {
} else {
}
break;
case 24:
if (fread(&b
, sizeof(unsigned char), 1, fimg
) != 1) { fprintf(stderr
, "Image read failed 1\n");
}
if (fread(&g
, sizeof(unsigned char), 1, fimg
) != 1) { fprintf(stderr
, "Image read failed 2\n"); }
if (fread(&r
, sizeof(unsigned char), 1, fimg
) != 1) { fprintf(stderr
, "Image read failed 3\n"); }
/*Obtenemos el valor del color a partir del RGB*/
rgb=65536 * r + 256 * g + b;
/*Marcamos el color obtenido*/
XSetForeground(display, gr_context1, rgb);
/*Dibujamos el punto correspondiente*/
XDrawPoint(display, win1, gr_context1, i, j);
break;
}
}//i
}//j
}