java.lang.Object
org.miaixz.bus.extra.image.gif.NeuQuant
NeuQuant Neural-Net Quantizer.
This class provides a color quantization algorithm based on a neural network, which is used to reduce the number of colors in an image to a smaller palette (typically 256 colors for GIFs).
The algorithm is based on the work of Anthony Dekker.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intAlpha bias shift value for learning rate.protected intBiased alpha decrease factor.protected static final intAlpha-rad bias constant.protected static final intAlpha-rad bias shift value.protected static final intBeta shift value.protected int[]Bias array for learning.protected int[]Frequency array for learning.protected static final intGamma constant.protected static final intGamma shift value.protected static final intInitial alpha (learning rate) value.protected static final intInitial radius for convergence.protected static final intInitial radius value.protected static final intInteger bias constant.protected static final intBias for fractions.protected intThe total number of bytes in the input image (H*W*3).protected static final intThe maximum network position.protected static final intMinimum size for the input image in bytes.protected static final intNumber of learning cycles.protected static final intBias for color values.protected int[]A lookup table for the network.protected static final intThe number of colors used in the output palette.protected int[][]The neural network itself, storing BGR and index values.protected static final intFirst of four primes near 500, used for sampling.protected static final intSecond of four primes near 500, used for sampling.protected static final intThird of four primes near 500, used for sampling.protected static final intFourth of four primes near 500, used for sampling.protected static final intRad bias constant.protected static final intRad bias shift value.protected static final intRadius bias constant.protected static final intRadius bias shift value.protected static final intRadius decrease factor.protected int[]Precomputed values for radpower calculation.protected intThe sampling factor (1-30).protected byte[]The input image itself as a byte array. -
Constructor Summary
ConstructorsConstructorDescriptionNeuQuant(byte[] thepic, int len, int sample) Initializes the neural network with parameters. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidalterneigh(int rad, int i, int b, int g, int r) Moves adjacent neurons by a precomputed alpha factor based on their distance from the winning neuron.protected voidaltersingle(int alpha, int i, int b, int g, int r) Moves neuron `i` towards the biased (b,g,r) color by a factor of alpha.byte[]colorMap()Generates the color map from the trained network.protected intcontest(int b, int g, int r) Searches for the biased BGR values to find the winning neuron.voidinxbuild()Performs an insertion sort on the network and builds the `netindex` lookup table.voidlearn()The main learning loop for the neural network.intmap(int b, int g, int r) Searches for the BGR values (0-255) after the network is unbiased and returns the color index.byte[]process()Executes the complete quantization process.voidUnbiases the network to give byte values from 0 to 255 and records the position `i` to prepare for sorting.
-
Field Details
-
netsize
protected static final int netsizeThe number of colors used in the output palette.- See Also:
-
prime1
protected static final int prime1First of four primes near 500, used for sampling.- See Also:
-
prime2
protected static final int prime2Second of four primes near 500, used for sampling.- See Also:
-
prime3
protected static final int prime3Third of four primes near 500, used for sampling.- See Also:
-
prime4
protected static final int prime4Fourth of four primes near 500, used for sampling.- See Also:
-
minpicturebytes
protected static final int minpicturebytesMinimum size for the input image in bytes.- See Also:
-
maxnetpos
protected static final int maxnetposThe maximum network position.- See Also:
-
netbiasshift
protected static final int netbiasshiftBias for color values.- See Also:
-
ncycles
protected static final int ncyclesNumber of learning cycles.- See Also:
-
intbiasshift
protected static final int intbiasshiftBias for fractions.- See Also:
-
intbias
protected static final int intbiasInteger bias constant.- See Also:
-
gammashift
protected static final int gammashiftGamma shift value.- See Also:
-
gamma
protected static final int gammaGamma constant.- See Also:
-
betashift
protected static final int betashiftBeta shift value.- See Also:
-
initrad
protected static final int initradInitial radius for convergence.- See Also:
-
radiusbiasshift
protected static final int radiusbiasshiftRadius bias shift value.- See Also:
-
radiusbias
protected static final int radiusbiasRadius bias constant.- See Also:
-
initradius
protected static final int initradiusInitial radius value.- See Also:
-
radiusdec
protected static final int radiusdecRadius decrease factor.- See Also:
-
alphabiasshift
protected static final int alphabiasshiftAlpha bias shift value for learning rate.- See Also:
-
initalpha
protected static final int initalphaInitial alpha (learning rate) value.- See Also:
-
radbiasshift
protected static final int radbiasshiftRad bias shift value.- See Also:
-
radbias
protected static final int radbiasRad bias constant.- See Also:
-
alpharadbshift
protected static final int alpharadbshiftAlpha-rad bias shift value.- See Also:
-
alpharadbias
protected static final int alpharadbiasAlpha-rad bias constant.- See Also:
-
alphadec
protected int alphadecBiased alpha decrease factor. -
thepicture
protected byte[] thepictureThe input image itself as a byte array. -
lengthcount
protected int lengthcountThe total number of bytes in the input image (H*W*3). -
samplefac
protected int samplefacThe sampling factor (1-30). -
network
protected int[][] networkThe neural network itself, storing BGR and index values. -
netindex
protected int[] netindexA lookup table for the network. -
bias
protected int[] biasBias array for learning. -
freq
protected int[] freqFrequency array for learning. -
radpower
protected int[] radpowerPrecomputed values for radpower calculation.
-
-
Constructor Details
-
NeuQuant
public NeuQuant(byte[] thepic, int len, int sample) Initializes the neural network with parameters. The network is initialized with colors ranging from (0,0,0) to (255,255,255).- Parameters:
thepic- The input image as a byte array.len- The length of the image byte array.sample- The sampling factor (1-30).
-
-
Method Details
-
colorMap
public byte[] colorMap()Generates the color map from the trained network.- Returns:
- A byte array representing the color map (palette).
-
inxbuild
public void inxbuild()Performs an insertion sort on the network and builds the `netindex` lookup table. This should be called after `unbiasnet`. -
learn
public void learn()The main learning loop for the neural network. This method trains the network by adjusting neuron values based on the input image pixels. -
map
public int map(int b, int g, int r) Searches for the BGR values (0-255) after the network is unbiased and returns the color index.- Parameters:
b- The blue component.g- The green component.r- The red component.- Returns:
- The index of the closest color in the palette.
-
process
public byte[] process()Executes the complete quantization process. It learns from the image, unbiases the network, builds the index, and returns the color map.- Returns:
- The generated color map as a byte array.
-
unbiasnet
public void unbiasnet()Unbiases the network to give byte values from 0 to 255 and records the position `i` to prepare for sorting. -
alterneigh
protected void alterneigh(int rad, int i, int b, int g, int r) Moves adjacent neurons by a precomputed alpha factor based on their distance from the winning neuron.- Parameters:
rad- The radius of influence.i- The index of the winning neuron.b- The blue component of the input pixel.g- The green component of the input pixel.r- The red component of the input pixel.
-
altersingle
protected void altersingle(int alpha, int i, int b, int g, int r) Moves neuron `i` towards the biased (b,g,r) color by a factor of alpha.- Parameters:
alpha- The learning rate.i- The index of the neuron to alter.b- The blue component of the input pixel.g- The green component of the input pixel.r- The red component of the input pixel.
-
contest
protected int contest(int b, int g, int r) Searches for the biased BGR values to find the winning neuron. It finds the closest neuron (minimum distance) and updates its frequency. It also finds the best neuron (minimum distance-bias) and returns its position.- Parameters:
b- The blue component of the input pixel.g- The green component of the input pixel.r- The red component of the input pixel.- Returns:
- The index of the best-matching neuron.
-