IndexedRasterImageTest.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * Many of these tests use an example image like this-
  4. *╭────╮
  5. *│░░▒▒│ ░░ = 0 ▓▓ = 2
  6. *│▓▓██│ ▒▒ = 1 ██ = 3
  7. *╰────╯
  8. */
  9. use PHPUnit\Framework\TestCase;
  10. use Mike42\GfxPhp\Image;
  11. use Mike42\GfxPhp\IndexedRasterImage;
  12. use Mike42\GfxPhp\RgbRasterImage;
  13. class IndexedRasterImageTest extends TestCase
  14. {
  15. protected function createIndexedTestImage() {
  16. $image = IndexedRasterImage::create(2, 2, null, [
  17. [255, 255, 255],
  18. [160, 160, 160],
  19. [80, 80, 80],
  20. [0, 0, 0]]);
  21. $image -> setPixel(0, 0, 0);
  22. $image -> setPixel(1, 0, 1);
  23. $image -> setPixel(0, 1, 2);
  24. $image -> setPixel(1, 1, 3);
  25. return $image;
  26. }
  27. public function testCreate()
  28. {
  29. $img = $this -> createIndexedTestImage();
  30. $this -> assertEquals(2, $img -> getWidth());
  31. $this -> assertEquals(2, $img -> getHeight());
  32. $this -> assertEquals(255, $img -> getMaxVal());
  33. $this -> assertEquals("▄▄\n", $img -> toBlackAndWhite() -> toString());
  34. }
  35. public function testReduceDepthTo1Bit()
  36. {
  37. $img = $this -> createIndexedTestImage();
  38. $img -> setMaxVal(1);
  39. $this -> assertEquals(0, $img -> getPixel(0, 0));
  40. $this -> assertEquals(0, $img -> getPixel(1, 0));
  41. $this -> assertEquals(1, $img -> getPixel(0, 1));
  42. $this -> assertEquals(1, $img -> getPixel(1, 1));
  43. }
  44. public function testToGrayscale() {
  45. $img = $this -> createIndexedTestImage() -> toGrayscale();
  46. $this -> assertEquals(255, $img -> getPixel(0, 0));
  47. $this -> assertEquals(160, $img -> getPixel(1, 0));
  48. $this -> assertEquals(80, $img -> getPixel(0, 1));
  49. $this -> assertEquals(0, $img -> getPixel(1, 1));
  50. }
  51. public function testToRgb() {
  52. $white = RgbRasterImage::rgbToInt(255, 255, 255);
  53. $lightGray = RgbRasterImage::rgbToInt(160, 160, 160);
  54. $darkGray = RgbRasterImage::rgbToInt(80, 80, 80);
  55. $black = RgbRasterImage::rgbToInt(0, 0, 0);
  56. $img = $this -> createIndexedTestImage() -> toRgb();
  57. $this -> assertEquals($white, $img -> getPixel(0, 0));
  58. $this -> assertEquals($lightGray, $img -> getPixel(1, 0));
  59. $this -> assertEquals($darkGray, $img -> getPixel(0, 1));
  60. $this -> assertEquals($black, $img -> getPixel(1, 1));
  61. }
  62. public function testRgbToIndexExists() {
  63. $img = $this -> createIndexedTestImage();
  64. $idx = $img -> rgbToIndex([80, 80, 80]);
  65. $this -> assertEquals(2, $idx);
  66. }
  67. public function testQuantizeGrayscale() {
  68. // Produce 8-bit grayscale image via high color-count RGBA
  69. $img = Image::fromFile(__DIR__ . "/../resources/pngsuite/basn2c08.png") -> toIndexed() -> toGrayscale();
  70. $this -> assertEquals(255, $img -> getMaxVal());
  71. $this -> assertEquals(255, $img -> getPixel(0, 0)); // White
  72. $this -> assertEquals(0, $img -> getPixel(31, 31)); // Black
  73. }
  74. public function testQuantizeRgb() {
  75. // Reduce depth of image with more than 16 colors.
  76. $img = Image::fromFile(__DIR__ . "/../resources/pngsuite/basn2c08.png") -> toIndexed();
  77. $this -> assertEquals(16777215, $img -> getMaxVal());
  78. $img -> setMaxVal(255);
  79. // Check new range.
  80. $this -> assertEquals(255, $img -> getMaxVal());
  81. $this -> assertEquals(255, $img -> getPixel(0, 0)); // White
  82. $this -> assertEquals(0, $img -> getPixel(31, 31)); // Black
  83. }
  84. }