Exemplo n.º 1
0
        private void Init(int width, int height, TextureFormat format, int depth, int mipMapCount)
        {
            width       = Math.Max(1, width);
            height      = Math.Max(1, height);
            depth       = Math.Max(1, depth);
            mipMapCount = Math.Max(1, mipMapCount);

            mSubTextures = new SubTexture[depth, mipMapCount];
            for (int i = 0; i < depth; i++)
            {
                for (int j = 0; j < mipMapCount; j++)
                {
                    mSubTextures[i, j] = new SubTexture(width >> j, height >> j, format, i * mipMapCount + j);
                }
            }
        }
Exemplo n.º 2
0
        public static unsafe Bitmap Decode(SubTexture subTexture)
        {
            var bitmap = new Bitmap(subTexture.Width, subTexture.Height);
            var rect   = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            if (subTexture.Format == TextureFormat.RGB)
            {
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

                fixed(byte *ptr = subTexture.Data)
                RGBtoBGR(ptr, ( byte * )bitmapData.Scan0, subTexture.Data.Length);

                bitmap.UnlockBits(bitmapData);
            }
            else if (subTexture.Format == TextureFormat.RGBA)
            {
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

                fixed(byte *ptr = subTexture.Data)
                ByteRGBAToInt32(ptr, ( int * )bitmapData.Scan0, subTexture.Data.Length);

                bitmap.UnlockBits(bitmapData);
            }
            else if (subTexture.Format == TextureFormat.RGBA4)
            {
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

                fixed(byte *ptr = subTexture.Data)
                RGBA4toRGBA(ptr, ( int * )bitmapData.Scan0, subTexture.Data.Length);

                bitmap.UnlockBits(bitmapData);
            }
            else
            {
                var buffer     = DDSCodec.DecompressPixelDataToRGBA(subTexture.Data, subTexture.Width, subTexture.Height, TextureUtilities.GetDDSPixelFormat(subTexture.Format));
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
                Marshal.Copy(buffer, 0, bitmapData.Scan0, buffer.Length);
                bitmap.UnlockBits(bitmapData);
            }

            return(bitmap);
        }
Exemplo n.º 3
0
        private static unsafe void Encode(SubTexture subTexture, Bitmap bitmap)
        {
            bool ownsBitmap = false;

            if (subTexture.Width != bitmap.Width || subTexture.Height != bitmap.Height)
            {
                ownsBitmap = true;
                bitmap     = new Bitmap(bitmap, subTexture.Width, subTexture.Height);
            }

            var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            if (subTexture.Format == TextureFormat.RGB)
            {
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

                fixed(byte *ptr = subTexture.Data)
                BGRtoRGB(( byte * )bitmapData.Scan0, ptr, subTexture.Data.Length);

                bitmap.UnlockBits(bitmapData);
            }
            else if (subTexture.Format == TextureFormat.RGBA)
            {
                var bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                fixed(byte *ptr = subTexture.Data)
                Int32RGBAToByte(( int * )bitmapData.Scan0, ptr, subTexture.Data.Length);

                bitmap.UnlockBits(bitmapData);
            }
            else
            {
                var compressedPixels = DDSCodec.CompressPixelData(bitmap, TextureUtilities.GetDDSPixelFormat(subTexture.Format));
                Array.Copy(compressedPixels, subTexture.Data, subTexture.Data.Length);
            }

            if (ownsBitmap)
            {
                bitmap.Dispose();
            }
        }
Exemplo n.º 4
0
        internal void Read(EndianBinaryReader reader)
        {
            reader.PushBaseOffset();

            var signature = reader.ReadInt32();

            if (signature != 0x04505854 && signature != 0x05505854)
            {
                throw new InvalidDataException("Invalid signature (expected TXP with type 4 or 5)");
            }

            int subTextureCount = reader.ReadInt32();
            int info            = reader.ReadInt32();

            int mipMapCount = info & 0xFF;
            int depth       = (info >> 8) & 0xFF;

            if (depth == 1 && mipMapCount != subTextureCount)
            {
                mipMapCount = ( byte )subTextureCount;
            }

            mSubTextures = new SubTexture[depth, mipMapCount];
            for (int i = 0; i < depth; i++)
            {
                for (int j = 0; j < mipMapCount; j++)
                {
                    reader.ReadOffset(() =>
                    {
                        mSubTextures[i, j] = new SubTexture(reader);
                    });
                }
            }

            reader.PopBaseOffset();
        }