c - XPutImage 不显示任何内容
问题描述
大家好,我在过去的周末试图在屏幕上放一张 png 图像,但没有成功。我左右看了看为什么,但无论我尝试什么,我似乎都找不到为什么有人能指出我正确的方向?
我用这个作为模板。然后我看了所有这些:
但什么也不偷
XPutImage(display,
*window,
((mc_drawable *)this)->_gc,
this->image,
0, 0,
0, 0,
this->_y, this->_x);
这就是我使用 XPutImage 的方式
#include <png.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include "modular/raise.h"
#include "internal/drawable/imagepr.h"
#include "internal/server_connection.h"
static void chechFile(
FILE *file)
{
unsigned char nbr[8] = "";
fread(nbr, 1, 8, file);
if (png_sig_cmp(nbr, 0, 8) != 0) {
raise("not a Png file");
}
}
static char parsePng(
FILE *file,
mc_imagePr *image)
{
char *data = NULL;
png_struct *pngPtr = png_create_read_struct(
PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
png_info *pngInfo = NULL;
int readFlag = PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND;
int colorType = 0;
int interlaceMethod = 0;
int rowBytes = 0;
png_uint_32 index = 0;
png_bytepp rowPointers = NULL;
png_uint_32 width = 0;
png_uint_32 height = 0;
int bitDepth = 0;
Display *dys = getDisplay();
if (!pngPtr) {
return (0);
} else if (!(pngInfo = png_create_info_struct(pngPtr))) {
png_destroy_read_struct(&pngPtr, NULL, NULL);
return (0);
}
if (setjmp(png_jmpbuf(pngPtr))) {
png_destroy_read_struct(&pngPtr, &pngInfo, NULL);
return (0);
}
png_init_io(pngPtr, file);
png_set_sig_bytes(pngPtr, 8);
png_read_png(pngPtr, pngInfo, readFlag, NULL);
png_get_IHDR(pngPtr,
pngInfo,
&width, &height,
&bitDepth,
&colorType,
&interlaceMethod,
NULL,
NULL);
rowBytes = png_get_rowbytes(pngPtr, pngInfo);
data = malloc(rowBytes * height);
if (!data) {
png_destroy_read_struct(&pngPtr, &pngInfo, NULL);
return (0);
}
rowPointers = png_get_rows(pngPtr, pngInfo);
while (index < height) {
memcpy(data + (index * rowBytes),
rowPointers[index],
rowBytes);
++index;
}
printf("PNG %d * %d\n rowbytes %d\n depth %d\ncolor type %d\n",
width,
height,
rowBytes,
bitDepth,
colorType);
image->image = XCreateImage(
dys,
CopyFromParent,
DefaultDepth(
dys,
DefaultScreen(dys)),
ZPixmap,
0,
data,
width,
height,
32,
rowBytes);
png_destroy_info_struct(pngPtr, &pngInfo);
png_destroy_read_struct(&pngPtr, &pngInfo, NULL);
return (1);
}
char readPng(
const char *path,
mc_imagePr *image)
{
FILE *fd = fopen(path, "r");
char res = 0;
if (!fd) {
raise("file not found\n");
}
chechFile(fd);
res = parsePng(
fd,
image);
fclose(fd);
return (res);
}
这是我用来创建 XImage 的文件
编辑:
在阅读了 NM 的回复后,我尝试再次绘制 50 x 50 的白色图像,但没有成功。这意味着我真的没有理解 XPutImage 是如何工作的。这是我的测试:
#include <memory.h>
#include <stdlib.h>
#include <assert.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
Window window;
Display *display = XOpenDisplay();
XSetWindowAttributes test = {};
XEvent e;
GC gc;
int whiteColor;
Atom delWin = XInternAtom(display, "WM_DELETE_WINDOW", True);
XImage *image = NULL;
char *data = malloc(50 * 50 * 4);
memset(data, 250, 50 * 50 * 4);
if (!display) {
return (84);
}
window = XCreateWindow(
display,
XDefaultRootWindow(display),
0, 0,
500, 500,
100,
CopyFromParent,
CopyFromParent,
CopyFromParent,
0,
&test);
XSelectInput(display, window, StructureNotifyMask);
XMapWindow(display, window);
gc = XCreateGC(display, window, 0, 0);
whiteColor = WhitePixel(display, DefaultScreen(display));
XSetForeground(display, gc, whiteColor);
XSetWMProtocols(display, window, &delWin, 1);
while (1) {
XNextEvent(display, &e);
if (e.type == MapNotify)
break;
}
XFlush(display);
image = XCreateImage(
display,
CopyFromParent,
DefaultDepth(
display,
DefaultScreen(display)),
ZPixmap,
0,
data,
50,
50,
32,
50 * 4);
while (1) {
XNextEvent(display, &e);
if (e.type == DestroyNotify ||
(e.type == ClientMessage &&
e.xclient.data.l[0] == (long)delWin))
break;
XPutImage(
display,
window,
gc,
image,
0, 0,
50, 50,
0, 0);
}
XCloseDisplay(display);
return (0);
解决方案
我觉得有点愚蠢,但我只是交换了 2 行并且无法看到它。感谢 nm 如何帮助我弄清楚
int main(void)
{
Window window;
Display *display = XOpenDisplay();
XSetWindowAttributes test = {};
XEvent e;
GC gc;
int whiteColor;
Atom delWin = XInternAtom(display, "WM_DELETE_WINDOW", True);
XImage *image = NULL;
char *data = malloc(50 * 50 * 4);
memset(data, 250, 50 * 50 * 4);
if (!display) {
return (84);
}
window = XCreateWindow(
display,
XDefaultRootWindow(display),
0, 0,
500, 500,
100,
CopyFromParent,
CopyFromParent,
CopyFromParent,
0,
&test);
XSelectInput(display, window, StructureNotifyMask);
XMapWindow(display, window);
gc = XCreateGC(display, window, 0, 0);
whiteColor = WhitePixel(display, DefaultScreen(display));
XSetForeground(display, gc, whiteColor);
XSetWMProtocols(display, window, &delWin, 1);
while (1) {
XNextEvent(display, &e);
if (e.type == MapNotify)
break;
}
XFlush(display);
image = XCreateImage(
display,
CopyFromParent,
DefaultDepth(
display,
DefaultScreen(display)),
ZPixmap,
0,
data,
50,
50,
32,
50 * 4);
while (1) {
XNextEvent(display, &e);
if (e.type == DestroyNotify ||
(e.type == ClientMessage &&
e.xclient.data.l[0] == (long)delWin))
break;
XPutImage(
display,
window,
gc,
image,
0, 0, // from which offset to start drawing
0, 0, // position on the screen
50, 50); // width and height of what you want to draw
}
XCloseDisplay(display);
return (0);
推荐阅读
- r - 拟合到正态分布后在 10% 尾部找到观测值
- xslt - XSL 显示操作数据值
- android - react-navigation中的React-native android后退按钮
- python - Scipy 和 MATLAB 网格数据的差异
- r - R - 计算概率并翻转累积曲线 (ECDF) 的 x/y 轴
- r - 带有代表其他向量的唯一组合的字母的新向量
- c# - 如何检查无效的 UTF-8 字符?
- python - Python - 将文本从 HTML 文件转换为没有唯一标识符标签的 csv
- python-3.x - 如何使用 ldap3 修改广义时间属性
- firebase - 在实时数据库中更新孩子?如何?