autocrop.test.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import { Jimp, mkJGD } from '@jimp/test-utils';
  2. import configure from '@jimp/custom';
  3. import crop from '../src';
  4. const jimp = configure({ plugins: [crop] }, Jimp);
  5. describe('Autocrop', () => {
  6. it('image with transparent surround color', async () => {
  7. const imgSrc = await jimp.read(
  8. mkJGD(
  9. ' ',
  10. ' ◆◆ ',
  11. ' ◆▦▦◆ ',
  12. ' ◆▦▦▦▦◆ ',
  13. ' ◆▦▦◆ ',
  14. ' ◆◆ ',
  15. ' '
  16. )
  17. );
  18. imgSrc
  19. .autocrop()
  20. .getJGDSync()
  21. .should.be.sameJGD(
  22. mkJGD(' ◆◆ ', ' ◆▦▦◆ ', '◆▦▦▦▦◆', ' ◆▦▦◆ ', ' ◆◆ ')
  23. );
  24. });
  25. it('image with opaque surround color', async () => {
  26. const imgSrc = await jimp.read(
  27. mkJGD(
  28. '▥▥▥▥▥▥▥▥▥▥',
  29. '▥▥▥▥◆◆▥▥▥▥',
  30. '▥▥▥◆▦▦◆▥▥▥',
  31. '▥▥◆▦▦▦▦◆▥▥',
  32. '▥▥▥◆▦▦◆▥▥▥',
  33. '▥▥▥▥◆◆▥▥▥▥',
  34. '▥▥▥▥▥▥▥▥▥▥'
  35. )
  36. );
  37. imgSrc
  38. .autocrop()
  39. .getJGDSync()
  40. .should.be.sameJGD(
  41. mkJGD('▥▥◆◆▥▥', '▥◆▦▦◆▥', '◆▦▦▦▦◆', '▥◆▦▦◆▥', '▥▥◆◆▥▥')
  42. );
  43. });
  44. it('image with one color border', async () => {
  45. const imgSrc = await jimp.read(
  46. mkJGD(
  47. '▥▥▥▥▥▥▥▥▥▥▥▥',
  48. '▥▥▥▥▥▥▥▥▥▥▥▥',
  49. '▥▥ ◆◆ ▥▥',
  50. '▥▥ ◆▦▦◆ ▥▥',
  51. '▥▥ ◆▦▦▦▦◆ ▥▥',
  52. '▥▥ ◆▦▦◆ ▥▥',
  53. '▥▥ ◆◆ ▥▥',
  54. '▥▥▥▥▥▥▥▥▥▥▥▥',
  55. '▥▥▥▥▥▥▥▥▥▥▥▥'
  56. )
  57. );
  58. imgSrc
  59. .autocrop()
  60. .getJGDSync()
  61. .should.be.sameJGD(
  62. mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')
  63. );
  64. });
  65. it('image border with small variation', async () => {
  66. const imgSrc = await jimp.read(
  67. mkJGD(
  68. '323232323232',
  69. '232323232323',
  70. '32 ◆◆ 32',
  71. '23 ◆▦▦◆ 23',
  72. '32 ◆▦▦▦▦◆ 32',
  73. '23 ◆▦▦◆ 23',
  74. '32 ◆◆ 32',
  75. '232323232323',
  76. '323232323232'
  77. )
  78. );
  79. imgSrc
  80. .clone()
  81. .autocrop()
  82. .getJGDSync()
  83. .should.be.sameJGD(
  84. mkJGD(
  85. '323232323232',
  86. '232323232323',
  87. '32 ◆◆ 32',
  88. '23 ◆▦▦◆ 23',
  89. '32 ◆▦▦▦▦◆ 32',
  90. '23 ◆▦▦◆ 23',
  91. '32 ◆◆ 32',
  92. '232323232323',
  93. '323232323232'
  94. )
  95. );
  96. imgSrc
  97. .clone()
  98. .autocrop(0.005)
  99. .getJGDSync()
  100. .should.be.sameJGD(
  101. mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')
  102. );
  103. });
  104. it('image border with small variation configured by options', async () => {
  105. const imgSrc = await Jimp.read(
  106. mkJGD(
  107. '323232323232',
  108. '232323232323',
  109. '32 ◆◆ 32',
  110. '23 ◆▦▦◆ 23',
  111. '32 ◆▦▦▦▦◆ 32',
  112. '23 ◆▦▦◆ 23',
  113. '32 ◆◆ 32',
  114. '232323232323',
  115. '323232323232'
  116. )
  117. );
  118. imgSrc
  119. .clone()
  120. .autocrop()
  121. .getJGDSync()
  122. .should.be.sameJGD(
  123. mkJGD(
  124. '323232323232',
  125. '232323232323',
  126. '32 ◆◆ 32',
  127. '23 ◆▦▦◆ 23',
  128. '32 ◆▦▦▦▦◆ 32',
  129. '23 ◆▦▦◆ 23',
  130. '32 ◆◆ 32',
  131. '232323232323',
  132. '323232323232'
  133. )
  134. );
  135. imgSrc
  136. .clone()
  137. .autocrop({ tolerance: 0.005 })
  138. .getJGDSync()
  139. .should.be.sameJGD(
  140. mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')
  141. );
  142. });
  143. it('image without frame', async () => {
  144. const imgSrc = await Jimp.read(
  145. mkJGD(
  146. '▥▥ ◆◆ ',
  147. '▥▥ ◆▦▦◆ ',
  148. '▥▥ ◆▦▦▦▦◆ ',
  149. '▥▥ ◆▦▦◆ ',
  150. '▥▥ ◆◆ ',
  151. '▥▥▥▥▥▥▥▥▥▥',
  152. '▥▥▥▥▥▥▥▥▥▥'
  153. )
  154. );
  155. imgSrc
  156. .autocrop(false)
  157. .getJGDSync()
  158. .should.be.sameJGD(
  159. mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')
  160. );
  161. });
  162. it('image without frame configured by options', async () => {
  163. const imgSrc = await Jimp.read(
  164. mkJGD(
  165. '▥▥ ◆◆ ',
  166. '▥▥ ◆▦▦◆ ',
  167. '▥▥ ◆▦▦▦▦◆ ',
  168. '▥▥ ◆▦▦◆ ',
  169. '▥▥ ◆◆ ',
  170. '▥▥▥▥▥▥▥▥▥▥',
  171. '▥▥▥▥▥▥▥▥▥▥'
  172. )
  173. );
  174. imgSrc
  175. .autocrop({ cropOnlyFrames: false })
  176. .getJGDSync()
  177. .should.be.sameJGD(
  178. mkJGD(' ◆◆ ', ' ◆▦▦◆ ', ' ◆▦▦▦▦◆ ', ' ◆▦▦◆ ', ' ◆◆ ')
  179. );
  180. });
  181. it('image with symmetric border configured by options', async () => {
  182. const imgSrc = await Jimp.read(
  183. mkJGD(
  184. '▥▥▥▥▥▥▥▥▥▥▥▥▥▥',
  185. '▥▥ ◆◆ ▥▥▥▥',
  186. '▥▥ ◆▦▦◆ ▥▥▥▥',
  187. '▥▥ ◆▦▦▦▦◆ ▥▥▥▥',
  188. '▥▥ ◆▦▦◆ ▥▥▥▥',
  189. '▥▥ ◆◆ ▥▥▥▥',
  190. '▥▥▥▥▥▥▥▥▥▥▥▥▥▥',
  191. '▥▥▥▥▥▥▥▥▥▥▥▥▥▥'
  192. )
  193. );
  194. imgSrc
  195. .autocrop({ cropSymmetric: true })
  196. .getJGDSync()
  197. .should.be.sameJGD(
  198. mkJGD(
  199. ' ◆◆ ▥▥',
  200. ' ◆▦▦◆ ▥▥',
  201. ' ◆▦▦▦▦◆ ▥▥',
  202. ' ◆▦▦◆ ▥▥',
  203. ' ◆◆ ▥▥',
  204. '▥▥▥▥▥▥▥▥▥▥'
  205. )
  206. );
  207. });
  208. it('image without frame and with symmetric border configured by options', async () => {
  209. const imgSrc = await Jimp.read(
  210. mkJGD(
  211. '▥▥ ◆◆ ▥▥▥▥',
  212. '▥▥ ◆▦▦◆ ▥▥▥▥',
  213. '▥▥ ◆▦▦▦▦◆ ▥▥▥▥',
  214. '▥▥ ◆▦▦◆ ▥▥▥▥',
  215. '▥▥ ◆◆ ▥▥▥▥',
  216. '▥▥▥▥▥▥▥▥▥▥▥▥▥▥',
  217. '▥▥▥▥▥▥▥▥▥▥▥▥▥▥'
  218. )
  219. );
  220. imgSrc
  221. .autocrop({ cropSymmetric: true, cropOnlyFrames: false })
  222. .getJGDSync()
  223. .should.be.sameJGD(
  224. mkJGD(
  225. ' ◆◆ ▥▥',
  226. ' ◆▦▦◆ ▥▥',
  227. ' ◆▦▦▦▦◆ ▥▥',
  228. ' ◆▦▦◆ ▥▥',
  229. ' ◆◆ ▥▥',
  230. '▥▥▥▥▥▥▥▥▥▥',
  231. '▥▥▥▥▥▥▥▥▥▥'
  232. )
  233. );
  234. });
  235. it('image without frame and with some border left', async () => {
  236. const imgSrc = await Jimp.read(
  237. mkJGD(
  238. '323232323232',
  239. '232323232323',
  240. '32 ◆◆ 32',
  241. '23 ◆▦▦◆ 23',
  242. '32 ◆▦▦▦▦◆ 32',
  243. '23 ◆▦▦◆ 23',
  244. '32 ◆◆ 32',
  245. '232323232323',
  246. '323232323232'
  247. )
  248. );
  249. imgSrc
  250. .autocrop({
  251. tolerance: 0.005,
  252. leaveBorder: 1
  253. })
  254. .getJGDSync()
  255. .should.be.sameJGD(
  256. mkJGD(
  257. '3232323232',
  258. '2 ◆◆ 3',
  259. '3 ◆▦▦◆ 2',
  260. '2 ◆▦▦▦▦◆ 3',
  261. '3 ◆▦▦◆ 2',
  262. '2 ◆◆ 3',
  263. '3232323232'
  264. )
  265. );
  266. });
  267. it('image not cropped given an out of bounds "leaveBorder" value ', async () => {
  268. const imgSrc = await Jimp.read(
  269. mkJGD(
  270. '323232323232',
  271. '232323232323',
  272. '32 ◆◆ 32',
  273. '23 ◆▦▦◆ 23',
  274. '32 ◆▦▦▦▦◆ 32',
  275. '23 ◆▦▦◆ 23',
  276. '32 ◆◆ 32',
  277. '232323232323',
  278. '323232323232'
  279. )
  280. );
  281. imgSrc
  282. .autocrop({
  283. tolerance: 0.005,
  284. leaveBorder: 100
  285. })
  286. .getJGDSync()
  287. .should.be.sameJGD(
  288. mkJGD(
  289. '323232323232',
  290. '232323232323',
  291. '32 ◆◆ 32',
  292. '23 ◆▦▦◆ 23',
  293. '32 ◆▦▦▦▦◆ 32',
  294. '23 ◆▦▦◆ 23',
  295. '32 ◆◆ 32',
  296. '232323232323',
  297. '323232323232'
  298. )
  299. );
  300. });
  301. it('image with top and bottom frame and leaveBorder', async () => {
  302. const imgSrc = await Jimp.read(
  303. mkJGD(
  304. '▥▥▥▥▥▥▥▥',
  305. '▥▥▥▥▥▥▥▥',
  306. '▥▥▥▥▥▥▥▥',
  307. ' ◆◆ ',
  308. ' ◆▦▦◆ ',
  309. ' ◆▦▦▦▦◆ ',
  310. ' ◆▦▦◆ ',
  311. ' ◆◆ ',
  312. '▥▥▥▥▥▥▥▥',
  313. '▥▥▥▥▥▥▥▥',
  314. '▥▥▥▥▥▥▥▥'
  315. )
  316. );
  317. imgSrc
  318. .autocrop({ cropSymmetric: true, cropOnlyFrames: false, leaveBorder: 2 })
  319. .getJGDSync()
  320. .should.be.sameJGD(
  321. mkJGD(
  322. '▥▥▥▥▥▥▥▥',
  323. '▥▥▥▥▥▥▥▥',
  324. ' ◆◆ ',
  325. ' ◆▦▦◆ ',
  326. ' ◆▦▦▦▦◆ ',
  327. ' ◆▦▦◆ ',
  328. ' ◆◆ ',
  329. '▥▥▥▥▥▥▥▥',
  330. '▥▥▥▥▥▥▥▥'
  331. )
  332. );
  333. });
  334. });