说明标题像素在GIMP中创建了一个XPM图像的C标题文件

时间:2021-10-10 15:08:41

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:

在GIMP中,可以将图像保存为C头文件。我使用了一个XPM文件,它看起来像下面的图片:

说明标题像素在GIMP中创建了一个XPM图像的C标题文件

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

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

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.

但这并不能帮助我理解所有的位移和位移以及为什么是负33等等。如果任何人都能对在标题中处理图像数据的过程给出解释,那将是非常值得赞赏的。

Thanks in advance!

提前谢谢!

1 个解决方案

#1


3  

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

每个像素由3个字节表示。这些像素被定义为一个名为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.

只使用可打印字符33到97就可以解决这个问题。这就产生了6比特的信息,所以每4个字符就会产生24比特,这就代表了3个字节的所有排列。

#1


3  

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

每个像素由3个字节表示。这些像素被定义为一个名为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.

只使用可打印字符33到97就可以解决这个问题。这就产生了6比特的信息,所以每4个字符就会产生24比特,这就代表了3个字节的所有排列。