neuquant.ts
12 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* NeuQuant Neural-Net Quantization Algorithm
* ------------------------------------------
*
* Copyright (c) 1994 Anthony Dekker
*
* NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994. See
* "Kohonen neural networks for optimal colour quantization" in "Network:
* Computation in Neural Systems" Vol. 5 (1994) pp 351-367. for a discussion of
* the algorithm.
*
* Any party obtaining a copy of these files from the author, directly or
* indirectly, is granted, free of charge, a full and unrestricted irrevocable,
* world-wide, paid up, royalty-free, nonexclusive right and license to deal in
* this software and documentation files (the "Software"), including without
* limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons who
* receive copies from any such party to do so, with the only requirement being
* that this copyright notice remain intact.
*/
/**
* @preserve TypeScript port:
* Copyright 2015-2016 Igor Bezkrovnyi
* All rights reserved. (MIT Licensed)
*
* neuquant.ts - part of Image Quantization Library
*/
import { Palette } from "../../utils/palette"
import { Point } from "../../utils/point"
import { PointContainer } from "../../utils/pointContainer"
import { AbstractDistanceCalculator } from "../../distance/abstractDistanceCalculator"
import { IPaletteQuantizer } from "../common"
// bias for colour values
const networkBiasShift = 3;
class Neuron {
r : number;
g : number;
b : number;
a : number;
constructor(defaultValue : number) {
this.r = this.g = this.b = this.a = defaultValue;
}
/**
* There is a fix in original NEUQUANT by Anthony Dekker (http://members.ozemail.com.au/~dekker/NEUQUANT.HTML)
* @example
* r = Math.min(255, (neuron.r + (1 << (networkBiasShift - 1))) >> networkBiasShift);
*/
toPoint() : Point {
return Point.createByRGBA(this.r >> networkBiasShift, this.g >> networkBiasShift, this.b >> networkBiasShift, this.a >> networkBiasShift);
}
subtract(r : number, g : number, b : number, a : number) : void {
this.r -= r | 0;
this.g -= g | 0;
this.b -= b | 0;
this.a -= a | 0;
}
/*
public subtract(r : number, g : number, b : number, a : number) : void {
this.r = (-r + this.r) | 0;
this.g = (-g + this.g) | 0;
this.b = (-b + this.b) | 0;
this.a = (-a + this.a) | 0;
this.r -= r;
this.g -= g;
this.b -= b;
this.a -= a;
this.r -= r | 0;
this.g -= g | 0;
this.b -= b | 0;
this.a -= a | 0;
}
*/
}
export class NeuQuant implements IPaletteQuantizer {
/*
four primes near 500 - assume no image has a length so large
that it is divisible by all four primes
*/
private static readonly _prime1 : number = 499;
private static readonly _prime2 : number = 491;
private static readonly _prime3 : number = 487;
private static readonly _prime4 : number = 503;
private static readonly _minpicturebytes : number = NeuQuant._prime4;
// no. of learning cycles
private static readonly _nCycles : number = 100;
// defs for freq and bias
private static readonly _initialBiasShift : number = 16;
// bias for fractions
private static readonly _initialBias : number = (1 << NeuQuant._initialBiasShift);
private static readonly _gammaShift : number = 10;
// gamma = 1024
// TODO: why gamma is never used?
//private static _gamma : number = (1 << NeuQuant._gammaShift);
private static readonly _betaShift : number = 10;
private static readonly _beta : number = (NeuQuant._initialBias >> NeuQuant._betaShift);
// beta = 1/1024
private static readonly _betaGamma : number = (NeuQuant._initialBias << (NeuQuant._gammaShift - NeuQuant._betaShift));
/*
* for 256 cols, radius starts
*/
private static readonly _radiusBiasShift : number = 6;
// at 32.0 biased by 6 bits
private static readonly _radiusBias : number = 1 << NeuQuant._radiusBiasShift;
// and decreases by a factor of 1/30 each cycle
private static readonly _radiusDecrease : number = 30;
/* defs for decreasing alpha factor */
// alpha starts at 1.0
private static readonly _alphaBiasShift : number = 10;
// biased by 10 bits
private static readonly _initAlpha : number = (1 << NeuQuant._alphaBiasShift);
/* radBias and alphaRadBias used for radpower calculation */
private static readonly _radBiasShift : number = 8;
private static readonly _radBias : number = 1 << NeuQuant._radBiasShift;
private static readonly _alphaRadBiasShift : number = NeuQuant._alphaBiasShift + NeuQuant._radBiasShift;
private static readonly _alphaRadBias : number = 1 << NeuQuant._alphaRadBiasShift;
private _pointArray : Point[];
private readonly _networkSize : number;
private _network : Neuron[];
/** sampling factor 1..30 */
private readonly _sampleFactor : number;
private _radPower : number[];
// bias and freq arrays for learning
private _freq : number[];
/* for network lookup - really 256 */
private _bias : number[];
private readonly _distance : AbstractDistanceCalculator;
constructor(colorDistanceCalculator : AbstractDistanceCalculator, colors : number = 256) {
this._distance = colorDistanceCalculator;
this._pointArray = [];
this._sampleFactor = 1;
this._networkSize = colors;
this._distance.setWhitePoint(255 << networkBiasShift, 255 << networkBiasShift, 255 << networkBiasShift, 255 << networkBiasShift);
}
sample(pointBuffer : PointContainer) : void {
this._pointArray = this._pointArray.concat(pointBuffer.getPointArray());
}
quantize() : Palette {
this._init();
this._learn();
return this._buildPalette();
}
private _init() : void {
this._freq = [];
this._bias = [];
this._radPower = [];
this._network = [];
for (let i = 0; i < this._networkSize; i++) {
this._network[ i ] = new Neuron((i << (networkBiasShift + 8)) / this._networkSize | 0);
// 1/this._networkSize
this._freq[ i ] = NeuQuant._initialBias / this._networkSize | 0;
this._bias[ i ] = 0;
}
}
/**
* Main Learning Loop
*/
private _learn() : void {
let sampleFactor = this._sampleFactor;
const pointsNumber = this._pointArray.length;
if (pointsNumber < NeuQuant._minpicturebytes) sampleFactor = 1;
const alphadec = 30 + (sampleFactor - 1) / 3 | 0,
pointsToSample = pointsNumber / sampleFactor | 0;
let delta = pointsToSample / NeuQuant._nCycles | 0,
alpha = NeuQuant._initAlpha,
radius = (this._networkSize >> 3) * NeuQuant._radiusBias;
let rad = radius >> NeuQuant._radiusBiasShift;
if (rad <= 1) rad = 0;
for (let i = 0; i < rad; i++) {
this._radPower[ i ] = alpha * (((rad * rad - i * i) * NeuQuant._radBias) / (rad * rad)) >>> 0;
}
let step : number;
if (pointsNumber < NeuQuant._minpicturebytes) {
step = 1;
} else if (pointsNumber % NeuQuant._prime1 != 0) {
step = NeuQuant._prime1;
} else if ((pointsNumber % NeuQuant._prime2) != 0) {
step = NeuQuant._prime2;
} else if ((pointsNumber % NeuQuant._prime3) != 0) {
step = NeuQuant._prime3;
} else {
step = NeuQuant._prime4;
}
for (let i = 0, pointIndex = 0; i < pointsToSample;) {
const point = this._pointArray[ pointIndex ],
b = point.b << networkBiasShift,
g = point.g << networkBiasShift,
r = point.r << networkBiasShift,
a = point.a << networkBiasShift,
neuronIndex = this._contest(b, g, r, a);
this._alterSingle(alpha, neuronIndex, b, g, r, a);
if (rad !== 0) this._alterNeighbour(rad, neuronIndex, b, g, r, a);
/* alter neighbours */
pointIndex += step;
if (pointIndex >= pointsNumber) pointIndex -= pointsNumber;
i++;
if (delta === 0) delta = 1;
if (i % delta === 0) {
alpha -= (alpha / alphadec) | 0;
radius -= (radius / NeuQuant._radiusDecrease) | 0;
rad = radius >> NeuQuant._radiusBiasShift;
if (rad <= 1) rad = 0;
for (let j = 0; j < rad; j++) this._radPower[ j ] = alpha * (((rad * rad - j * j) * NeuQuant._radBias) / (rad * rad)) >>> 0;
}
}
}
private _buildPalette() : Palette {
const palette = new Palette();
this._network.forEach(neuron => {
palette.add(neuron.toPoint());
});
palette.sort();
return palette;
}
/**
* Move adjacent neurons by precomputed alpha*(1-((i-j)^2/[r]^2)) in radpower[|i-j|]
*/
private _alterNeighbour(rad : number, i : number, b : number, g : number, r : number, al : number) : void {
let lo = i - rad;
if (lo < -1) lo = -1;
let hi = i + rad;
if (hi > this._networkSize) hi = this._networkSize;
let j = i + 1,
k = i - 1,
m = 1;
while (j < hi || k > lo) {
const a = this._radPower[ m++ ] / NeuQuant._alphaRadBias;
if (j < hi) {
const p = this._network[ j++ ];
p.subtract(
a * (p.r - r),
a * (p.g - g),
a * (p.b - b),
a * (p.a - al)
);
}
if (k > lo) {
const p = this._network[ k-- ];
p.subtract(
a * (p.r - r),
a * (p.g - g),
a * (p.b - b),
a * (p.a - al)
);
}
}
}
/**
* Move neuron i towards biased (b,g,r) by factor alpha
*/
private _alterSingle(alpha : number, i : number, b : number, g : number, r : number, a : number) : void {
alpha /= NeuQuant._initAlpha;
/* alter hit neuron */
const n = this._network[ i ];
n.subtract(
alpha * (n.r - r),
alpha * (n.g - g),
alpha * (n.b - b),
alpha * (n.a - a)
);
}
/**
* Search for biased BGR values
* description:
* finds closest neuron (min dist) and updates freq
* finds best neuron (min dist-bias) and returns position
* for frequently chosen neurons, freq[i] is high and bias[i] is negative
* bias[i] = _gamma*((1/this._networkSize)-freq[i])
*
* Original distance equation:
* dist = abs(dR) + abs(dG) + abs(dB)
*/
private _contest(b : number, g : number, r : number, a : number) : number {
const multiplier = (255 * 4) << networkBiasShift;
let bestd = ~(1 << 31),
bestbiasd = bestd,
bestpos = -1,
bestbiaspos = bestpos;
for (let i = 0; i < this._networkSize; i++) {
const n = this._network[ i ],
dist = this._distance.calculateNormalized(<any>n, <any>{ r, g, b, a }) * multiplier | 0;
if (dist < bestd) {
bestd = dist;
bestpos = i;
}
const biasdist = dist - ((this._bias[ i ]) >> (NeuQuant._initialBiasShift - networkBiasShift));
if (biasdist < bestbiasd) {
bestbiasd = biasdist;
bestbiaspos = i;
}
const betafreq = (this._freq[ i ] >> NeuQuant._betaShift);
this._freq[ i ] -= betafreq;
this._bias[ i ] += (betafreq << NeuQuant._gammaShift);
}
this._freq[ bestpos ] += NeuQuant._beta;
this._bias[ bestpos ] -= NeuQuant._betaGamma;
return bestbiaspos;
}
}