Module bus.extra

Class NeuQuant

java.lang.Object
org.miaixz.bus.extra.image.gif.NeuQuant

public class NeuQuant extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    protected static final int
    Alpha bias shift value for learning rate.
    protected int
    Biased alpha decrease factor.
    protected static final int
    Alpha-rad bias constant.
    protected static final int
    Alpha-rad bias shift value.
    protected static final int
    Beta shift value.
    protected int[]
    Bias array for learning.
    protected int[]
    Frequency array for learning.
    protected static final int
    Gamma constant.
    protected static final int
    Gamma shift value.
    protected static final int
    Initial alpha (learning rate) value.
    protected static final int
    Initial radius for convergence.
    protected static final int
    Initial radius value.
    protected static final int
    Integer bias constant.
    protected static final int
    Bias for fractions.
    protected int
    The total number of bytes in the input image (H*W*3).
    protected static final int
    The maximum network position.
    protected static final int
    Minimum size for the input image in bytes.
    protected static final int
    Number of learning cycles.
    protected static final int
    Bias for color values.
    protected int[]
    A lookup table for the network.
    protected static final int
    The number of colors used in the output palette.
    protected int[][]
    The neural network itself, storing BGR and index values.
    protected static final int
    First of four primes near 500, used for sampling.
    protected static final int
    Second of four primes near 500, used for sampling.
    protected static final int
    Third of four primes near 500, used for sampling.
    protected static final int
    Fourth of four primes near 500, used for sampling.
    protected static final int
    Rad bias constant.
    protected static final int
    Rad bias shift value.
    protected static final int
    Radius bias constant.
    protected static final int
    Radius bias shift value.
    protected static final int
    Radius decrease factor.
    protected int[]
    Precomputed values for radpower calculation.
    protected int
    The sampling factor (1-30).
    protected byte[]
    The input image itself as a byte array.
  • Constructor Summary

    Constructors
    Constructor
    Description
    NeuQuant(byte[] thepic, int len, int sample)
    Initializes the neural network with parameters.
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    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.
    byte[]
    Generates the color map from the trained network.
    protected int
    contest(int b, int g, int r)
    Searches for the biased BGR values to find the winning neuron.
    void
    Performs an insertion sort on the network and builds the `netindex` lookup table.
    void
    The main learning loop for the neural network.
    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.
    byte[]
    Executes the complete quantization process.
    void
    Unbiases the network to give byte values from 0 to 255 and records the position `i` to prepare for sorting.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • netsize

      protected static final int netsize
      The number of colors used in the output palette.
      See Also:
    • prime1

      protected static final int prime1
      First of four primes near 500, used for sampling.
      See Also:
    • prime2

      protected static final int prime2
      Second of four primes near 500, used for sampling.
      See Also:
    • prime3

      protected static final int prime3
      Third of four primes near 500, used for sampling.
      See Also:
    • prime4

      protected static final int prime4
      Fourth of four primes near 500, used for sampling.
      See Also:
    • minpicturebytes

      protected static final int minpicturebytes
      Minimum size for the input image in bytes.
      See Also:
    • maxnetpos

      protected static final int maxnetpos
      The maximum network position.
      See Also:
    • netbiasshift

      protected static final int netbiasshift
      Bias for color values.
      See Also:
    • ncycles

      protected static final int ncycles
      Number of learning cycles.
      See Also:
    • intbiasshift

      protected static final int intbiasshift
      Bias for fractions.
      See Also:
    • intbias

      protected static final int intbias
      Integer bias constant.
      See Also:
    • gammashift

      protected static final int gammashift
      Gamma shift value.
      See Also:
    • gamma

      protected static final int gamma
      Gamma constant.
      See Also:
    • betashift

      protected static final int betashift
      Beta shift value.
      See Also:
    • initrad

      protected static final int initrad
      Initial radius for convergence.
      See Also:
    • radiusbiasshift

      protected static final int radiusbiasshift
      Radius bias shift value.
      See Also:
    • radiusbias

      protected static final int radiusbias
      Radius bias constant.
      See Also:
    • initradius

      protected static final int initradius
      Initial radius value.
      See Also:
    • radiusdec

      protected static final int radiusdec
      Radius decrease factor.
      See Also:
    • alphabiasshift

      protected static final int alphabiasshift
      Alpha bias shift value for learning rate.
      See Also:
    • initalpha

      protected static final int initalpha
      Initial alpha (learning rate) value.
      See Also:
    • radbiasshift

      protected static final int radbiasshift
      Rad bias shift value.
      See Also:
    • radbias

      protected static final int radbias
      Rad bias constant.
      See Also:
    • alpharadbshift

      protected static final int alpharadbshift
      Alpha-rad bias shift value.
      See Also:
    • alpharadbias

      protected static final int alpharadbias
      Alpha-rad bias constant.
      See Also:
    • alphadec

      protected int alphadec
      Biased alpha decrease factor.
    • thepicture

      protected byte[] thepicture
      The input image itself as a byte array.
    • lengthcount

      protected int lengthcount
      The total number of bytes in the input image (H*W*3).
    • samplefac

      protected int samplefac
      The sampling factor (1-30).
    • network

      protected int[][] network
      The neural network itself, storing BGR and index values.
    • netindex

      protected int[] netindex
      A lookup table for the network.
    • bias

      protected int[] bias
      Bias array for learning.
    • freq

      protected int[] freq
      Frequency array for learning.
    • radpower

      protected int[] radpower
      Precomputed 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.