Esempio n. 1
0
        public static unsafe void Clamp(this Bitmap bmp, ColorPalette palette)
        {
            int        w = bmp.Width, h = bmp.Height, e = palette.Entries.Length;
            BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            for (ARGBPixel *ptr = (ARGBPixel *)data.Scan0, ceil = ptr + (w * h); ptr < ceil;)
            {
                *ptr = (ARGBPixel)palette.Entries[palette.FindMatch(*ptr++)];

                //ARGBPixel p = *ptr;
                //int bestDist = Int32.MaxValue, bestIndex = 0;
                //for(int i = 0 ; i < e ; i++)
                //{
                //    int dist = p.DistanceTo(palette.Entries[i]);
                //    if(dist < bestDist)
                //    {
                //        bestDist = dist;
                //        bestIndex = i;
                //        if (dist == 0) break;
                //    }
                //}
                //*ptr++ = (ARGBPixel)palette.Entries[bestIndex];
            }
            bmp.UnlockBits(data);
        }