Exemple #1
0
        private static byte[] LookupPixels(int[] imageData, uint[] palette)
        {
            byte[]        indexedData   = new byte[imageData.Length];
            PaletteLookup paletteLookup = new PaletteLookup(palette);

            for (int i = 0; i < indexedData.Length; ++i)
            {
                indexedData[i] = paletteLookup.GetPaletteIndex((uint)imageData[i]);
            }
            return(indexedData);
        }
        private static byte[] IndexedPixels(Color[] pixels, WuPixel[] lookups, int alphaThreshold, int maxColors, PaletteColorHistory[] paletteHistogram)
        {
            var length = maxColors == 256 ? pixels.Length : maxColors == 16 ? pixels.Length / 2 : throw new Exception("IndexedPixels: unknown pixelformat");

            byte[]        returned = new byte[length];
            PaletteLookup lookup   = new PaletteLookup(lookups);

            --maxColors;
            for (int i = 0; i < pixels.Length; i++)
            {
                WuPixel pixel     = new WuPixel(pixels[i].A, pixels[i].R, pixels[i].G, pixels[i].B);
                byte    bestMatch = (byte)maxColors;
                if (pixel.Alpha > alphaThreshold)
                {
                    bestMatch = lookup.GetPaletteIndex(pixel);
                    paletteHistogram[bestMatch].AddPixel(pixel);
                }
                switch (maxColors)
                {
                case 256 - 1:
                    returned[i] = bestMatch;
                    break;

                case 16 - 1:
                    if (i % 2 == 0)
                    {
                        returned[i / 2] = (byte)(bestMatch << 4);
                    }
                    else
                    {
                        returned[i / 2] |= (byte)(bestMatch & 0x0F);
                    }
                    break;

                case 2 - 1:
                    if (i % 8 == 0)
                    {
                        returned[i / 8] = (byte)(bestMatch << 7);
                    }
                    else
                    {
                        returned[i / 8] |= (byte)((bestMatch & 0x01) << (7 - (i % 8)));
                    }
                    break;
                }
            }

            return(returned);
        }