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)
188 lines (187 loc) • 6.67 kB
TypeScript
/** @format */
import { Color } from '../color/color.js';
import { MemoryImage } from './image.js';
import { PaletteUint8 } from './palette-uint8.js';
import { Quantizer } from './quantizer.js';
/**
* Compute a color map with a given number of colors that best represents
* the given image.
*/
export declare class NeuralQuantizer implements Quantizer {
/** Number of learning cycles */
private static readonly _numCycles;
/** Alpha starts at 1 */
private static readonly _alphaBiasShift;
/** Initial alpha value, biased by 10 bits */
private static readonly _initAlpha;
/** Radius bias shift value */
private static readonly _radiusBiasShift;
/** Radius bias value */
private static readonly _radiusBias;
/** Alpha radius bias shift value */
private static readonly _alphaRadiusBiasShift;
/** Alpha radius bias value */
private static readonly alphaRadiusBias;
/** Radius decrement factor, 1/30 each cycle */
private static readonly _radiusDec;
/** Gamma value */
private static readonly _gamma;
/** Beta value */
private static readonly _beta;
/** Beta times gamma value */
private static readonly _betaGamma;
/** Prime number 1, used for image length assumption */
private static readonly _prime1;
/** Prime number 2, used for image length assumption */
private static readonly _prime2;
/** Prime number 3, used for image length assumption */
private static readonly _prime3;
/** Prime number 4, used for image length assumption */
private static readonly _prime4;
/** Small image bytes calculation */
private static readonly _smallImageBytes;
/** Network index array */
private readonly _netIndex;
/** Sampling factor */
private _samplingFactor;
/** Number of colors used */
private _netSize;
/** Number of reserved colors used */
private _specials;
/** Reserved background color */
private _bgColor;
/** Cut network size */
private _cutNetSize;
/** Maximum network position */
private _maxNetPos;
/** Initial radius value, starts at 32 for 256 colors */
private _initRadius;
/** Initial bias radius value */
private _initBiasRadius;
/** Radius power array */
private _radiusPower;
/** The network itself */
private _network;
/** Internal palette */
private _paletteInternal;
/** External palette */
private _palette;
/** Get the palette */
get palette(): PaletteUint8;
/** Bias array for learning */
private _bias;
/** Frequency array for learning */
private _freq;
/** How many colors are in the palette? */
get numColors(): number;
/**
* 10 is a reasonable samplingFactor according to
* https://scientificgems.wordpress.com/stuff/neuquant-fast-high-quality-image-quantization/.
* @param {MemoryImage} image - The image to be quantized.
* @param {number} [numberOfColors=256] - The number of colors to quantize to.
* @param {number} [samplingFactor=10] - The sampling factor.
*/
constructor(image: MemoryImage, numberOfColors?: number, samplingFactor?: number);
/**
* Initialize the quantizer with the given number of colors.
* @param {number} numberOfColors - The number of colors to initialize with.
*/
private initialize;
/**
* Update the radius power.
* @param {number} rad - The radius.
* @param {number} alpha - The alpha value.
*/
private updateRadiusPower;
/**
* Find a special color in the network.
* @param {number} b - Blue component.
* @param {number} g - Green component.
* @param {number} r - Red component.
* @returns {number} The index of the special color.
*/
private specialFind;
/**
* Search for biased BGR values.
* @param {number} b - Blue component.
* @param {number} g - Green component.
* @param {number} r - Red component.
* @returns {number} The index of the closest color.
*/
private contest;
/**
* Move neuron i towards biased (b,g,r) by factor alpha.
* @param {number} alpha - The alpha value.
* @param {number} i - The neuron index.
* @param {number} b - Blue component.
* @param {number} g - Green component.
* @param {number} r - Red component.
*/
private alterSingle;
/**
* Alter neighbors of neuron i.
* @param {number} _ - Unused parameter.
* @param {number} rad - The radius.
* @param {number} i - The neuron index.
* @param {number} b - Blue component.
* @param {number} g - Green component.
* @param {number} r - Red component.
*/
private alterNeighbors;
/**
* Learn from the image.
* @param {MemoryImage} image - The image to learn from.
*/
private learn;
/**
* Fix the palette.
*/
private fix;
/**
* Insertion sort of network and building of netindex[0..255].
*/
private inxBuild;
/**
* Copy the palette.
*/
private copyPalette;
/**
* Search for BGR values 0..255 and return color index
* @param {number} b - Blue component (0-255)
* @param {number} g - Green component (0-255)
* @param {number} r - Red component (0-255)
* @returns {number} The index of the closest color in the palette
*/
private inxSearch;
/**
* Find the index of the closest color to **c** in the **palette**.
* @param {Color} c - The color to find the closest index for
* @returns {number} The index of the closest color in the palette
*/
getColorIndex(c: Color): number;
/**
* Find the index of the closest color to **r**,**g**,**b** in the **palette**.
* @param {number} r - Red component (0-255)
* @param {number} g - Green component (0-255)
* @param {number} b - Blue component (0-255)
* @returns {number} The index of the closest color in the palette
*/
getColorIndexRgb(r: number, g: number, b: number): number;
/**
* Find the color closest to **c** in the **palette**.
* @param {Color} c - The color to find the closest match for
* @returns {Color} The closest color in the palette
*/
getQuantizedColor(c: Color): Color;
/**
* Convert the **image** to a palette image.
* @param {MemoryImage} image - The image to convert
* @returns {MemoryImage} The converted palette image
*/
getIndexImage(image: MemoryImage): MemoryImage;
/**
* Add an image to the quantized color table.
* @param {MemoryImage} image - The image to add
*/
addImage(image: MemoryImage): void;
}