Exemple #1
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);
        }
Exemple #2
0
        public static unsafe Bitmap Decode(Texture texture)
        {
            if (texture.IsYCbCr)
            {
                var bitmap = new Bitmap(texture[0].Width, texture[0].Height);
                var rect   = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

                var lumBuffer = DDSCodec.DecompressPixelDataToRGBA(
                    texture[0].Data, texture[0].Width, texture[0].Height,
                    TextureUtilities.GetDDSPixelFormat(texture.Format));

                var cbrBuffer = DDSCodec.DecompressPixelDataToRGBA(
                    texture[1].Data, texture[1].Width, texture[1].Height,
                    TextureUtilities.GetDDSPixelFormat(texture.Format));

                var bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
                fixed(byte *lumPtr = lumBuffer)
                fixed(byte *cbrPtr = cbrBuffer)
                {
                    var lumIntPtr = ( int * )lumPtr;
                    var cbrIntPtr = ( int * )cbrPtr;

                    ConvertYCbCrToRGBA(lumIntPtr, cbrIntPtr, ( int * )bitmapData.Scan0, bitmap.Width, bitmap.Height);
                }

                bitmap.UnlockBits(bitmapData);
                return(bitmap);
            }

            if (texture.UsesDepth)
            {
                var bitmap = new Bitmap(texture.Width * texture.Depth, texture.Height);
                using (var gfx = Graphics.FromImage(bitmap))
                {
                    int currentIndex = 0;
                    foreach (int i in CubeMapToDDSCubeMap())
                    {
                        gfx.DrawImageUnscaled(Decode(texture[i, 0]), currentIndex++ *texture.Width, 0);
                    }
                }

                return(bitmap);
            }

            return(Decode(texture[0]));
        }
Exemple #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();
            }
        }
Exemple #4
0
        public static void DecodeToDDS(Texture texture, Stream destination)
        {
            var ddsHeader = new DDSHeader(texture.Width, texture.Height,
                                          TextureUtilities.GetDDSPixelFormat(texture.Format));

            if (texture.UsesDepth)
            {
                ddsHeader.Depth  = texture.Depth;
                ddsHeader.Flags |= DDSHeaderFlags.Depth;
                ddsHeader.Caps  |= DDSHeaderCaps.Complex;
                ddsHeader.Caps2 |= DDSHeaderCaps2.CubeMap |
                                   DDSHeaderCaps2.CubeMapPositiveX | DDSHeaderCaps2.CubeMapNegativeX |
                                   DDSHeaderCaps2.CubeMapPositiveY | DDSHeaderCaps2.CubeMapNegativeY |
                                   DDSHeaderCaps2.CubeMapPositiveZ | DDSHeaderCaps2.CubeMapNegativeZ;
            }

            if (texture.UsesMipMaps)
            {
                ddsHeader.MipMapCount = texture.MipMapCount;
                ddsHeader.Flags      |= DDSHeaderFlags.MipMapCount;
                ddsHeader.Caps       |= DDSHeaderCaps.Complex;
            }

            ddsHeader.Save(destination);

            if (texture.UsesDepth)
            {
                foreach (int i in CubeMapToDDSCubeMap())
                {
                    foreach (var mipMap in texture.EnumerateMipMaps(i))
                    {
                        destination.Write(mipMap.Data, 0, mipMap.Data.Length);
                    }
                }
            }

            else
            {
                foreach (var mipMap in texture.EnumerateMipMaps())
                {
                    destination.Write(mipMap.Data, 0, mipMap.Data.Length);
                }
            }
        }