image-in-browser
Version:
Package for encoding / decoding images, transforming images, applying filters, drawing primitives on images on the client side (no need for server Node.js)
50 lines • 1.96 kB
JavaScript
import { ColorRgb8 } from '../color/color-rgb8.js';
import { ColorUtils } from '../color/color-utils.js';
import { MemoryImage } from './image.js';
import { PaletteUint8 } from './palette-uint8.js';
export class BinaryQuantizer {
get palette() {
return this._palette;
}
get threshold() {
return this._threshold;
}
constructor(threshold = 0.5) {
this._palette = new PaletteUint8(2, 3);
this._threshold = threshold;
this._palette.setRgb(1, 255, 255, 255);
}
getColorIndex(c) {
return c.luminanceNormalized < this._threshold ? 0 : 1;
}
getColorIndexRgb(r, g, b) {
return ColorUtils.getLuminanceRgb(r, g, b) < this._threshold ? 0 : 1;
}
getQuantizedColor(c) {
return c.luminanceNormalized < this._threshold
? new ColorRgb8(Math.trunc(this._palette.getRed(0)), Math.trunc(this._palette.getGreen(0)), Math.trunc(this._palette.getBlue(0)))
: new ColorRgb8(Math.trunc(this._palette.getRed(1)), Math.trunc(this._palette.getGreen(1)), Math.trunc(this._palette.getBlue(1)));
}
getIndexImage(image) {
const target = new MemoryImage({
width: image.width,
height: image.height,
numChannels: 1,
palette: this.palette,
});
target.frameIndex = image.frameIndex;
target.frameType = image.frameType;
target.frameDuration = image.frameDuration;
const imageIt = image[Symbol.iterator]();
const targetIt = target[Symbol.iterator]();
let imageItRes = undefined;
let targetItRes = undefined;
while ((((imageItRes = imageIt.next()), (targetItRes = targetIt.next())),
!imageItRes.done && !targetItRes.done)) {
const t = targetItRes.value;
t.setChannel(0, this.getColorIndex(imageItRes.value));
}
return target;
}
}
//# sourceMappingURL=binary-quantizer.js.map