UNPKG

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