Imaging.NET Source Code - Pixel/Image Sample

The central components of the Imaging.NET application rely heavily on the Pixel class to perform all image processing operations in the application.  All source code markup was produced by the excellent Copy As HTML Visual Studio extension.


  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.IO;
  5. using System.Collections;
  6.  
  7. using System.Windows.Forms;
  8.  
  9. //////////////////////////////////////////////////////////////////////////////////////////////
  10. //// Pixel Class
  11. //////////////////////////////////////////////////////////////////////////////////////////////
  12. //// For purposes of this project,
  13. //// a Pixel consists of i (row) and j (column) coordinates only.
  14. ////
  15. //// NOTE: a Pixel (and/or its neighbors) can have negative coordinates
  16. ////
  17. public class Pixel : IComparable, ICloneable
  18. {
  19.  
  20.     public int i; // the pixel row
  21.     public int j; // the pixel column
  22.  
  23.     public Pixel(int new_i, int new_j)
  24.     {
  25.         i = new_i;
  26.         j = new_j;
  27.     }
  28.  
  29.     public Pixel(Pixel pixel)
  30.     {
  31.         i = pixel.i;
  32.         j = pixel.j;
  33.     }
  34.  
  35.     static readonly int[] di = { 0, -1, -1, -1, 0, 1, 1, 1 };
  36.     static readonly int[] dj = { -1, -1, 0, 1, 1, 1, 0, -1 };
  37.  
  38.     //// Gets the specified 8 neighbor of this pixel //
  39.     public Pixel Neighbor(int index)
  40.     {
  41.         return new Pixel(i + di[index], j + dj[index]);
  42.     }
  43.  
  44.  
  45.     //// Returns a collection of the 4 neighbors of this pixel //
  46.     public PixelList Get4Neighbors()
  47.     {
  48.         PixelList result;
  49.  
  50.         result = new PixelList(4);
  51.  
  52.         result.Add(Neighbor(0));
  53.         result.Add(Neighbor(2));
  54.         result.Add(Neighbor(4));
  55.         result.Add(Neighbor(6));
  56.  
  57.         return result;
  58.     }
  59.  
  60.     //// Returns a collection of the 8 neighbors of this pixel //
  61.     public PixelList Get8Neighbors()
  62.     {
  63.         PixelList result;
  64.  
  65.         result = new PixelList(8);
  66.         result.Add(Neighbor(0));
  67.         result.Add(Neighbor(1));
  68.         result.Add(Neighbor(2));
  69.         result.Add(Neighbor(3));
  70.         result.Add(Neighbor(4));
  71.         result.Add(Neighbor(5));
  72.         result.Add(Neighbor(6));
  73.         result.Add(Neighbor(7));
  74.  
  75.         return result;
  76.     }
  77.  
  78.  
  79.     //// Key for comparing pixels //
  80.     public int SortingKey
  81.     {
  82.         get
  83.         {
  84.             const int INDEX_MULT = 10000;
  85.  
  86.             //// Return a unique index for this pixel (in a 10000 x 10000 matrix) //
  87.             return (i * INDEX_MULT) + j;
  88.         }
  89.     }
  90.  
  91.     public int ArrayIndex(int width)
  92.     {
  93.         return (i * width) + j;
  94.     }
  95.  
  96.  
  97.     //// Compare pixels //
  98.     public bool Equals(Pixel pixel)
  99.     {
  100.         //// Pixels are equal if their sorting keys are equal //
  101.         return SortingKey == pixel.SortingKey;
  102.     }
  103.  
  104.  
  105.     //// Compare two pixels
  106.     //// IComparable interface requires this function //
  107.     //// It returns -1 if less, 0 if equal, 1 if more
  108.     int IComparable.CompareTo(object obj)
  109.     {
  110.         int thisSortingKey;
  111.         int otherSortingKey;
  112.  
  113.         //// Prepare to compare sorting keys //
  114.         thisSortingKey = SortingKey;
  115.         otherSortingKey = ((Pixel)obj).SortingKey;
  116.  
  117.         if (thisSortingKey < otherSortingKey)
  118.         {
  119.             return -1;
  120.         }
  121.  
  122.         if (thisSortingKey == otherSortingKey)
  123.         {
  124.             return 0;
  125.         }
  126.  
  127.         return 1;
  128.     }
  129.  
  130.     //// Make a copy of this pixel
  131.     object ICloneable.Clone()
  132.     {
  133.         return new Pixel(this);
  134.     }
  135. }