private static Bitmap WildBoardHelepr(Bitmap img, int n, WildBoardVariant variant) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); if (Checks.RGBinput(img)) { var ColorList = Helpers.GetPixels(img); int[,] resultR = new int[img.Height, img.Width]; int[,] resultG = new int[img.Height, img.Width]; int[,] resultB = new int[img.Height, img.Width]; int Boardrows = (int)Math.Ceiling((double)img.Height / n); int Boardcols = (int)Math.Ceiling((double)img.Width / n); var board = CheckerBoardHelper(n, Boardrows, Boardcols); var gray = Helpers.RGBToGrayArray(img); for (int i = 0; i < img.Height; i++) { for (int j = 0; j < img.Width; j++) { if (variant == WildBoardVariant.Variant1) { if (board[i, j] == 0) { resultR[i, j] = ColorList[0].Color[i, j]; resultG[i, j] = ColorList[1].Color[i, j]; resultB[i, j] = ColorList[2].Color[i, j]; } else { resultR[i, j] = gray[i, j]; resultG[i, j] = gray[i, j]; resultB[i, j] = gray[i, j]; } } else { if (board[i, j] == 255) { resultR[i, j] = ColorList[0].Color[i, j]; resultG[i, j] = ColorList[1].Color[i, j]; resultB[i, j] = ColorList[2].Color[i, j]; } else { resultR[i, j] = gray[i, j]; resultG[i, j] = gray[i, j]; resultB[i, j] = gray[i, j]; } } } } image = Helpers.SetPixels(image, resultR, resultG, resultB); } return(image); }
private static Bitmap InvertColorPlaneHelper(Bitmap img, InveseVariant variant) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); int[,] resultR = new int[img.Height, img.Width]; int[,] resultG = new int[img.Height, img.Width]; int[,] resultB = new int[img.Height, img.Width]; if (Checks.RGBinput(img)) { List <ArraysListInt> ColorList = Helpers.GetPixels(img); switch (variant) { case InveseVariant.InverseR: resultR = ColorList[0].Color.ConstSubArrayElements(255); resultG = ColorList[1].Color; resultB = ColorList[2].Color; break; case InveseVariant.InverseG: resultG = ColorList[1].Color.ConstSubArrayElements(255); resultR = ColorList[0].Color; resultB = ColorList[2].Color; break; case InveseVariant.InverseB: resultB = ColorList[2].Color.ConstSubArrayElements(255); resultR = ColorList[0].Color; resultG = ColorList[1].Color; break; case InveseVariant.InverseRG: resultR = ColorList[0].Color.ConstSubArrayElements(255); resultG = ColorList[1].Color.ConstSubArrayElements(255); resultB = ColorList[2].Color; break; case InveseVariant.InverseRB: resultR = ColorList[0].Color.ConstSubArrayElements(255); resultB = ColorList[2].Color.ConstSubArrayElements(255); resultG = ColorList[1].Color; break; case InveseVariant.InverseGB: resultG = ColorList[1].Color.ConstSubArrayElements(255); resultB = ColorList[2].Color.ConstSubArrayElements(255); resultR = ColorList[0].Color; break; case InveseVariant.InverseRGB: //negative resultR = ColorList[0].Color.ConstSubArrayElements(255); resultG = ColorList[1].Color.ConstSubArrayElements(255); resultB = ColorList[2].Color.ConstSubArrayElements(255); break; } } image = Helpers.SetPixels(image, resultR, resultG, resultB); return(image); }
//convert RGB image to 24bpp BW and return bitmap object public static Bitmap RGBtoBlackWhite24bppBitmap(Bitmap img) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); if (Checks.RGBinput(img)) { var gray = Helpers.RGBToGrayArray(img); image = Helpers.SetPixels(image, gray, gray, gray); } return(image); }
//convert RGB image to 24bpp BW and save to file public static void RGBtoBlackWhite24bpp(Bitmap img) { string imgExtension = GetImageInfo.Imginfo(Imageinfo.Extension); string imgName = GetImageInfo.Imginfo(Imageinfo.FileName); string defPath = GetImageInfo.MyPath("Rand"); Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); if (Checks.RGBinput(img)) { var gray = Helpers.RGBToGrayArray(img); image = Helpers.SetPixels(image, gray, gray, gray); string outName = defPath + "_rgbToGray24bpp" + imgExtension; Helpers.SaveOptions(img, outName, imgExtension); } }
private static Bitmap RGB2Gray8bppConveter(Bitmap img) { int r, ic, oc, bmpStride, outputStride; ColorPalette palette; BitmapData inputData, outputData; Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format8bppIndexed); double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); if (Depth == 8) { Console.WriteLine("Image already 8bit. Method RGB2Gray8bpp. Return themself"); return(img); } if (Checks.RGBinput(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 inputData = 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 = inputData.Stride; outputStride = outputData.Stride; try { //Traverse each pixel of the image unsafe { byte *bmpPtr = (byte *)inputData.Scan0.ToPointer(); var outputPtr = (byte *)outputData.Scan0.ToPointer(); //Convert the pixel to it's luminance using the formula: // L = .299*R + .587*G + .114*B //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) (0.299f * bmpPtr[r * bmpStride + ic] + 0.587f * bmpPtr[r * bmpStride + ic + 1] + 0.114f * bmpPtr[r * bmpStride + ic + 2]); } } } } catch (Exception e) { Console.WriteLine("Some problems while using unsafe code. Method: RGB2Gray8bpp. \nMessage: " + e.Message); } finally { //Unlock the images img.UnlockBits(inputData); image.UnlockBits(outputData); } } return(image); }
private static Bitmap ContrastRGBProcess(Bitmap img, double Rc_low_in, double Rc_high_in, double Gc_low_in, double Gc_high_in, double Bc_low_in, double Bc_high_in, double Rc_low_out, double Rc_high_out, double Gc_low_out, double Gc_high_out, double Bc_low_out, double Bc_high_out, double gamma) { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); if (Checks.RGBinput(img)) { List <ArraysListInt> ColorList = Helpers.GetPixels(img); string outName = String.Empty; //default //gamma - linear mapping gamma = 1 //In values between low_in and high_in //Only positive values in range [0:1] //low and high specifying the In contrast limits double[] RcIn = new double[2] { 0, 1 }; double[] GcIn = new double[2] { 0, 1 }; double[] BcIn = new double[2] { 0, 1 }; //make Out intensity to values between low_out and high_out //Only positive values in range [0:1; 0:1] // If high_out < low_out, the output image is reversed, as in a photographic negative. double[] RcOut = new double[2] { 0, 1 }; double[] GcOut = new double[2] { 0, 1 }; double[] BcOut = new double[2] { 0, 1 }; if (Rc_low_in > 1 || Rc_high_in > 1 || Gc_low_in > 1 || Gc_high_in > 1 || Bc_low_in > 1 || Bc_high_in > 1 || Rc_low_in < 0 || Rc_high_in < 0 || Gc_low_in < 0 || Gc_high_in < 0 || Bc_low_in < 0 || Bc_high_in < 0 || Rc_low_out > 1 || Rc_high_out > 1 || Gc_low_out > 1 || Gc_high_out > 1 || Bc_low_out > 1 || Bc_high_out > 1 || Rc_low_out < 0 || Rc_high_out < 0 || Gc_low_out < 0 || Gc_high_out < 0 || Bc_low_out < 0 || Bc_high_out < 0) { Console.WriteLine("low_in limit and high_in limits must be in range [0:1]\n" + "low_out and high_out limits must be in range [0:1] or [1 0]"); } else if (Rc_low_in >= Rc_high_in || Gc_low_in >= Gc_high_in || Bc_low_in >= Bc_high_in) { Console.WriteLine("low_in limit must be less then high_in limit for each color plane"); } else { RcIn = new double[2] { Rc_low_in, Rc_high_in }; GcIn = new double[2] { Gc_low_in, Gc_high_in }; BcIn = new double[2] { Bc_low_in, Bc_high_in }; if (Rc_low_out != 0 || Rc_high_out != 0 || Gc_low_out != 0 || Gc_high_out != 0 || Bc_low_out != 0 || Bc_high_out != 0) { RcOut = new double[2] { Rc_low_out, Rc_high_out }; GcOut = new double[2] { Gc_low_out, Gc_high_out }; BcOut = new double[2] { Bc_low_out, Bc_high_out }; } //prevent obtain black square if all low_out = high_out = 0 in this case used default Out { 0, 1 }; //Convert integer values using lookup table var ContRc = ContrastProcess.InlutContrast(ColorList[0].Color, ContrastProcess.CountLut(RcIn, RcOut, gamma)); var ContGc = ContrastProcess.InlutContrast(ColorList[1].Color, ContrastProcess.CountLut(GcIn, GcOut, gamma)); var ContBc = ContrastProcess.InlutContrast(ColorList[2].Color, ContrastProcess.CountLut(BcIn, BcOut, gamma)); image = Helpers.SetPixels(image, ContRc, ContGc, ContBc); } } return(image); }
//obtain selected Color plane in file or their combo. public static void RGBcomponents(Bitmap img, ColorPlaneRGB colorPlane) { string imgExtension = GetImageInfo.Imginfo(Imageinfo.Extension); string imgName = GetImageInfo.Imginfo(Imageinfo.FileName); string defPath = GetImageInfo.MyPath("ColorSpace\\ColorSpacePlane"); Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); Bitmap outRes = new Bitmap(img.Width, img.Height, PixelFormat.Format8bppIndexed); string outName = String.Empty; if (Checks.RGBinput(img)) { //obtain color components var ColorList = Helpers.GetPixels(img); var Red = ColorList[0].Color; var Green = ColorList[1].Color; var Blue = ColorList[2].Color; switch (colorPlane) { case ColorPlaneRGB.R: image = MoreHelpers.SetColorPlanePixels(image, Red, ColorPlaneRGB.R); outName = defPath + imgName + "_Rcplane" + imgExtension; break; case ColorPlaneRGB.G: image = MoreHelpers.SetColorPlanePixels(image, Green, ColorPlaneRGB.G); outName = defPath + imgName + "_Gcplane" + imgExtension; break; case ColorPlaneRGB.B: image = MoreHelpers.SetColorPlanePixels(image, Blue, ColorPlaneRGB.B); outName = defPath + imgName + "_Bcplane" + imgExtension; break; case ColorPlaneRGB.RGB: image = MoreHelpers.SetColorPlanePixels(image, Red, ColorPlaneRGB.R); outName = defPath + imgName + "_Rcplane" + imgExtension; Helpers.SaveOptions(image, outName, imgExtension); image = MoreHelpers.SetColorPlanePixels(image, Green, ColorPlaneRGB.G); outName = defPath + imgName + "_Gcplane" + imgExtension; Helpers.SaveOptions(image, outName, imgExtension); image = MoreHelpers.SetColorPlanePixels(image, Blue, ColorPlaneRGB.B); outName = defPath + imgName + "_Bcplane" + imgExtension; Helpers.SaveOptions(image, outName, imgExtension); break; case ColorPlaneRGB.RGcombo: image = Helpers.SetPixels(image, Red, Green, new int[Blue.GetLength(0), Blue.GetLength(1)]); outName = defPath + imgName + "_RGplanes" + imgExtension; break; case ColorPlaneRGB.RBcombo: image = Helpers.SetPixels(image, Red, new int[Blue.GetLength(0), Blue.GetLength(1)], Blue); outName = defPath + imgName + "_RBplanes" + imgExtension; break; case ColorPlaneRGB.GBcombo: image = Helpers.SetPixels(image, new int[Blue.GetLength(0), Blue.GetLength(1)], Green, Blue); outName = defPath + imgName + "_GBplanes" + imgExtension; break; case ColorPlaneRGB.Rnarkoman: image = MoreHelpers.SetColorPlanePixels(image, Red, ColorPlaneRGB.R); outRes = MoreHelpers.Narko8bppPalette(image); outName = defPath + imgName + "_RcplaneNarkoman" + imgExtension; Helpers.SaveOptions(image, outName, imgExtension); break; case ColorPlaneRGB.Gnarkoman: image = MoreHelpers.SetColorPlanePixels(image, Green, ColorPlaneRGB.G); outRes = MoreHelpers.Narko8bppPalette(image); outName = defPath + imgName + "_GcplaneNarkoman" + imgExtension; Helpers.SaveOptions(image, outName, imgExtension); break; case ColorPlaneRGB.Bnarkoman: image = MoreHelpers.SetColorPlanePixels(image, Blue, ColorPlaneRGB.B); outRes = MoreHelpers.Narko8bppPalette(image); outName = defPath + imgName + "_BcplaneNarkoman" + imgExtension; Helpers.SaveOptions(image, outName, imgExtension); break; default: break; } if (colorPlane != ColorPlaneRGB.RGB && colorPlane != ColorPlaneRGB.Rnarkoman && colorPlane != ColorPlaneRGB.Gnarkoman && colorPlane != ColorPlaneRGB.Bnarkoman) { Helpers.SaveOptions(image, outName, imgExtension); } } }
private static Bitmap HighContrastProcess(Bitmap img, ContrastFilter filter, HighContastRGB cPlane, [CallerMemberName] string callName = "") { Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); int[,] resultR = new int[img.Height, img.Width]; int[,] resultG = new int[img.Height, img.Width]; int[,] resultB = new int[img.Height, img.Width]; double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat); int[,] filterWindow = new int[3, 3]; if (filter == ContrastFilter.filterOne) { filterWindow = ImageFilter.Ix3FWindow("HighContrast1"); } else { filterWindow = ImageFilter.Ix3FWindow("HighContrast2"); } if (callName == "HighContrastBlackWhite") { if (Depth == 8 || Checks.BlackandWhite24bppCheck(img)) { var GrayC = MoreHelpers.BlackandWhiteProcessHelper(img); resultR = ImageFilter.Filter_int(GrayC, filterWindow, PadType.replicate); resultG = resultR; resultB = resultR; } else { Console.WriteLine("There non 8bit or 24bit black and white image at input. Method:" + callName); } } else { if (Checks.RGBinput(img)) { List <ArraysListInt> ColorList = Helpers.GetPixels(img); switch (cPlane) { case HighContastRGB.R: resultR = ImageFilter.Filter_int(ColorList[0].Color, filterWindow, PadType.replicate); resultG = ColorList[1].Color; resultB = ColorList[2].Color; break; case HighContastRGB.G: resultG = ImageFilter.Filter_int(ColorList[1].Color, filterWindow, PadType.replicate); resultR = ColorList[0].Color; resultB = ColorList[2].Color; break; case HighContastRGB.B: resultB = ImageFilter.Filter_int(ColorList[2].Color, filterWindow, PadType.replicate); resultR = ColorList[0].Color; resultG = ColorList[1].Color; break; case HighContastRGB.RGB: resultR = ImageFilter.Filter_int(ColorList[0].Color, filterWindow, PadType.replicate); resultG = ImageFilter.Filter_int(ColorList[1].Color, filterWindow, PadType.replicate); resultB = ImageFilter.Filter_int(ColorList[2].Color, filterWindow, PadType.replicate); break; } } } image = Helpers.SetPixels(image, resultR, resultG, resultB); if (Depth == 8) { image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image); } return(image); }
//if direct from file. Alert: don`t recommended to use public static void ColorSpaceToFileDirectFromImage(Bitmap img, ColorSpaceType colorSpace) { string imgExtension = GetImageInfo.Imginfo(Imageinfo.Extension); string imgName = GetImageInfo.Imginfo(Imageinfo.FileName); string defPath = GetImageInfo.MyPath("ColorSpace"); Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); //back result [0 .. 255] int[,] colorPlaneOne = new int[img.Height, img.Width]; int[,] colorPlaneTwo = new int[img.Height, img.Width]; int[,] colorPlaneThree = new int[img.Height, img.Width]; List <ArraysListInt> rgbResult = new List <ArraysListInt>(); string outName = String.Empty; if (Checks.RGBinput(img)) { switch (colorSpace.ToString()) { case "rgb2hsv": var hsvResult = RGBandHSV.RGB2HSV(img); colorPlaneOne = (hsvResult[0].Color).ArrayDivByConst(360).ImageDoubleToUint8(); colorPlaneTwo = (hsvResult[1].Color).ImageDoubleToUint8(); colorPlaneThree = (hsvResult[2].Color).ImageDoubleToUint8(); outName = defPath + imgName + "_rgb2hsv" + imgExtension; break; case "hsv2rgb": rgbResult = RGBandHSV.HSV2RGB(img); colorPlaneOne = rgbResult[0].Color; colorPlaneTwo = rgbResult[1].Color; colorPlaneThree = rgbResult[2].Color; outName = defPath + imgName + "_hsv2rgb" + imgExtension; break; case "rgb2ntsc": var ntscResult = RGBandNTSC.RGB2NTSC(img); colorPlaneOne = (ntscResult[0].Color).ArrayToUint8(); colorPlaneTwo = (ntscResult[1].Color).ArrayToUint8(); colorPlaneThree = (ntscResult[2].Color).ArrayToUint8(); //if we want to save rgb2ntsc result in file //approximate result in file, coz we lost negative values in I and Q outName = defPath + imgName + "_rgb2ntsc" + imgExtension; break; case "ntsc2rgb": rgbResult = RGBandNTSC.NTSC2RGB(img); colorPlaneOne = rgbResult[0].Color; colorPlaneTwo = rgbResult[1].Color; colorPlaneThree = rgbResult[2].Color; //when ntsc2rgb from file //approximate result in file, coz we lost negative values in I and Q when saving ntsc result in file [0..255] outName = defPath + imgName + "_ntsc2rgb" + imgExtension; break; case "rgb2cmy": var cmyResult = RGBandCMY.RGB2CMY(img); colorPlaneOne = (cmyResult[0].Color).ImageDoubleToUint8(); colorPlaneTwo = (cmyResult[1].Color).ImageDoubleToUint8(); colorPlaneThree = (cmyResult[2].Color).ImageDoubleToUint8(); outName = defPath + imgName + "_rgb2cmy" + imgExtension; break; case "cmy2rgb": rgbResult = RGBandCMY.CMY2RGB(img); colorPlaneOne = rgbResult[0].Color; colorPlaneTwo = rgbResult[1].Color; colorPlaneThree = rgbResult[2].Color; outName = defPath + imgName + "_cmy2rgb" + imgExtension; break; case "rgb2YCbCr": var YCbCrResult = RGBandYCbCr.RGB2YCbCr(img); colorPlaneOne = (YCbCrResult[0].Color).ArrayToUint8(); colorPlaneTwo = (YCbCrResult[1].Color).ArrayToUint8(); colorPlaneThree = (YCbCrResult[2].Color).ArrayToUint8(); outName = defPath + imgName + "_rgb2YCbCr" + imgExtension; break; case "YCbCr2rgb": rgbResult = RGBandYCbCr.YCbCr2RGB(img); colorPlaneOne = rgbResult[0].Color; colorPlaneTwo = rgbResult[1].Color; colorPlaneThree = rgbResult[2].Color; outName = defPath + imgName + "_YCbCr2rgb" + imgExtension; break; case "rgb2xyz": var xyzrgbResult = RGBandXYZ.RGB2XYZ(img); colorPlaneOne = (xyzrgbResult[0].Color).ArrayToUint8(); colorPlaneTwo = (xyzrgbResult[1].Color).ArrayToUint8(); colorPlaneThree = (xyzrgbResult[2].Color).ArrayToUint8(); //approximate result in file, coz we lost values after comma in saving ntsc result in file [0..255] and heavy round them outName = defPath + imgName + "_rgb2xyz" + imgExtension; break; case "xyz2rgb": rgbResult = RGBandXYZ.XYZ2RGB(img); colorPlaneOne = rgbResult[0].Color; colorPlaneTwo = rgbResult[1].Color; colorPlaneThree = rgbResult[2].Color; //bad when from file, coz using heavy rounded X Y Z values; when writing them to file outName = defPath + imgName + "_xyz2rgb" + imgExtension; break; case "xyz2lab": var xyzlabResult = XYZandLab.XYZ2Lab(img); colorPlaneOne = (xyzlabResult[0].Color).ArrayToUint8(); colorPlaneTwo = (xyzlabResult[1].Color).ArrayToUint8(); colorPlaneThree = (xyzlabResult[2].Color).ArrayToUint8(); //bad when from file, coz xyz values rounded; and lost negative value in a & b when saving in [0..255] range into file outName = defPath + imgName + "_xyz2lab" + imgExtension; break; case "lab2xyz": var labxyzResult = XYZandLab.Lab2XYZ(img); colorPlaneOne = (labxyzResult[0].Color).ArrayToUint8(); colorPlaneTwo = (labxyzResult[1].Color).ArrayToUint8(); colorPlaneThree = (labxyzResult[2].Color).ArrayToUint8(); //bad when from file, coz lost a and b negative value when save to file. And lost X Y Z values when round before save in [0..255] range into file outName = defPath + imgName + "_lab2xyz" + imgExtension; break; case "rgb2lab": var rgblabResult = RGBandLab.RGB2Lab(img); colorPlaneOne = (rgblabResult[0].Color).ArrayToUint8(); colorPlaneTwo = (rgblabResult[1].Color).ArrayToUint8(); colorPlaneThree = (rgblabResult[2].Color).ArrayToUint8(); //bad, coz lost negative value in a & b when saving in [0..255] range into file outName = defPath + imgName + "_rgb2lab" + imgExtension; break; case "rgb2lab1976": var rgblab1976Result = RGBandLab.RGB2Lab1976(img); colorPlaneOne = (rgblab1976Result[0].Color).ArrayToUint8(); colorPlaneTwo = (rgblab1976Result[1].Color).ArrayToUint8(); colorPlaneThree = (rgblab1976Result[2].Color).ArrayToUint8(); //bad, coz lost negative value in a & b when saving in [0..255] range into file outName = defPath + imgName + "_rgb2lab1976" + imgExtension; break; case "lab2rgb": rgbResult = RGBandLab.Lab2RGB(img); colorPlaneOne = rgbResult[0].Color; colorPlaneTwo = rgbResult[1].Color; colorPlaneThree = rgbResult[2].Color; //very bad, coz lost a lot in converting and round everywhere... outName = defPath + imgName + "_lab2rgb" + imgExtension; break; default: colorPlaneOne = Helpers.GetPixels(img)[0].Color; colorPlaneTwo = Helpers.GetPixels(img)[1].Color; colorPlaneThree = Helpers.GetPixels(img)[2].Color; outName = defPath + imgName + "_defaultNonColorSpace" + imgExtension; break; } image = Helpers.SetPixels(image, colorPlaneOne, colorPlaneTwo, colorPlaneThree); Helpers.SaveOptions(image, outName, imgExtension); } }