interlace.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. // Adam 7
  3. // 0 1 2 3 4 5 6 7
  4. // 0 x 6 4 6 x 6 4 6
  5. // 1 7 7 7 7 7 7 7 7
  6. // 2 5 6 5 6 5 6 5 6
  7. // 3 7 7 7 7 7 7 7 7
  8. // 4 3 6 4 6 3 6 4 6
  9. // 5 7 7 7 7 7 7 7 7
  10. // 6 5 6 5 6 5 6 5 6
  11. // 7 7 7 7 7 7 7 7 7
  12. let imagePasses = [
  13. {
  14. // pass 1 - 1px
  15. x: [0],
  16. y: [0],
  17. },
  18. {
  19. // pass 2 - 1px
  20. x: [4],
  21. y: [0],
  22. },
  23. {
  24. // pass 3 - 2px
  25. x: [0, 4],
  26. y: [4],
  27. },
  28. {
  29. // pass 4 - 4px
  30. x: [2, 6],
  31. y: [0, 4],
  32. },
  33. {
  34. // pass 5 - 8px
  35. x: [0, 2, 4, 6],
  36. y: [2, 6],
  37. },
  38. {
  39. // pass 6 - 16px
  40. x: [1, 3, 5, 7],
  41. y: [0, 2, 4, 6],
  42. },
  43. {
  44. // pass 7 - 32px
  45. x: [0, 1, 2, 3, 4, 5, 6, 7],
  46. y: [1, 3, 5, 7],
  47. },
  48. ];
  49. exports.getImagePasses = function (width, height) {
  50. let images = [];
  51. let xLeftOver = width % 8;
  52. let yLeftOver = height % 8;
  53. let xRepeats = (width - xLeftOver) / 8;
  54. let yRepeats = (height - yLeftOver) / 8;
  55. for (let i = 0; i < imagePasses.length; i++) {
  56. let pass = imagePasses[i];
  57. let passWidth = xRepeats * pass.x.length;
  58. let passHeight = yRepeats * pass.y.length;
  59. for (let j = 0; j < pass.x.length; j++) {
  60. if (pass.x[j] < xLeftOver) {
  61. passWidth++;
  62. } else {
  63. break;
  64. }
  65. }
  66. for (let j = 0; j < pass.y.length; j++) {
  67. if (pass.y[j] < yLeftOver) {
  68. passHeight++;
  69. } else {
  70. break;
  71. }
  72. }
  73. if (passWidth > 0 && passHeight > 0) {
  74. images.push({ width: passWidth, height: passHeight, index: i });
  75. }
  76. }
  77. return images;
  78. };
  79. exports.getInterlaceIterator = function (width) {
  80. return function (x, y, pass) {
  81. let outerXLeftOver = x % imagePasses[pass].x.length;
  82. let outerX =
  83. ((x - outerXLeftOver) / imagePasses[pass].x.length) * 8 +
  84. imagePasses[pass].x[outerXLeftOver];
  85. let outerYLeftOver = y % imagePasses[pass].y.length;
  86. let outerY =
  87. ((y - outerYLeftOver) / imagePasses[pass].y.length) * 8 +
  88. imagePasses[pass].y[outerYLeftOver];
  89. return outerX * 4 + outerY * width * 4;
  90. };
  91. };