Skip to content

Commit

Permalink
Fixed image comparison testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mazinsw committed Jul 23, 2018
1 parent 5758c70 commit 7363688
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 28 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"ext-mbstring": "*"
},
"require-dev": {
"ext-imagick": "*",
"phpunit/phpunit": "5.*",
"scrutinizer/ocular": "@stable",
"squizlabs/php_codesniffer": "@stable"
"squizlabs/php_codesniffer": "@stable",
"lupka/phpunit-compare-images": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions src/Thermal/Graphics/Filter/BayerOrdered.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ class BayerOrdered implements Filter
* Convert an image resource to black and white aplying dither.
* The original image resource will not be changed, a new image resource will be created.
*
* @param ImageResource $image The source image resource
* @return ImageResource The black and white image resource
* @param \resource $image The source image resource
* @return \resource The black and white image resource
*/
public function process($image)
{
$width = imagesx($image);
$height = imagesy($image);
$new_image = imagecreatetruecolor($width, $height);
// sets background to black
$black = imagecolorallocate($new_image, 0, 0, 0);
imagecolorallocate($new_image, 0, 0, 0);
$white = imagecolorallocate($new_image, 255, 255, 255);
$pattern = [
[ 15, 195, 60, 240],
Expand Down
4 changes: 2 additions & 2 deletions src/Thermal/Graphics/Filter/FloydSteinberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class FloydSteinberg implements Filter
* Convert an image resource to black and white aplying dither.
* The original image resource will not be changed, a new image resource will be created.
*
* @param ImageResource $image The source image resource
* @return ImageResource The black and white image resource
* @param \resource $image The source image resource
* @return \resource The black and white image resource
*/
public function process($image)
{
Expand Down
23 changes: 9 additions & 14 deletions src/Thermal/Graphics/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Thermal\Graphics;

use Thermal\Printer;
use Thermal\Buffer\Encoding;
use Thermal\Connection\Connection;
use Thermal\Graphics\Filter\FloydSteinberg;

class Image
Expand Down Expand Up @@ -49,8 +46,7 @@ public function bytesPerRow()
*
* @param string $filename The filename to load from
* @param Filter $filter filter process
* @throws Exception if the image format is not supported,
* or the file cannot be opened.
* @throws \Exception if the image format is not supported or the file cannot be opened.
*/
protected function loadImage($filename, $filter)
{
Expand Down Expand Up @@ -80,10 +76,9 @@ protected function loadImage($filename, $filter)
/**
* Load an image from disk, into memory, using GD.
*
* @param string $filename The filename to load from
* @param array $data Info array with name and data keys
* @param Filter $filter filter process
* @throws Exception if the image format is not supported,
* or the file cannot be opened.
* @throws \Exception if the image format is not supported or the file cannot be opened.
*/
protected function loadImageData($data, $filter)
{
Expand All @@ -100,7 +95,7 @@ protected function loadImageData($data, $filter)
/**
* Load actual image pixels from GD resource.
*
* @param resource $image GD resource to use
* @param \resource $image GD resource to use
*/
private function readImage($image)
{
Expand All @@ -114,17 +109,17 @@ private function readImage($image)
$this->lines = (int)($height / $bits);
$pos = 0;
$data = str_repeat("\x00", $width * $height / 8);
for ($by = 0; $by < $img_height; $by += $bits) {
for ($x = 0; $x < $width; $x++) {
for ($offset_y = 0; $offset_y < $img_height; $offset_y += $bits) {
for ($img_x = 0; $img_x < $width; $img_x++) {
// loop slices
for ($s = 0; $s < $slices; $s++) {
$slice = 0b00000000;
for ($bit = 0; $bit < 8; $bit++) {
$y = $by + $s * 8 + $bit;
if ($y >= $img_height) {
$img_y = $offset_y + $s * 8 + $bit;
if ($img_y >= $img_height) {
break;
}
$color = imagecolorat($image, $x, $y);
$color = imagecolorat($image, $img_x, $img_y);
$alpha = ($color >> 24) & 0xFF;
$red = ($color >> 16) & 0xFF;
$green = ($color >> 8) & 0xFF;
Expand Down
13 changes: 9 additions & 4 deletions tests/Thermal/Graphics/Filter/BayerOrderedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Thermal\Graphics\Filter;

use Thermal\PrinterTest;
use Lupka\PHPUnitCompareImages\CompareImagesTrait;

class BayerOrderedTest extends \PHPUnit_Framework_TestCase
{
use CompareImagesTrait;

public function testProcess()
{
$image_path = dirname(dirname(dirname(__DIR__))) . '/resources/sample.jpg';
Expand All @@ -18,9 +21,11 @@ public function testProcess()
$image_data = ob_get_contents();
ob_end_clean();
imagedestroy($new_image);
$this->assertEquals(
PrinterTest::getExpectedBuffer('sample_bayer_ordered.png', $image_data),
$image_data
);
$result_image = new \Imagick();
$result_image->readImageBlob($image_data);
$expected_data = PrinterTest::getExpectedBuffer('sample_bayer_ordered.png', $image_data);
$expected_image = new \Imagick();
$expected_image->readImageBlob($expected_data);
$this->assertImagesSame($expected_image, $result_image);
}
}
13 changes: 9 additions & 4 deletions tests/Thermal/Graphics/Filter/FloydSteinbergTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Thermal\Graphics\Filter;

use Thermal\PrinterTest;
use Lupka\PHPUnitCompareImages\CompareImagesTrait;

class FloydSteinbergTest extends \PHPUnit_Framework_TestCase
{
use CompareImagesTrait;

public function testProcess()
{
$image_path = dirname(dirname(dirname(__DIR__))) . '/resources/sample.jpg';
Expand All @@ -18,9 +21,11 @@ public function testProcess()
$image_data = ob_get_contents();
ob_end_clean();
imagedestroy($new_image);
$this->assertEquals(
PrinterTest::getExpectedBuffer('sample_floyd_steinberg.png', $image_data),
$image_data
);
$result_image = new \Imagick();
$result_image->readImageBlob($image_data);
$expected_data = PrinterTest::getExpectedBuffer('sample_floyd_steinberg.png', $image_data);
$expected_image = new \Imagick();
$expected_image->readImageBlob($expected_data);
$this->assertImagesSame($expected_image, $result_image);
}
}

0 comments on commit 7363688

Please sign in to comment.