-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
medianCut.ts
276 lines (254 loc) · 7.46 KB
/
medianCut.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { ColorHistogram, getHistogram } from "../mod.ts";
import { Color } from "../../deps/color.ts";
import { getAverageColor, getColorRange } from "./common.ts";
import type { ColorRange } from "./common.ts";
export function quantizeByMedianCut(
pixels: Color[],
extractCount: number,
): Color[] {
const vbox = getColorRange(pixels);
const histo = getHistogram(pixels);
return quantize(vbox, histo, extractCount);
}
function quantize(
vbox: ColorRange,
histo: ColorHistogram,
extractCount: number,
): Color[] {
const vboxes: ColorRange[] = [vbox];
// Avoid an infinite loop
const maxIter = 1000;
let i = 0;
const firstExtractCount = ~~(extractCount >> 1);
let generated = 1;
while (i < maxIter) {
const lastBox = vboxes.shift();
if (!lastBox) break; // This shouldn't happen
if (!vboxSize(lastBox, histo)) {
vboxes.push(lastBox);
i += 1;
continue;
}
const cut = medianCutApply(lastBox, histo);
if (cut) {
vboxes.push(cut[0], cut[1]);
generated += 1;
} else vboxes.push(lastBox);
if (generated >= firstExtractCount) break;
}
vboxes.sort((a, b) =>
(vboxSize(b, histo) * vboxVolume(b)) - (vboxSize(a, histo) * vboxVolume(a))
);
const secondExtractCount = extractCount - vboxes.length;
i = 0;
generated = 0;
while (i < maxIter) {
const lastBox = vboxes.shift();
if (!lastBox) break; // This shouldn't happen
if (!vboxSize(lastBox, histo)) {
vboxes.push(lastBox);
i += 1;
continue;
}
const cut = medianCutApply(lastBox, histo);
if (cut) {
vboxes.push(cut[0], cut[1]);
generated += 1;
} else vboxes.push(lastBox);
if (generated >= secondExtractCount) break;
}
vboxes.sort((a, b) => vboxSize(b, histo) - vboxSize(a, histo));
return vboxes.map((x) => getAverageColor(x, histo)).slice(0, extractCount);
}
/** Get number of colors in vbox */
function vboxSize(vbox: ColorRange, histo: ColorHistogram): number {
let count = 0;
let ri = vbox.r.min;
while (ri <= vbox.r.max) {
let gi = vbox.g.min;
while (gi <= vbox.g.max) {
let bi = vbox.b.min;
while (bi <= vbox.b.max) {
count += histo.get(new Color(ri, gi, bi, 255)) || 0;
bi += 1;
}
gi += 1;
}
ri += 1;
}
return count;
}
/** Get volume by dimensions of vbox */
function vboxVolume(vbox: ColorRange): number {
return ~~(vbox.r.max - vbox.r.min) * ~~(vbox.g.max - vbox.g.min) *
~~(vbox.b.max - vbox.b.min);
}
/** Cut vbox into two */
function medianCutApply(
vbox: ColorRange,
histo: ColorHistogram,
): [ColorRange, ColorRange] | false {
const count = vboxSize(vbox, histo);
if (!count || count === 1) return false;
const rw = vbox.r.max - vbox.r.min + 1;
const gw = vbox.g.max - vbox.g.min + 1;
const bw = vbox.b.max - vbox.b.min + 1;
const axis = Math.max(rw, gw, bw);
// Find partial sums along each axis
const sumAlongAxis = [];
// avoid running another loop to compute sum
let totalSum = 0;
switch (axis) {
case rw: {
let i = vbox.r.min;
while (i <= vbox.r.max) {
let tempSum = 0;
let j = vbox.g.min;
while (j < vbox.g.max) {
let k = vbox.b.min;
while (k < vbox.b.max) {
tempSum += histo.getQuantized([i, j, k]) || 0;
k += 1;
}
j += 1;
}
totalSum += tempSum;
sumAlongAxis[i] = totalSum;
i += 1;
}
break;
}
case gw: {
let i = vbox.g.min;
while (i <= vbox.g.max) {
let tempSum = 0;
let j = vbox.r.min;
while (j < vbox.r.max) {
let k = vbox.b.min;
while (k < vbox.b.max) {
tempSum += histo.getQuantized([j, i, k]) || 0;
k += 1;
}
j += 1;
}
totalSum += tempSum;
sumAlongAxis[i] = totalSum;
i += 1;
}
break;
}
default: {
let i = vbox.b.min;
while (i <= vbox.b.max) {
let tempSum = 0;
let j = vbox.r.min;
while (j < vbox.r.max) {
let k = vbox.g.min;
while (k < vbox.g.max) {
tempSum += histo.getQuantized([j, k, i]) || 0;
k += 1;
}
j += 1;
}
totalSum += tempSum;
sumAlongAxis[i] = totalSum;
i += 1;
}
break;
}
}
// Apply median cut
switch (axis) {
case rw: {
let i = vbox.r.min;
while (i <= vbox.r.max) {
// Find the mid point through linear search
if (sumAlongAxis[i] < totalSum / 2) {
let cutAt = 0;
const vbox1 = {
r: { min: vbox.r.min, max: vbox.r.max },
g: { min: vbox.g.min, max: vbox.g.max },
b: { min: vbox.b.min, max: vbox.b.max },
};
const vbox2 = {
r: { min: vbox.r.min, max: vbox.r.max },
g: { min: vbox.g.min, max: vbox.g.max },
b: { min: vbox.b.min, max: vbox.b.max },
};
const left = i - vbox.r.min;
const right = vbox.r.max - i;
if (left <= right) {
cutAt = Math.min(vbox.r.max - 1, Math.trunc(i + right / 2));
} else cutAt = Math.max(vbox.r.min, Math.trunc(i - 1 - left / 2));
while (!sumAlongAxis[cutAt]) cutAt += 1;
vbox1.r.max = cutAt;
vbox2.r.min = cutAt + 1;
return [vbox1, vbox2];
}
i += 1;
}
break;
}
case gw: {
let i = vbox.g.min;
while (i <= vbox.g.max) {
// Find the mid point through linear search
if (sumAlongAxis[i] < totalSum / 2) {
let cutAt = 0;
const vbox1 = {
r: { min: vbox.r.min, max: vbox.r.max },
g: { min: vbox.g.min, max: vbox.g.max },
b: { min: vbox.b.min, max: vbox.b.max },
};
const vbox2 = {
r: { min: vbox.r.min, max: vbox.r.max },
g: { min: vbox.g.min, max: vbox.g.max },
b: { min: vbox.b.min, max: vbox.b.max },
};
const left = i - vbox.g.min;
const right = vbox.g.max - i;
if (left <= right) {
cutAt = Math.min(vbox.g.max - 1, Math.trunc(i + right / 2));
} else cutAt = Math.max(vbox.g.min, Math.trunc(i - 1 - left / 2));
while (!sumAlongAxis[cutAt]) cutAt += 1;
vbox1.g.max = cutAt;
vbox2.g.min = cutAt + 1;
return [vbox1, vbox2];
}
i += 1;
}
break;
}
default: {
let i = vbox.b.min;
while (i <= vbox.b.max) {
// Find the mid point through linear search
if (sumAlongAxis[i] < totalSum / 2) {
let cutAt = 0;
const vbox1 = {
r: { min: vbox.r.min, max: vbox.r.max },
g: { min: vbox.g.min, max: vbox.g.max },
b: { min: vbox.b.min, max: vbox.b.max },
};
const vbox2 = {
r: { min: vbox.r.min, max: vbox.r.max },
g: { min: vbox.g.min, max: vbox.g.max },
b: { min: vbox.b.min, max: vbox.b.max },
};
const left = i - vbox.b.min;
const right = vbox.b.max - i;
if (left <= right) {
cutAt = Math.min(vbox.b.max - 1, Math.trunc(i + right / 2));
} else cutAt = Math.max(vbox.b.min, Math.trunc(i - 1 - left / 2));
while (!sumAlongAxis[cutAt]) cutAt += 1;
vbox1.b.max = cutAt;
vbox2.b.min = cutAt + 1;
return [vbox1, vbox2];
}
i += 1;
}
break;
}
}
return false;
}