Summary
Implement standard image compressors for JPEG and PNG formats using libjpeg and libpng libraries to complement the existing BPG and neural compressors in Kaira.
Motivation
Currently, Kaira supports BPG compression and neural compression methods, but lacks support for the most widely used image formats: JPEG and PNG. Adding these standard compressors would:
- Improve compatibility - Enable compression/decompression of the most common image formats
- Provide baselines - Allow fair comparison between modern methods (BPG, neural) and traditional standards
- Enable broader research - Support studies comparing classical vs. modern compression techniques
- Increase usability - Make Kaira more practical for real-world applications
Proposed Implementation
JPEGCompressor Class
class JPEGCompressor:
def __init__(self, quality: int = 75, optimize: bool = True, progressive: bool = False):
"""
JPEG compressor using libjpeg via PIL/Pillow.
Args:
quality: JPEG quality (1-100, higher = better quality)
optimize: Enable JPEG optimization
progressive: Enable progressive JPEG encoding
"""
def compress(self, image: PIL.Image.Image) -> bytes:
"""Compress PIL Image to JPEG bytes."""
def decompress(self, data: bytes) -> PIL.Image.Image:
"""Decompress JPEG bytes to PIL Image."""
def get_compression_ratio(self, original_size: int, compressed_size: int) -> float:
"""Calculate compression ratio."""
PNGCompressor Class
class PNGCompressor:
def __init__(self, compress_level: int = 6, optimize: bool = True):
"""
PNG compressor using libpng via PIL/Pillow.
Args:
compress_level: PNG compression level (0-9, higher = better compression)
optimize: Enable PNG optimization
"""
def compress(self, image: PIL.Image.Image) -> bytes:
"""Compress PIL Image to PNG bytes."""
def decompress(self, data: bytes) -> PIL.Image.Image:
"""Decompress PNG bytes to PIL Image."""
def get_compression_ratio(self, original_size: int, compressed_size: int) -> float:
"""Calculate compression ratio."""
Technical Details
Backend Libraries
- JPEG: Use PIL/Pillow with libjpeg backend (already available in project dependencies)
- PNG: Use PIL/Pillow with libpng backend (already available in project dependencies)
- Integration: Follow existing compressor patterns from
BPGCompressor and NeuralCompressor
API Design
- Consistent interface with existing compressors
- Support for both file and memory-based operations
- Quality/compression level parameters matching industry standards
- Comprehensive error handling and validation
Example Usage
from kaira.models.image.compressors import JPEGCompressor, PNGCompressor
# JPEG compression
jpeg_comp = JPEGCompressor(quality=85, optimize=True)
jpeg_data = jpeg_comp.compress(pil_image)
recovered_image = jpeg_comp.decompress(jpeg_data)
# PNG compression
png_comp = PNGCompressor(compress_level=9, optimize=True)
png_data = png_comp.compress(pil_image)
recovered_image = png_comp.decompress(png_data)
Implementation Checklist
Testing Requirements
- Unit tests for compression/decompression cycles
- Quality parameter validation
- Error handling for invalid inputs
- Performance benchmarking against reference implementations
- Integration tests with existing Kaira components
Documentation
- API documentation following existing patterns
- Usage examples and tutorials
- Performance comparison with other compressors
- Best practices for quality/compression level selection
Alternative Compression Formats for Future Consideration
Beyond the core JPEG and PNG implementation, the following compression formats could be considered for future iterations of Kaira:
Modern Web Formats
- WebP - Google's format with superior compression vs JPEG/PNG
- AVIF - Next-gen format based on AV1 codec, excellent compression
- HEIF/HEIC - Apple's format, good compression but limited browser support
Next-Generation Formats
- JPEG XL - ISO standard replacement for JPEG with much better compression
- FLIF - Free Lossless Image Format (development discontinued but innovative)
Video Codec-Based Formats
- HEVC (H.265) Image - Using video codec for still images
- AV1 Image Format (AVIF) - Using AV1 video codec
- VVC Image - Using Versatile Video Coding for images
Neural/AI-Based Compression
- Neural Image Compression - Deep learning approaches (already supported)
- Learned Image Compression - Various academic methods
- Compressive Autoencoders - VAE-based approaches
Classical/Specialized Algorithms
- JPEG 2000 - Wavelet-based, better than JPEG but limited adoption
- JBIG/JBIG2 - For bi-level (black/white) images
- JPEG-LS - Lossless/near-lossless variant
- PNG variants - APNG (animated), WebP lossless mode
Implementation Priority Recommendations
- High Priority: WebP, AVIF (modern web standards)
- Medium Priority: JPEG XL, HEIF (emerging standards)
- Research Interest: Various neural compression methods
- Specialized Use: JPEG 2000, JBIG for specific domains
Each format would require:
- Library compatibility assessment
- Performance benchmarking
- Integration complexity evaluation
- Community interest/demand analysis
The modular compressor architecture in Kaira makes it well-suited for adding these formats incrementally based on research needs and community feedback.
Summary
Implement standard image compressors for JPEG and PNG formats using libjpeg and libpng libraries to complement the existing BPG and neural compressors in Kaira.
Motivation
Currently, Kaira supports BPG compression and neural compression methods, but lacks support for the most widely used image formats: JPEG and PNG. Adding these standard compressors would:
Proposed Implementation
JPEGCompressor Class
PNGCompressor Class
Technical Details
Backend Libraries
BPGCompressorandNeuralCompressorAPI Design
Example Usage
Implementation Checklist
JPEGCompressorclass inkaira/models/image/compressors/jpeg.pyPNGCompressorclass inkaira/models/image/compressors/png.pykaira/models/image/compressors/__init__.pyTesting Requirements
Documentation
Alternative Compression Formats for Future Consideration
Beyond the core JPEG and PNG implementation, the following compression formats could be considered for future iterations of Kaira:
Modern Web Formats
Next-Generation Formats
Video Codec-Based Formats
Neural/AI-Based Compression
Classical/Specialized Algorithms
Implementation Priority Recommendations
Each format would require:
The modular compressor architecture in Kaira makes it well-suited for adding these formats incrementally based on research needs and community feedback.