public static void RgbToLab(BitmapWrapper input, BitmapWrapper outL, BitmapWrapper outA, BitmapWrapper outB, LabSettings s) { //create conversion matrix Func <Chromacity, (float, float, float)> getCoordinaes = c => (c.X / c.Y, 1, (1 - c.X - c.Y) / c.Y); (float Xr, float Yr, float Zr) = getCoordinaes(s.RedPrimary); (float Xg, float Yg, float Zg) = getCoordinaes(s.GreenPrimary); (float Xb, float Yb, float Zb) = getCoordinaes(s.BluePrimary); (float Xw, float Yw, float Zw) = getCoordinaes(s.WhitePoint); float[,] sMatrix = new float[3, 3] { { Xr, Xg, Xb }, { Yr, Yg, Yb }, { Zr, Zg, Zb } }; InverseMatrix3(sMatrix); float[] SrSgSb = Matrix3TimesVector(sMatrix, new float[] { Xw, Yw, Zw }); float Sr = SrSgSb[0]; float Sg = SrSgSb[1]; float Sb = SrSgSb[2]; float[,] M = new float[3, 3] { { Sr *Xr, Sg *Xg, Sb *Xb }, { Sr *Yr, Sg *Yg, Sb *Yb }, { Sr *Zr, Sg *Zg, Sb *Zb } }; //transform var size = input.GetSize(); Parallel.For(0, size.Height, h => { for (int x = 0; x < size.Width; x++) { (float L, float a, float b) = RgbToLab(input.GetPixel(x, h), M, new float[3] { Xw, Yw, Zw }, s.Gamma); int li = (int)(L / 100f * 255f); int ai = (int)(a * 128f / 100f) + 127; int bi = (int)(b * 128f / 100f) + 127; li = ClampColorInt(li); ai = ClampColorInt(ai); bi = ClampColorInt(bi); outL.SetPixel(x, h, Color.FromArgb(li, li, li)); outA.SetPixel(x, h, Color.FromArgb(ai, 255 - ai, 127)); outB.SetPixel(x, h, Color.FromArgb(bi, 127, 255 - bi)); } }); }
private void separateButton_Click(object sender, EventArgs e) { if (inputBitmap == null) { MessageBox.Show("No input bitmap!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var size = inputBitmap.GetSize(); outputBitmaps = new BitmapWrapper[3]; for (int i = 0; i < 3; i++) { outputBitmaps[i] = new BitmapWrapper(size.Width, size.Height); } Cursor.Current = Cursors.WaitCursor; try { if (variables.ColorRepresentation == ColorRepresentation.HSV) { Transforms.RgbToHsv(inputBitmap, outputBitmaps[0], outputBitmaps[1], outputBitmaps[2]); } else if (variables.ColorRepresentation == ColorRepresentation.YCbCr) { Transforms.RgbToYCbCr(inputBitmap, outputBitmaps[0], outputBitmaps[1], outputBitmaps[2]); } else if (variables.ColorRepresentation == ColorRepresentation.Lab) { //get lab settings var labSettings = new LabSettings { RedPrimary = new Chromacity((float)redPrimaryX.Value, (float)redPrimaryY.Value), GreenPrimary = new Chromacity((float)greenPrimaryX.Value, (float)greenPrimaryY.Value), BluePrimary = new Chromacity((float)bluePrimaryX.Value, (float)bluePrimaryY.Value), WhitePoint = new Chromacity((float)whitePointX.Value, (float)whitePointY.Value), Gamma = (float)gamma.Value }; Transforms.RgbToLab(inputBitmap, outputBitmaps[0], outputBitmaps[1], outputBitmaps[2], labSettings); } } catch (Exception ex) { Cursor.Current = Cursors.Default; MessageBox.Show("Error while transforming! \n" + ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } output1.BackgroundImage = outputBitmaps[0].ToBitmap(); output2.BackgroundImage = outputBitmaps[1].ToBitmap(); output3.BackgroundImage = outputBitmaps[2].ToBitmap(); var outLabels = variables.GetRepresentationLabels(); out1box.Text = outLabels[0]; out2box.Text = outLabels[1]; out3box.Text = outLabels[2]; GC.Collect(); Cursor.Current = Cursors.Default; }