Repository navigation
fix(modifiers): rotate 16-bit sources at non-quarter angles - #119
Merged
olivervogel merged 1 commit intoAug 21, 2026
Merged
olivervogel merged 1 commit into
olivervogel merged 1 commit into
Conversation
RotateModifier exports the background color against the image, then adds an alpha band to that image before handing both to similarity(). The exported vector is sized to the bandcount as it was before the band was added, so a three-band source produces a three-element background for a four-band image and libvips throws `linear: vector must have 1 or 4 elements`. Sources with an SRGB interpretation never reach that state, because NativeObjectDecoder normalizes them to four bands on decode. 16-bit PNG and TIFF sources load as RGB16 and keep three bands, so every non-quarter rotation of one throws. Quarter turns are unaffected, they take the rot90/rot180/rot270 path and paint no background at all. Add the alpha band before the color is exported, so the exported vector matches the bandcount similarity() sees, and so the alpha channel of the background color is kept rather than truncated away. Use addalpha() instead of bandjoin_const(255): it picks the opaque value that goes with the interpretation, where a hardcoded 255 leaves a ushort image at 0.4% opacity. Scale the exported color to the value range of the image for the same reason, 0-255 on a 16-bit image paints near black instead of white. Adds three regression tests: an arbitrary angle, an arbitrary angle with a translucent background, and a quarter turn that must not gain an alpha band.
Member
|
Thanks @nlemoine |
Contributor
Author
|
Thanks @olivervogel! |
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.
Rotating a 16-bit PNG or TIFF at anything other than 0/90/180/270 throws.
Repro:
Repro'd on libvips 8.18.1 (the version in the repo
Dockerfile) and on 8.18.5 locally, withintervention/image4.2.1.What happens:
apply()exports the background color against the image, which has three bands, soColorProcessor::export()truncates the vector to three elements.rotate()then callsbandjoin_const(255)and hands that stale three-element vector tosimilarity()on a now four-band image.8-bit sources never get there, because
NativeObjectDecoderbandjoins them to four bands on decode, but only when the interpretation issrgb. A 16-bit source loads asrgb16and keeps its three bands. Quarter turns are fine, they take therot90/rot180/rot270path and paint no background at all.What I measured before the patch:
rotate(90)rotate(10)The fix adds the alpha band before the color is exported, so the exported vector is sized to what
similarity()actually sees. It also keeps the alpha channel of the background color, which the truncation was dropping. Two other things had to come with it:bandjoin_const(255)becomesaddalpha(). On aushortimage 255 is about 0.4% opacity, so fixing only the vector length would have produced a nearly invisible image.export()always returns 0-255, and 255 on a 16-bit image paints near black instead of white. I used the same ranges libvips uses to pick an opaque alpha inaddalpha()(65535 forrgb16andgrey16, 1 forscrgb), so the added band and the background agree.After the patch a 16-bit rotation matches the 8-bit one, translucent backgrounds included (corner pixel exposed by
rotate(45)):fff[255,255,255,255][65535,65535,65535,65535]ffffff00[0,0,0,0][0,0,0,0]ff000080[255,0,0,128][65535,0,0,32896]Three regression tests added: an arbitrary angle, an arbitrary angle with a translucent background, and a quarter turn that must not gain an alpha band. Full suite, phpstan and phpcs all pass in the Docker setup.
One thing I deliberately left out: the 0-255 scale in
ColorProcessor::export()is not specific to rotation.FillModifierandContainModifierpaint near black on 16-bit sources for the same reason,fill('ff0000')gives[255,0,0]where the image range is 0-65535. My guess is it should be fixed inColorProcessoritself, but that touches every modifier, so I kept it out of here. Happy to open a separate issue or PR for it if you want.