private static Bitmap InverseBinaryHelper(Bitmap img, OutType type) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); int[,] result = new int[img.Height, img.Width]; if (Checks.BWinput(img)) { List <ArraysListInt> ColorList = Helpers.GetPixels(img); result = MoreHelpers.InvertBinaryArray(ColorList[0].Color); if (result.Cast <int>().Max() == 1) { for (int i = 0; i < result.GetLength(0); i++) { for (int j = 0; j < result.GetLength(1); j++) { if (result[i, j] == 1) { result[i, j] = 255; } } } } image = Helpers.SetPixels(image, result, result, result); if (type == OutType.OneBpp) { image = PixelFormatWorks.ImageTo1BppBitmap(image, 0.5); } else if (type == OutType.EightBpp) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } } return(image); }
private static Bitmap Bpp24Gray2Gray8bppConverter(Bitmap img) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format8bppIndexed); int r, ic, oc, bmpStride, outputStride; ColorPalette palette; BitmapData bmpData, outputData; double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); if (Depth == 8) { Console.WriteLine("Image already 8bit. Method Bpp24Gray2Gray8bpp. Return themself"); return(img); } if (Checks.BWinput(img)) { //Build a grayscale color Palette palette = image.Palette; for (int i = 0; i < 256; i++) { Color tmp = Color.FromArgb(255, i, i, i); palette.Entries[i] = Color.FromArgb(255, i, i, i); } image.Palette = palette; //Lock the images bmpData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, img.PixelFormat); outputData = image.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); bmpStride = bmpData.Stride; outputStride = outputData.Stride; try { //Traverse each pixel of the image unsafe { byte *bmpPtr = (byte *)bmpData.Scan0.ToPointer(); var outputPtr = (byte *)outputData.Scan0.ToPointer(); //Note that ic is the input column and oc is the output column for (r = 0; r < img.Height; r++) { for (ic = oc = 0; oc < img.Width; ic += 3, ++oc) { outputPtr[r * outputStride + oc] = (byte)(int) (bmpPtr[r * bmpStride + ic]); } } } } catch (Exception e) { Console.WriteLine("Some problems while using unsafe code.Method: Bpp24Gray2Gray8bpp. \nMessage: " + e.Message); } finally { //Unlock the imagess img.UnlockBits(bmpData); image.UnlockBits(outputData); } } return(image); }