ImageResizer  3.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Classes | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Properties | List of all members
ImageResizer.Plugins.PrettyGifs.OctreeQuantizer Class Reference

Quantize using an Octree More...

Inheritance diagram for ImageResizer.Plugins.PrettyGifs.OctreeQuantizer:
Inheritance graph
[legend]
Collaboration diagram for ImageResizer.Plugins.PrettyGifs.OctreeQuantizer:
Collaboration graph
[legend]

Public Member Functions

 OctreeQuantizer (int maxColors, int maxColorBits)
 Construct the octree quantizer More...
 
override void Reset ()
 Clears the octree More...
 
void Reset (int maxColors, int maxColorBits)
 Clears the octree and reconfigures color settings More...
 
- Public Member Functions inherited from ImageResizer.Plugins.PrettyGifs.Quantizer
 Quantizer (bool fixedPalette)
 Construct the quantizer More...
 
Bitmap Quantize (Image src)
 Quantize an image and return the resulting output bitmap More...
 

Public Attributes

float[,] DitherMatrix
 a Floyd-Steinberg dither matrix new float[,] {{0,0,0}, {0,0,0.44f}, {0.19f,0.31f,0.06f}}; More...
 
float DitherPercent = .3f
 

Protected Member Functions

override void InitialQuantizePixel (Color32 pixel)
 Process the pixel in the first pass of the algorithm More...
 
override byte QuantizePixel (Color32 pixel)
 Override this to process the pixel in the second pass of the algorithm More...
 
override ColorPalette GetPalette (ColorPalette original)
 Retrieve the palette for the quantized image More...
 
- Protected Member Functions inherited from ImageResizer.Plugins.PrettyGifs.Quantizer
virtual void ValidatePropertyValues ()
 
Bitmap QuantizeFullTrust (Bitmap firstPass, Bitmap copy, Bitmap copy2, Bitmap output)
 
Bitmap QuantizeLowTrust (Bitmap firstPass, Bitmap copy, Bitmap copy2, Bitmap output)
 
virtual void AnalyzeImage (BitmapData sourceData, int width, int height)
 Execute the first pass through the pixels in the image More...
 
virtual void AnalyzeImageLowTrust (Bitmap b, int width, int height)
 
virtual void QuantizeImage (BitmapData sourceData, BitmapData intermediate, Bitmap output, int width, int height, Rectangle bounds)
 Execute a second pass through the bitmap. If dithering is enabled, sourceData will be modified. More...
 
void AdjustNeighborSource (int offsetX, int offsetY, int deltaR, int deltaG, int deltaB, int deltaA)
 Can only be called from QuantizePixel... This is how dithering is done... 5-18-09 ndj More...
 
byte ToByte (int i)
 

Protected Attributes

bool _dither = false
 

Properties

bool Dither [get, set]
 Uses a Floyd-Steinberg dither More...
 
- Properties inherited from ImageResizer.Plugins.PrettyGifs.Quantizer
bool FixedPalette [get]
 (Readonly) If true, the algorithm can do everything in QuantizePixel, and InitialQuantizePixel will not be called. Implies ResizeForFirstPass=False and FourPass=false= More...
 
int PixelSize [get]
 The number of bytes in a ARGB structure. Should be 4 More...
 
bool FullTrust [get, set]
 If true, pointer arithmetic will be used instead of GetPixel. GetPixel is much slower. If false, OmitFinalStage will be assumed true, as only palette generation is possible in low trust. Defaults to true. More...
 
bool ResizeForFirstPass [get, set]
 If true, the first pass (InitialQuantizePixel) will be performed on a size-limited version of the original image to control performance. Ignored if FixedPalette=True More...
 
long FirstPassPixelCount [get, set]
 The approximate number of pixels to use when making a scaled copy of the image for the first pass. Only used when ResizeForFirstPass=True and FirstPassPixelThreshold is exceeded. More...
 
long FirstPassPixelThreshold [get, set]
 The maximum number of pixels the original image may contain before a scaled copy is made for the first pass. Only relevant when ResizeForFirstPass=True More...
 
bool FourPass [get, set]
 If true, image is re-paletted after quantization - forces 2 clones of the original image to be created. FixedPalette and OmitFinalStage should be false if this is used. More...
 
bool OmitFinalStage [get, set]
 If true, a 32-bit image with an 8-bit palette will be returned instead of an 8-bit image, which GDI can save using median-cut quantization. Much faster than our final quantization pass, although it can't do transparency. Assumed true if FullTrust is false. More...
 

Detailed Description

Quantize using an Octree

Definition at line 27 of file OctreeQuantizer.cs.

Constructor & Destructor Documentation

ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.OctreeQuantizer ( int  maxColors,
int  maxColorBits 
)
inline

Construct the octree quantizer

The Octree quantizer is a two pass algorithm. The initial pass sets up the octree, the second pass quantizes a color based on the nodes in the tree

Parameters
maxColorsThe maximum number of colors to return
maxColorBitsThe number of significant bits

Definition at line 38 of file OctreeQuantizer.cs.

38  : base ( false )
39  {
40  Reset(maxColors, maxColorBits);
41  }
override void Reset()
Clears the octree

Member Function Documentation

override ColorPalette ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.GetPalette ( ColorPalette  original)
inlineprotectedvirtual

Retrieve the palette for the quantized image

Parameters
originalAny old palette, this is overrwritten
Returns
The new color palette

Implements ImageResizer.Plugins.PrettyGifs.Quantizer.

Definition at line 175 of file OctreeQuantizer.cs.

