interlace.js 1.9 KB

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