Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/Modifiers/GrayscaleModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
61 changes: 61 additions & 0 deletions tests/Unit/Modifiers/GrayscaleModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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);
}
}