How lclevy/libcraw2 works
Project Type & Purpose
• This is a mixed-language C/Python framework (“CRaw2”) for parsing, manipulating and rendering Canon CR2 (Raw v2) files.
• It comprises three primary deliverables:
– libcraw2: a native C library implementing CR2 file parsing, TIFF/JPEG handling, pixel-level operations.
– craw2tool: a CLI binary (in C) exposing libcraw2’s API for shell‐based workflows.
– PyCraw2: a Python extension module (C under the hood, built via setup.py) plus pure-Python utilities (in the python/ folder) that wrap libcraw2 and offer NumPy-driven image processing.
File Structure Analysis
Top‐level:
• Makefile, setup/config scripts (src/setup.py), VS2015 solution for Windows build
• Shell scripts (cr2_database.sh, dng_info.sh, unknow_tags.sh) to generate camera-specific data files
• python/ – pure-Python demos & tools (cr2_analysis.py, thumbnails.py, PyCanonRaw2.py, etc.)
• src/ – C source & headers for libcraw2, craw2tool, yuv, lljpeg, tiff modules; setup.py glue for PyCraw2
• pics/ – sample CR2 inputs
Key README Insights
• Layered design: core C library → C CLI → Python extension → Python tooling
• Supports TIFF tag introspection, lossless JPEG operations, RGGB and YCbCr data export/import, 16-bit TIFF output
• Camera-specific data is externalized (cr2_database.txt, dng_info.txt) – keeps core code camera-agnostic
Diagramming the Architecture
• libcraw2 Core (C library)
• craw2tool CLI (C binary)
• PyCraw2 Extension (built from C code, loaded into Python)
• Python Utilities (pure-Python modules under python/)
• Shell Scripts (camera data generation)
• Data Repositories (cr2_database.txt, dng_info.txt, sample .CR2)
• Build System (Makefile, setup.py, VS2015 proj)
• CLI & Python modules → link/load → libcraw2
• Shell scripts → generate → camera data files
• Input: .CR2 files (pics/) → processed by libcraw2 via either CLI or PyCraw2 → Outputs: TIFF/CSV/console logs
• PyCraw2 → interacts with NumPy for array operations → delivers rendered RGB arrays
c. Architectural Patterns & Principles:
• Layering: Core (performance‐critical C) vs. Bindings (extension) vs. High-level scripting
• Separation of concerns:
– Parsing & pixel ops in libcraw2
– User interaction via CLI or Python
– Camera metadata kept external for flexibility
• Modularity: distinct modules for JPEG, TIFF, YUV, RGB pipelines
d. Key Technologies & Frameworks:
• C99, Make & autotools (Makefile), VS2015 projects
• Python 3.6, setup.py for extension build
• NumPy (for Python rendering)
• Standard TIFF/JPEG libs & custom “lljpeg” for lossless operations