Ejemplo n.º 1
0
Archivo: DXT.cs Proyecto: talestra/tov
        /// <summary>
        ///
        /// </summary>
        /// <param name="File"></param>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        /// <param name="_Depth"></param>
        /// <param name="Swizzled"></param>
        /// <returns></returns>
        private BitmapList _LoadSwizzled(Stream File, int Width, int Height, int?_Depth, bool Swizzled = true)
        {
            if ((Width % 4) != 0 || (Height % 4) != 0)
            {
                throw (new InvalidDataException(String.Format("Invalid size {0}x{1} must be multiple of 4", Width, Height)));
            }

            int  Depth              = _Depth ?? 1;
            bool Is3D               = _Depth.HasValue;
            var  BitmapList         = new BitmapList(Depth);
            var  BitmapListData     = new BitmapData[Depth];
            var  BitmapListPointers = new ARGB_Rev *[Depth];

            for (int n = 0; n < Depth; n++)
            {
                BitmapList.Bitmaps[n] = new Bitmap(Width, Height);
            }

            for (int n = 0; n < Depth; n++)
            {
                var Bitmap = BitmapList.Bitmaps[n];
                BitmapListData[n]     = Bitmap.LockBits(Bitmap.GetFullRectangle(), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
                BitmapListPointers[n] = (ARGB_Rev *)BitmapListData[n].Scan0.ToPointer();
            }

            int BlockWidth           = Width / 4;
            int BlockHeight          = Height / 4;
            var CurrentDecodedColors = new ARGB_Rev[4 * 4];

            var ExpectedBlockCount = BlockWidth * BlockHeight * Depth;
            int RealUsedBlockCount;

            if (Is3D)
            {
                RealUsedBlockCount = Swizzling.XGAddress3DTiledExtent(Width / 4, Height / 4, Depth, BlockSize);
            }
            else
            {
                RealUsedBlockCount = Swizzling.XGAddress2DTiledExtent(Width / 4, Height / 4, BlockSize);
            }
            //Console.WriteLine("{0} - {1}", ExpectedBlockCount, UsedBlockCount);

            var BlockCount = RealUsedBlockCount;

            //var BlockCount = ExpectedBlockCount;

            if (BlockCount * Marshal.SizeOf(typeof(TBlock)) > File.Length)
            {
                Console.Error.WriteLine("File too small");
                //throw(new Exception("File too small"));
                return(new BitmapList(0));
            }

            var Blocks = File.ReadStructVector <TBlock>((uint)BlockCount, -1);

            //Console.WriteLine(Blocks.Length);

            for (int BlockN = 0; BlockN < BlockCount; BlockN++)
            {
                int TileX, TileY, TileZ;

                if (Swizzled)
                {
                    if (Is3D)
                    {
                        Swizzling.XGAddress3DTiledXYZ(BlockN, BlockWidth, BlockHeight, BlockSize, out TileX, out TileY, out TileZ);
                    }
                    else
                    {
                        Swizzling.XGAddress2DTiledXY(BlockN, BlockWidth, BlockSize, out TileX, out TileY);
                        TileZ = 0;
                    }
                }
                else
                {
                    TileX = BlockN % BlockWidth;
                    TileY = BlockN / BlockWidth;
                    TileZ = 0;
                    Console.Error.Write("(Not implemented!)");
                }

                // Skip blocks.
                if (TileX >= BlockWidth || TileY >= BlockHeight)
                {
                    continue;
                }

                DecodeBlock(ref Blocks[BlockN], ref CurrentDecodedColors);

                //Console.WriteLine("{0}", CurrentDecodedColors[0]);

                int PositionX = TileX * 4;
                int PositionY = TileY * 4;

                var BlockBitmap = BitmapList.Bitmaps[TileZ];

                if ((PositionX + 3 >= BlockBitmap.Width) || (PositionY + 3 >= BlockBitmap.Height))
                {
                    Console.Error.WriteLine(
                        "(Warning! [Read] Position outside ({0}, {1}) - ({2}x{3}) ;; ({4}, {5})) - ({6}x{7}) ;; {8}",
                        PositionX, PositionY,
                        Width, Height,
                        TileX, TileY,
                        BlockWidth, BlockHeight,
                        BlockN
                        );
                    continue;
                }

                int n             = 0;
                var BitmapPointer = BitmapListPointers[TileZ];
                for (int y = 0; y < 4; y++)
                {
                    int BaseOffset = (PositionY + y) * Width + (PositionX);
                    for (int x = 0; x < 4; x++)
                    {
                        BitmapPointer[BaseOffset + x] = CurrentDecodedColors[n];
                        n++;
                    }
                }
            }

            for (int n = 0; n < Depth; n++)
            {
                BitmapList.Bitmaps[n].UnlockBits(BitmapListData[n]);
            }

            return(BitmapList);
        }
Ejemplo n.º 2
0
Archivo: DXT.cs Proyecto: talestra/tov
        //static private ARGB_Rev FindNearColorInPalette(IEnumerable<ARGB_Rev> Palette, ARGB_Rev Color)
        //{
        //	return Palette.OrderBy(PaletteColor => ARGB_Rev.DistanceRGB(PaletteColor, Color)).First();
        //}
        //
        //static private ARGB_Rev FindFarColorInPalette(IEnumerable<ARGB_Rev> Palette, ARGB_Rev Color)
        //{
        //	return Palette.OrderBy(PaletteColor => ARGB_Rev.DistanceRGB(PaletteColor, Color)).Last();
        //}
        //
        //static private ARGB_Rev AverageColors(IEnumerable<ARGB_Rev> Palette)
        //{
        //	return new ARGB_Rev(
        //		(byte)Palette.Average(Item => Item.A),
        //		(byte)Palette.Average(Item => Item.R),
        //		(byte)Palette.Average(Item => Item.G),
        //		(byte)Palette.Average(Item => Item.B)
        //	);
        //}
        //
        //private void ReduceColors(List<ARGB_Rev> Palette, int ColorCount)
        //{
        //	while (Palette.Count > ColorCount)
        //	{
        //		Palette.Remove(FindNearColorInPalette(Palette, AverageColors(Palette)));
        //	}
        //}
        //
        //private void DoReduceColors(ref ARGB_Rev[] Colors, int ColorCount)
        //{
        //	var ReducedColorsList = Colors.ToList();
        //	ReduceColors(ReducedColorsList, ColorCount);
        //	//Console.WriteLine(String.Join(",", Colors.ToArray()));
        //	//Console.WriteLine(String.Join(",", ReducedColorsList.ToArray()));
        //	for (int n = 0; n < Colors.Length; n++)
        //	{
        //		Colors[n] = FindNearColorInPalette(ReducedColorsList, Colors[n]);
        //	}
        //}

        /// <summary>
        ///
        /// </summary>
        /// <param name="Bitmap"></param>
        /// <param name="File"></param>
        /// <param name="mode"></param>
        public void SaveSwizzled2D(Bitmap Bitmap, Stream File, CompressDXT.CompressionMode mode = CompressDXT.CompressionMode.Normal, int ReduceColors = 16, bool ShowWarnings = false)
        {
            int Width = Bitmap.Width, Height = Bitmap.Height;

            if ((Width % 4) != 0 || (Height % 4) != 0)
            {
                throw (new InvalidDataException());
            }

            Bitmap.LockBitsUnlock(PixelFormat.Format32bppArgb, (BitmapData) =>
            {
                var Base = (ARGB_Rev *)BitmapData.Scan0.ToPointer();

                int BlockWidth  = Width / 4;
                int BlockHeight = Height / 4;

                //var BlockCount = BlockWidth * BlockHeight;
                var ExpectedBlockCount = BlockWidth * BlockHeight;
                int RealUsedBlockCount;

                RealUsedBlockCount = Swizzling.XGAddress2DTiledExtent(Width / 4, Height / 4, BlockSize);
                //Console.WriteLine("{0} - {1}", ExpectedBlockCount, UsedBlockCount);

                var BlockCount = RealUsedBlockCount;

                var CurrentDecodedColors = new ARGB_Rev[4 * 4];
                var Blocks = new TBlock[(uint)BlockCount];

                for (int dxt5_n = 0; dxt5_n < BlockCount; dxt5_n++)
                {
                    int TileX, TileY;
                    Swizzling.XGAddress2DTiledXY(dxt5_n, BlockWidth, BlockSize, out TileX, out TileY);

                    int PositionX = TileX * 4;
                    int PositionY = TileY * 4;
                    int n         = 0;

                    if ((PositionX + 3 >= Width) || (PositionY + 3 >= Height))
                    {
                        if (ShowWarnings)
                        {
                            Console.Error.WriteLine("(Warning! [Write] Position outside ({0}, {1}) - ({2}x{3}))", PositionX, PositionY, Width, Height);
                        }
                        continue;
                    }

                    for (int y = 0; y < 4; y++)
                    {
                        for (int x = 0; x < 4; x++)
                        {
                            CurrentDecodedColors[n] = Base[(PositionY + y) * Width + (PositionX + x)];
                            n++;
                        }
                    }

                    //if (ReduceColors != 16) DoReduceColors(ref CurrentDecodedColors, ReduceColors);

                    EncodeBlock(ref Blocks[dxt5_n], ref CurrentDecodedColors, mode);
                }

                File.WriteStructVector(Blocks);
                File.Flush();
            });
        }