Skip to content

fix(modifiers): rotate 16-bit sources at non-quarter angles - #119

Merged
olivervogel merged 1 commit into
Intervention:developfrom
nlemoine:fix/rotate-modifier-16bit-background
Aug 21, 2026
Merged

olivervogel merged 1 commit into
Intervention:developfrom
nlemoine:fix/rotate-modifier-16bit-background

Conversation

@nlemoine

Copy link
Copy Markdown
Contributor

Rotating a 16-bit PNG or TIFF at anything other than 0/90/180/270 throws.

Repro:

use Intervention\Image\Drivers\Vips\Driver;
use Intervention\Image\ImageManager;
use Jcupitt\Vips\Image as VipsImage;

$png = VipsImage::black(32, 32, ['bands' => 3])
    ->add(30000)
    ->cast('ushort')
    ->copy(['interpretation' => 'rgb16'])
    ->writeToBuffer('.png', ['bitdepth' => 16]);

ImageManager::usingDriver(Driver::class)->decodeBinary($png)->rotate(10);
ModifierException: Failed to apply ...\Vips\Modifiers\RotateModifier, unable to rotate image
Caused by: Jcupitt\Vips\Exception: libvips error: linear: vector must have 1 or 4 elements

Repro'd on libvips 8.18.1 (the version in the repo Dockerfile) and on 8.18.5 locally, with intervention/image 4.2.1.

What happens: apply() exports the background color against the image, which has three bands, so ColorProcessor::export() truncates the vector to three elements. rotate() then calls bandjoin_const(255) and hands that stale three-element vector to similarity() on a now four-band image.

8-bit sources never get there, because NativeObjectDecoder bandjoins them to four bands on decode, but only when the interpretation is srgb. A 16-bit source loads as rgb16 and keeps its three bands. Quarter turns are fine, they take the rot90/rot180/rot270 path and paint no background at all.

What I measured before the patch:

source decoded rotate(90) rotate(10)
PNG 8-bit RGB 4 / uchar / srgb ok ok
PNG 8-bit RGBA 4 / uchar / srgb ok ok
PNG 8-bit grey 4 / uchar / srgb ok ok
PNG 16-bit RGB 3 / ushort / rgb16 ok throws
PNG 16-bit RGBA 4 / ushort / rgb16 ok ok
PNG 16-bit grey 3 / ushort / rgb16 ok throws
TIFF 16-bit RGB 3 / ushort / rgb16 ok throws

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) becomes addalpha(). On a ushort image 255 is about 0.4% opacity, so fixing only the vector length would have produced a nearly invisible image.
  • the exported color gets scaled to the value range of the 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 in addalpha() (65535 for rgb16 and grey16, 1 for scrgb), 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)):

background 8-bit 16-bit
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. FillModifier and ContainModifier paint 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 in ColorProcessor itself, 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.

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.
@olivervogel
olivervogel merged commit 5e2fece into Intervention:develop Aug 21, 2026
6 checks passed
@olivervogel

Copy link
Copy Markdown
Member

Thanks @nlemoine

@nlemoine

Copy link
Copy Markdown
Contributor Author

Thanks @olivervogel!

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