176  {
177  // First off convert the octree to _maxColors colors
178  ArrayList palette = _octree.Palletize ( _maxColors - 1 ) ;
179 
180  // Then convert the palette based on those colors
181  for ( int index = 0 ; index < palette.Count ; index++ )
182  original.Entries[index + (TransparencyAtZero ? 1 : 0)] = (Color)palette[index] ;
183 
184  // Add the transparent color (Needs to be inserted at 0, not 255)
185  if (TransparencyAtZero)
186  original.Entries[0] = Color.FromArgb(0, 0, 0, 0);
187  else
188  original.Entries[_maxColors] = Color.FromArgb ( 0 , 0 , 0 , 0 ) ;
189 
190  _lastPalette = original; //may-18-2009, ndj: Saving a reference so we can lookup error amounts from QuantizePixel
191  return original ;
192  }
override void ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.InitialQuantizePixel ( Color32  pixel)
inlineprotectedvirtual

Process the pixel in the first pass of the algorithm

Parameters
pixelThe pixel to quantize

This function need only be overridden if your quantize algorithm needs two passes, such as an Octree quantizer.

Reimplemented from ImageResizer.Plugins.PrettyGifs.Quantizer.

Definition at line 79 of file OctreeQuantizer.cs.

80  {
81  // Add the color to the octree
82  _octree.AddColor ( pixel ) ;
83  }
override byte ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.QuantizePixel ( Color32  pixel)
inlineprotectedvirtual

Override this to process the pixel in the second pass of the algorithm

Parameters
pixelThe pixel to quantize
Returns
The quantized value

Implements ImageResizer.Plugins.PrettyGifs.Quantizer.

Definition at line 112 of file OctreeQuantizer.cs.

113  {
114  byte paletteIndex; // The color at [_maxColors] is set to transparent
115 
116  //TODO: Other sources claim only the first pallete color can be transparent
117  //Quote: Second, I've found a solution for preserving GIF transparency when invoking Image::Save(...). The .NET (tested on v2.0)
118  //GIF encoder considers transparent the first color found in the palette, so I've changed some methods (replace them with the supplied ones):
119 
120  // Get the palette index if this non-transparent
121  if (pixel.Alpha > 0)
122  {
123  paletteIndex = (byte)_octree.GetPaletteIndex(pixel);
124 
125 
126  //For dithering we need to track 6 pixels.
127 
128  //To dither, we find the error (difference from real pixel to the palette color), and subtract it from the next pixel. When that color is paletted,
129  //The dither occurs.
130  //The issue is that error adjustments build up, so you're never working off the original color. Hopefully this doesn't cause issues.
131  if (Dither)
132  {
133  Color n = _lastPalette.Entries[paletteIndex]; //The palette version
134  //Calculate error for the current pixel
135  int errorR = n.R - pixel.Red;
136  int errorG = n.G - pixel.Green;
137  int errorB = n.B - pixel.Blue;
138  int errorA = n.A - pixel.Alpha;
139 
140 
141  for (int y = 1; y < 3;y++)
142  for (int x = 0; x < 3; x++){
143  float adjust = DitherMatrix[y, x] * DitherPercent;//How much of the error should be passed on (in negative form) the this neighbor pixel
144  if (adjust != 0)
145  {
146 
147  AdjustNeighborSource(x - 1, y - 1, (int)((float)errorR * adjust * -1),
148  (int)((float)errorG * adjust * -1),
149  (int)((float)errorB * adjust * -1),
150  (int)((float)errorA * adjust * -1));
151 
152  }
153  }
154 
155  }
156 
157  if (TransparencyAtZero) paletteIndex++;
158  }
159  else
160  {
161  if (TransparencyAtZero)
162  paletteIndex = 0;
163  else
164  paletteIndex = (byte)_maxColors;
165  }
166 
167  return paletteIndex ;
168  }
void AdjustNeighborSource(int offsetX, int offsetY, int deltaR, int deltaG, int deltaB, int deltaA)
Can only be called from QuantizePixel... This is how dithering is done... 5-18-09 ndj ...
Definition: Quantizer.cs:461
float[,] DitherMatrix
a Floyd-Steinberg dither matrix new float[,] {{0,0,0}, {0,0,0.44f}, {0.19f,0.31f,0.06f}};
bool Dither
Uses a Floyd-Steinberg dither
override void ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.Reset ( )
inlinevirtual

Clears the octree

Reimplemented from ImageResizer.Plugins.PrettyGifs.Quantizer.

Definition at line 45 of file OctreeQuantizer.cs.

46  {
47  base.Reset();
48  Reset(_maxColors, _maxColorBits);
49  }
override void Reset()
Clears the octree
void ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.Reset ( int  maxColors,
int  maxColorBits 
)
inline

Clears the octree and reconfigures color settings

Parameters
maxColors
maxColorBits

Definition at line 56 of file OctreeQuantizer.cs.

57  {
58 
59  if (maxColors > 255)
60  throw new ArgumentOutOfRangeException("maxColors", maxColors, "The number of colors should be less than 256");
61 
62  if ((maxColorBits < 1) | (maxColorBits > 8))
63  throw new ArgumentOutOfRangeException("maxColorBits", maxColorBits, "This should be between 1 and 8");
64 
65  _maxColorBits = maxColorBits;
66  // Construct the octree
67  _octree = new Octree(maxColorBits);
68  _maxColors = maxColors;
69  }

Member Data Documentation

float [,] ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.DitherMatrix
Initial value:
= new float[,] {{0,0,0},
{0,0,0.44f},
{0.19f,0.31f,0.06f}}

a Floyd-Steinberg dither matrix new float[,] {{0,0,0}, {0,0,0.44f}, {0.19f,0.31f,0.06f}};

Definition at line 102 of file OctreeQuantizer.cs.

Property Documentation

bool ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.Dither
getset

Uses a Floyd-Steinberg dither

Definition at line 92 of file OctreeQuantizer.cs.


The documentation for this class was generated from the following file: