Skip to content

Commit

Permalink
Remove collecting of original color palette status in Origin::class
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Jul 14, 2024
1 parent 52f3d0d commit 51f23b2
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 109 deletions.
9 changes: 1 addition & 8 deletions src/Drivers/Gd/Decoders/NativeObjectDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,17 @@ public function decode(mixed $input): ImageInterface|ColorInterface

if (!imageistruecolor($input)) {
imagepalettetotruecolor($input);
$indexed = true;
}

imagesavealpha($input, true);

// build image instance
$image = new Image(
return new Image(
$this->driver(),
new Core([
new Frame($input)
])
);

// set indexed color palette status of original
$image->origin()->setIndexed($indexed ?? false);

return $image;
}

/**
Expand Down Expand Up @@ -106,7 +100,6 @@ protected function decodeGif(mixed $input): ImageInterface

// set media type
$image->origin()->setMediaType('image/gif');
$image->origin()->setIndexed(true);

return $image;
}
Expand Down
4 changes: 0 additions & 4 deletions src/Drivers/Gd/Encoders/PngEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ private function maybeToPalette(ImageInterface $image): GdImage
return $image->core()->native();
}

if (is_null($this->indexed) && !$image->origin()->isIndexed()) {
return $image->core()->native();
}

return $image->reduceColors(256)->core()->native();
}
}
38 changes: 0 additions & 38 deletions src/Drivers/Imagick/Decoders/NativeObjectDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public function decode(mixed $input): ImageInterface|ColorInterface
throw new DecoderException('Unable to decode input');
}

// get original indexed palette status for origin
if ($this->isPaletteImage($input)) {
$indexed = true;
}

// For some JPEG formats, the "coalesceImages()" call leads to an image
// completely filled with background color. The logic behind this is
// incomprehensible for me; could be an imagick bug.
Expand All @@ -57,40 +52,7 @@ public function decode(mixed $input): ImageInterface|ColorInterface

// set media type & palette status on origin
$image->origin()->setMediaType($input->getImageMimeType());
$image->origin()->setIndexed($indexed ?? false);

return $image;
}

/**
* Determine if given imagick instance is a indexed palette color image
*
* @param Imagick $imagick
* @return bool
*/
private function isPaletteImage(Imagick $imagick): bool
{
// Palette PNG files with alpha channel result incorrectly in truecolor with Alpha
// in imagemagick 6. This issue makes in impossible to rely on Imagick::getImageType().
// This workaround looks at the the PNG data directly to decode the color type byte.

// detect imagick major version
$imagickVersion = dechex(Imagick::getVersion()['versionNumber']);
$imagickVersion = substr($imagickVersion, 0, 1);

// detect palette status manually in imagemagick 6
if (version_compare($imagickVersion, '6', '<=') && $imagick->getImageFormat() === 'PNG') {
$data = $imagick->getImageBlob();
$pos = strpos($data, 'IHDR');
$type = substr($data, $pos + 13, 1);
$type = unpack('C', $type)[1];

return $type === 3; // color type 3 is a PNG with indexed palette
}

return in_array(
$imagick->getImageType(),
[Imagick::IMGTYPE_PALETTE, Imagick::IMGTYPE_PALETTEMATTE],
);
}
}
27 changes: 8 additions & 19 deletions src/Drivers/Imagick/Encoders/PngEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
use Intervention\Image\Encoders\PngEncoder as GenericPngEncoder;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Origin;

