publicclass HexagonPixelateEffect : IEffect {publicstring Name { get { return"Pixelate"; } }// y, xprivateint[,] _pixelated = null;privateint hexWidth = 0;privateint _hexVSide = 0;privateint hexHeight = 0;privateint _width = 0;privateint _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 variablesvar width = input.PixelWidth;var height = input.PixelHeight;return Process(input.Pixels, width, height, 100).ToWriteableBitmap(width, height); }publicint[] Process(int[] inputPixels, int width, int height) {returnthis.Process(inputPixels, width, height, 4); }publicint[] Process(int[] inputPixels, int width, int height, int scale) {this._pixelated = inputPixels.To2DArray(width, height);this._width = width;this._height = height;// Horizontalthis.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(); }privatevoid DrawHorizontalHex(int x, int y) {// Colourint colour = 0;if(x >= 0 && x < _width && y >= 0 && y < _height) colour = _pixelated[y, x];elseif (x >= 0 && y >= 0 && y < _height) colour = _pixelated[y, _width - 1];elseif (x < _width && y >= 0 && y < _height) colour = _pixelated[y, 0];elseif (x >= 0 && x < _width && y < _height) colour = _pixelated[0, x];elseif (x >= 0 && x < _width && y >= 0) colour = _pixelated[_height - 1, x];elseif (x < 0 && y < 0) colour = _pixelated[0, 0];else colour = _pixelated[_height - 1, _width - 1];// Initial start and endint xStart = x;int xEnd = xStart + hexWidth - hexHeight;// Grow out until half way down then contractfor (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--; } } } }
↧
New Post: HexagonPixelate
↧