HexagonPixelate

Feb 28, 2012 at 4:20 PM

public class HexagonPixelateEffect : IEffect
    {
        public string Name { get { return "Pixelate"; } }

        // y, x
        private int[,] _pixelated = null;

        private int hexWidth = 0;
        private int _hexVSide = 0;
        private int hexHeight = 0;
        private int _width = 0;
        private int _height = 0;

        public HexagonPixelateEffect()
        {

        }

        /// <summary>
        /// Processes a bitmap and returns a new processed WriteabelBitmap.
        /// </summary>
        /// <param name="input">The input bitmap.</param>
        /// <returns>The result of WriteabelBitmap processing.</returns>
        public WriteableBitmap Process(WriteableBitmap input)
        {
            // Prepare some variables
            var width = input.PixelWidth;
            var height = input.PixelHeight;
            return Process(input.Pixels, width, height, 100).ToWriteableBitmap(width, height);
        }

        public int[] Process(int[] inputPixels, int width, int height)
        {
            return this.Process(inputPixels, width, height, 4);
        }

        public int[] Process(int[] inputPixels, int width, int height, int scale)
        {
            this._pixelated = inputPixels.To2DArray(width, height);
            this._width = width;
            this._height = height;

            // Horizontal
            this.hexWidth = 4 * scale;
            this.hexHeight = 3 * scale;

            int xStart = -1;
            for (int y = 0; y < height + hexHeight; y += hexHeight / 2)
            {
                // Toggle start position
                xStart = xStart == 0 ? (hexWidth - hexHeight / 2) : 0;

                for (int x = xStart; x < width + hexWidth; x += (2 * hexWidth - hexHeight))
                {
                    this.DrawHorizontalHex(x, y);
                }
            }

            return _pixelated.ToArray();
        }

        private void DrawHorizontalHex(int x, int y)
        {
            // Colour
            int colour = 0;
            if(x >= 0 && x < _width && y >= 0 && y < _height)
                colour = _pixelated[y, x];
            else if (x >= 0 && y >= 0 && y < _height)
                colour = _pixelated[y, _width - 1];
            else if (x < _width && y >= 0 && y < _height)
                colour = _pixelated[y, 0];
            else if (x >= 0 && x < _width && y < _height)
                colour = _pixelated[0, x];
            else if (x >= 0 && x < _width && y >= 0)
                colour = _pixelated[_height - 1, x];
            else if (x < 0 && y < 0)
                colour = _pixelated[0, 0];
            else
                colour = _pixelated[_height - 1, _width - 1];

            // Initial start and end
            int xStart = x;
            int xEnd = xStart + hexWidth - hexHeight;

            // Grow out until half way down then contract
            for (int j = y - hexHeight / 2; j <= y + hexHeight / 2; j++)
            {
                for (int i = xStart; i <= xEnd; i++)
                {
                    if (i >= 0 && i < _width && j >= 0 && j < _height)
                        _pixelated[j, i] = colour;
                }

                if (j <= y) { xStart--; xEnd++; }
                else { xStart++; xEnd--; }
            }
        }
    }

Mar 1, 2012 at 9:29 AM

thanks for posting the extra effects. Can i asked about To2DArray method ? Is it just a straight forward jagged array where you fill each row sequentially ? 

Mar 1, 2012 at 9:45 AM

Yeah sorry, I added 2 helper methods to ArrayExtensions:

public static int[,] To2DArray(this int[] input, int width, int height)
      {
          var result = new int[height, width];
          System.Buffer.BlockCopy(input, 0, result, 0, input.Length * 4);

          return result;
      }

      public static int[] ToArray(this int[,] input)
      {
          var result = new int[input.Length];
          System.Buffer.BlockCopy(input, 0, result, 0, input.Length * 4);

          return result;
      }

Mar 1, 2012 at 11:50 AM
Thank you very much. I'll have a look at this one and other effects you posted.

Sent from my Windows Phone

From: geoff_cross
Sent: 01/03/2012 09:45
To: hermitd@hotmail.com
Subject: Re: HexagonPixelate [picfx:346586]

From: geoff_cross

Yeah sorry, I added 2 helper methods to ArrayExtensions:

public static int[,] To2DArray(this int[] input, int width, int height)
      {
          var result = new int[height, width];
          System.Buffer.BlockCopy(input, 0, result, 0, input.Length * 4);

          return result;
      }

      public static int[] ToArray(this int[,] input)
      {
          var result = new int[input.Length];
          System.Buffer.BlockCopy(input, 0, result, 0, input.Length * 4);

          return result;
      }