WbmpCodecTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. use Mike42\GfxPhp\BlackAndWhiteRasterImage;
  3. use Mike42\GfxPhp\Codec\Common\DataBlobInputStream;
  4. use Mike42\GfxPhp\Codec\WbmpCodec;
  5. use PHPUnit\Framework\TestCase;
  6. class WbmpCodecTest extends TestCase {
  7. const WBMP_IMAGE = "\x00\x00\x0c\x06\x24\x90\xff\xf0\x49\x20\xff\xf0\x92\x40\xff\xf0";
  8. public function testIdentify() {
  9. $codec = new WbmpCodec();
  10. $this -> assertEquals("wbmp", $codec -> identify(self::WBMP_IMAGE));
  11. $this -> assertEquals("", $codec -> identify("HELLO"));
  12. }
  13. public function testDecode() {
  14. $codec = new WbmpCodec();
  15. $image = $codec -> decode(self::WBMP_IMAGE) -> toBlackAndWhite();
  16. $this -> assertEquals(12, $image -> getWidth());
  17. $this -> assertEquals(6, $image -> getHeight());
  18. $content = "▀▀ ▀▀ ▀▀ ▀▀ \n" .
  19. "▀ ▀▀ ▀▀ ▀▀ ▀\n" .
  20. " ▀▀ ▀▀ ▀▀ ▀▀\n";
  21. $this -> assertEquals($content, $image -> toString());
  22. }
  23. public function testEncode() {
  24. // Raster representation is inverse to WBMP format.
  25. $image = BlackAndWhiteRasterImage::create(12, 6, [0xdb, 0x6f, 0x00, 0x0f, 0xb6, 0xdf, 0x00, 0x0f, 0x6d, 0xbf, 0x00, 0x0f]);
  26. $codec = new WbmpCodec();
  27. $data = $codec -> encode($image, "wbmp");
  28. $this -> assertEquals(self::WBMP_IMAGE, $data);
  29. }
  30. public function testReadOneByte() {
  31. $data = DataBlobInputStream::fromBlob("\x60");
  32. $codec = new WbmpCodec();
  33. $val = $codec -> readInt($data);
  34. $this -> assertEquals(0x60, $val);
  35. }
  36. public function testReadMultibyte() {
  37. $data = DataBlobInputStream::fromBlob("\x81\x20");
  38. $codec = new WbmpCodec();
  39. $val = $codec -> readInt($data);
  40. $this -> assertEquals(0xA0, $val);
  41. }
  42. public function testReadMax() {
  43. $data = DataBlobInputStream::fromBlob("\xFF\xFF\xFF\x7F\x00"); // Final byte not used
  44. $codec = new WbmpCodec();
  45. $val = $codec -> readInt($data);
  46. $this -> assertEquals(268435455, $val);
  47. }
  48. public function testReadMultibyteOverflow() {
  49. // Appears to be no limit in WBMP to image dimensions, but we stop reading the multibyte-ints after 28 bits.
  50. $this -> expectException(Exception::class);
  51. $data = DataBlobInputStream::fromBlob("\xFF\xFF\xFF\x80\x00"); // (value in testMax()) + 1
  52. $codec = new WbmpCodec();
  53. $codec -> readInt($data);
  54. }
  55. public function testWriteOneByte() {
  56. $codec = new WbmpCodec();
  57. $val = $codec -> writeInt(0x60);
  58. $this -> assertEquals("\x60", $val);
  59. }
  60. public function testWriteMultibyte() {
  61. $codec = new WbmpCodec();
  62. $val = $codec -> writeInt(0xA0);
  63. $this -> assertEquals("\x81\x20", $val);
  64. }
  65. public function testWriteMax() {
  66. $codec = new WbmpCodec();
  67. $val = $codec -> writeInt(268435455);
  68. $this -> assertEquals("\xFF\xFF\xFF\x7F", $val);
  69. }
  70. public function testWriteMultibyteOverflow() {
  71. $this -> expectException(Exception::class);
  72. $codec = new WbmpCodec();
  73. $val = $codec -> writeInt(268435456); // As testWriteMax(), +1
  74. }
  75. }