Skip to content

Recognize Ycbcr/Chroma subsampling tags#319

Merged
197g merged 4 commits intomainfrom
ycbcr-subsampling
Jan 5, 2026
Merged

Recognize Ycbcr/Chroma subsampling tags#319
197g merged 4 commits intomainfrom
ycbcr-subsampling

Conversation

@197g
Copy link
Member

@197g 197g commented Jan 5, 2026

Note that in the decoder we error late if the information in the tag has at least the right type and is plausible. We only have a problem supporting the buffer layout that would be calculated, iterating over the image itself leaves us in a fully consistent state otherwise.

Also fixes a bug in the encoder where we apparently already have support. The default value for the tag, i.e. its interpretation if absent, is (2, 2), which implies that some form of subsampling should be used. Since the samples we encode are not subsampled (we explicitly enforce the sample count of a 4:4:4 layout) the tag must be present and specify this. Here's two images that show the difference:

ycbcr-correct.tiff
ycbcr-wrong.tiff

Note that the intended data is a full 256×256 grid going from R,G=0 to R,G=255 in X and Y orientation respectively with blue being the middle point between the other colors. That is, the top-left corner must be full black and the bottom-right must be full white. For the second image the samples are interpreted incorrectly and we get a mash. The first image is the state after this PR.

197g added 4 commits January 5, 2026 21:14
In particular we avoid mistaking the absence of this tag for an absence
of subsampling. The specification instead requires it to mean half the
samples in both directions—a mode that we do not support yet. Instead of
returning erroneous samples or an error in the data stream (which would
appear too short) we return an error when asked to layout the read
buffer for these images.
I didn't realize we support this in a slightly weird manner where the
tag is completely ignored and zune-jpeg performs the upsampling as
indicated in the JPEG stream. This is a recurring problem with the
compression scheme and tags in TIFF. In the end, decoder can only handle
the data as indicated by the JPEG steam and while the specification
requires the tags to be consistent with the underlying data, sometimes
they are not. The useful fallback is of course to ignore the tag.

Ignoring the tag of course buts us in a weird spot for zero-copy
decoding since the buffer indication to the caller will be incorrect.
For now, maybe the best way of treating subsampling is to upsample all
the cases when we add support.
@197g 197g merged commit a3ab252 into main Jan 5, 2026
15 checks passed
@197g 197g deleted the ycbcr-subsampling branch January 5, 2026 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants