Skip to content

Commit

Permalink
Merge pull request #34 from mike42/feature/17-read-gif
Browse files Browse the repository at this point in the history
Correct some GIF errors
  • Loading branch information
mike42 authored Feb 23, 2019
2 parents 0fcbc54 + 6d5c679 commit b1a1377
Show file tree
Hide file tree
Showing 23 changed files with 119 additions and 12 deletions.
16 changes: 12 additions & 4 deletions src/Mike42/GfxPhp/Codec/Gif/GifGraphicsBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ public static function fromBin(DataInputStream $in) : GifGraphicsBlock
$blockId = $peek[0];
$extensionId = $peek[1];
}
if ($blockId == GifData::GIF_EXTENSION && $extensionId == GifData::GIF_EXTENSION_APPLICATION) {
// ImageMagick drops an 'application' block here, which we can discard.
// We would need a slight re-structure to record this correctly.
$application = GifApplicationExt::fromBin($in);
while ($blockId == GifData::GIF_EXTENSION && $extensionId != GifData::GIF_EXTENSION_PLAINTEXT) {
// We would need a slight re-structure to record these blocks correctly.
if ($extensionId == GifData::GIF_EXTENSION_APPLICATION) {
// ImageMagick drops an 'application' block here, which we can discard (gfx-php does not use it at this stage)
GifApplicationExt::fromBin($in);
} else if ($extensionId == GifData::GIF_EXTENSION_COMMENT) {
// Also GIMP places a 'comment' block here.
GifCommentExt::fromBin($in);
} else {
// And who knows what else we will have to skip. This should cover it anyway.
GifUnknownExt::fromBin($in);
}
$peek = $in -> peek(2);
$blockId = $peek[0];
$extensionId = $peek[1];
Expand Down
2 changes: 1 addition & 1 deletion src/Mike42/GfxPhp/Codec/GifCodec.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function encode(RasterImage $image, string $format): string
// Transparent color for graphic control
$transparentColorFlag = 0x00;
$transparentColor = 0;
if ($image -> getTransparentColor() !== null) {
if ($image -> getTransparentColor() >= 0) {
$transparentColor = $image -> getTransparentColor() & 0xFF;
$transparentColorFlag = 0x01;
}
Expand Down
8 changes: 6 additions & 2 deletions src/Mike42/GfxPhp/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ class Image

public static function fromFile(string $filename) : RasterImage
{
$blob = file_get_contents($filename);
// Attempt to catch the cause of any errors
error_clear_last();
$blob = @file_get_contents($filename);
if ($blob === false) {
throw new \Exception("Could not retrieve image data from '$filename'. Check that the file exists and can be read.");
$e = error_get_last();
$error = (isset($e) && isset($e['message']) && $e['message'] != "") ? $e['message'] : "Check that the file exists and can be read.";
throw new \Exception("Could not retrieve image data from '$filename'. $error");
}
return self::fromBlob($blob, $filename);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Mike42/GfxPhp/IndexedRasterImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class IndexedRasterImage extends AbstractRasterImage

protected $palette;

protected $transparentColor = null;
protected $transparentColor = -1;

protected function __construct($width, $height, array $data, int $maxVal, array $palette)
{
Expand Down Expand Up @@ -127,7 +127,7 @@ public function toIndexed(): IndexedRasterImage

public function indexToRgb(int $index)
{
if ($index == $this -> transparentColor) {
if ($index === $this -> transparentColor) {
// White
return [255, 255, 255];
}
Expand All @@ -150,12 +150,12 @@ public function rgbToIndex(array $rgb)
return 0;
}

public function getTransparentColor()
public function getTransparentColor() : int
{
return $this -> transparentColor;
}

public function setTransparentColor(int $color = null)
public function setTransparentColor(int $color)
{
$this -> transparentColor = $color;
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/PngsuiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Simple check that all files in pngsuite are correctly accepted or rejected, and contain
* correct width.
*/
class Pngsuite extends TestCase
class PngsuiteTest extends TestCase
{
function loadImage(string $filename) {
return Image::fromFile(__DIR__ . "/../resources/pngsuite/$filename");
Expand Down
77 changes: 77 additions & 0 deletions test/integration/SmallFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

use Mike42\GfxPhp\Image;
use PHPUnit\Framework\TestCase;

class SmallFileTest extends TestCase
{
function loadImage(string $filename) {
$img = Image::fromFile(__DIR__ . "/../resources/small-files/$filename");
return $img -> toBlackAndWhite();
}

function testWhite() {
$expected = " \n";
$width = 1;
$height = 1;
foreach(['canvas_white.gif', 'canvas_white.png'] as $fn) { // Also jpg and bmp
$img = $this -> loadImage($fn);
$this -> assertEquals($expected, $img -> toString());
$this -> assertEquals($height, $img -> getHeight());
$this -> assertEquals($width, $img -> getWidth());
}
}

function testBlack() {
$expected = "\n";
$width = 1;
$height = 1;
foreach(['canvas_black.gif', 'canvas_black.png'] as $fn) { // Also jpg and bmp
$img = $this -> loadImage($fn);
$this -> assertEquals($expected, $img -> toString());
$this -> assertEquals($height, $img -> getHeight());
$this -> assertEquals($width, $img -> getWidth());
}
}

function testBlackWhite() {
$expected = "▀▀\n";
$width = 2;
$height = 2;
foreach(['black_white.gif', 'black_white.png'] as $fn) { // Also jpg and bmp
$img = $this -> loadImage($fn);
$this -> assertEquals($expected, $img -> toString());
$this -> assertEquals($height, $img -> getHeight());
$this -> assertEquals($width, $img -> getWidth());
}
}

function testBlackTransparent() {
$expected = "▀▀\n";
$width = 2;
$height = 2;
foreach(['black_transparent.gif', 'black_transparent.png'] as $fn) { // Also jpg and bmp
$img = $this -> loadImage($fn);
$this -> assertEquals($expected, $img -> toString());
$this -> assertEquals($height, $img -> getHeight());
$this -> assertEquals($width, $img -> getWidth());
}
}

function testBlackWhiteTall() {
$expected = "██\n" .
"██\n" .
"██\n" .
"██\n" .
" \n" .
" \n" .
" \n" .
" \n";
$width = 2;
$height = 16;
$img = $this -> loadImage('black_white_tall.png');
$this -> assertEquals($expected, $img -> toString());
$this -> assertEquals($height, $img -> getHeight());
$this -> assertEquals($width, $img -> getWidth());
}
}
6 changes: 6 additions & 0 deletions test/resources/small-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Small files

These test files were produded for `escpos-php`.

The same file should have the same representation in black & white from any source format.

Binary file added test/resources/small-files/black_transparent.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/resources/small-files/black_transparent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/resources/small-files/black_white.bmp
Binary file not shown.
Binary file added test/resources/small-files/black_white.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/resources/small-files/black_white.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/resources/small-files/black_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/resources/small-files/black_white_tall.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/resources/small-files/canvas_black.bmp
Binary file not shown.
Binary file added test/resources/small-files/canvas_black.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/resources/small-files/canvas_black.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/resources/small-files/canvas_black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/resources/small-files/canvas_white.bmp
Binary file not shown.
Binary file added test/resources/small-files/canvas_white.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/resources/small-files/canvas_white.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/resources/small-files/canvas_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions test/unit/ImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Mike42\GfxPhp\Image;
use PHPUnit\Framework\TestCase;

class ImageTest extends TestCase
{
public function testBadFilename() {
$this -> expectException(Exception::class);
Image::fromFile("not a real file");
}
}

0 comments on commit b1a1377

Please sign in to comment.