YAIF - Custom Image Format

YAIF - Custom Image Format

Open-source custom image format with encoder and viewer. Palette-based compression using nibble-packed pixel data and median-cut color quantization.

C#, .NET, Windows Forms, AForge
Started: 8/27/2018
Updated: 3/15/2024
image-processing,compression,graphics,format,desktop

Overview

YAIF (Yet Another Image Format) is a custom open-source image format designed for educational purposes and experimentation with image compression techniques. The project includes both an encoder for converting standard images to YAIF format and a Windows Forms viewer for displaying YAIF files.

What is YAIF?

YAIF is a palette-based image format that uses 4-bit color indices and intelligent color quantization to achieve compact file sizes while maintaining visual quality for images with limited color ranges. It's particularly well-suited for pixel art, logos, and retro graphics.

Project Components

YAIF Encoder

Command-line tool that converts standard image formats (PNG, JPG, etc.) into YAIF files using color quantization and binary encoding.

Repository: yaifEncoder

YAIF Viewer

Windows Forms desktop application that decodes and displays YAIF images with custom graphics rendering.

Repository: yaifViewer-dotnet

Features

Encoder Features

  • Color Quantization - Reduces images to 16-color palette using median-cut algorithm
  • Palette Optimization - Uses AForge imaging library for intelligent color selection
  • Nibble Packing - Stores two 4-bit pixel indices per byte for 50% compression
  • Command-Line Interface - Easy batch processing and automation

Viewer Features

  • YAIF Format Support - Full decoder for YAIF binary format
  • Palette Rendering - Reconstructs images from indexed color data
  • Windows Forms UI - Native Windows desktop application
  • Double Buffering - Smooth, flicker-free rendering
  • Drag-and-Drop Support - Easy file loading

File Format Specification

The YAIF format uses a compact binary layout:

Offset | Size     | Description
-------|----------|-------------
0x00   | 4 bytes  | Magic number: "YAIF"
0x04   | 2 bytes  | Version number
0x06   | 2 bytes  | Image width
0x08   | 2 bytes  | Image height
0x0A   | 1 byte   | Palette size (colors × 3 for RGB)
0x0B   | 4 bytes  | Image data size (in nibble pairs)
0x0F   | 4 bytes  | Data section marker
0x13   | Variable | Color palette (RGB triplets)
...    | Variable | Nibble-packed pixel indices

Full Specification

Technical Implementation

Encoding Process

  1. Load Source Image - Read standard image file formats
  2. Color Quantization - Apply median-cut algorithm to reduce to 16 colors
  3. Palette Generation - Extract optimal color palette
  4. Index Mapping - Map each pixel to palette index (0-15)
  5. Nibble Packing - Pack two 4-bit indices per byte
  6. Binary Output - Write structured binary YAIF file

Decoding Process

  1. Load Binary File - Read YAIF file into memory
  2. Parse Header - Extract metadata and validate format
  3. Extract Palette - Read RGB color values
  4. Unpack Pixels - Convert nibble pairs to pixel indices
  5. Reconstruct Image - Map indices to palette colors
  6. Render Display - Draw pixels using Windows Forms graphics

Color Quantization

Uses the AForge.Imaging library's median-cut quantizer:

  • Analyzes color distribution in source image
  • Selects optimal 16 colors to minimize visual loss
  • Produces color palette that best represents the original

Nibble Packing

Each byte stores two 4-bit pixel indices:

// Encoding
output = (pixel1 << 4) | pixel2;

// Decoding
nibble1 = byte & 0x0F;  // Lower 4 bits
nibble2 = byte >> 4;    // Upper 4 bits

Compression Ratio

  • Each pixel uses 4 bits (nibble) instead of 24 bits (RGB)
  • Theoretical compression: 6:1 for pixel data
  • Includes overhead for palette storage (16 colors × 3 bytes = 48 bytes)
  • Best suited for images with limited color ranges

Usage

Encoding Images

# Convert image to YAIF format
YaifEncoder.exe input.png

# Output: input.yaif

Viewing Images

# View a YAIF file
yaifViewer.exe image.yaif

# Or drag and drop YAIF files onto the executable

Technologies Used

  • C# / .NET - Core programming language and runtime
  • Windows Forms - Desktop UI framework
  • AForge.Imaging - Color quantization and image processing
  • System.Drawing - Image loading and pixel manipulation

Educational Value

This project demonstrates:

Image Processing Concepts:

  • Binary file formats and custom format design
  • Color theory and palette-based graphics
  • Lossless data packing and compression techniques
  • Color quantization algorithms (median-cut)

Systems Programming:

  • Bit manipulation and byte-level operations
  • Little-endian integer encoding
  • Binary file I/O and parsing
  • Memory-efficient data structures

Application Development:

  • Windows desktop applications
  • Graphics rendering and double buffering
  • Command-line tool design
  • File format validation

Best Use Cases

  • Pixel Art - Natural fit for limited color palettes
  • Retro Graphics - Authentic low-color aesthetic
  • Logos and Icons - Simple graphics with few colors
  • Learning Tool - Understanding image formats and compression
  • Experimentation - Custom format development

Limitations

  • Maximum 16 colors per image
  • No compression of palette or pixel data (beyond nibble packing)
  • Lossy conversion due to color reduction
  • Windows-only viewer (requires .NET Framework)
  • Not suitable for photographs or gradients

Future Enhancements

  • Support for multiple compression modes
  • Extended palette sizes (256 colors)
  • Run-length encoding for solid color areas
  • Metadata support (author, description, timestamps)
  • Multi-image/animation support
  • Cross-platform viewer (WPF, Avalonia, or MAUI)
  • Export to standard formats from viewer
  • Batch conversion tools

Impact

YAIF serves as both a practical tool for palette-based graphics and an educational project for understanding how image formats work at a fundamental level. It demonstrates that creating custom file formats is accessible and provides insight into how industry-standard formats like PNG and GIF handle compression and color management.