Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var num_pixels = width * height;
this.logDebug(6, "Quantizing image", { colors: opts.colors, dither: opts.dither, width: width, height: height, pixels: num_pixels } );
// get rgba pixels from canvas
var imgData = this.context.getImageData(0, 0, width, height);
// desired colors number
var targetColors = parseInt( opts.colors || 0 );
if (!targetColors || isNaN(targetColors) || (targetColors < 2) || (targetColors > 256)) {
return this.doError('quantize', "Invalid palette size: " + targetColors);
}
// create pointContainer and fill it with image
var pointContainer = iq.utils.PointContainer.fromImageData( imgData );
// create chosen distance calculator (see classes inherited from `iq.distance.AbstractDistanceCalculator`)
var distanceCalculator = opts.alpha ?
(new iq.distance.EuclideanRgbQuantWithAlpha()) :
(new iq.distance.EuclideanRgbQuantWOAlpha());
// create chosen palette quantizer (see classes implementing `iq.palette.IPaletteQuantizer`)
var paletteQuantizer = new iq.palette[opts.quantizer](distanceCalculator, targetColors);
// feed out pointContainer filled with image to paletteQuantizer
paletteQuantizer.sample(pointContainer);
// generate palette
var palette = paletteQuantizer.quantize();
// get color array
images.forEach(image => {
const imageBuf = image.bitmap.data;
const inputBuf = new ArrayBuffer(imageBuf.length);
const inputArray = new Uint32Array(inputBuf);
for (let bi = 0, ai = 0; bi < imageBuf.length; bi += 4, ++ai) {
inputArray[ai] = imageBuf.readUInt32LE(bi, true);
}
const inputContainer = ImageQ.utils.PointContainer.fromUint32Array(
inputArray, image.bitmap.width, image.bitmap.height);
quantizer.sample(inputContainer);
inputContainers.push(inputContainer);
});