Example #1
0
        public Rgb24 ToRgb24()
        {
            const double c00 = 0.57735;
            const double c01 = 0.408248;
            const double c02 = 0.707107;
            const double c10 = 0.57735;
            const double c11 = 0.408248;
            const double c12 = -0.707107;
            const double c20 = 0.57735;
            const double c21 = -0.8164965;
            const double c22 = 0;

            double l = c00 * L + c01 * Alpha + c02 * Beta;
            double m = c10 * L + c11 * Alpha + c12 * Beta;
            double s = c20 * L + c21 * Alpha + c22 * Beta;

            l = Math.Pow(10, l);
            m = Math.Pow(10, m);
            s = Math.Pow(10, s);

            int r = (int)(4.4679 * l -3.5873 * m + 0.1193 * s);
            int g = (int)(-1.2186 * l + 2.3809 * m - 0.1624 * s);
            int b = (int)(0.0497 * l - 0.2439 * m + 1.2045 * s);
            if (r < 0) r = 0; else if (r > 255) r = 255;
            if (g < 0) g = 0; else if (g > 255) g = 255;
            if (b < 0) b = 0; else if (b > 255) b = 255;
            Rgb24 rgb = new Rgb24();
            rgb.Red = (Byte)r;
            rgb.Green = (Byte)g;
            rgb.Blue = (Byte)b;

            return rgb;
        }
 public static unsafe UnmanagedImage<TPixel> ForEach(this UnmanagedImage<TPixel> src, TPixel* start, uint length, ActionOnPixel handler)
 {
     TPixel* end = start + src.Length;
     while (start != end)
     {
         handler(start);
         ++start;
     }
     return src;
 }
 public unsafe void Replace(TPixel pixel, TPixel replaced)
 {
     TPixel* p = this.Start;
     TPixel* end = p + this.Length;
     while (p != end)
     {
         if (*p == pixel)
         {
             *p = replaced;
         }
         p++;
     }
 }
        public void FloodFill(System.Drawing.Point location, TPixel anchorColor, TPixel replecedColor)
        {
            int width = this.Width;
            int height = this.Height;
            if (location.X < 0 || location.X >= width || location.Y < 0 || location.Y >= height) return;

            if (anchorColor == replecedColor) return;
            if (this[location.Y, location.X] != anchorColor) return;

            Stack<System.Drawing.Point> points = new Stack<System.Drawing.Point>();
            points.Push(location);

            int ww = width - 1;
            int hh = height - 1;

            while (points.Count > 0)
            {
                System.Drawing.Point p = points.Pop();
                this[p.Y, p.X] = replecedColor;
                if (p.X > 0 && this[p.Y, p.X - 1] == anchorColor)
                {
                    this[p.Y, p.X - 1] = replecedColor;
                    points.Push(new System.Drawing.Point(p.X - 1, p.Y));
                }

                if (p.X < ww && this[p.Y, p.X + 1] == anchorColor)
                {
                    this[p.Y, p.X + 1] = replecedColor;
                    points.Push(new System.Drawing.Point(p.X + 1, p.Y));
                }

                if (p.Y > 0 && this[p.Y - 1, p.X] == anchorColor)
                {
                    this[p.Y - 1, p.X] = replecedColor;
                    points.Push(new System.Drawing.Point(p.X, p.Y - 1));
                }

                if (p.Y < hh && this[p.Y + 1, p.X] == anchorColor)
                {
                    this[p.Y + 1, p.X] = replecedColor;
                    points.Push(new System.Drawing.Point(p.X, p.Y + 1));
                }
            }
        }
 public unsafe void Fill(TPixel pixel)
 {
     TPixel* p = this.Start;
     TPixel* end = p + this.Length;
     while (p != end)
     {
         *p = pixel;
         p++;
     }
 }
        public static unsafe void ToRgb24(byte* from, Rgb24* to, int length = 1)
        {
            if (length < 1) return;

            Byte* end = from + length;
            while (from != end)
            {
                Byte val = *from;
                to->Blue = val;
                to->Green = val;
                to->Red = val;
                from++;
                to++;
            }
        }
        public static unsafe void ToSignedArgb64(Rgb24* from, SignedArgb64* to, int length = 1)
        {
            if (length < 1) return;

            Rgb24* end = from + length;
            while (from != end)
            {
                to->Blue = from->Blue;
                to->Green = from->Green;
                to->Red = from->Red;
                to->Alpha = 255;
                from++;
                to++;
            }
        }
 public static unsafe void ToRgb24(Argb32* from, Rgb24* to, int length = 1)
 {
     if (length < 1) return;
     Argb32* end = from + length;
     while (from != end)
     {
         *to = *((Rgb24*)from);
         from++;
         to++;
     }
 }
        public static unsafe void ToRgb24(Lab24* from, Rgb24* to, int length=1)
        {
            if (length < 1) return;

            // 使用 OpenCV 中的算法实现
            const float coeff0 = 0.39215686274509809f;
            const float coeff1 = 0.0f;
            const float coeff2 = 1.0f;
            const float coeff3 = (-128.0f);
            const float coeff4 = 1.0f;
            const float coeff5 = (-128.0f);

            Lab24* end = from + length;
            float x, y, z,l,a,b;
            int blue, green, red;

            while (from != end)
            {
                l = from->L * coeff0 + coeff1;
                a = from->A * coeff2 + coeff3;
                b = from->B * coeff4 + coeff5;

                l = (l + labLShift_32f) * (1.0f / labLScale_32f);
                x = (l + a * 0.002f);
                z = (l - b * 0.005f);

                y = l * l * l;
                x = x * x * x;
                z = z * z * z;

                blue = (int)((x * labBx_32f + y * labBy_32f + z * labBz_32f) * 255 + 0.5);
                green = (int)((x * labGx_32f + y * labGy_32f + z * labGz_32f) * 255 + 0.5);
                red = (int)((x * labRx_32f + y * labRy_32f + z * labRz_32f) * 255 + 0.5);

                red = red < 0 ? 0 : red > 255 ? 255 : red;
                green = green < 0 ? 0 : green > 255 ? 255 : green;
                blue = blue < 0 ? 0 : blue > 255 ? 255 : blue;

                to->Red = (byte)red;
                to->Green = (byte)green;
                to->Blue = (byte)blue;

                from++;
                to++;
            }
        }
        public static unsafe void ToLab24(Rgb24* from, Lab24* to, int length=1)
        {
            // 使用 OpenCV 中的算法实现

            if (length < 1) return;

            Rgb24* end = from + length;

            int x, y, z;
            int l, a, b;
            bool flag;

            while (from != end)
            {
                Byte red = from->Red;
                Byte green = from->Green;
                Byte blue = from->Blue;

                x = blue * labXb + green * labXg + red * labXr;
                y = blue * labYb + green * labYg + red * labYr;
                z = blue * labZb + green * labZg + red * labZr;

                flag = x > labT;

                x = (((x) + (1 << ((lab_shift) - 1))) >> (lab_shift));

                if (flag)
                    x = icvLabCubeRootTab[x];
                else
                    x = (((x * labSmallScale + labSmallShift) + (1 << ((lab_shift) - 1))) >> (lab_shift));

                flag = z > labT;
                z = (((z) + (1 << ((lab_shift) - 1))) >> (lab_shift));

                if (flag == true)
                    z = icvLabCubeRootTab[z];
                else
                    z = (((z * labSmallScale + labSmallShift) + (1 << ((lab_shift) - 1))) >> (lab_shift));

                flag = y > labT;
                y = (((y) + (1 << ((lab_shift) - 1))) >> (lab_shift));

                if (flag == true)
                {
                    y = icvLabCubeRootTab[y];
                    l = (((y * labLScale - labLShift) + (1 << ((2 * lab_shift) - 1))) >> (2 * lab_shift));
                }
                else
                {
                    l = (((y * labLScale2) + (1 << ((lab_shift) - 1))) >> (lab_shift));
                    y = (((y * labSmallScale + labSmallShift) + (1 << ((lab_shift) - 1))) >> (lab_shift));
                }

                a = (((500 * (x - y)) + (1 << ((lab_shift) - 1))) >> (lab_shift)) + 129;
                b = (((200 * (y - z)) + (1 << ((lab_shift) - 1))) >> (lab_shift)) + 128;

                l = l > 255 ? 255 : l < 0 ? 0 : l;
                a = a > 255 ? 255 : a < 0 ? 0 : a;
                b = b > 255 ? 255 : b < 0 ? 0 : b;

                to->L = (byte)l;
                to->A = (byte)a;
                to->B = (byte)b;

                from++;
                to++;
            }
        }
        public static unsafe void ToByte(Rgb24* from, byte* to, int length = 1)
        {
            if (length < 1) return;

            Rgb24* end = from + length;
            while (from != end)
            {
                *to = (Byte)(from->Blue * 0.114 + from->Green * 0.587 + from->Red * 0.299);
                from++;
                to++;
            }
        }
Example #12
0
 public static Cmyk CreateFrom(Rgb24 rgb)
 {
     return CreateFrom(rgb.Red, rgb.Green, rgb.Blue);
 }