public static SBSurface FromBitmap(Bitmap bmp) { SBSurface surface = new SBSurface(); surface.Name = ""; surface.Width = bmp.Width; surface.Height = bmp.Height; surface.InternalFormat = InternalFormat.Rgba; surface.PixelFormat = PixelFormat.Bgra; surface.PixelType = PixelType.UnsignedByte; var bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var length = bitmapData.Stride * bitmapData.Height; byte[] bytes = new byte[length]; Marshal.Copy(bitmapData.Scan0, bytes, 0, length); bmp.UnlockBits(bitmapData); var mip = new MipArray(); mip.Mipmaps.Add(bytes); surface.Arrays.Add(mip); return(surface); }
public static SBSurface Open(string FilePath) { using (BinaryReader reader = new BinaryReader(new FileStream(FilePath, FileMode.Open))) { // TODO: Why are there empty streams? if (reader.BaseStream.Length == 0) { return(null); } SBSurface surface = new SBSurface(); reader.BaseStream.Position = reader.BaseStream.Length - 0xB0; int[] mipmapSizes = new int[16]; for (int i = 0; i < mipmapSizes.Length; i++) { mipmapSizes[i] = reader.ReadInt32(); } reader.ReadChars(4); // TNX magic string texName = ReadTexName(reader); surface.Name = texName; surface.Width = reader.ReadInt32(); surface.Height = reader.ReadInt32(); surface.Depth = reader.ReadInt32(); var Format = (NUTEX_FORMAT)reader.ReadByte(); reader.ReadByte(); ushort Padding = reader.ReadUInt16(); reader.ReadUInt32(); int MipCount = reader.ReadInt32(); int Alignment = reader.ReadInt32(); surface.ArrayCount = reader.ReadInt32(); int ImageSize = reader.ReadInt32(); char[] Magic = reader.ReadChars(4); int MajorVersion = reader.ReadInt16(); int MinorVersion = reader.ReadInt16(); if (pixelFormatByNuTexFormat.ContainsKey(Format)) { surface.PixelFormat = pixelFormatByNuTexFormat[Format]; } if (internalFormatByNuTexFormat.ContainsKey(Format)) { surface.InternalFormat = internalFormatByNuTexFormat[Format]; } surface.PixelType = GetPixelType(Format); reader.BaseStream.Position = 0; byte[] ImageData = reader.ReadBytes(ImageSize); for (int array = 0; array < surface.ArrayCount; array++) { MipArray arr = new MipArray(); for (int i = 0; i < MipCount; i++) { byte[] deswiz = SwitchSwizzler.GetImageData(surface, ImageData, array, i, MipCount); arr.Mipmaps.Add(deswiz); } surface.Arrays.Add(arr); } return(surface); } }