InkSwatch ColorScan
A Rust crate for analyzing fountain pen ink colors from digital photographs with calibrated color measurement.
Features
- Color Measurement: CIE Lab and LCh color spaces for perceptually uniform analysis
- D65 Calibration: Normalizes colors to D65 illuminant
- Munsell & ISCC-NBS Names: Converts colors to Munsell notation and descriptive color names
- HEIC/HEIF Support: Loads iPhone photos via libheif (optional dependency)
- Multiple Image Formats: Supports JPEG, PNG, TIFF, WebP, HEIC, AVIF, and others
- Smartphone Compatible: Handles varying lighting and camera characteristics
- EXIF Orientation: Automatic image rotation based on EXIF metadata
- White Balance: Paper band sampling for color correction
- JSON Configuration: Pipeline parameters configurable via JSON
- Confidence Scoring: Provides reliability metrics for measurements
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
Library Usage
use ;
use Path;
With Configuration
use ;
use Path;
Command Line Usage
# Analyze a single image
# Analyze with JSON output
# Batch processing with configuration file
# Batch with CSV output
Batch Configuration (config.json):
Note: The CLI tools are development and testing utilities only:
- Dynamically linked to system OpenCV for fast compile times
- Not intended for distribution to end users
- Used for validating crate functionality and iterating on algorithms
For production applications, see the Deployment section below.
How It Works
- Paper Detection: Locates and rectifies the paper/card surface using edge detection
- White Balance: Estimates illuminant from paper region and applies D65 normalization
- Swatch Isolation: Detects ink regions and separates from background
- Color Extraction: Computes representative color using statistical methods
- Color Naming: Converts Lab values to Munsell notation and ISCC-NBS color names
Technical Details
Color Spaces
- Input Processing: sRGB from camera with EXIF-based corrections
- Analysis: CIE Lab for perceptual uniformity and color difference calculations
- Output: Lab, LCh, sRGB, and hex representations
Calibration Standards
- Reference Illuminant: D65 (6504K) standard illuminant
- Color Difference: Uses CIEDE2000 for perceptual color comparisons
- White Balance: Designed for indoor lighting (3000K-6500K range)
Performance
- Processing Time: Typically under 100ms per image on modern hardware
- Format Support: JPEG, PNG, TIFF, WebP, HEIC/HEIF, AVIF, BMP, GIF, ICO, OpenEXR, PNM, QOI, TGA, and others
Requirements
System Dependencies
-
OpenCV: Required for computer vision operations
# macOS # Ubuntu/Debian # Windows # See opencv-rust documentation -
libheif (Optional): Required for HEIC/HEIF image support (iPhone photos)
# macOS # Ubuntu/Debian # Windows # See libheif documentationNote: Without libheif, the crate still supports 20+ other formats including JPEG, PNG, TIFF, and WebP.
Rust Version
- Minimum supported Rust version: 1.70.0
- Uses 2021 edition features
Development Setup
Environment Variables
The project automatically detects OpenCV through pkg-config. No additional environment variables are typically needed.
Compilation Requirements
The following system components are required for building:
-
OpenCV 4.x - Computer vision library
- Tested with OpenCV 4.12.0
- Must be discoverable via
pkg-config --exists opencv4 - Required modules: core, imgproc, imgcodecs, photo
-
libheif (Optional) - HEIC/HEIF image format support
- Required only for iPhone HEIC photo support
- Must be discoverable via
pkg-config --exists libheif - Without this, all other formats still work
-
pkg-config - Build configuration discovery
- Used for OpenCV flags:
pkg-config --cflags --libs opencv4 - Handles include paths and linking automatically
- Used for OpenCV flags:
-
C++ Compiler - For OpenCV Rust bindings
- Clang (preferred) or GCC
- Required for bindgen code generation
Verified Configurations
macOS (Homebrew)
# OpenCV 4.12.0 confirmed working
# libheif 1.19+ for HEIC support
Build Verification
# Test OpenCV detection
&&
# Test compilation
Troubleshooting
If compilation fails with OpenCV errors:
- Verify OpenCV installation:
pkg-config --exists opencv4 - Check modules are available:
pkg-config --libs opencv4 | grep core - Ensure pkg-config is in PATH
- On macOS, verify Homebrew paths are correct
The opencv Rust crate (v0.95.1) uses buildtime-bindgen and should automatically:
- Detect system OpenCV via pkg-config
- Generate appropriate Rust bindings
- Handle linking flags
Image Guidelines
For best results:
- Swatch Size: Ink area should occupy 10-15% of total image
- Background: Place swatch on white or light-colored paper
- Lighting: Use even lighting, avoid harsh shadows
- Focus: Ensure ink area is sharp and well-focused
- Distance: Close enough to see ink texture, far enough to include paper margin
Error Handling
The library provides detailed error information:
match analyze_swatch
Deployment Options
For Library Users (Recommended)
When using inkswatch_colorscan as a library dependency in your application, you have several OpenCV deployment options:
1. Dynamic Linking (Development/Testing)
- Default configuration - links to system-installed OpenCV
- Fast compile times, smaller binaries
- Users must have OpenCV installed on their system
- Current crate configuration
2. Static Linking (Standalone Applications)
[]
= { = "0.95", = ["static"] }
- Embeds OpenCV into your application binary
- Single self-contained executable (~50-100MB)
- No runtime OpenCV dependency required
- Ideal for CLI tools distributed to end users
3. Bundled Libraries (Desktop Applications)
- Ship OpenCV dynamic libraries alongside your application
- Smaller main binary, libraries loaded at runtime
- Common for macOS .app bundles, Windows installers
- Platform-specific packaging required
4. vcpkg Integration (Reproducible Builds)
- Builds OpenCV as part of your build process
- Reproducible across development environments
- opencv-rust has built-in vcpkg support
Recommendation: Document OpenCV as a system dependency and let consuming applications choose their deployment strategy based on their distribution requirements.
Development Status
Current Version: 0.1.0 (Early Development)
This crate is functional but still under active development. The API may change between versions.
Implemented
- Project structure and dependencies
- Core types and error handling
- D65 calibration constants and color spaces
- CLI interface (single and batch processing)
- Image loader with multiple format support
- HEIC/HEIF support (requires libheif)
- EXIF orientation handling
- Paper/swatch detection
- White balance estimation
- Color analysis pipeline
- Munsell notation conversion
- ISCC-NBS color naming
- JSON configuration system
- Unit tests
Known Limitations
- Detection may fail on complex backgrounds or unusual lighting
- White balance assumes neutral paper background
- Not tested with all smartphone camera models
Planned
- Multi-tone shading detection
- API refinement
- Additional camera profile support
Contributing
This project is in early development. Contributions welcome!
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT License (LICENSE-MIT)
at your option.
Research References
- CIE 15:2004 Colorimetry, 3rd edition
- Adobe DNG Specification
- "Color Science" by Wyszecki & Stiles
- CIEDE2000 color difference formula
- Smartphone camera calibration research (2024)
Acknowledgments
This crate uses the following dependencies:
- image - Image decoding
- libheif-rs - HEIC/HEIF support
- munsellspace - Munsell color system and ISCC-NBS color naming
- opencv - Computer vision
- palette - Color space conversions
- empfindung - CIEDE2000 color differences
- serde - Serialization