title: Floyd Steinberg algorithm implementation in C#
body: <p>Im trying to implement Floyd-Steinberg algorithm i c# to dithered 24 bit image to 8 bit but i have exception error System.IndexOutOfRangeException (Index was outside the boundaries of the array.) in line :g1[x - 1, y + 1] += err_g * 3 / 16; Has anyomeone tell me where I made a mistake and what should i change?<br>
  r1 means old pixel
  r2 - new pixel </p>
  
  <pre><code>{
  
              Color Pix;
             int[,] r1 = new int[bmp.Width, bmp.Height];
             int[,] r2 = new int[bmp.Width, bmp.Height];
             int[,] g1 = new int[bmp.Width, bmp.Height];
             int[,] g2 = new int[bmp.Width, bmp.Height];
             int[,] b1 = new int[bmp.Width, bmp.Height];
             int[,] b2 = new int[bmp.Width, bmp.Height];
  
  
  
  
              for (int y = 0; y &lt; bmp.Height; y++)
              {
  
  
                  for (int x = 0; x &lt; bmp.Width; x++)
                  {
                      Pix = bmp.GetPixel(x, y);
                      r1[x, y] = (Pix.R + Pix.G + Pix.B);
                      g1[x, y] = (Pix.R + Pix.G + Pix.B);
                      b1[x, y] = (Pix.R + Pix.G + Pix.B);               
                  }
              }
  
              int err_r;
              int err_g;
              int err_b;
  
              for (int y = 0; y &lt; bmp.Height; y++)
              {
  
  
                  for (int x = 0; x &lt; bmp.Width; x++)
                  {
  
                      if (g1[x, y] &lt; 128)
                      {
                          g2[x, y] = 0;
  
                      }
                      else
                      {
                          g2[x, y] = 255;
  
                      }
                      if (r1[x, y] &lt; 128)
                      {
                          r2[x, y] = 0;
                                         }
                      else
                      {
                          r2[x, y] = 255;
                                         }
                      if (b1[x, y] &lt; 128)
                      {
                          b2[x, y] = 0;
  
                      }
                      else
                      {
                          b2[x, y] = 255;
  
                      }
  
                      err_r = r1[x,y] - r2[x,y];
                      err_g = g1[x,y] - g2[x,y];
                      err_b = b1[x,y] - b2[x,y];
  
  
  
                      if (x &lt; bmp.Width - 1)
                          r1[x + 1, y] += err_r * 7 / 16;
                          g1[x + 1, y] += err_g * 7/ 16;
                          b1[x + 1, y] += err_b * 7/ 16;
                      if (y &lt; bmp.Height - 1)
                          r1[x, y + 1] += err_r * 5 / 16;
                          g1[x, y + 1] += err_g * 5 / 16;
                          b1[x, y + 1] += err_b * 5 / 16;
                      if (x &lt; bmp.Width - 1 &amp;&amp; y &lt; bmp.Height - 1)
                          r1[x + 1, y + 1] += err_r / 16;
                          g1[x + 1, y + 1] += err_g / 16;
                          b1[x + 1, y + 1] += err_b / 16;
                      if (x &gt; 0 &amp;&amp; y &lt; bmp.Height - 1)
                          r1[x - 1, y + 1] += err_r * 3 / 16;
                          g1[x - 1, y + 1] += err_g * 3 / 16;
                          b1[x - 1, y + 1] += err_b * 3/ 16;
  
  
  
                  }
              }
  
              for (int y = 0; y &lt; bmp.Height; y++)
              {
  
  
                  for (int x = 0; x &lt; bmp.Width; x++)
                  {
                      bmp.SetPixel(x, y, Color.FromArgb(r2[x, y], g2[x, y], b1[x, y]));
  
                  }
              }
  </code></pre>