Repository navigation
Palette mode in the container, encode options (mode/effort), faster decoding - #2
Merged
Merged
Conversation
Encoder:
- phf_encode_memory() and phf_write_file() take phf_encode_options_t
{ mode, effort } (NULL or {0} = AUTO, BEST).
mode: MED (photos, camera, video), PALETTE (graphics, few colours: the
monograph's compression by pixel, already in the NHF3 engine), AUTO.
effort: BEST tries MED and palette up to 4096 colours and both plane
variants (plain/run-length), keeping the smaller; FAST encodes once,
for streaming (AUTO uses palette only up to 256 colours).
- FAST picks the run-length variant only from 60% repeated symbols: the
discarded second pass was 40-65% of encode time on photos and camera.
- Palette files carry flags 0x08/0x09 (no MED/DECORR bits).
Decoder (format unchanged):
- Fix palette mode leaving alpha at 0 when decoding RGB into a 4-byte
stride (images were transparent in the browser).
- Allocate Huffman decoders only for the tables a plane carries instead of
all 259 (618 KB -> typically 50-160 KB per plane).
- Inverse MED runs the three colour planes in one interleaved pass when
OpenMP does not split them (about 2x for that stage).
Measured single-threaded on Apple M3 Pro, main -> this branch:
- MED+FAST: 1080p photo encode 38.8 -> 15.4 ms, decode 30.9 -> 27.2 ms;
sizes within +-2%.
- AUTO+BEST on graphics: 71-76% smaller, 2-3x faster to decode.
Also: SPEC.md section 4 documents the palette mode byte by byte
(validated with an independent decoder written from the text only), CLI
gains --mode=auto|med|palette and --fast and reports the coding mode,
bench.py and README follow the new API, and the test suite grows from 470
to 2686 checks (every mode and effort, mode selection, palette fuzz).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 28, 2026
Closed
libphf
added a commit
that referenced
this pull request
Oct 28, 2026
Palette mode in the container, encode options (mode/effort), faster decoding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Brings the monograph's compression by pixel (the NHF3 palette mode, already in the engine) into the PHF container, lets callers choose between speed and size, and makes decoding faster and lighter. The file format is unchanged except that writers may now emit the palette mode, which this PR documents in
SPEC.md.Encoder options
phf_encode_memory()/phf_write_file()now takeconst phf_encode_options_t *(NULLor{0}=AUTO,BEST):PHF_EFFORT_BEST(files)PHF_EFFORT_FAST(streaming)PHF_MODE_MEDPHF_MODE_PALETTEPHF_MODE_AUTOStreaming applications are expected to fix the mode up front (webcam/video → MED, screen content → palette). CLI:
--mode=auto|med|palette,--fast;inforeports the coding mode.Decoder (no format change)
Measurements
Single thread, Apple M3 Pro,
main→ this branch:MED+FAST sizes stay within ±2 % of
main. BEST encodes more variants, so it is slower than before (still well below PNG level 9).Spec
SPEC.mdsection 4 describes the palette stream byte by byte; flags0x08/0x09mark palette files. An independent decoder written only from the text reproduced 12 encoder outputs bit-exactly (RGB/RGBA, 1–300 colours, with and without runs, both efforts).Breaking changes
optionsparameter onphf_encode_memory()andphf_write_file();nhf3_encode()/nhf3_plane_encode()gain aneffortargument.apps/web/phf.wasmmust be rebuilt for the alpha fix before palette files are served to the web viewer.phf-stream-webcam/phf-stream-videocall the old signature; they keep working with their bundled library and will be updated to{ MED, FAST }in follow-up PRs.Test plan
sample.phf.🤖 Generated with Claude Code