NVGIF v5 Specification
1. Header Layout
Each NVGIF v5 file begins with a fixed header:
| Field | Size (bytes) | Description |
|---|---|---|
| Magic | 3 | ASCII "NVG" |
| Version | 1 | Format version (0x05 for v5) |
| Compression | 1 | Bitfield of compression flags |
| Alpha | 1 | 0 = disabled, 1 = enabled |
| Width | 2 | Image width (big‑endian) |
| Height | 2 | Image height (big‑endian) |
| Reserved | 1 | Always 0 |
| Payload | variable | Encoded pixel data |
2. Compression Flags
The Compression byte is a bitmask:
| Flag | Value | Meaning |
|---|---|---|
| None | 0 | No compression |
| RLE | 1 | Run‑length encoding |
| Zlib | 2 | zlib compression |
| RGB565 | 4 | Lossy RGB565 encoding (no alpha) |
Multiple flags may be combined using bitwise or (e.g. RLE | Zlib).
3. Payload Encoding
3.1 Raw RGB/RGBA
- Stored row by row.
- Each row begins with a 2‑byte big‑endian length.
- Followed by pixel data (
RGBif alpha disabled,RGBAif enabled).
3.2 RGB565 (Lossy)
- Each pixel stored as a 16‑bit integer:
- Red: 5 bits (bits 11–15)
- Green: 6 bits (bits 5–10)
- Blue: 5 bits (bits 0–4)
- Alpha is discarded.
- Rows still prefixed with 2‑byte length.
3.3 RLE
- Applied to the payload before zlib.
- Format:
[count, value]pairs. countis 1–255,valueis a single byte.- Expands to
valuerepeatedcounttimes.
3.4 ZLIB
- If flagged, the entire payload (after RLE, if present) is compressed with zlib (level 9).
4. Decoding Order
- Read header.
- If ZLIB flag set → decompress payload.
- If RLE flag set → expand payload.
- Parse rows:
- If RGB565 flag set → decode 16‑bit pixels.
- Else → read raw RGB/RGBA rows.
- Construct image with dimensions and alpha mode.
5. Notes
- RGB565 is lossy: reduces color precision and discards alpha.
- Alpha support: unavailable in RGB565 mode.
- Reserved byte: reserved for future use, must be
0.
6. Example
Encoding a 2×2 RGBA image with RLE+ZLIB:
Header: 4E 56 47 05 03 01 00 02 02 00
Payload: [zlib-compressed RLE stream of row data]