/// <summary> /// Iterates through all the pixels in a rectangular region of the image. /// </summary> /// <param name="left">The left edge of the rectangular region.</param> /// <param name="top">The top edge of the rectangular region.</param> /// <param name="right">The right edge of the rectangular region.</param> /// <param name="bottom">The bottom edge of the rectangular region.</param> /// <param name="processor">The <see cref="PixelProcessor"/> delegate to call.</param> /// <remarks> /// It is often desirable to iterate through all the pixels in a rectangular /// region of an image. This method encapsulates all the boilerplate code /// required to do that. /// </remarks> public void ForEachPixel( int left, int top, int right, int bottom, PixelProcessor processor) { int i = 0; int temp; if (top > bottom) { temp = top; top = bottom; bottom = temp; } if (left > right) { temp = left; left = right; right = temp; } Platform.CheckArgumentRange(left, 0, _columns - 1, "left"); Platform.CheckArgumentRange(right, 0, _columns - 1, "right"); Platform.CheckArgumentRange(top, 0, _rows - 1, "top"); Platform.CheckArgumentRange(bottom, 0, _rows - 1, "bottom"); bool usingGetter = _pixelData == null; try { if (usingGetter) { _pixelData = _pixelDataGetter(); //store pixel data temporarily. } int pixelIndex = top * _columns + left; int offset = (_columns - right) + left - 1; for (int y = top; y <= bottom; y++) { for (int x = left; x <= right; x++) { processor(i, x, y, pixelIndex); pixelIndex++; i++; } pixelIndex += offset; } } finally { if (usingGetter) { _pixelData = null; } } }
private void ProcessBitmapPixels(Bitmap image, PixelProcessor pixelProcessor) { Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); BitmapData bitmapdata = image.LockBits(rect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb); try { IntPtr scan0 = bitmapdata.Scan0; int length = Math.Abs(bitmapdata.Stride) * bitmapdata.Height; byte[] numArray = new byte[length]; Marshal.Copy(scan0, numArray, 0, length); int index = 0; while (index < numArray.Length) { pixelProcessor(ref numArray[index + 3], ref numArray[index + 2], ref numArray[index + 1], ref numArray[index]); index += 4; } Marshal.Copy(numArray, 0, scan0, length); } finally { image.UnlockBits(bitmapdata); } }
/// <summary> /// Iterates through all the pixels in a rectangular region of the image. /// </summary> /// <param name="left">The left edge of the rectangular region.</param> /// <param name="top">The top edge of the rectangular region.</param> /// <param name="right">The right edge of the rectangular region.</param> /// <param name="bottom">The bottom edge of the rectangular region.</param> /// <param name="processor">The <see cref="PixelProcessor"/> delegate to call.</param> /// <remarks> /// It is often desirable to iterate through all the pixels in a rectangular /// region of an image. This method encapsulates all the boilerplate code /// required to do that. /// </remarks> public void ForEachPixel( int left, int top, int right, int bottom, PixelProcessor processor) { int i = 0; int temp; if (top > bottom) { temp = top; top = bottom; bottom = temp; } if (left > right) { temp = left; left = right; right = temp; } Platform.CheckArgumentRange(left, 0, _columns - 1, "left"); Platform.CheckArgumentRange(right, 0, _columns - 1, "right"); Platform.CheckArgumentRange(top, 0, _rows - 1, "top"); Platform.CheckArgumentRange(bottom, 0, _rows - 1, "bottom"); bool usingGetter = _pixelData == null; try { if (usingGetter) _pixelData = _pixelDataGetter(); //store pixel data temporarily. int pixelIndex = top * _columns + left; int offset = (_columns - right) + left - 1; for (int y = top; y <= bottom; y++) { for (int x = left; x <= right; x++) { processor(i, x, y, pixelIndex); pixelIndex++; i++; } pixelIndex += offset; } } finally { if (usingGetter) _pixelData = null; } }
/// <summary> /// Iterates through all the pixels in the image. /// </summary> /// <param name="processor">Called for each pixel.</param> /// <remarks> /// It is often desirable to iterate through all the pixels in an image. /// This method encapsulates all the boilerplate code required to do that. /// </remarks> public void ForEachPixel(PixelProcessor processor) { ForEachPixel(0, 0, _columns - 1, _rows - 1, processor); }
void Awake() { Instance = this; }