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

Class which encapsulates each node in the tree More...

Public Member Functions

 OctreeNode (int level, int colorBits, Octree octree)
 Construct the node More...
 
void AddColor (Color32 pixel, int colorBits, int level, Octree octree)
 Add a color into the tree More...
 
int Reduce ()
 Reduce this node by removing all of its children More...
 
void ConstructPalette (ArrayList palette, ref int paletteIndex)
 Traverse the tree, building up the color palette More...
 
int GetPaletteIndex (Color32 pixel, int level)
 Return the palette index for the passed color More...
 
OctreeNode FindClosestMatch (Color32 pixel)
 Added may 19-09. Should help with dithering. More...
 
void Increment (Color32 pixel)
 Increment the pixel count and add to the color information More...
 

Properties

OctreeNode NextReducible [get, set]
 Get/Set the next reducible node More...
 
OctreeNode[] Children [get]
 Return the child nodes More...
 

Detailed Description

Class which encapsulates each node in the tree

Definition at line 367 of file OctreeQuantizer.cs.

Constructor & Destructor Documentation

ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.Octree.OctreeNode.OctreeNode ( int  level,
int  colorBits,
Octree  octree 
)
inline

Construct the node

Parameters
levelThe level in the tree = 0 - 7
colorBitsThe number of significant color bits in the image
octreeThe tree to which this node belongs

Definition at line 375 of file OctreeQuantizer.cs.

376  {
377  // Construct the new node
378  _leaf = ( level == colorBits ) ;
379 
380  _red = _green = _blue = 0 ;
381  _pixelCount = 0 ;
382 
383  // If a leaf, increment the leaf count
384  if ( _leaf )
385  {
386  octree.Leaves++ ;
387  _nextReducible = null ;
388  _children = null ;
389  }
390  else
391  {
392  // Otherwise add this to the reducible nodes
393  _nextReducible = octree.ReducibleNodes[level] ;
394  octree.ReducibleNodes[level] = this ;
395  _children = new OctreeNode[8] ;
396  }
397  }
OctreeNode(int level, int colorBits, Octree octree)
Construct the node

Member Function Documentation

void ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.Octree.OctreeNode.AddColor ( Color32  pixel,
int  colorBits,
int  level,
Octree  octree 
)
inline

Add a color into the tree

Parameters
pixelThe color
colorBitsThe number of significant color bits
levelThe level in the tree
octreeThe tree to which this node belongs

Definition at line 406 of file OctreeQuantizer.cs.

407  {
408  // Update the color information if this is a leaf
409  if ( _leaf )
410  {
411  Increment ( pixel ) ;
412  // Setup the previous node
413  octree.TrackPrevious ( this ) ;
414  }
415  else
416  {
417  // Go to the next level down in the tree
418  int shift = 7 - level ;
419  int index = ( ( pixel.Red & mask[level] ) >> ( shift - 2 ) ) |
420  ( ( pixel.Green & mask[level] ) >> ( shift - 1 ) ) |
421  ( ( pixel.Blue & mask[level] ) >> ( shift ) ) ;
422 
423  OctreeNode child = _children[index] ;
424 
425  if ( null == child )
426  {
427  // Create a new child node & store in the array
428  child = new OctreeNode ( level + 1 , colorBits , octree ) ;
429  _children[index] = child ;
430  }
431 
432  // Add the color to the child node
433  child.AddColor ( pixel , colorBits , level + 1 , octree ) ;
434  }
435 
436  }
void Increment(Color32 pixel)
Increment the pixel count and add to the color information
OctreeNode(int level, int colorBits, Octree octree)
Construct the node
void ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.Octree.OctreeNode.ConstructPalette ( ArrayList  palette,
ref int  paletteIndex 
)
inline

Traverse the tree, building up the color palette

Parameters
paletteThe palette
paletteIndexThe current palette index

Definition at line 490 of file OctreeQuantizer.cs.

