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 static unsafe void ForEach(this UnmanagedImage<TPixel> src, TPixel* start, uint length, ActionOnPixel handler)
 {
     TPixel* end = start + src.Length;
     while (start != end)
     {
         handler(start);
         ++start;
     }
 }
 public unsafe void Fill(TPixel pixel)
 {
     TPixel* p = this.Start;
     TPixel* end = p + this.Length;
     while (p != end)
     {
         *p = pixel;
         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 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 ToLab24(Argb32* from, Lab24* to, int length = 1)
        {
            // 使用 OpenCV 中的算法实现

            if (length < 1) return;

            Argb32* 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(Argb32* from, byte* to, int length = 1)
        {
            if (length < 1) return;

            Argb32* end = from + length;
            while (from != end)
            {
                *to = (Byte)(from->Blue * 0.114 + from->Green * 0.587 + from->Red * 0.299);
                from++;
                to++;
            }
        }
        public static unsafe void ToArgb32(Byte* from, Argb32* 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;
                to->Alpha = 255;
                from++;
                to++;
            }
        }
        public static unsafe void ToArgb32(Rgb24* from, Argb32* 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++;
            }
        }