Exemple #1
0
        internal static Image ApplyPalette(this Image image, IList <Color> palette)
        {
            Image dst = new Image(image.Width, image.Height, 32, image);

            int height       = image.Height;
            int bitsPerPixel = image.BitsPerPixel;

            ulong[] srcbits   = image.Bits;
            ulong[] dstbits   = dst.Bits;
            int     srcstride = image.Stride;
            int     dststride = dst.Stride;

            int srcwidthbits   = image.WidthBits;
            int srcwidthbits2  = image.WidthBits & ~1;
            int srcwidthbits64 = image.WidthBits & ~63;

            uint[] colormap = palette.Select(x => x.Argb).ToArray();

            for (int iy = 0, yoffsrc = 0, yoffdst = 0; iy < height; iy++, yoffsrc += srcstride, yoffdst += dststride)
            {
                int xoffsrc = yoffsrc;
                int xoffdst = yoffdst;
                int ix      = 0;

                for (; ix < srcwidthbits64; ix += 64, xoffsrc++)
                {
                    ulong bits = srcbits[xoffsrc];
                    for (int i = 0; i < 64; i += 2 * bitsPerPixel, xoffdst++)
                    {
                        dstbits[xoffdst] =
                            (ulong)colormap[BitUtils.GetBits(bits, i, bitsPerPixel)] |
                            ((ulong)colormap[BitUtils.GetBits(bits, i + bitsPerPixel, bitsPerPixel)] << 32);
                    }
                }

                if (ix < srcwidthbits)
                {
                    ulong bits = srcbits[xoffsrc];

                    for (; ix < srcwidthbits2; ix += 2 * bitsPerPixel, xoffdst++)
                    {
                        dstbits[xoffdst] =
                            (ulong)colormap[BitUtils.GetBits(bits, ix & 63, bitsPerPixel)] |
                            ((ulong)colormap[BitUtils.GetBits(bits, (ix + bitsPerPixel) & 63, bitsPerPixel)] << 32);
                    }

                    if (ix < srcwidthbits)
                    {
                        dstbits[xoffdst] = (ulong)colormap[BitUtils.GetBits(bits, ix & 63, bitsPerPixel)];
                    }
                }
            }

            return(dst);
        }
Exemple #2
0
        public uint GetPixel(int x, int y)
        {
            this.ValidatePosition(x, y);

            int xpos = x * this.BitsPerPixel;
            int pos  = (y * this.Stride) + (xpos >> 6);

            xpos &= 63;

            if (this.BitsPerPixel == 24 && xpos + 24 > 64)
            {
                int rem = 64 - xpos;
                return((uint)BitUtils.GetBits(this.Bits[pos], xpos, rem) |
                       (uint)(BitUtils.GetBits(this.Bits[pos + 1], 0, 24 - rem) << rem));
            }
            else
            {
                return((uint)BitUtils.GetBits(this.Bits[pos], xpos, this.BitsPerPixel));
            }
        }