internal void Read(EndianBinaryReader reader) { reader.PushBaseOffset(); int 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(); }
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); }
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); } } }
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(); } }
private void Init(int width, int height, TextureFormat format, int arraySize, int mipMapCount) { width = Math.Max(1, width); height = Math.Max(1, height); arraySize = Math.Max(1, arraySize); mipMapCount = Math.Max(1, mipMapCount); mSubTextures = new SubTexture[arraySize, mipMapCount]; for (int i = 0; i < arraySize; i++) { for (int j = 0; j < mipMapCount; j++) { mSubTextures[i, j] = new SubTexture(width >> j, height >> j, format); } } }