下面是XCreateImage函数。我可以让它与我的程序一起工作,但图像打印不正确。应该如何格式化此函数中的数据变量以打印出良好的图像。比如它应该是 r g b r g b r g b 还是有一个标题或这个函数工作所需的任何东西?我想我会发布我的整个来源,但您仍然需要图像文件来理解我所看到的,它打印出图像但并不完美。所以很好奇如何格式化数据变量以打印出好的图像。
extern XImage *XCreateImage(
Display* /* display */,
Visual* /* visual */,
unsigned int /* depth */,
int /* format */,
int /* offset */,
char* /* data */,
unsigned int /* width */,
unsigned int /* height */,
int /* bitmap_pad */,
int /* bytes_per_line */
);
#include <arpa/inet.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <X11/Xutil.h>
Display *d; /* Display */
int s; /* Screen */
/* Windows */
Window w;
Window ngb;
Window lgb;
Visual *v;
int looping = 1;
XEvent e;
struct timeval tv;
unsigned char *AOI = 0;
unsigned char *load_bmp_image(const char *loc) {
unsigned char *image=0;
unsigned char *tmp=0;
FILE *fp;
char buffer[288];
int i=0,c=0,br=0;
size_t si=0;
fp=fopen(loc, "r");
if(fp==NULL) {
snprintf(buffer, 287, "Unable to open %s for reading!", loc);
buffer[287] = 0;
perror(buffer);
return 0;
}
while(!feof(fp)){ fgetc(fp); br++; }
si=br-138;
image=malloc(si);
if(image==0) {
fclose(fp);
perror("Error allocating memory for bmp image!");
return 0;
}
tmp=malloc(si);
if(tmp==0) {
free(image);
fclose(fp);
perror("Error allocating memory for bmp tmp!");
return 0;
}
image[si-1]=0;
rewind(fp);
while(br>=0) {
br--;
if(c<138) {
fgetc(fp);
c++;
continue;
}
tmp[i++]=fgetc(fp);
}
fclose(fp);
for(i=0;i<si;i++){
image[i] = tmp[si-i-1];
}
free(tmp);
return image;
}
void draw_image(unsigned char *image){
unsigned int i=0;
int c=0;
unsigned int ch;
XImage *img = XCreateImage(d,v,DefaultDepth(d,s),ZPixmap, 0, (char *)image, 500,500,32,0);
/*
while(image[i]!=0) {
switch(c) {
case 0:
ch = image[i]<<16;
c=1;
case 1:
ch+= image[i]<<8;
c=2;
case 2:
ch+= image[i];
c=0;
XAddPixel(img, ch);
default:
perror("Error drawing image!");
}
i++;
}*/
XPutImage(d,w,DefaultGC(d,s),img,0,0,0,0,500,500);
XDestroyImage(img);
}
void map_main_menu () {
w = XCreateSimpleWindow (d, RootWindow (d, s), 100, 100, 500, 500, 10, 0x802222, 0xFFFFFF);
XSelectInput (d, w, ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask);
XMapWindow (d, w);
XSetStandardProperties(d, w, "The life of Jesus", "tloj" , None, NULL, 0, NULL);
ngb = XCreateSimpleWindow (d, w, 165, 372, 170, 54, 0, 0x802222, 0x808080);
XSelectInput (d, w, ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask);
XMapWindow (d, ngb);
lgb = XCreateSimpleWindow (d, w, 165, 436, 170, 54, 0, 0x802222, 0x808080);
XSelectInput (d, w, ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask);
XMapWindow (d, lgb);
AOI=load_bmp_image("imgs/AlphaOmegaMonogram_500x500.bmp");
}
void close_program() {
if(AOI)
free(AOI);
XDestroyWindow(d, w);
XCloseDisplay(d);
exit(EXIT_SUCCESS);
}
int main (int argc, char *argv[]) {
int x11_fd;
fd_set in_fds;
KeySym key;
char text[255];
setbuf (stdout, NULL);
d = XOpenDisplay (NULL);
if (d == NULL) {
perror ("Cannot open display!");
exit (EXIT_FAILURE);
}
s = DefaultScreen (d);
v = DefaultVisual(d,s);
map_main_menu ();
// This returns the FD of the X11 display (or something like that)
x11_fd = ConnectionNumber (d);
/* Set our select timer. */
tv.tv_usec = 0;
tv.tv_sec = 0;
while (looping) {
// Create a File Description Set containing x11_fd
FD_ZERO (&in_fds);
FD_SET (x11_fd, &in_fds);
// Wait for X Event or a Timer
int num_ready_fds = select (x11_fd + 1, &in_fds, NULL, NULL, &tv);
if (num_ready_fds > 0) {
//printf("Event Received!\n");
}
else if (num_ready_fds < 0)
printf ("An error occured with select!\n");
// Handle XEvents and flush the input
while (XPending (d)) {
XNextEvent (d, &e);
if (e.xany.window == w) {
if (e.type == Expose) {
XDrawLine (d, w, DefaultGC (d, s), 10, 10, 100, 100);
draw_image(AOI);
XFlush (d);
}
if (e.type==KeyPress && XLookupString(&e.xkey, text, 255, &key, 0)==1){
if(text[0] == 'q') {
close_program();
}
}
}
else if (e.xany.window == ngb) {
if (e.type == ButtonPress) {
if (e.xbutton.button == 1) {
printf("GOT HERE!\n");
}
}
if (e.type == ButtonRelease) {
if (e.xbutton.button == 1) {
}
}
}
else if (e.xany.window == lgb) {
if (e.type == ButtonPress) {
if (e.xbutton.button == 1) {
}
}
if (e.type == ButtonRelease) {
if (e.xbutton.button == 1) {
}
}
}
}
}
return 0;
}
这是图片https://pasteboard.co/HQezZBJHwXy6.bmp
我尝试将 bmp 文件中的数据从小端复制到大端,然后将该数据提供给 XCreateImage 函数,但打印效果并不完美。我期待它能打印出完美的图像。