Skip to content

Conversation

@Eta0
Copy link

@Eta0 Eta0 commented Nov 23, 2025

AVIF Grayscale Handling

This change fixes an issue I noticed when testing my other fix for #277. The AVIF encoder currently mishandles single-channel (monochrome) input data, accidentally tiling it 3x horizontally in the resulting image.

For instance, this 300x300 source image:

A white square with a black rectangle 1/3 the width and 2/3 the height of the image in the top left corner.

Would transcode to this output image using the transcoder example program (invoked with example -input grayscale.png -output grayscale.avif -width 300 -height 300 -stretch):

Three tall white rectangles fit into a square, each with a narrow black rectangle in their upper-left corners, and thin black vertical lines separating the white rectangles. The image is mostly black and white, but features some chromatic aberration, adding small fringes with higher chroma.

(Converted back to a PNG here for previewing compatibility.)

To fix this, this change skips RGB to YUV conversion for single-channel images during the AVIF encoding process, since they aren't RGB anyway. Instead, for grayscale source data, it sets the AVIF output format to YUV400 (i.e. luminance only with no subsambling), and points its Y plane directly at the grayscale data. This requires no conversions, so it has perfect accuracy, requires no memory allocations, and runs quite a bit faster than before in addition to being more correct. Hooray for a win-win!

@salarkhan
Copy link
Contributor

hey! thanks so much for the PRs this week. just calling out that we've seen em and will be taking a look after the thanksgiving holidays.

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