GIMP title pixel explanation created by XPM image header file

In GIMP, you can save the image as a header file C. I did this with an XPM file that looks like this:

If I wanted to save the XPM image as a C header file, GIMP would output that C header file .

To process each pixel of data in a given image, the header pixel is called repeatedly. I don’t understand what the header pixel does for data processing 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 that it was being used in another person’s code , they stated that the byte order was in the wrong order and rebuilt it on their own. 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 this did not help me understand what is happening with all bit offsets and bitwise or "why minus 33?". and so on. If anyone can give an explanation of what is happening to process the image data in the header, that would be greatly appreciated.

Thanks in advance!

+4
c header-files gimp xlib
Jan 16 '12 at 2:27
source share
1 answer

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 may exist in this header file.

This is only permitted using printable characters 33 through 97 . This gives 6 bits of information, so every four characters produce 24 bits, which can represent all permutations of 3 bytes.

+3
Jan 16
source share



All Articles