public Bitmap GetProcessedImage(bool useTransparency, out int activePixel) { Bitmap img = OCRImage.Clone(BoundingBox, PixelFormat.Format32bppArgb); activePixel = 0; BitmapData srcData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); IntPtr Scan0 = srcData.Scan0; int stride = srcData.Stride; unsafe { byte *p = (byte *)(void *)Scan0; for (int x = 0; x < img.Width; x++) { for (int y = 0; y < img.Height; y++) { int idx = (y * stride) + x * 4; double s_value = ColorExt.GetSaturation(p[idx + 2], p[idx + 1], p[idx + 0]); byte bval = (byte)(255 - (p[idx + 2] + p[idx + 1] + p[idx + 0]) / 3); if (s_value >= 80) { p[idx + 0] = 255; p[idx + 1] = 255; p[idx + 2] = 255; p[idx + 3] = (byte)(useTransparency ? 0 : 255); } else { activePixel++; p[idx + 0] = bval; p[idx + 1] = bval; p[idx + 2] = bval; p[idx + 3] = 255; } } } } img.UnlockBits(srcData); return(img); }
public Bitmap GetProcessedImage(bool useTransparency, out int activePixel) { Bitmap img = OCRImage.Clone(GetBoundingBox(), PixelFormat.Format32bppArgb); double hexheight = OCRRadius * (Math.Sin(MathExt.ToRadians(60)) / Math.Sin(MathExt.ToRadians(90))); activePixel = 0; BitmapData srcData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); IntPtr Scan0 = srcData.Scan0; int stride = srcData.Stride; unsafe { byte *p = (byte *)(void *)Scan0; for (int x = 0; x < img.Width; x++) { for (int y = 0; y < img.Height; y++) { int idx = (y * stride) + x * 4; double px = Math.Abs(x - img.Width / 2); double py = Math.Abs(y - img.Height / 2); double pd = Math.Sqrt(px * px + py * py); if (py < hexheight && py + 2 * (px - OCRRadius) < 0) { byte repl_R = 255; byte repl_G = 255; byte repl_B = 255; byte repl_A = (byte)(useTransparency ? 0 : 255); switch (Type) { case HexagonType.HIDDEN: // TRANSPARENT break; case HexagonType.ACTIVE: { double s_value = ColorExt.GetSaturation(p[idx + 2], p[idx + 1], p[idx + 0]); if (s_value >= 80 || pd > (OCRHeight - 1)) { // TRANSPARENT } else { activePixel++; repl_R = (byte)(255 - (p[idx + 2] + p[idx + 1] + p[idx + 0]) / 3); repl_G = (byte)(255 - (p[idx + 2] + p[idx + 1] + p[idx + 0]) / 3); repl_B = (byte)(255 - (p[idx + 2] + p[idx + 1] + p[idx + 0]) / 3); repl_A = 255; } } break; case HexagonType.INACTIVE: { int grayness = (p[idx + 2] + p[idx + 1] + p[idx + 0]) / 3; if (grayness < 128 || pd > (OCRHeight - 1)) { // TRANSPARENT } else { activePixel++; repl_R = (byte)(255 - (p[idx + 2] + p[idx + 1] + p[idx + 0]) / 3); repl_G = (byte)(255 - (p[idx + 2] + p[idx + 1] + p[idx + 0]) / 3); repl_B = (byte)(255 - (p[idx + 2] + p[idx + 1] + p[idx + 0]) / 3); repl_A = 255; } } break; case HexagonType.NOCELL: { double c_distance = ColorExt.GetColorDistance(p[idx + 2], p[idx + 1], p[idx + 0], COLOR_CELL_NOCELL); double hsv_value = ColorExt.GetValue(p[idx + 2], p[idx + 1], p[idx + 0]); if (c_distance < 32 || hsv_value > 75) { // TRANSPARENT } else { activePixel++; repl_R = (byte)((p[idx + 2] + p[idx + 1] + p[idx + 0]) / 3); repl_G = (byte)((p[idx + 2] + p[idx + 1] + p[idx + 0]) / 3); repl_B = (byte)((p[idx + 2] + p[idx + 1] + p[idx + 0]) / 3); repl_A = 255; } } break; default: case HexagonType.UNKNOWN: // TRANSPARENT break; } p[idx + 0] = repl_B; p[idx + 1] = repl_G; p[idx + 2] = repl_R; p[idx + 3] = repl_A; } else { p[idx + 0] = 255; p[idx + 1] = 255; p[idx + 2] = 255; p[idx + 3] = (byte)(useTransparency ? 0 : 255); } } } } img.UnlockBits(srcData); return(img); }