1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
Display * display;
Window window;
if (XInitThreads() == 0) {
printf("Error : initializing XInitThreads.\n");
return 1;
}
display = XOpenDisplay(NULL);
if (display == NULL) {
printf("Error : Cannot open display.\n");
}
window = XCreateSimpleWindow(display,XDefaultRootWindow(display),0,0,800,600,0,0,0);
XSelectInput(display,window,ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask );
XMapWindow(display,window);
// charger l'image
FILE *f;
int x=0,y=0;
char *buffer=NULL;
f = fopen("test.raw","rb");
if (f == NULL) {
printf("Error : loading image.\n");
}
else {
fread(&x,4,1,f);
fread(&y,4,1,f);
buffer = malloc(x*y*3);
if (buffer == NULL){
printf("Error : malloc\n");
}
else {
fread(buffer,x*y*3,1,f);
}
fclose(f);
}
XImage *image = NULL;
if (buffer) {
image = XCreateImage(display, DefaultVisual(display,DefaultScreen(display)),24,ZPixmap,0,buffer,x,y,8,0);
}
int running = 1;
while (running) {
XEvent event;
XNextEvent(display,&event);
switch (event.type) {
case ButtonPress :
if (image) {
XLockDisplay(display);
XPutImage(display,window,DefaultGC(display,DefaultScreen(display)),image,0,0,event.xbutton.x,event.xbutton.y,x,y);
XSync(display,0);
XUnlockDisplay(display);
}
break;
case KeyPress :
running = 0; // quitter
break;
}
}
if (image) {
XDestroyImage(image);
}
XUnmapWindow(display,window);
XDestroyWindow(display,window);
return 0;
} |
Partager