491  {
492  if ( _leaf )
493  {
494  // Consume the next palette index
495  _paletteIndex = paletteIndex++ ;
496 
497  // And set the color of the palette entry
498  palette.Add ( Color.FromArgb ( _red / _pixelCount , _green / _pixelCount , _blue / _pixelCount ) ) ;
499  }
500  else
501  {
502  // Loop through children looking for leaves
503  for ( int index = 0 ; index < 8 ; index++ )
504  {
505  if ( null != _children[index] )
506  _children[index].ConstructPalette ( palette , ref paletteIndex ) ;
507  }
508  }
509  }
OctreeNode ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.Octree.OctreeNode.FindClosestMatch ( Color32  pixel)
inline

Added may 19-09. Should help with dithering.

Parameters
pixel
Returns

Definition at line 544 of file OctreeQuantizer.cs.

545  {
546  if (_leaf) return this;
547 
548  long min = long.MaxValue; //The shortest distance found.
549  OctreeNode closest = null; //The closest node found
550 
551  for (int i = 0; i < _children.Length; i++)
552  {
553  if (null != _children[i])
554  {
555  long distance = sqr((long)pixel.Red - _children[i]._red) + //No need for Math.Abs when squaring
556  sqr((long)pixel.Green - _children[i]._green) +
557  sqr((long)pixel.Blue - _children[i]._blue);
558 
559  if (distance < min)
560  {
561  distance = min;
562  closest = _children[i];
563  }
564  }
565 
566  }
567  return closest;
568 
569  }
OctreeNode(int level, int colorBits, Octree octree)
Construct the node
int ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.Octree.OctreeNode.GetPaletteIndex ( Color32  pixel,
int  level 
)
inline

Return the palette index for the passed color

Definition at line 514 of file OctreeQuantizer.cs.

515  {
516  int paletteIndex = _paletteIndex ;
517 
518  if ( !_leaf )
519  {
520  int shift = 7 - level ;
521  int index = ( ( pixel.Red & mask[level] ) >> ( shift - 2 ) ) |
522  ( ( pixel.Green & mask[level] ) >> ( shift - 1 ) ) |
523  ( ( pixel.Blue & mask[level] ) >> ( shift ) ) ;
524 
525  if (null != _children[index])
526  paletteIndex = _children[index].GetPaletteIndex(pixel, level + 1);
527  else
528  {
529  //NDJ May-18-09: Occurrs when dithering is enabled, since dithering causes new colors to appear in the image
530  //throw new Exception("Didn't expect this!");
531  //Find closest one nearby
532  OctreeNode n = FindClosestMatch(pixel);
533  if (n != null) paletteIndex = n._paletteIndex;
534  }
535  }
536 
537  return paletteIndex ;
538  }
OctreeNode(int level, int colorBits, Octree octree)
Construct the node
OctreeNode FindClosestMatch(Color32 pixel)
Added may 19-09. Should help with dithering.
void ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.Octree.OctreeNode.Increment ( Color32  pixel)
inline

Increment the pixel count and add to the color information

Definition at line 578 of file OctreeQuantizer.cs.

579  {
580  _pixelCount++ ;
581  _red += pixel.Red ;
582  _green += pixel.Green ;
583  _blue += pixel.Blue ;
584  }
int ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.Octree.OctreeNode.Reduce ( )
inline

Reduce this node by removing all of its children

Returns
The number of leaves removed

Definition at line 459 of file OctreeQuantizer.cs.

460  {
461  _red = _green = _blue = 0 ;
462  int children = 0 ;
463 
464  // Loop through all children and add their information to this node
465  for ( int index = 0 ; index < 8 ; index++ )
466  {
467  if ( null != _children[index] )
468  {
469  _red += _children[index]._red ;
470  _green += _children[index]._green ;
471  _blue += _children[index]._blue ;
472  _pixelCount += _children[index]._pixelCount ;
473  ++children ;
474  _children[index] = null ;
475  }
476  }
477 
478  // Now change this to a leaf node
479  _leaf = true ;
480 
481  // Return the number of nodes to decrement the leaf count by
482  return ( children - 1 ) ;
483  }

Property Documentation

OctreeNode [] ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.Octree.OctreeNode.Children
get

Return the child nodes

Definition at line 451 of file OctreeQuantizer.cs.

OctreeNode ImageResizer.Plugins.PrettyGifs.OctreeQuantizer.Octree.OctreeNode.NextReducible
getset

Get/Set the next reducible node

Definition at line 442 of file OctreeQuantizer.cs.


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