Пример #1
0
        public static int GetClosestColorWithAlpha(Color c, Color[] pal)
        {
            int   bestInd = 0;
            float bestDif = ImageIndexer.ColorDifference(pal[0], c);

            for (int i = 0; i < pal.Length; i++)
            {
                float d = ImageIndexer.ColorDifference(pal[i], c);
                if (d < bestDif)
                {
                    bestDif = d;
                    bestInd = i;
                }
            }

            return(bestInd);
        }
Пример #2
0
        public static EncodedImage Encode_Tex4x4(Bitmap Image, int MaximumNumberofPalettes)
        {
            int tx = Image.Width / 4;
            int ty = Image.Height / 4;

            Color[][] Palettes      = new Color[tx * ty][];
            int[]     PaletteCounts = new int[tx * ty];
            int[,] PaletteNumbers = new int[tx, ty];

            int PaletteNumber = 0;

            for (int x = 0; x < tx; x++)
            {
                for (int y = 0; y < ty; y++)
                {
                    ImageIndexer Indexer = new ImageIndexer(Image, x * 4, y * 4);
                    Palettes[PaletteNumber]      = Indexer.palette;
                    PaletteNumbers[x, y]         = PaletteNumber;
                    PaletteCounts[PaletteNumber] = 1;
                    PaletteNumber++;
                }
            }

            int Current = 0;

            Color[] FinalPalette      = new Color[MaximumNumberofPalettes * 4];
            int[]   NewPaletteNumbers = new int[Palettes.Length];
            for (int i = 0; i < Palettes.Length; i++)
            {
                if (PaletteCounts[i] != 0)
                {
                    NewPaletteNumbers[i] = Current;
                    Array.Copy(Palettes[i], 0, FinalPalette, Current * 4, 4);
                    Current++;
                }
            }

            List <byte> Data1List = new List <byte>();
            List <byte> Data2List = new List <byte>();

            for (int y = 0; y < ty; y++)
            {
                for (int x = 0; x < tx; x++)
                {
                    bool HasTransparentPixels = false;
                    for (int yy = 0; yy < 4; yy++)
                    {
                        for (int xx = 0; xx < 4; xx++)
                        {
                            Color Colorl = Image.GetPixel(x * 4 + xx, y * 4 + yy);
                            if (Colorl.A < 128)
                            {
                                HasTransparentPixels = true;
                            }
                        }
                    }

                    for (int yy = 0; yy < 4; yy++)
                    {
                        byte FinalByte = 0;
                        byte Pow       = 1;

                        for (int xx = 0; xx < 4; xx++)
                        {
                            Color Colorl = Image.GetPixel(x * 4 + xx, y * 4 + yy);
                            byte  Color;

                            if (Colorl.A < 128)
                            {
                                Color = 3;
                            }
                            else
                            {
                                if (!HasTransparentPixels)
                                {
                                    Color = (byte)GetClosestColor(Colorl, Palettes[PaletteNumbers[x, y]]);
                                }
                                else
                                {
                                    Color = (byte)GetClosestColorWithAlpha(Colorl, Palettes[PaletteNumbers[x, y]]);
                                }

                                if (Color == 3)
                                {
                                    Color = 2;
                                }
                            }

                            FinalByte |= (byte)(Pow * Color);
                            Pow       *= 4;
                        }
                        Data1List.Add(FinalByte);
                    }

                    UInt16 Data = (UInt16)(NewPaletteNumbers[PaletteNumbers[x, y]] * 2);
                    if (!HasTransparentPixels)
                    {
                        Data |= 2 << 14;
                    }

                    byte[] P = BitConverter.GetBytes((UInt16)Data);
                    Data2List.Add(P[0]);
                    Data2List.Add(P[1]);
                }
            }

            return(new EncodedImage(Data1List.ToArray(), EncodePalette(FinalPalette), Data2List.ToArray()));
        }