Skip to content

Commit

Permalink
more tests, subImage and GrayscaleRasterImage this time
Browse files Browse the repository at this point in the history
  • Loading branch information
mike42 committed Jun 10, 2018
1 parent 4456f89 commit 1526729
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/unit/BlackAndWhiteRasterImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ public function testRectEmpty()
$this -> assertEquals($expected, $img -> toString());
}

public function testSubImageEmpty()
{
$img = BlackAndWhiteRasterImage::create(5, 5);
$img -> rect(1, 1, 3, 3);
$img = $img -> subImage(1, 1, 3, 3);
$expected = "█▀█\n" .
"▀▀▀\n";
$this -> assertEquals($expected, $img -> toString());
}

public function testRectFilled()
{
$img = BlackAndWhiteRasterImage::create(5, 5);
Expand Down
56 changes: 56 additions & 0 deletions test/unit/GrayscaleRasterImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* Many of these tests use an example image like this-
*╭────╮
*│░░▒▒│ ░░ = 255 ▓▓ = 80
*│▓▓██│ ▒▒ = 160 ██ = 0
*╰────╯
*/
use PHPUnit\Framework\TestCase;
use Mike42\GfxPhp\GrayscaleRasterImage;
use Mike42\GfxPhp\RgbRasterImage;

class GrayscaleRasterImageTest extends TestCase {
protected function createGrayscaleTestImage() {
$image = GrayscaleRasterImage::create(2, 2);
$image -> setPixel(0, 0, 255);
$image -> setPixel(1, 0, 160);
$image -> setPixel(0, 1, 80);
$image -> setPixel(1, 1, 0);
return $image;
}

public function testCreate()
{
$img = $this -> createGrayscaleTestImage();
$this -> assertEquals(2, $img -> getWidth());
$this -> assertEquals(2, $img -> getHeight());
$this -> assertEquals(255, $img -> getMaxVal());
$this -> assertEquals("▄▄\n", $img -> toBlackAndWhite() -> toString());
}


public function testToRgb()
{
$white = RgbRasterImage::rgbToInt(255, 255, 255);
$lightGray = RgbRasterImage::rgbToInt(160, 160, 160);
$darkGray = RgbRasterImage::rgbToInt(80, 80, 80);
$black = RgbRasterImage::rgbToInt(0, 0, 0);
$img = $this -> createGrayscaleTestImage() -> toRgb();
$this -> assertEquals($white, $img -> getPixel(0, 0));
$this -> assertEquals($lightGray, $img -> getPixel(1, 0));
$this -> assertEquals($darkGray, $img -> getPixel(0, 1));
$this -> assertEquals($black, $img -> getPixel(1, 1));
}

public function testToIndexed()
{
// Same raster data is used, with grayscale 'palette'
$img = $this -> createGrayscaleTestImage() -> toIndexed();
$this -> assertEquals(255, $img -> getPixel(0, 0));
$this -> assertEquals(160, $img -> getPixel(1, 0));
$this -> assertEquals(80, $img -> getPixel(0, 1));
$this -> assertEquals(0, $img -> getPixel(1, 1));
}
}

0 comments on commit 1526729

Please sign in to comment.