rotation.test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. import { Jimp, mkJGD } from '@jimp/test-utils';
  2. import configure from '@jimp/custom';
  3. import blit from '@jimp/plugin-blit';
  4. import crop from '@jimp/plugin-crop';
  5. import resize from '@jimp/plugin-resize';
  6. import rotate from '../src';
  7. const jimp = configure({ plugins: [rotate, blit, crop, resize] }, Jimp);
  8. describe('Rotate a image with even size', () => {
  9. let imgSrc = null;
  10. before(done => {
  11. jimp
  12. .read(
  13. mkJGD(
  14. '▰▴▴▴▪▪▪▰',
  15. '▴▴▴▴▪▪▪▪',
  16. '▴▴▴▴▪▪▪▪',
  17. '▴▴▴▴▪▪▪▪',
  18. '▪▪▪▪▴▴▴▴',
  19. '▪▪▪▪▴▴▴▴',
  20. '▪▪▪▪▴▴▴▴',
  21. '▦▪▪▪▴▴▴▦'
  22. )
  23. )
  24. .then(imgJimp => {
  25. imgSrc = imgJimp;
  26. done();
  27. })
  28. .catch(done);
  29. });
  30. it('1 degrees', () => {
  31. imgSrc
  32. .clone()
  33. .rotate(1, true)
  34. .getJGDSync()
  35. .should.be.sameJGD(
  36. mkJGD(
  37. '▰▴▴▴▪▪▪▰ ',
  38. '▴▴▴▴▪▪▪▪ ',
  39. '▴▴▴▴▪▪▪▪ ',
  40. '▴▴▴▴▪▪▪▪ ',
  41. '▪▪▪▪▴▴▴▴ ',
  42. '▪▪▪▪▴▴▴▴ ',
  43. '▪▪▪▪▴▴▴▴ ',
  44. '▦▪▪▪▴▴▴▦ ',
  45. ' ',
  46. ' '
  47. )
  48. );
  49. });
  50. it('91 degrees', () => {
  51. imgSrc
  52. .clone()
  53. .rotate(91, true)
  54. .getJGDSync()
  55. .should.be.sameJGD(
  56. mkJGD(
  57. ' ',
  58. '▰▪▪▪▴▴▴▦ ',
  59. '▪▪▪▪▴▴▴▴ ',
  60. '▪▪▪▪▴▴▴▴ ',
  61. '▪▪▪▪▴▴▴▴ ',
  62. '▴▴▴▴▪▪▪▪ ',
  63. '▴▴▴▴▪▪▪▪ ',
  64. '▴▴▴▴▪▪▪▪ ',
  65. '▰▴▴▴▪▪▪▦ ',
  66. ' '
  67. )
  68. );
  69. });
  70. it('30 degrees', () => {
  71. imgSrc
  72. .clone()
  73. .rotate(30, true)
  74. .getJGDSync()
  75. .should.be.sameJGD(
  76. mkJGD(
  77. ' ▰▰ ',
  78. ' ▪▪▪▪ ',
  79. ' ▴▪▪▪▪▪ ',
  80. '▴▴▴▴▪▪▪▪ ',
  81. '▴▴▴▴▪▪▴▴▴ ',
  82. '▴▴▴▴▪▴▴▴▴▴ ',
  83. ' ▴▴▪▪▴▴▴▴▦ ',
  84. ' ▪▪▪▪▪▴▴▴ ',
  85. ' ▪▪▪▪▪ ',
  86. ' ▪▪▪ ',
  87. ' ▦ ',
  88. ' '
  89. )
  90. );
  91. });
  92. it('45 degrees', () => {
  93. imgSrc
  94. .clone()
  95. .rotate(45, true)
  96. .getJGDSync()
  97. .should.be.sameJGD(
  98. mkJGD(
  99. ' ',
  100. ' ▰ ',
  101. ' ▪▪▪ ',
  102. ' ▪▪▪▪▪ ',
  103. ' ▴▴▪▪▪▪▴ ',
  104. ' ▴▴▴▴▪▪▴▴▴ ',
  105. '▰▰▴▴▴▴▴▴▴▴▦ ',
  106. ' ▴▴▴▴▪▪▴▴▴ ',
  107. ' ▴▴▪▪▪▪▴ ',
  108. ' ▪▪▪▪▪ ',
  109. ' ▪▪▪ ',
  110. ' ▦ ',
  111. ' ',
  112. ' '
  113. )
  114. );
  115. });
  116. it('60 degrees', () => {
  117. imgSrc
  118. .clone()
  119. .rotate(60, true)
  120. .getJGDSync()
  121. .should.be.sameJGD(
  122. mkJGD(
  123. ' ▰ ',
  124. ' ▪▪▪ ',
  125. ' ▪▪▪▪▪ ',
  126. ' ▪▪▪▪▪▴▴▴ ',
  127. ' ▴▴▪▪▴▴▴▴▦ ',
  128. '▴▴▴▴▪▴▴▴▴▴ ',
  129. '▴▴▴▴▪▪▴▴▴ ',
  130. '▴▴▴▴▪▪▪▪ ',
  131. ' ▴▪▪▪▪▪ ',
  132. ' ▪▪▪▪ ',
  133. ' ▦▦ ',
  134. ' '
  135. )
  136. );
  137. });
  138. it('90 degrees', () => {
  139. imgSrc
  140. .clone()
  141. .rotate(90, true)
  142. .getJGDSync()
  143. .should.be.sameJGD(
  144. mkJGD(
  145. ' ',
  146. '▰▪▪▪▴▴▴▦ ',
  147. '▪▪▪▪▴▴▴▴ ',
  148. '▪▪▪▪▴▴▴▴ ',
  149. '▪▪▪▪▴▴▴▴ ',
  150. '▴▴▴▴▪▪▪▪ ',
  151. '▴▴▴▴▪▪▪▪ ',
  152. '▴▴▴▴▪▪▪▪ ',
  153. '▰▴▴▴▪▪▪▦ ',
  154. ' '
  155. )
  156. );
  157. });
  158. it('120 degrees', () => {
  159. imgSrc
  160. .clone()
  161. .rotate(120, true)
  162. .getJGDSync()
  163. .should.be.sameJGD(
  164. mkJGD(
  165. ' ',
  166. ' ▴▦ ',
  167. ' ▴▴▴▴ ',
  168. ' ▪▪▴▴▴▴ ',
  169. '▰▪▪▪▴▴▴▴▪ ',
  170. '▰▪▪▪▪▴▴▪▪▪ ',
  171. ' ▪▪▪▪▪▪▪▪▪ ',
  172. ' ▪▪▴▴▴▪▪▪▪▦ ',
  173. ' ▴▴▴▴▴▪▪ ',
  174. ' ▴▴▴▴▪ ',
  175. ' ▴▴▴ ',
  176. ' '
  177. )
  178. );
  179. });
  180. it('135 degrees', () => {
  181. imgSrc
  182. .clone()
  183. .rotate(135, true)
  184. .getJGDSync()
  185. .should.be.sameJGD(
  186. mkJGD(
  187. ' ',
  188. ' ',
  189. ' ▦ ',
  190. ' ▴▴▴ ',
  191. ' ▴▴▴▴▴ ',
  192. ' ▪▪▴▴▴▪▪ ',
  193. ' ▪▪▪▪▴▪▪▪▪ ',
  194. ' ▰▪▪▪▪▴▪▪▪▪▦ ',
  195. ' ▪▪▪▴▴▴▪▪▪ ',
  196. ' ▪▴▴▴▴▴▪ ',
  197. ' ▴▴▴▴▴ ',
  198. ' ▴▰▴ ',
  199. ' ▰ ',
  200. ' '
  201. )
  202. );
  203. });
  204. it('180 degrees', () => {
  205. imgSrc
  206. .clone()
  207. .rotate(180, true)
  208. .getJGDSync()
  209. .should.be.sameJGD(
  210. mkJGD(
  211. ' ',
  212. ' ▦▴▴▴▪▪▪▦ ',
  213. ' ▴▴▴▴▪▪▪▪ ',
  214. ' ▴▴▴▴▪▪▪▪ ',
  215. ' ▴▴▴▴▪▪▪▪ ',
  216. ' ▪▪▪▪▴▴▴▴ ',
  217. ' ▪▪▪▪▴▴▴▴ ',
  218. ' ▪▪▪▪▴▴▴▴ ',
  219. ' ▰▪▪▪▴▴▴▰ ',
  220. ' '
  221. )
  222. );
  223. });
  224. it('225 degrees', () => {
  225. imgSrc
  226. .clone()
  227. .rotate(225, true)
  228. .getJGDSync()
  229. .should.be.sameJGD(
  230. mkJGD(
  231. ' ',
  232. ' ▦ ',
  233. ' ▪▪▪ ',
  234. ' ▪▪▪▪▪ ',
  235. ' ▴▪▪▪▪▴▴ ',
  236. ' ▴▴▴▪▪▴▴▴▴ ',
  237. ' ▦▴▴▴▴▴▴▴▴▰▰ ',
  238. ' ▴▴▴▪▪▴▴▴▴ ',
  239. ' ▴▪▪▪▪▴▴ ',
  240. ' ▪▪▪▪▪ ',
  241. ' ▪▪▪ ',
  242. ' ▰ ',
  243. ' ',
  244. ' '
  245. )
  246. );
  247. });
  248. it('270 degrees', () => {
  249. imgSrc
  250. .clone()
  251. .rotate(270, true)
  252. .getJGDSync()
  253. .should.be.sameJGD(
  254. mkJGD(
  255. ' ▦▪▪▪▴▴▴▰ ',
  256. ' ▪▪▪▪▴▴▴▴ ',
  257. ' ▪▪▪▪▴▴▴▴ ',
  258. ' ▪▪▪▪▴▴▴▴ ',
  259. ' ▴▴▴▴▪▪▪▪ ',
  260. ' ▴▴▴▴▪▪▪▪ ',
  261. ' ▴▴▴▴▪▪▪▪ ',
  262. ' ▦▴▴▴▪▪▪▰ ',
  263. ' ',
  264. ' '
  265. )
  266. );
  267. });
  268. it('315 degrees', () => {
  269. imgSrc
  270. .clone()
  271. .rotate(315, true)
  272. .getJGDSync()
  273. .should.be.sameJGD(
  274. mkJGD(
  275. ' ▰ ',
  276. ' ▴▰▴ ',
  277. ' ▴▴▴▴▴ ',
  278. ' ▪▴▴▴▴▴▪ ',
  279. ' ▪▪▪▴▴▴▪▪▪ ',
  280. ' ▦▪▪▪▪▴▪▪▪▪▰ ',
  281. ' ▪▪▪▪▴▪▪▪▪ ',
  282. ' ▪▪▴▴▴▪▪ ',
  283. ' ▴▴▴▴▴ ',
  284. ' ▴▴▴ ',
  285. ' ▦ ',
  286. ' ',
  287. ' ',
  288. ' '
  289. )
  290. );
  291. });
  292. it('360 degrees', () => {
  293. imgSrc
  294. .clone()
  295. .rotate(360, true)
  296. .getJGDSync()
  297. .should.be.sameJGD(
  298. mkJGD(
  299. '▰▴▴▴▪▪▪▰ ',
  300. '▴▴▴▴▪▪▪▪ ',
  301. '▴▴▴▴▪▪▪▪ ',
  302. '▴▴▴▴▪▪▪▪ ',
  303. '▪▪▪▪▴▴▴▴ ',
  304. '▪▪▪▪▴▴▴▴ ',
  305. '▪▪▪▪▴▴▴▴ ',
  306. '▦▪▪▪▴▴▴▦ ',
  307. ' ',
  308. ' '
  309. )
  310. );
  311. });
  312. });
  313. describe('Rotate a image with odd size', () => {
  314. let imgSrc = null;
  315. before(done => {
  316. jimp
  317. .read(
  318. mkJGD(
  319. '▴▴▴▦▪▪▪',
  320. '▴▴▴▦▪▪▪',
  321. '▴▴▴▦▪▪▪',
  322. '▦▦▦▦▦▦▦',
  323. '▴▴▴▦▴▴▴',
  324. '▴▴▴▦▴▴▴',
  325. '▴▴▴▦▴▴▴'
  326. )
  327. )
  328. .then(imgJimp => {
  329. imgSrc = imgJimp;
  330. done();
  331. })
  332. .catch(done);
  333. });
  334. it('45 degrees', () => {
  335. imgSrc
  336. .clone()
  337. .rotate(45, true)
  338. .getJGDSync()
  339. .should.be.sameJGD(
  340. mkJGD(
  341. ' ',
  342. ' ▪ ',
  343. ' ▪▪▪ ',
  344. ' ▦▪▪▪▦ ',
  345. ' ▴▴▦▪▦▴▴ ',
  346. ' ▴▴▴▴▦▴▴▴▴ ',
  347. ' ▴▴▦▴▦▴▴ ',
  348. ' ▦▴▴▴▦ ',
  349. ' ▴▴▴ ',
  350. ' ▴ ',
  351. ' ',
  352. ' '
  353. )
  354. );
  355. });
  356. it('135 degrees', () => {
  357. imgSrc
  358. .clone()
  359. .rotate(135, true)
  360. .getJGDSync()
  361. .should.be.sameJGD(
  362. mkJGD(
  363. ' ',
  364. ' ▴ ',
  365. ' ▴▴▴ ',
  366. ' ▦▴▴▴▦ ',
  367. ' ▪▪▦▴▦▴▴ ',
  368. ' ▪▪▪▪▦▴▴▴▴ ',
  369. ' ▪▪▦▴▦▴▴ ',
  370. ' ▦▴▴▴▦ ',
  371. ' ▴▴▴ ',
  372. ' ▴ ',
  373. ' ',
  374. ' '
  375. )
  376. );
  377. });
  378. it('225 degrees', () => {
  379. imgSrc
  380. .clone()
  381. .rotate(225, true)
  382. .getJGDSync()
  383. .should.be.sameJGD(
  384. mkJGD(
  385. ' ',
  386. ' ▴ ',
  387. ' ▴▴▴ ',
  388. ' ▦▴▴▴▦ ',
  389. ' ▴▴▦▴▦▴▴ ',
  390. ' ▴▴▴▴▦▴▴▴▴ ',
  391. ' ▴▴▦▪▦▴▴ ',
  392. ' ▦▪▪▪▦ ',
  393. ' ▪▪▪ ',
  394. ' ▪ ',
  395. ' ',
  396. ' '
  397. )
  398. );
  399. });
  400. it('315 degrees', () => {
  401. imgSrc
  402. .clone()
  403. .rotate(315, true)
  404. .getJGDSync()
  405. .should.be.sameJGD(
  406. mkJGD(
  407. ' ',
  408. ' ▴ ',
  409. ' ▴▴▴ ',
  410. ' ▦▴▴▴▦ ',
  411. ' ▴▴▦▴▦▪▪ ',
  412. ' ▴▴▴▴▦▪▪▪▪ ',
  413. ' ▴▴▦▴▦▪▪ ',
  414. ' ▦▴▴▴▦ ',
  415. ' ▴▴▴ ',
  416. ' ▴ ',
  417. ' ',
  418. ' '
  419. )
  420. );
  421. });
  422. });
  423. describe('Rotate a non-square image', () => {
  424. let imgSrc = null;
  425. before(done => {
  426. jimp
  427. .read(mkJGD('▴▴▴▴▪▪▪▪', '▴▴▴▴▪▪▪▪', '▦▦▦▦▴▴▴▴', '▦▦▦▦▴▴▴▴'))
  428. .then(imgJimp => {
  429. imgSrc = imgJimp;
  430. done();
  431. })
  432. .catch(done);
  433. });
  434. it('1 degrees', () => {
  435. imgSrc
  436. .clone()
  437. .rotate(1, true)
  438. .getJGDSync()
  439. .should.be.sameJGD(
  440. mkJGD(
  441. '▴▴▴▴▪▪▪▪ ',
  442. '▴▴▴▴▪▪▪▪ ',
  443. '▦▦▦▦▴▴▴▴ ',
  444. '▦▦▦▦▴▴▴▴ ',
  445. ' ',
  446. ' '
  447. )
  448. );
  449. });
  450. it('10 degrees', () => {
  451. imgSrc
  452. .clone()
  453. .rotate(10, true)
  454. .getJGDSync()
  455. .should.be.sameJGD(
  456. mkJGD(
  457. ' ▪ ',
  458. ' ▴▴▴▪▪▪▪ ',
  459. '▴▴▴▴▪▪▪▴ ',
  460. '▴▴▦▦▴▴▴▴ ',
  461. '▦▦▦▦▴▴▴ ',
  462. '▦▦ ',
  463. ' ',
  464. ' '
  465. )
  466. );
  467. });
  468. it('30 degrees', () => {
  469. imgSrc
  470. .clone()
  471. .rotate(30, true)
  472. .getJGDSync()
  473. .should.be.sameJGD(
  474. mkJGD(
  475. ' ',
  476. ' ▪▪ ',
  477. ' ▪▪▪▪ ',
  478. ' ▴▴▪▪▴▴▴ ',
  479. '▴▴▴▦▴▴▴ ',
  480. '▴▴▦▦▴▴ ',
  481. '▦▦▦▦ ',
  482. ' ▦ ',
  483. ' ',
  484. ' '
  485. )
  486. );
  487. });
  488. it('45 degrees', () => {
  489. imgSrc
  490. .clone()
  491. .rotate(45, true)
  492. .getJGDSync()
  493. .should.be.sameJGD(
  494. mkJGD(
  495. ' ',
  496. ' ▪▪ ',
  497. ' ▪▪▪▴ ',
  498. ' ▴▪▪▴▴▴ ',
  499. ' ▴▴▴▴▴▴ ',
  500. '▴▴▴▦▦▴ ',
  501. '▴▴▦▦▦ ',
  502. ' ▦▦▦ ',
  503. ' ▦ ',
  504. ' '
  505. )
  506. );
  507. });
  508. it('90 degrees', () => {
  509. imgSrc
  510. .clone()
  511. .rotate(90, true)
  512. .getJGDSync()
  513. .should.be.sameJGD(
  514. mkJGD(
  515. ' ',
  516. '▪▪▴▴ ',
  517. '▪▪▴▴ ',
  518. '▪▪▴▴ ',
  519. '▪▪▴▴ ',
  520. '▴▴▦▦ ',
  521. '▴▴▦▦ ',
  522. '▴▴▦▦ ',
  523. '▴▴▦▦ ',
  524. ' '
  525. )
  526. );
  527. });
  528. it('135 degrees', () => {
  529. imgSrc
  530. .clone()
  531. .rotate(135, true)
  532. .getJGDSync()
  533. .should.be.sameJGD(
  534. mkJGD(
  535. ' ',
  536. ' ▴ ',
  537. ' ▴▴▴ ',
  538. ' ▪▪▴▴▴ ',
  539. ' ▪▪▪▴▦▦ ',
  540. ' ▪▪▴▦▦▦ ',
  541. ' ▴▴▴▦▦▦ ',
  542. ' ▴▴▴▦ ',
  543. ' ▴▴ ',
  544. ' '
  545. )
  546. );
  547. });
  548. it('180 degrees', () => {
  549. imgSrc
  550. .clone()
  551. .rotate(180, true)
  552. .getJGDSync()
  553. .should.be.sameJGD(
  554. mkJGD(
  555. ' ',
  556. ' ▴▴▴▴▦▦▦▦ ',
  557. ' ▴▴▴▴▦▦▦▦ ',
  558. ' ▪▪▪▪▴▴▴▴ ',
  559. ' ▪▪▪▪▴▴▴▴ ',
  560. ' '
  561. )
  562. );
  563. });
  564. it('225 degrees', () => {
  565. imgSrc
  566. .clone()
  567. .rotate(225, true)
  568. .getJGDSync()
  569. .should.be.sameJGD(
  570. mkJGD(
  571. ' ▦ ',
  572. ' ▦▦▦ ',
  573. ' ▦▦▦▴▴ ',
  574. ' ▴▦▦▴▴▴ ',
  575. ' ▴▴▴▴▴▴ ',
  576. ' ▴▴▴▪▪▴ ',
  577. ' ▴▪▪▪ ',
  578. ' ▪▪ ',
  579. ' ',
  580. ' '
  581. )
  582. );
  583. });
  584. it('315 degrees', () => {
  585. imgSrc
  586. .clone()
  587. .rotate(315, true)
  588. .getJGDSync()
  589. .should.be.sameJGD(
  590. mkJGD(
  591. ' ▴▴ ',
  592. ' ▦▴▴▴ ',
  593. '▦▦▦▴▴▴ ',
  594. ' ▦▦▦▴▪▪ ',
  595. ' ▦▦▴▪▪▪ ',
  596. ' ▴▴▴▪▪ ',
  597. ' ▴▴▴ ',
  598. ' ▴ ',
  599. ' ',
  600. ' '
  601. )
  602. );
  603. });
  604. });