png.test.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Jimp, getTestDir } from '@jimp/test-utils';
  2. import configure from '@jimp/custom';
  3. import png from '../src';
  4. const jimp = configure({ types: [png] }, Jimp);
  5. describe('PNG', () => {
  6. const imagesDir = getTestDir(__dirname) + '/images';
  7. it('load PNG', async () => {
  8. const image = await jimp.read(imagesDir + '/dice.png');
  9. image.getPixelColor(10, 10).should.be.equal(0x00000000);
  10. image.getPixelColor(160, 80).should.be.equal(0x1c1cd4ff);
  11. image.getPixelColor(400, 250).should.be.equal(0x7e0c0cda);
  12. });
  13. it('export PNG', async () => {
  14. const jgd = await jimp.read({
  15. width: 3,
  16. height: 3,
  17. data: [
  18. 0xff0000ff,
  19. 0xff0080ff,
  20. 0xff00ffff,
  21. 0xff0080ff,
  22. 0xff00ffff,
  23. 0x8000ffff,
  24. 0xff00ffff,
  25. 0x8000ffff,
  26. 0x0000ffff
  27. ]
  28. });
  29. const buffer = await jgd.getBufferAsync('image/png');
  30. buffer.toString().should.match(/^.PNG\r\n/);
  31. });
  32. it('should use png options', async () => {
  33. const jgd = await jimp.read({
  34. width: 20,
  35. height: 20,
  36. data: [
  37. 0xff0000ff,
  38. 0xff0080ff,
  39. 0xff00ffff,
  40. 0xff0080ff,
  41. 0xff00ffff,
  42. 0x8000ffff,
  43. 0xff00ffff,
  44. 0x8000ffff,
  45. 0x0000ffff,
  46. 0xff0000ff,
  47. 0xff0080ff,
  48. 0xff00ffff,
  49. 0xff0080ff,
  50. 0xff00ffff,
  51. 0x8000ffff,
  52. 0xff00ffff,
  53. 0x8000ffff,
  54. 0x0000ffff,
  55. 0xff0000ff,
  56. 0xff0080ff,
  57. 0xff00ffff,
  58. 0xff0080ff,
  59. 0xff00ffff,
  60. 0x8000ffff,
  61. 0xff00ffff,
  62. 0x8000ffff,
  63. 0x0000ffff,
  64. 0xff0000ff,
  65. 0xff0080ff,
  66. 0xff00ffff,
  67. 0xff0080ff,
  68. 0xff00ffff,
  69. 0x8000ffff,
  70. 0xff00ffff,
  71. 0x8000ffff,
  72. 0x0000ffff
  73. ]
  74. });
  75. const image = await jgd
  76. .deflateStrategy(0)
  77. .colorType(0)
  78. .getBufferAsync(Jimp.MIME_PNG);
  79. const expected = await jimp.read(imagesDir + '/options.png');
  80. const expectedBuffer = await expected
  81. .deflateStrategy(0)
  82. .colorType(0)
  83. .getBufferAsync(Jimp.MIME_PNG);
  84. image.should.be.deepEqual(expectedBuffer);
  85. });
  86. });