GIMP 创建 XPM 图像的 C 头文件中的头像素说明

发布于 2024-12-26 22:06:40 字数 1451 浏览 3 评论 0原文

在 GIMP 中,您可以将图像保存为 C 头文件。我使用 XPM 文件执行此操作,如下图所示:

如果我要将 XPM 图像保存为 C头文件,GIMP 将输出这个 C 头文件

为了处理给定图像数据的每个像素,重复调用标题像素。我不明白的是标​​题像素首先是如何处理数据的。

#define HEADER_PIXEL(data,pixel) {\
pixel[0] = (((data[0] - 33) << 2) | ((data[1] - 33) >> 4)); \
pixel[1] = ((((data[1] - 33) & 0xF) << 4) | ((data[2] - 33) >> 2)); \
pixel[2] = ((((data[2] - 33) & 0x3) << 6) | ((data[3] - 33))); \
data += 4; \
}

当我看到它在 另一个人的中使用时代码,他们指出字节顺序是错误的,并自行重新排列。他们这样使用它:

char *pixel, *data = header_data;
int i = width * height;
*processed_data = pixel = malloc(i * 4 + 1);
while(i-- > 0) {
    pixel[0] = ((((data[2] - 33) & 0x3) << 6) | ((data[3] - 33)));
    pixel[1] = ((((data[1] - 33) & 0xF) << 4) | ((data[2] - 33) >> 2));
    pixel[2] = (((data[0] - 33) << 2) | ((data[1] - 33) >> 4));
    pixel[3] = 0;
    data += 4;
    pixel += 4;
}

但这并没有真正帮助我理解所有位移位和按位或的情况以及“为什么是负33?”等等。如果有人可以解释如何处理标题中的图像数据,我们将不胜感激。

提前致谢!

In GIMP, you're able to save an image as a C header file. I did so with an XPM file, which looks like the image below:

If I were to save the XPM image as a C header file, GIMP will output this C header file.

In order to process each pixel of the given image data, the header pixel is called repeatedly. What I don't understand is what the header pixel does to process the data in the first place.

#define HEADER_PIXEL(data,pixel) {\
pixel[0] = (((data[0] - 33) << 2) | ((data[1] - 33) >> 4)); \
pixel[1] = ((((data[1] - 33) & 0xF) << 4) | ((data[2] - 33) >> 2)); \
pixel[2] = ((((data[2] - 33) & 0x3) << 6) | ((data[3] - 33))); \
data += 4; \
}

When I saw it in use in another person's code, they stated the byte order was in the wrong order and rearranged it themselves. They used it like this:

char *pixel, *data = header_data;
int i = width * height;
*processed_data = pixel = malloc(i * 4 + 1);
while(i-- > 0) {
    pixel[0] = ((((data[2] - 33) & 0x3) << 6) | ((data[3] - 33)));
    pixel[1] = ((((data[1] - 33) & 0xF) << 4) | ((data[2] - 33) >> 2));
    pixel[2] = (((data[0] - 33) << 2) | ((data[1] - 33) >> 4));
    pixel[3] = 0;
    data += 4;
    pixel += 4;
}

But that didn't really help me understand what is going on with all the bit shifting and bitwise or's and "why minus 33?" and so forth. If anyone can give an explanation on what is going on to process to the image data in the header, that would be much appreciated.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雅心素梦 2025-01-02 22:06:40

每个像素由 3 个字节表示。这些像素被定义为一个字符数组,名为header_data

问题在于,并非每个字节都是该头文件中可能存在的可打印字符

仅使用可打印字符 3397 即可解决此问题。这提供了 6 位信息,因此每四个字符将提供 24 位,可以表示 3 个字节的所有排列。

Each pixel is represented by 3 bytes. These pixels are defined as a character array, named header_data.

The problem is that not every byte is a printable character that could exist in that header file.

This is solved by only using the printable characters 33 through 97. That gives 6 bits of information, so every four characters will give 24 bits, which can represent all permutations of 3 bytes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文