Qt Qimage支持的没用过的图像格式:PBM, PGM, PPM, XBM, XPM

Question: image format introduce(quality and compress): PBM, PGM, PPM, XBM, XPM

  • author: eton & deepseek

Answer:

其实这些都是历史存在但是现在几乎不用的图像文件格式,都不支持压缩,只有存储交换而已。 bitmap则是非黑即白的单个bit位存储的图像;

Of course. This is an excellent question that covers the foundational image formats of computing. These formats are often grouped together as the Netpbm suite (PBM, PGM, PPM) and the X Window System bitmaps (XBM, XPM).

Here is a detailed introduction to each, focusing on their structure, quality, and compression.


Overview: The Common Theme

These formats are all simple, uncompressed, and lossless. They were designed for ease of programming and portability between different machines, not for efficient storage. Their "compression" is often just the avoidance of complex encoding schemes.


1. PBM (Portable BitMap)

  • Type: Bitmap (1-bit)
  • Color: Monochrome (Pure Black and White only, no grays).
  • Data Representation: Each pixel is represented by a single bit (0 for white, 1 for black, or vice versa).
  • Quality: Perfect for its intended purpose: storing 1-bit data like simple shapes, scaned text, or masks. There is no quality degradation because there is no color or grayscale information to lose.
  • "Compression":
    • ASCII (P1): Human-readable. A 1 represents a black pixel, a 0 represents white. Extremely inefficient in file size.
    • Binary (P4): Much more efficient. Each pixel is stored as a single bit, packed 8 pixels to a byte. This is the most compact representation possible for 1-bit data without using a compression algorithm like Run-Length Encoding (RLE).

Example (ASCII PBM):

P1
# This is a simple example
4 4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0

This creates a 4x4 image with a 2x2 black square in the center.


2. PGM (Portable GrayMap)

  • Type: Grayscale
  • Color: Up to 16-bit grayscale (65,536 shades of gray), though 8-bit (256 shades) is most common.
  • Data Representation: Each pixel is a single number representing its intensity (e.g., 0 = black, 255 = white for 8-bit).
  • Quality: Excellent for lossless storage of grayscale images. Since it's uncompressed, it preserves every single pixel value exactly as intended. Often used in scientific imaging and computer vision where data integrity is paramount.
  • "Compression":
    • ASCII (P2): Human-readable. Each pixel's value is written as a decimal number. Very large file size.
    • Binary (P5): Efficient. Each pixel's value is stored as one or two bytes (depending on the max value). This is the standard way to use PGM and is vastly smaller than the ASCII version.

3. PPM (Portable PixMap)

  • Type: Full Color
  • Color: 24-bit RGB color (8 bits per channel, leading to over 16 million colors).
  • Data Representation: Each pixel is represented by three numbers in sequence: Red, Green, and Blue intensity values.
  • Quality: Perfect, lossless quality. It stores the raw RGB data without any alteration. This makes it a useful intermediate format for image processing pipelines where saving a JPEG (which is lossy) between steps would degrade the image.
  • "Compression":
    • ASCII (P3): Human-readable. Each RGB triplet is written as three decimal numbers. Absurdly large file size (a 100x100 image would have 30,000 numbers in the file).
    • Binary (P6): Efficient. Each RGB value is stored as a single byte, so each pixel takes exactly 3 bytes. This is the standard way to use PPM. It is still 3x larger than a high-quality JPEG of the same image but is completely lossless.

Use Case: The binary PPM (P6) is famously the output format of the aarexis raytracer in the classic book "Physically Based Rendering: From Theory to Implementation," as it's trivial to write out raw pixel data from a renderer.


4. XBM (X BitMap)

  • Type: Bitmap (1-bit)
  • Color: Monochrome (like PBM).
  • Data Representation: This is the key difference. XBM is not just an image format; it is C programming language source code. The image data is defined as a static array of hexadecimal bytes.
  • Quality: Identical to PBM—perfect for black-and-white graphics.
  • "Compression": None. The data is represented as hexadecimal values in a C code array. This makes it incredibly useful for its original purpose: including small icons and cursor shapes directly in the source code of X Window System applications. The "compression" is that it's compiled directly into the program binary.

Example:

#define logo_width 16
#define logo_height 16
static unsigned char logo_bits[] = {
  0xF0, 0x0F,
  0x1C, 0x38,
  0x06, 0x60,
  0x03, 0xC0,
  0x01, 0x80,
  0x81, 0x81,
  // ... more hex data ...
};

5. XPM (X PixMap)

  • Type: Color
  • Color: Can support multiple color depths, but most commonly used for 256-color images. Its killer feature is support for 1-bit transparency.
  • Data Representation: This is also C source code. It uses a creative approach: it defines a string array where each string represents a row of pixels. Each character in the string is a key that maps to a color defined in a color lookup table below. The format also allows for defining a transparent color (None).
  • Quality: Lossless for indexed color. The quality is limited by the number of colors in its palette. It is not suitable for photorealistic images but is perfect for icons and GUI elements where transparency is needed.
  • "Compression": The use of a single character to represent a color is a form of lightweight indexing. For small icons with limited colors, it can be quite efficient. Its primary advantage is its seamless integration into X11 code.

Example (Simplified):

/* XPM */
static char * example_xpm[] = {
"4 4 2 1",        /* width=4, height=4, num_colors=2, chars_per_pixel=1 */
"# c #000000",    /* character '#' is black */
". c #ffffff",    /* character '.' is white */
".##.",
"#..#",
"#..#",
".##."
};

Summary Table

Format Type Colors Key Feature "Compression" Best For
PBM Bitmap 2 (B&W) Simplicity Binary Packing Masks, text, simple graphics
PGM Grayscale 256-65K Lossless Grayscale Binary Data Scientific imaging, computer vision
PPM Full Color 16M+ Lossless RGB Color Binary Data Intermediate format for rendering/editing
XBM Bitmap 2 (B&W) C Code Format Hexadecimal Array Icons in X11 source code
XPM Indexed Color 2-256+ C Code + Transparency Color Indexing Color icons with transparency in X11

Conclusion

While you would never use PPM to store photos on your website (use JPEG or WebP) or PBM for a modern app icon (use PNG or SVG), these formats are far from obsolete. Their simplicity, reliability, and lossless nature make them incredibly valuable tools in programming, graphics research, scientific computing, and anywhere else where data integrity and ease of use are more important than file size.

posted on 2025-08-29 09:18  Eton..Bee  阅读(17)  评论(0)    收藏  举报