在我继续提出这个问题之前,我想说的是我自己学习所有东西并且没有大学经验。我买不起。因此,如果有人知道一个好的资源,他们可以指出我在科学层面上与这个主题有关,我将不胜感激!
我注意到当我运行一个将颜色图绘制到屏幕上的函数时,它在一次调用时不起作用。调用必须至少循环17次,才能将色彩图实际绘制到屏幕上。
我在X11中为Linux编程,这是我的代码无效的示例--->
Canvas_Draw(display, &canvas);
尽管如此,当我将其更改为<>;
时,它仍然有效unsigned int number = 0;
while (number < 17) {
Canvas_Draw(display, &canvas);
number++;
}
只要它循环至少17次,它就能工作。然而,对于诸如加载和解码文件之类的其他功能,情况并非如此,即使它很大......甚至是屏幕外渲染。这可能与此功能正在将图像绘制到屏幕这一事实有关吗?这是被调用函数的代码---&gt;
GC gc_field;
XGCValues gc_values;
gc_field = XCreateGC(display, canvas->canvas, GCForeground, &gc_values);
XPutImage(display, canvas->canvas, gc_field, canvas->image, 0, 0, 0, 0, canvas->width, canvas->height);
XFlush(display);
任何帮助向我解释这一点,或者甚至指向一个有效的资源解释这一点将不胜感激!谢谢!
修改的
在为此示例重新制作之后,重要数字发生了变化!现在不再是17岁,而是13岁!我也注意到有一个不同的图像,它不需要循环功能,而它仍然在第一个测试图像上!
注意,BMP解码器仅解码24位未压缩的BMP图像。
这是一个可验证的例子---&gt;
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#define BYTE unsigned char
#define UINT unsigned int
#define ULONG unsigned long
#define COLOR unsigned int
#define ARRAY unsigned char*
typedef struct Position Position;
typedef struct Size Size;
typedef struct Array Array;
typedef struct Pixelmap Pixelmap;
typedef struct BMP BMP;
typedef struct Canvas Canvas;
typedef struct Panel Panel;
struct Position {
int x, y;
};
struct Size {
unsigned int width, height;
};
struct Array {
ARRAY data;
ULONG dataCount;
};
struct Pixelmap {
unsigned int* pixels;
UINT depth;
Size size;
UINT pixelCount;
};
struct BMP {
UINT width;
UINT height;
UINT depth;
UINT compressionType;
unsigned int* pixels;
};
struct Canvas {
int loaded;
int x, y, width, height;
COLOR forecolor, backcolor;
Pixelmap *field;
XImage *image;
Window canvas;
};
struct Panel {
int x, y, width, height, forecolor, backcolor;
Window panel;
};
void Canvas_Load_BMP(Display *display, Canvas *canvas, BMP *bmp);
void Canvas_Render(Display* display, Canvas *canvas);
void Canvas_Draw(Display *display, Canvas *canvas);
BMP *BMPDecoder_Decode(Array *bmpData);
Array *BMPDecoder_Load_File(char* Path);
Pixelmap *Pixelmap_Create(Size size, UINT Depth);
int Pixelmap_Add_Pixel(Pixelmap *pMap, COLOR Pixel);
int ArrayComparator(ARRAY A, ARRAY B, UINT Size);
Array *Array_Create(ULONG Size);
UINT Array_To_UINT(ARRAY Integer);
COLOR Color_Alpha_Blend(COLOR color, COLOR BG);
int main(int argc, char* argv[])
{
Display* display;
Screen* screen;
int screenIndex;
Size screenSize;
Window window;
XEvent event;
GC gc_canvas;
int x, y, width, height;
Position w_position;
Size w_size;
int rendered = 0;
const char* programName = "Image Display Test";
display = XOpenDisplay(NULL);
screen = DefaultScreenOfDisplay(display);
screenSize.width = screen->width;
screenSize.height = screen->height;
if (display == NULL) {
fprintf(stderr, "Failed to open display!\n");
exit(1);
}
width = 800;
height = 600;
x = (screenSize.width/2) - (width/2);
y = (screenSize.height/2) - (height/2);
w_position.x = x;
w_position.y = y;
w_size.width = width;
w_size.height = height;
screenIndex = DefaultScreen(display);
window = XCreateSimpleWindow(display, RootWindow(display, screenIndex), x, y, width, height, 1, 0, 0xFFFFFF);
XSelectInput(display, window, KeyPressMask | PropertyChangeMask | StructureNotifyMask | PointerMotionMask | ButtonPressMask);
XMapWindow(display, window);
XFlush(display);
XStoreName(display, window, programName);
XWindowChanges changes;
changes.x = w_position.x;
changes.y = w_position.y;
XConfigureWindow(display, window, CWX | CWY, &changes);
Panel cPanel;
cPanel.x = 0;
cPanel.y = 0;
cPanel.width = width;
cPanel.height = height;
cPanel.panel = XCreateSimpleWindow(display, window, 0, 0, width, height, 0, 0x000000, 0xFF0000);
XSelectInput(display, cPanel.panel, KeyPressMask | ButtonPressMask | StructureNotifyMask);
XMapWindow(display, cPanel.panel);
XFlush(display);
int created = 0;
Canvas canvas;
int c_width = width/2;
int c_height = height/2;
canvas.canvas = XCreateSimpleWindow(display, cPanel.panel, width/2-(c_width/2), width/2-(c_width/2), c_width, c_height, 0, 0, 0x000000);
canvas.x = x;
canvas.y = y;
canvas.width = width;
canvas.height = height;
canvas.loaded = 0;
XSelectInput(display, canvas.canvas, KeyPressMask | KeyReleaseMask | ButtonPressMask | StructureNotifyMask);
XMapWindow(display, canvas.canvas);
XFlush(display);
Size size;
size.width = canvas.width;
size.height = canvas.height;
canvas.field = Pixelmap_Create(size, 4);
UINT index = 0;
while (index < canvas.width*canvas.height) {
Pixelmap_Add_Pixel(canvas.field, 0x00000000);
index++;
}
canvas.backcolor = 0x00000000;
XSetWindowBackground(display, canvas.canvas, 0x000000);
Array *bmpData = BMPDecoder_Load_File("/home/user/Example 1.bmp");
BMP *bmp = BMPDecoder_Decode(bmpData);
Canvas_Load_BMP(display, &canvas, bmp);
Canvas_Render(display, &canvas);
UINT number = 0;
while(1) {
XNextEvent(display, &event);
if (number < 13) {
Canvas_Draw(display, &canvas);
number++;
}
if (event.xany.window == window) {
if (event.type == ConfigureNotify) {
XWindowAttributes attributes;
XGetWindowAttributes(display, window, &attributes);
Size np_size;
np_size.width = attributes.width;
np_size.height = attributes.height;
if (np_size.width > 0 && np_size.height > 0) {
cPanel.width = np_size.width;
cPanel.height = np_size.height;
XWindowChanges changes;
changes.width = np_size.width;
changes.height = np_size.height;
XConfigureWindow(display, cPanel.panel, CWWidth | CWHeight, &changes);
}
Position nc_position;
nc_position.x = cPanel.width/2-(canvas.width/2);
nc_position.y = cPanel.height/2-(canvas.height/2);
canvas.x = nc_position.x;
canvas.y = nc_position.y;
XWindowChanges changes;
changes.x = nc_position.x;
changes.y = nc_position.y;
XConfigureWindow(display, canvas.canvas, CWX | CWY, &changes);
Canvas_Draw(display, &canvas);
}
}
}
return 0;
}
void Canvas_Load_BMP(Display *display, Canvas *canvas, BMP *bmp) {
canvas->width = bmp->width;
canvas->height = bmp->height;
Size size;
size.width = bmp->width;
size.height = bmp->height;
canvas->field = Pixelmap_Create(size, 4);
char* data = calloc(bmp->width*bmp->height, 4);
canvas->image = XCreateImage(display, DefaultVisual(display, 0), DefaultDepth(display, 0), ZPixmap, 0, data, bmp->width, bmp->height, 32, 0);
XWindowChanges changes;
changes.width = size.width;
changes.height = size.height;
XConfigureWindow(display, canvas->canvas, CWWidth | CWHeight, &changes);
int x = 0;
int y = bmp->height-1;
UINT increment = 0;
while (y > -1) {
while (x < bmp->width) {
Pixelmap_Add_Pixel(canvas->field, bmp->pixels[bmp->width*y+x]);
x++;
}
x = 0;
y--;
}
canvas->loaded = 1;
}
void Canvas_Render(Display* display, Canvas *canvas) {
if (canvas->loaded == 0)
return;
int x = 0;
int y = 0;
while (y < canvas->height) {
while (x < canvas->width) {
XPutPixel(canvas->image, x, y, Color_Alpha_Blend(canvas->field->pixels[(canvas->width*y)+x], canvas->backcolor));
x++;
}
x = 0;
y++;
}
XFlush(display);
}
void Canvas_Draw(Display *display, Canvas *canvas) {
if (canvas->loaded == 0)
return;
GC gc_field;
XGCValues gc_values;
gc_field = XCreateGC(display, canvas->canvas, GCForeground, &gc_values);
XPutImage(display, canvas->canvas, gc_field, canvas->image, 0, 0, 0, 0, canvas->width, canvas->height);
XFlush(display);
}
BMP *BMPDecoder_Decode(Array *bmpData) {
BMP *bmp = malloc(sizeof(BMP));
BYTE bheader[] = { 0x42, 0x4D };
BYTE fSizeBytes[] = { bmpData->data[5], bmpData->data[4], bmpData->data[3], bmpData->data[2] };
BYTE dataOffsetBytes[] = { bmpData->data[13], bmpData->data[12], bmpData->data[11], bmpData->data[10] };
BYTE widthBytes[] = { bmpData->data[21], bmpData->data[20], bmpData->data[19], bmpData->data[18] };
BYTE heightBytes[] = { bmpData->data[25], bmpData->data[24], bmpData->data[23], bmpData->data[22] };
BYTE depthBytes[] = { 0x00, 0x00, bmpData->data[29], bmpData->data[28] };
BYTE compressionTypeBytes[] = { bmpData->data[33], bmpData->data[32], bmpData->data[31], bmpData->data[30] };
BYTE bitmapSizeBytes[] = { bmpData->data[37], bmpData->data[36], bmpData->data[35], bmpData->data[34] };
ARRAY fheader = calloc(2, sizeof(BYTE));
fheader[0] = bmpData->data[0];
fheader[1] = bmpData->data[1];
if (!ArrayComparator(fheader, bheader, 2)) {
free(fheader);
return NULL;
}
free(fheader);
UINT fSize = Array_To_UINT(fSizeBytes);
UINT dataOffset = Array_To_UINT(dataOffsetBytes);
UINT width = Array_To_UINT(widthBytes);
UINT height = Array_To_UINT(heightBytes);
UINT depth = Array_To_UINT(depthBytes);
UINT compressionType = Array_To_UINT(compressionTypeBytes);
UINT bitmapSize = Array_To_UINT(bitmapSizeBytes);
if (fSize != bmpData->dataCount)
return NULL;
if (fSize-dataOffset != bitmapSize)
return NULL;
int padding = (width*(depth/8))%4;
if (padding != 0) {
padding -= 4;
padding *= -1;
}
bmp->width = width;
bmp->height = height;
bmp->depth = depth;
bmp->compressionType = compressionType;
bmp->pixels = calloc(width*height, sizeof(COLOR));
int x = 0;
int y = 0;
BYTE colorBytes[depth/8];
while (y < height) {
while (x < width) {
colorBytes[0] = bmpData->data[dataOffset+(width*(depth/8))*y+y*padding+x*(depth/8)+2];
colorBytes[1] = bmpData->data[dataOffset+(width*(depth/8))*y+y*padding+x*(depth/8)+1];
colorBytes[2] = bmpData->data[dataOffset+(width*(depth/8))*y+y*padding+x*(depth/8)];
bmp->pixels[width*y+x] = (0xFF & colorBytes[0]) << 24 | (0xFF & colorBytes[1]) << 16 | (0xFF & colorBytes[2]) << 8 | 0xFF;
x++;
}
x = 0;
y++;
}
return bmp;
}
Array *BMPDecoder_Load_File(char* Path) {
FILE *fileptr;
ARRAY buffer;
ULONG filelen;
UINT i = 0;
fileptr = fopen(Path, "rb");
fseek(fileptr, 0, SEEK_END);
filelen = ftell(fileptr);
rewind(fileptr);
buffer = calloc(filelen, sizeof(BYTE));
Array *data = Array_Create(filelen);
while (i < filelen) {
fread(data->data+i, 1, 1, fileptr);
i++;
}
if (data->dataCount <= 0)
return NULL;
return data;
}
Array *Array_Create(ULONG Size) {
Array *array = malloc(sizeof(Array));
array->data = calloc(Size, sizeof(BYTE));
array->dataCount = Size;
return array;
}
Pixelmap *Pixelmap_Create(Size size, UINT Depth) {
Pixelmap *pMap = malloc(sizeof(Pixelmap));
pMap->depth = Depth;
pMap->size = size;
pMap->pixels = calloc(1, sizeof(Depth));
pMap->pixelCount = 0;
return pMap;
}
int Pixelmap_Add_Pixel(Pixelmap *pMap, COLOR Pixel) {
UINT pixelCount = pMap->pixelCount+1;
void *newPixels = realloc(pMap->pixels, sizeof(pMap->depth)*pixelCount);
if (newPixels != NULL)
pMap->pixels = newPixels;
else
return 0;
pMap->pixelCount++;
pMap->pixels[pixelCount-1] = Pixel;
return 1;
}
int ArrayComparator(ARRAY A, ARRAY B, UINT Size) {
int index = 0;
while (index < Size) {
if (A[index] != B[index])
return 0;
index++;
}
return 1;
}
UINT Array_To_UINT(ARRAY Integer) {
return Integer[0] << 24 | Integer[1] << 16 | Integer[2] << 8 | Integer[3];
}
COLOR Color_Alpha_Blend(COLOR color, COLOR BG) {
COLOR cred = (color >> 24) & 0xFF;
COLOR cgreen = (color >> 16) & 0xFF;
COLOR cblue = (color >> 8) & 0xFF;
COLOR alpha = color & 0xFF;
COLOR bred = (BG >> 24) & 0xFF;
COLOR bgreen = (BG >> 16) & 0xFF;
COLOR bblue = (BG >> 8) & 0xFF;
COLOR red = ((cred*alpha)/0xFF) + ((bred*(0xFF-alpha))/0xFF);
COLOR green = ((cgreen*alpha)/0xFF) + ((bgreen*(0xFF-alpha))/0xFF);
COLOR blue = ((cblue*alpha)/0xFF) + ((bblue*(0xFF-alpha))/0xFF);
return red << 16 | green << 8 | blue;
}
GCC编译行---&gt;
gcc <source-file> -lX11 -o <executable-file>
这是加载第一个示例图像并且调用Canvas_Draw()函数少于13次时发生的情况---&gt;
以下是第一个示例图像加载并且Canvas_Draw()函数被调用13次或更多时发生的情况---&gt;
以下是加载第二个示例图像并且Canvas_Draw()函数被调用1次或更多时发生的情况---&gt;
以下是压缩zip文件夹中两个可测试图像的链接---&gt;
如果有人能弄明白问题是什么并帮助我,我将不胜感激!谢谢!
答案 0 :(得分:0)
您的事件循环已中断。
首先,您需要为窗口选择曝光事件:
XSelectInput(display, canvas.canvas, KeyPressMask | KeyReleaseMask |
ButtonPressMask | StructureNotifyMask |
ExposureMask); // <===== here
其次,您只需要在事件表明您需要绘制时绘制:
while(1) {
XNextEvent(display, &event);
if (event.type == Expose && // <=== not on other events
event.xexpose.window == canvas.canvas) { // <=== not on events from other windows
Canvas_Draw(display, &canvas);
continue;
}
这会在第一次调用Canvas_Draw
时显示您的图片。
当第一个曝光事件到达时,窗口已准备就绪。在此之前尝试绘制是没有意义的。过早的请求可能会也可能不会达到你想要的效果。
之后,您可以自由重新按下按钮或任何其他想要的内容。