PnmCodecTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use Mike42\GfxPhp\RgbRasterImage;
  4. use Mike42\GfxPhp\Codec\PngCodec;
  5. use Mike42\GfxPhp\Codec\PnmCodec;
  6. use Mike42\GfxPhp\BlackAndWhiteRasterImage;
  7. use Mike42\GfxPhp\GrayscaleRasterImage;
  8. /*
  9. * Tiny example image used throughout this test looks like this:
  10. *╭────╮
  11. *│██░░│ ██ = 0x00
  12. *│░░██│ ░░ = 0xFF
  13. *╰────╯
  14. */
  15. class PnmCodecTest extends TestCase
  16. {
  17. const IMAGE_TEXT = "▀▄\n";
  18. const PBM_EXAMPLE = "P4\x0a2 2\x0a\x80@";
  19. const PGM_EXAMPLE = "P5\x0a2 2\x0a255\x0a\x00\xff\xff\x00";
  20. const PPM_EXAMPLE = "P6\x0a2 2\x0a255\x0a\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00";
  21. protected function createRgbTestImage() {
  22. $image = RgbRasterImage::create(2, 2);
  23. $black = $image -> rgbToInt(0, 0, 0);
  24. $white = $image -> rgbToInt(255, 255, 255);
  25. $image -> setPixel(0, 0, $black);
  26. $image -> setPixel(1, 0, $white);
  27. $image -> setPixel(0, 1, $white);
  28. $image -> setPixel(1, 1, $black);
  29. return $image;
  30. }
  31. public function testPnmEncodeRgb() {
  32. // Test image as RGB
  33. $encoder = new PnmCodec();
  34. $image = $this -> createRgbTestImage();
  35. // Encode
  36. $blob = $encoder -> encode($image, 'pnm');
  37. // Should have auto-selected PPM based on RgbTestImage input
  38. $this -> assertEquals(self::PPM_EXAMPLE, $blob);
  39. }
  40. public function testPbmEncodeRgb() {
  41. // Test image as RGB
  42. $encoder = new PnmCodec();
  43. $image = $this -> createRgbTestImage();
  44. // Encode
  45. $blob = $encoder -> encode($image, 'pbm');
  46. // Check against known-good encoding of PBM
  47. $this -> assertEquals(self::PBM_EXAMPLE, $blob);
  48. }
  49. public function testPbmDecode() {
  50. $decoder = new PnmCodec();
  51. $image = $decoder -> decode(self::PBM_EXAMPLE);
  52. $this -> assertTrue($image instanceof BlackAndWhiteRasterImage);
  53. $this -> assertEquals(self::IMAGE_TEXT, $image -> toString());
  54. }
  55. public function testPgmEncodeRgb() {
  56. // Test image as RGB
  57. $encoder = new PnmCodec();
  58. $image = $this -> createRgbTestImage();
  59. // Encode
  60. $blob = $encoder -> encode($image, 'pgm');
  61. // Check against known-good encoding of PGM
  62. $this -> assertEquals(self::PGM_EXAMPLE, $blob);
  63. }
  64. public function testPgmDecode() {
  65. $decoder = new PnmCodec();
  66. $image = $decoder -> decode(self::PGM_EXAMPLE);
  67. $this -> assertTrue($image instanceof GrayscaleRasterImage);
  68. $this -> assertEquals(self::IMAGE_TEXT, $image -> toBlackAndWhite() -> toString());
  69. }
  70. public function testPpmEncodeRgb() {
  71. // Test image as RGB
  72. $encoder = new PnmCodec();
  73. $image = $this -> createRgbTestImage();
  74. // Encode
  75. $blob = $encoder -> encode($image, 'ppm');
  76. // Check against known-good encoding of PPM
  77. $this -> assertEquals(self::PPM_EXAMPLE, $blob);
  78. }
  79. public function testPpmDecode() {
  80. $decoder = new PnmCodec();
  81. $image = $decoder -> decode(self::PPM_EXAMPLE);
  82. $this -> assertTrue($image instanceof RgbRasterImage);
  83. $this -> assertEquals(self::IMAGE_TEXT, $image -> toBlackAndWhite() -> toString());
  84. }
  85. }