blit.test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { Jimp, mkJGD, getTestDir } from '@jimp/test-utils';
  2. import jpeg from '@jimp/jpeg';
  3. import configure from '@jimp/custom';
  4. import blit from '../src';
  5. const jimp = configure({ types: [jpeg], plugins: [blit] }, Jimp);
  6. const testDir = getTestDir(__dirname);
  7. describe('Blit over image', function() {
  8. this.timeout(15000);
  9. const targetJGD = mkJGD(
  10. '▴▴▴▴▸▸▸▸',
  11. '▴▴▴▴▸▸▸▸',
  12. '▴▴▴▴▸▸▸▸',
  13. '▴▴▴▴▸▸▸▸',
  14. '▾▾▾▾◆◆◆◆',
  15. '▾▾▾▾◆◆◆◆',
  16. '▾▾▾▾◆◆◆◆',
  17. '▾▾▾▾◆◆◆◆'
  18. );
  19. const srcJGD = mkJGD(
  20. '□□□□□□',
  21. '□▥▥▥▥□',
  22. '□▥■■▥□',
  23. '□▥■■▥□',
  24. '□▥▥▥▥□',
  25. '□□□□□□'
  26. );
  27. let targetImg;
  28. let srcImg; // stores the Jimp instances of the JGD images above.
  29. before(done => {
  30. const img1 = jimp.read(targetJGD);
  31. const img2 = jimp.read(srcJGD);
  32. Promise.all([img1, img2])
  33. .then(images => {
  34. targetImg = images[0];
  35. srcImg = images[1];
  36. done();
  37. })
  38. .catch(done);
  39. });
  40. it('blit on top, with no crop', () => {
  41. targetImg
  42. .clone()
  43. .blit(srcImg, 0, 0)
  44. .getJGDSync()
  45. .should.be.sameJGD(
  46. mkJGD(
  47. '□□□□□□▸▸',
  48. '□▥▥▥▥□▸▸',
  49. '□▥■■▥□▸▸',
  50. '□▥■■▥□▸▸',
  51. '□▥▥▥▥□◆◆',
  52. '□□□□□□◆◆',
  53. '▾▾▾▾◆◆◆◆',
  54. '▾▾▾▾◆◆◆◆'
  55. )
  56. );
  57. });
  58. it('blit on middle, with no crop', () => {
  59. targetImg
  60. .clone()
  61. .blit(srcImg, 1, 1)
  62. .getJGDSync()
  63. .should.be.sameJGD(
  64. mkJGD(
  65. '▴▴▴▴▸▸▸▸',
  66. '▴□□□□□□▸',
  67. '▴□▥▥▥▥□▸',
  68. '▴□▥■■▥□▸',
  69. '▾□▥■■▥□◆',
  70. '▾□▥▥▥▥□◆',
  71. '▾□□□□□□◆',
  72. '▾▾▾▾◆◆◆◆'
  73. )
  74. );
  75. });
  76. it('blit on middle, with x,y crop', () => {
  77. targetImg
  78. .clone()
  79. .blit(srcImg, 2, 2, 1, 1, 5, 5)
  80. .getJGDSync()
  81. .should.be.sameJGD(
  82. mkJGD(
  83. '▴▴▴▴▸▸▸▸',
  84. '▴▴▴▴▸▸▸▸',
  85. '▴▴▥▥▥▥□▸',
  86. '▴▴▥■■▥□▸',
  87. '▾▾▥■■▥□◆',
  88. '▾▾▥▥▥▥□◆',
  89. '▾▾□□□□□◆',
  90. '▾▾▾▾◆◆◆◆'
  91. )
  92. );
  93. });
  94. it('blit on middle, with x,y,w,h crop', () => {
  95. targetImg
  96. .clone()
  97. .blit(srcImg, 2, 2, 1, 1, 4, 4)
  98. .getJGDSync()
  99. .should.be.sameJGD(
  100. mkJGD(
  101. '▴▴▴▴▸▸▸▸',
  102. '▴▴▴▴▸▸▸▸',
  103. '▴▴▥▥▥▥▸▸',
  104. '▴▴▥■■▥▸▸',
  105. '▾▾▥■■▥◆◆',
  106. '▾▾▥▥▥▥◆◆',
  107. '▾▾▾▾◆◆◆◆',
  108. '▾▾▾▾◆◆◆◆'
  109. )
  110. );
  111. });
  112. it('blit partially out, on top-left', () => {
  113. targetImg
  114. .clone()
  115. .blit(srcImg, -1, -1)
  116. .getJGDSync()
  117. .should.be.sameJGD(
  118. mkJGD(
  119. '▥▥▥▥□▸▸▸',
  120. '▥■■▥□▸▸▸',
  121. '▥■■▥□▸▸▸',
  122. '▥▥▥▥□▸▸▸',
  123. '□□□□□◆◆◆',
  124. '▾▾▾▾◆◆◆◆',
  125. '▾▾▾▾◆◆◆◆',
  126. '▾▾▾▾◆◆◆◆'
  127. )
  128. );
  129. });
  130. it('blit partially out, on bottom-right', () => {
  131. targetImg
  132. .clone()
  133. .blit(srcImg, 3, 3)
  134. .getJGDSync()
  135. .should.be.sameJGD(
  136. mkJGD(
  137. '▴▴▴▴▸▸▸▸',
  138. '▴▴▴▴▸▸▸▸',
  139. '▴▴▴▴▸▸▸▸',
  140. '▴▴▴□□□□□',
  141. '▾▾▾□▥▥▥▥',
  142. '▾▾▾□▥■■▥',
  143. '▾▾▾□▥■■▥',
  144. '▾▾▾□▥▥▥▥'
  145. )
  146. );
  147. });
  148. it('blit alpha', async () => {
  149. const expectedImg = await Jimp.read(testDir + '/images/blit-alpha.png');
  150. const dice = await Jimp.read(testDir + '/images/dice.png');
  151. const image = await Jimp.read(testDir + '/images/cops.jpg');
  152. image
  153. .blit(dice, 0, 0)
  154. .bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);
  155. });
  156. async function createCat(catNum, len) {
  157. let imgHeight = 60;
  158. const butt = await Jimp.read(testDir + '/images/cat_butt.png');
  159. const head = await Jimp.read(testDir + '/images/cat_head.png');
  160. const fuzz = await Jimp.read(testDir + '/images/cat_fuzz.png');
  161. let longCat = len;
  162. longCat = longCat > 20 ? 20 : longCat;
  163. longCat = longCat <= 1 ? 1 : longCat;
  164. const cat =
  165. Math.floor(catNum * (head.bitmap.height / imgHeight)) * imgHeight;
  166. const newImage = await Jimp.create(
  167. butt.bitmap.width + head.bitmap.width + fuzz.bitmap.width * longCat,
  168. imgHeight,
  169. 0x00000000
  170. );
  171. newImage.blit(butt, 0, 0, 0, cat, butt.bitmap.width, imgHeight);
  172. for (let i = 0; i < longCat; i++) {
  173. newImage.blit(
  174. fuzz,
  175. butt.bitmap.width + fuzz.bitmap.width * i,
  176. 0,
  177. 0,
  178. cat,
  179. fuzz.bitmap.width,
  180. imgHeight
  181. );
  182. }
  183. newImage.blit(
  184. head,
  185. butt.bitmap.width + fuzz.bitmap.width * longCat,
  186. 0,
  187. 0,
  188. cat,
  189. head.bitmap.width,
  190. imgHeight
  191. );
  192. return newImage;
  193. }
  194. it('uses src params correctly', async () => {
  195. const expectedSmall = await Jimp.read(
  196. testDir + '/images/cat-results/small-cat.png'
  197. );
  198. const small = await createCat(0.3, 1);
  199. small.bitmap.data.should.be.deepEqual(expectedSmall.bitmap.data);
  200. const expectedMedium = await Jimp.read(
  201. testDir + '/images/cat-results/medium-cat.png'
  202. );
  203. const medium = await createCat(0.6, 7);
  204. medium.bitmap.data.should.be.deepEqual(expectedMedium.bitmap.data);
  205. const expectedLarge = await Jimp.read(
  206. testDir + '/images/cat-results/large-cat.png'
  207. );
  208. const large = await createCat(0.9, 20);
  209. large.bitmap.data.should.be.deepEqual(expectedLarge.bitmap.data);
  210. });
  211. });