PngCodecTest.php 5.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. use Mike42\GfxPhp\Image;
  3. use PHPUnit\Framework\TestCase;
  4. use Mike42\GfxPhp\RgbRasterImage;
  5. use Mike42\GfxPhp\Codec\PngCodec;
  6. class PngCodecTest extends TestCase
  7. {
  8. // Example file that we compare encoder results against, and decode.
  9. const SMALL_FILE = "\x89PNG\x0d\x0a\x1a\x0a\x00\x00\x00\x0dIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDATx\x9cc\xf8\xff\xff?\x00\x05\xfe\x02\xfe\x0d\xefF\xb8\x00\x00\x00\x00IEND\xaeB`\x82";
  10. public function testEncode() {
  11. // Simplest image I can come up with
  12. $img = RgbRasterImage::create(1, 1);
  13. $codec = new PngCodec();
  14. // Encode
  15. $pngStr = $codec -> encode($img, "png");
  16. // Compare to known-good PNG
  17. $this -> assertEquals(self::SMALL_FILE, $pngStr);
  18. }
  19. public function testDecode() {
  20. $codec = new PngCodec();
  21. $img = $codec -> decode(self::SMALL_FILE);
  22. $this -> assertTrue($img instanceof RgbRasterImage);
  23. $this -> assertEquals(1, $img -> getWidth());
  24. $this -> assertEquals(1, $img -> getHeight());
  25. }
  26. public function testBlackAndWhiteImageLoad() {
  27. // Simple test of a black-and-white, interlaced & non-interlaced image, since
  28. // we can do a text-based assertion on the actual image content.
  29. $expected = " ▄█\n" .
  30. " ▄███\n" .
  31. " ██ ██ ▄█████\n" .
  32. " ██ ▄▄ ██ ▄███████\n" .
  33. " ██ ██ ██ ▄█████████\n" .
  34. " ████████ ▄███████████\n" .
  35. " ██ ██ ▄█████████████\n" .
  36. " ▄███████████████\n" .
  37. " ▄█████████████████\n" .
  38. " ▄███████ █████\n" .
  39. " ▄█████████ ████ ████\n" .
  40. " ▄███████████ █████\n" .
  41. " ▄█████████████ ████ ████\n" .
  42. " ▄███████████████ █████\n" .
  43. " ▄█████████████████████████████\n" .
  44. "▄███████████████████████████████\n";
  45. // Load interlaced & non-interlaced
  46. $interlacedImage = Image::fromFile(__DIR__ . "/../../resources/pngsuite/basi0g01.png");
  47. $interlacedResult = $interlacedImage -> toString();
  48. $nonInterlacedImage = Image::fromFile(__DIR__ . "/../../resources/pngsuite/basn0g01.png");
  49. $nonInterlacedResult = $nonInterlacedImage -> toString();
  50. // These should both match the expected output
  51. $this -> assertEquals($expected, $interlacedResult);
  52. $this -> assertEquals($expected, $nonInterlacedResult);
  53. }
  54. public function testIndexedImageLoad() {
  55. // Load an interlaced 8-bit image, which follows a very different code path to
  56. // lower bit depths.
  57. $img = Image::fromFile(__DIR__ . "/../../resources/pngsuite/basi3p08.png") -> toIndexed();
  58. $this -> assertEquals(255, $img -> getMaxVal());
  59. $this -> assertEquals(32, $img -> getWidth());
  60. $this -> assertEquals(32, $img -> getHeight());
  61. }
  62. public function testFiltering() {
  63. // Expected value is sensitive to the grayscale -> B&W conversion
  64. $expected = "████████████████████████████████\n" .
  65. "███████████▀▀▀▀▀▀▀▀▀▀███████████\n" .
  66. "████████▀ ▀████████\n" .
  67. "██████▀ ▀██████\n" .
  68. "████▀ ▀████\n" .
  69. "███▀ ▀███\n" .
  70. "███ ███\n" .
  71. "███ ███\n" .
  72. "███ ███\n" .
  73. "███ ███\n" .
  74. "███▄ ▄███\n" .
  75. "████▄ ▄████\n" .
  76. "██████▄ ▄██████\n" .
  77. "████████▄ ▄████████\n" .
  78. "███████████▄▄▄▄▄▄▄▄▄▄███████████\n" .
  79. "████████████████████████████████\n";
  80. // Image with different filters on each scanline
  81. $img = Image::fromFile(__DIR__ . "/../../resources/pngsuite/f99n0g04.png") -> toBlackAndWhite();
  82. $result = $img -> toString();
  83. $this -> assertEquals($expected, $result);
  84. }
  85. }