class PngEncoder extends GenericPngEncoder implements SpecializedInterface
{
public function encode(ImageInterface $image): EncodedImage
{
$imagick = clone $image->core()->native();
$imagick = $this->setFormat($imagick, $image->origin());
$imagick = $this->setFormat($imagick);
$imagick = $this->setCompression($imagick);
$imagick = $this->setInterlaced($imagick);

Expand All @@ -43,27 +42,17 @@ private function setCompression(Imagick $imagick): Imagick
* Set format according to encoder settings on imagick output
*
* @param Imagick $imagick
* @param Origin $origin
* @throws ImagickException
* @return Imagick
*/
private function setFormat(Imagick $imagick, Origin $origin): Imagick
private function setFormat(Imagick $imagick): Imagick
{
switch (true) {
case $this->indexed === false:
$imagick->setFormat('PNG32');
$imagick->setImageFormat('PNG32');
break;

case $this->indexed === true:
$imagick->setFormat('PNG8');
$imagick->setImageFormat('PNG8');
break;

default:
$imagick->setFormat($origin->isIndexed() ? 'PNG8' : 'PNG32');
$imagick->setImageFormat($origin->isIndexed() ? 'PNG8' : 'PNG32');
break;
if ($this->indexed) {
$imagick->setFormat('PNG8');
$imagick->setImageFormat('PNG8');
} else {
$imagick->setFormat('PNG32');
$imagick->setImageFormat('PNG32');
}

return $imagick;
Expand Down
2 changes: 1 addition & 1 deletion src/Encoders/PngEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class PngEncoder extends SpecializableEncoder
{
public function __construct(public bool $interlaced = false, public ?bool $indexed = null)
public function __construct(public bool $interlaced = false, public bool $indexed = false)
{
}
}
27 changes: 1 addition & 26 deletions src/Origin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ class Origin
*
* @param string $mediaType
* @param null|string $filePath
* @param bool $indexed
* @return void
*/
public function __construct(
protected string $mediaType = 'application/octet-stream',
protected ?string $filePath = null,
protected bool $indexed = false
protected ?string $filePath = null
) {
}

Expand Down Expand Up @@ -87,27 +85,4 @@ public function fileExtension(): ?string
{
return empty($this->filePath) ? null : pathinfo($this->filePath, PATHINFO_EXTENSION);
}

/**
* Determine if current instance containing indices into a palette of colors
*
* @return bool
*/
public function isIndexed(): bool
{
return $this->indexed;
}

/**
* Set indexed state of origin
*
* @param bool $state
* @return Origin
*/
public function setIndexed(bool $state): self
{
$this->indexed = $state;

return $this;
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Drivers/Gd/Encoders/PngEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public function testEncoderInitialFormat(): void

$image = $this->readTestImage('tile.png'); // indexed with alpha
$result = (new PngEncoder())->encode($image);
$this->assertEquals('indexed', $this->pngColorType((string) $result));
$this->assertEquals('truecolor-alpha', $this->pngColorType((string) $result));

$image = $this->readTestImage('indexed.png'); // indexed
$result = (new PngEncoder())->encode($image);
$this->assertEquals('indexed', $this->pngColorType((string) $result));
$this->assertEquals('truecolor-alpha', $this->pngColorType((string) $result));
}

public function testEncoderTransformFormat(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Drivers/Imagick/Encoders/PngEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public function testEncoderInitialFormat(): void

$image = $this->readTestImage('tile.png'); // indexed with alpha
$result = (new PngEncoder())->encode($image);
$this->assertEquals('indexed', $this->pngColorType((string) $result));
$this->assertEquals('truecolor-alpha', $this->pngColorType((string) $result));

$image = $this->readTestImage('indexed.png'); // indexed
$result = (new PngEncoder())->encode($image);
$this->assertEquals('indexed', $this->pngColorType((string) $result));
$this->assertEquals('truecolor-alpha', $this->pngColorType((string) $result));
}

public function testEncoderTransformFormat(): void
Expand Down
9 changes: 0 additions & 9 deletions tests/Unit/OriginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,4 @@ public function testSetGetMediaType(): void
$this->assertEquals('image/jpeg', $origin->mediaType());
$this->assertEquals('image/jpeg', $result->mediaType());
}

public function testSetGetIndexed(): void
{
$origin = new Origin();
$this->assertFalse($origin->isIndexed());
$result = $origin->setIndexed(true);
$this->assertTrue($origin->isIndexed());
$this->assertTrue($result->isIndexed());
}
}

0 comments on commit 51f23b2

Please sign in to comment.