diff --git a/src/Modifiers/GrayscaleModifier.php b/src/Modifiers/GrayscaleModifier.php index d795f9a..322930e 100644 --- a/src/Modifiers/GrayscaleModifier.php +++ b/src/Modifiers/GrayscaleModifier.php @@ -5,9 +5,11 @@ namespace Intervention\Image\Drivers\Vips\Modifiers; use Intervention\Image\Exceptions\DriverException; +use Intervention\Image\Exceptions\ModifierException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; use Intervention\Image\Modifiers\GrayscaleModifier as GenericGrayscaleModifier; +use Jcupitt\Vips\Exception as VipsException; use Jcupitt\Vips\Interpretation; class GrayscaleModifier extends GenericGrayscaleModifier implements SpecializedInterface @@ -18,18 +20,34 @@ class GrayscaleModifier extends GenericGrayscaleModifier implements SpecializedI * @see Intervention\Image\Interfaces\ModifierInterface::apply() * * @throws DriverException + * @throws ModifierException */ public function apply(ImageInterface $image): ImageInterface { - // turn image to grayscale - $image->core()->setNative( - $image->core()->native()->colourspace(Interpretation::B_W), - ); - - // return to srgb colorspace with grayscale image - $image->core()->setNative( - $image->core()->native()->colourspace(Interpretation::SRGB), - ); + // grayscale is a color operation and is not meant to move the image to + // another colorspace, so the b-w detour has to end back where it + // started + $interpretation = $image->core()->native()->interpretation; + + try { + $grayscale = $image->core()->native()->colourspace(Interpretation::B_W); + + try { + $native = $grayscale->colourspace($interpretation); + } catch (VipsException) { + // colourspace() takes its target verbatim. Interpretations + // like multiband, matrix or rgb are tags rather than color + // spaces and it has no route back to them, so those images + // stay in srgb the way they did before. + $native = $grayscale->colourspace(Interpretation::SRGB); + } + } catch (VipsException $e) { + throw new ModifierException('Failed to modify image to grayscale', previous: $e); + } + + // the whole conversion is committed at once, a failed restore must not + // leave the caller holding the b-w intermediate + $image->core()->setNative($native); return $image; } diff --git a/tests/Unit/Modifiers/GrayscaleModifierTest.php b/tests/Unit/Modifiers/GrayscaleModifierTest.php index f97a632..46a0ef1 100644 --- a/tests/Unit/Modifiers/GrayscaleModifierTest.php +++ b/tests/Unit/Modifiers/GrayscaleModifierTest.php @@ -4,8 +4,15 @@ namespace Intervention\Image\Drivers\Vips\Tests\Unit\Modifiers; +use Intervention\Image\Colors\Cmyk\Channels\Key; +use Intervention\Image\Colors\Cmyk\Colorspace as CmykColorspace; +use Intervention\Image\Drivers\Vips\Decoders\NativeObjectDecoder; +use Intervention\Image\Drivers\Vips\Driver; use Intervention\Image\Drivers\Vips\Modifiers\GrayscaleModifier; use Intervention\Image\Drivers\Vips\Tests\BaseTestCase; +use Jcupitt\Vips\BandFormat; +use Jcupitt\Vips\Image as VipsImage; +use Jcupitt\Vips\Interpretation; use PHPUnit\Framework\Attributes\CoversClass; #[CoversClass(GrayscaleModifier::class)] @@ -18,4 +25,58 @@ public function testColorChange(): void $image->modify(new GrayscaleModifier()); $this->assertTrue($image->colorAt(0, 0)->isGrayscale()); } + + public function testColorspaceIsPreserved(): void + { + $image = $this->readTestImage('cmyk.jpg'); + $this->assertEquals(Interpretation::CMYK, $image->core()->native()->interpretation); + $this->assertEquals(CmykColorspace::class, $image->colorspace()::class); + + $image->modify(new GrayscaleModifier()); + + // grayscale is a color operation, it has no business moving a cmyk + // source to another colorspace + $this->assertEquals(Interpretation::CMYK, $image->core()->native()->interpretation); + $this->assertEquals(CmykColorspace::class, $image->colorspace()::class); + $this->assertEquals(4, $image->core()->native()->bands); + + // the grey has to sit in the black channel, the three ink channels + // carry no color anymore. The exact key value goes through libvips' + // bundled cmyk profile and the lcms2 it was built against, so it is + // asserted as a range rather than pinned. + $color = $image->colorAt(0, 0); + $this->assertTrue($color->isGrayscale()); + $this->assertGreaterThanOrEqual(35, $color->channel(Key::class)->value()); + $this->assertLessThanOrEqual(45, $color->channel(Key::class)->value()); + } + + public function testInterpretationsWithoutARouteBackFallBackToSrgb(): void + { + // multiband is a tag rather than a colorspace, colourspace() can guess + // it as srgb on the way in but has no route back to it + $native = VipsImage::black(4, 4, ['bands' => 3])->add([200, 30, 90])->cast(BandFormat::UCHAR); + $this->assertEquals(Interpretation::MULTIBAND, $native->interpretation); + + $image = (new Driver())->decodeImage($native, [NativeObjectDecoder::class]); + $image->modify(new GrayscaleModifier()); + + $this->assertEquals(Interpretation::SRGB, $image->core()->native()->interpretation); + $this->assertTrue($image->colorAt(0, 0)->isGrayscale()); + } + + public function testColorspaceIsPreservedForSrgb(): void + { + // pinning test, the srgb path already behaved this way, it guards the + // common case against the change made for cmyk + $image = $this->readTestImage('trim.png'); + $bands = $image->core()->native()->bands; + $this->assertEquals(4, $bands); + + $image->modify(new GrayscaleModifier()); + + // the srgb path is the common one and has to come out unchanged, + // bandcount included + $this->assertEquals(Interpretation::SRGB, $image->core()->native()->interpretation); + $this->assertEquals($bands, $image->core()->native()->bands); + } }