public void Read(Texture tex) { ImageKey = "Texture"; SelectedImageKey = "Texture"; Text = tex.Name; ChannelRed = tex.CompSelR; ChannelGreen = tex.CompSelG; ChannelBlue = tex.CompSelB; ChannelAlpha = tex.CompSelA; renderedTex.width = (int)tex.Width; renderedTex.height = (int)tex.Height; format = (int)tex.Format; int swizzle = (int)tex.Swizzle; int pitch = (int)tex.Pitch; uint bpp = GTX.surfaceGetBitsPerPixel((uint)format) >> 3; GTX.GX2Surface surf = new GTX.GX2Surface(); surf.bpp = bpp; for (int surfaceLevel = 0; surfaceLevel < tex.ArrayLength; surfaceLevel++) { } GTX.Decode(surf, tex.MipData); }
public static GTX.GX2Surface CreateGx2Texture(byte[] imageData, GTXImporterSettings setting, uint tileMode, uint AAMode) { var Format = (GTX.GX2SurfaceFormat)setting.Format; Console.WriteLine("Format " + Format + " " + setting.TexName); var surfOut = GTX.getSurfaceInfo(Format, setting.TexWidth, setting.TexHeight, 1, 1, tileMode, 0, 0); uint imageSize = (uint)surfOut.surfSize; uint alignment = surfOut.baseAlign; uint pitch = surfOut.pitch; uint mipSize = 0; uint dataSize = (uint)imageData.Length; uint bpp = GTX.surfaceGetBitsPerPixel((uint)setting.Format) >> 3; if (dataSize <= 0) { throw new Exception($"Image is empty!!"); } if (surfOut.depth != 1) { throw new Exception($"Unsupported Depth {surfOut.depth}!"); } uint s = 0; switch (tileMode) { case 1: case 2: case 3: case 16: s = 0; break; default: s = 0xd0000 | setting.swizzle << 8; break; } uint blkWidth, blkHeight; if (GTX.IsFormatBCN(Format)) { blkWidth = 4; blkHeight = 4; } else { blkWidth = 1; blkHeight = 1; } List <uint> mipOffsets = new List <uint>(); List <byte[]> Swizzled = new List <byte[]>(); for (int mipLevel = 0; mipLevel < setting.MipCount; mipLevel++) { var result = TextureHelper.GetCurrentMipSize(setting.TexWidth, setting.TexHeight, blkWidth, blkHeight, bpp, mipLevel); uint offset = result.Item1; uint size = result.Item2; Console.WriteLine("Swizzle Size " + size); Console.WriteLine("Swizzle offset " + offset); Console.WriteLine("bpp " + bpp); Console.WriteLine("TexWidth " + setting.TexWidth); Console.WriteLine("TexHeight " + setting.TexHeight); Console.WriteLine("blkWidth " + blkWidth); Console.WriteLine("blkHeight " + blkHeight); Console.WriteLine("mipLevel " + mipLevel); byte[] data_ = new byte[size]; Array.Copy(imageData, offset, data_, 0, size); uint width_ = Math.Max(1, setting.TexWidth >> mipLevel); uint height_ = Math.Max(1, setting.TexHeight >> mipLevel); if (mipLevel != 0) { surfOut = GTX.getSurfaceInfo(Format, setting.TexWidth, setting.TexHeight, 1, 1, tileMode, 0, mipLevel); if (mipLevel == 1) { mipOffsets.Add(imageSize); } else { mipOffsets.Add(mipSize); } } data_ = Utils.CombineByteArray(data_, new byte[surfOut.surfSize - size]); byte[] dataAlignBytes = new byte[RoundUp(mipSize, surfOut.baseAlign) - mipSize]; if (mipLevel != 0) { mipSize += (uint)(surfOut.surfSize + dataAlignBytes.Length); } byte[] SwizzledData = GTX.swizzle(width_, height_, surfOut.height, (uint)Format, surfOut.tileMode, s, surfOut.pitch, surfOut.bpp, data_); Swizzled.Add(dataAlignBytes.Concat(SwizzledData).ToArray()); } GTX.GX2Surface surf = new GTX.GX2Surface(); surf.depth = setting.Depth; surf.width = setting.TexWidth; surf.height = setting.TexHeight; surf.depth = 1; surf.use = 1; surf.dim = (uint)setting.SurfaceDim; surf.tileMode = tileMode; surf.swizzle = s; surf.resourceFlags = 0; surf.pitch = pitch; surf.bpp = bpp; surf.format = (uint)setting.Format; surf.numMips = setting.MipCount; surf.aa = AAMode; surf.mipOffset = mipOffsets.ToArray(); surf.numMips = (uint)Swizzled.Count; surf.alignment = alignment; surf.mipSize = mipSize; surf.imageSize = imageSize; surf.data = Swizzled[0]; List <byte[]> mips = new List <byte[]>(); for (int mipLevel = 1; mipLevel < Swizzled.Count; mipLevel++) { mips.Add(Swizzled[mipLevel]); Console.WriteLine(Swizzled[mipLevel].Length); } surf.mipData = Utils.CombineByteArray(mips.ToArray()); mips.Clear(); Console.WriteLine(""); Console.WriteLine("// ----- GX2Surface Swizzled Info ----- "); Console.WriteLine(" dim = 1"); Console.WriteLine(" width = " + surf.width); Console.WriteLine(" height = " + surf.height); Console.WriteLine(" depth = 1"); Console.WriteLine(" numMips = " + surf.numMips); Console.WriteLine(" format = " + surf.format); Console.WriteLine(" aa = 0"); Console.WriteLine(" use = 1"); Console.WriteLine(" imageSize = " + surf.imageSize); Console.WriteLine(" mipSize = " + surf.mipSize); Console.WriteLine(" tileMode = " + surf.tileMode); Console.WriteLine(" swizzle = " + surf.swizzle); Console.WriteLine(" alignment = " + surf.alignment); Console.WriteLine(" pitch = " + surf.pitch); Console.WriteLine(" data = " + surf.data.Length); Console.WriteLine(" mipData = " + surf.mipData.Length); Console.WriteLine(""); Console.WriteLine(" GX2 Component Selector:"); Console.WriteLine(""); Console.WriteLine(" bits per pixel = " + (surf.bpp << 3)); Console.WriteLine(" bytes per pixel = " + surf.bpp); Console.WriteLine(" realSize = " + imageData.Length); return(surf); }
public void Read(Texture tex) { ImageKey = "Texture"; SelectedImageKey = "Texture"; Text = tex.Name; texture = tex; Width = tex.Width; Height = tex.Height; Format = ConvertFormat(tex.Format); format = (int)tex.Format; int swizzle = (int)tex.Swizzle; int pitch = (int)tex.Pitch; uint bpp = GTX.surfaceGetBitsPerPixel((uint)format) >> 3; GTX.GX2Surface surf = new GTX.GX2Surface(); surf.bpp = bpp; surf.height = tex.Height; surf.width = tex.Width; surf.aa = (uint)tex.AAMode; surf.alignment = tex.Alignment; surf.depth = tex.Depth; surf.dim = (uint)tex.Dim; surf.format = (uint)tex.Format; surf.use = (uint)tex.Use; surf.pitch = tex.Pitch; surf.data = tex.Data; surf.numMips = tex.MipCount; surf.mipOffset = tex.MipOffsets; surf.mipData = tex.MipData; surf.tileMode = (uint)tex.TileMode; surf.swizzle = tex.Swizzle; //Determine tex2 botw files to get mip maps string Tex1 = GetFilePath(); if (Tex1.Contains(".Tex1")) { string Tex2 = Tex1.Replace(".Tex1", ".Tex2"); Console.WriteLine(Tex2); if (System.IO.File.Exists(Tex2)) { ResFile resFile2 = new ResFile(new System.IO.MemoryStream( EveryFileExplorer.YAZ0.Decompress(Tex2))); if (resFile2.Textures.ContainsKey(tex.Name)) { surf.mipData = resFile2.Textures[tex.Name].MipData; surf.mipOffset = resFile2.Textures[tex.Name].MipOffsets; } } } if (surf.mipData == null) { surf.numMips = 1; } List <byte[]> mips = GTX.Decode(surf); Surfaces.Add(new Surface() { mipmaps = mips }); RenderableTex.LoadOpenGLTexture(this); }
private void ReadGx2(FileReader reader) { reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian; header = new GTXHeader(); header.Read(reader); Console.WriteLine("header size " + header.HeaderSize); uint surfBlockType; uint dataBlockType; uint mipBlockType; if (header.MajorVersion == 6) { surfBlockType = 0x0A; dataBlockType = 0x0B; mipBlockType = 0x0C; } else if (header.MajorVersion == 7) { surfBlockType = 0x0B; dataBlockType = 0x0C; mipBlockType = 0x0D; } else { throw new Exception($"Unsupported GTX version {header.MajorVersion}"); } if (header.GpuVersion != 2) { throw new Exception($"Unsupported GPU version {header.GpuVersion}"); } reader.Position = header.HeaderSize; bool blockB = false; bool blockC = false; uint ImageInfo = 0; uint images = 0; while (reader.Position < reader.BaseStream.Length) { GTXDataBlock block = new GTXDataBlock(); block.Read(reader); blocks.Add(block); //Here we use "if" instead of "case" statements as types vary between versions if ((uint)block.BlockType == surfBlockType) { ImageInfo += 1; blockB = true; var surface = new SurfaceInfoParse(); surface.Read(new FileReader(block.data)); if (surface.numMips > 14) { throw new Exception($"Invalid number of mip maps {surface.numMips}!"); } TextureData textureData = new TextureData(); textureData.surface = surface; textureData.Text = "Texture" + ImageInfo; Nodes.Add(textureData); textures.Add(textureData); } else if ((uint)block.BlockType == dataBlockType) { images += 1; blockC = true; data.Add(block.data); } else if ((uint)block.BlockType == mipBlockType) { mipMaps.Add(block.data); } } if (textures.Count != data.Count) { throw new Exception($"Bad size! {textures.Count} {data.Count}"); } int curTex = 0; int curMip = 0; foreach (var node in Nodes) { TextureData tex = (TextureData)node; tex.surface.data = data[curTex]; tex.surface.bpp = GTX.surfaceGetBitsPerPixel(tex.surface.format) >> 3; Console.WriteLine(); if (tex.surface.numMips > 1) { tex.surface.mipData = mipMaps[curMip++]; } else { tex.surface.mipData = new byte[0]; } Console.WriteLine(""); Console.WriteLine("// ----- GX2Surface Info ----- "); Console.WriteLine(" dim = " + tex.surface.dim); Console.WriteLine(" width = " + tex.surface.width); Console.WriteLine(" height = " + tex.surface.height); Console.WriteLine(" depth = " + tex.surface.depth); Console.WriteLine(" numMips = " + tex.surface.numMips); Console.WriteLine(" format = " + tex.surface.format); Console.WriteLine(" aa = " + tex.surface.aa); Console.WriteLine(" use = " + tex.surface.use); Console.WriteLine(" imageSize = " + tex.surface.imageSize); Console.WriteLine(" mipSize = " + tex.surface.mipSize); Console.WriteLine(" tileMode = " + tex.surface.tileMode); Console.WriteLine(" swizzle = " + tex.surface.swizzle); Console.WriteLine(" alignment = " + tex.surface.alignment); Console.WriteLine(" pitch = " + tex.surface.pitch); Console.WriteLine(" bits per pixel = " + (tex.surface.bpp << 3)); Console.WriteLine(" bytes per pixel = " + tex.surface.bpp); Console.WriteLine(" data size = " + tex.surface.data.Length); Console.WriteLine(" mip size = " + tex.surface.mipData.Length); Console.WriteLine(" realSize = " + tex.surface.imageSize); List <byte[]> mips = GTX.Decode(tex.surface); tex.Surfaces.Add(new STGenericTexture.Surface() { mipmaps = mips }); tex.RenderableTex.LoadOpenGLTexture(tex); curTex++; } }
private void ReadGx2(FileReader reader) { reader.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian; header = new GTXHeader(); header.Read(reader); Console.WriteLine("header size " + header.HeaderSize); uint surfBlockType; uint dataBlockType; uint mipBlockType; if (header.MajorVersion == 6) { surfBlockType = 0x0A; dataBlockType = 0x0B; mipBlockType = 0x0C; } else if (header.MajorVersion == 7) { surfBlockType = 0x0B; dataBlockType = 0x0C; mipBlockType = 0x0D; } else { throw new Exception($"Unsupported GTX version {header.MajorVersion}"); } if (header.GpuVersion != 2) { throw new Exception($"Unsupported GPU version {header.GpuVersion}"); } reader.Position = header.HeaderSize; bool blockB = false; bool blockC = false; uint ImageInfo = 0; uint images = 0; while (reader.Position < reader.BaseStream.Length) { GTXDataBlock block = new GTXDataBlock(); block.Read(reader); blocks.Add(block); //Here we use "if" instead of "case" statements as types vary between versions if ((uint)block.BlockType == surfBlockType) { ImageInfo += 1; blockB = true; var surface = new SurfaceInfoParse(); surface.Read(new FileReader(block.data)); if (surface.numMips > 14) { throw new Exception($"Invalid number of mip maps {surface.numMips}!"); } TextureData textureData = new TextureData(); textureData.surface = surface; textureData.MipCount = surface.numMips; textureData.ArrayCount = surface.numArray; textureData.Text = "Texture" + ImageInfo; Nodes.Add(textureData); textures.Add(textureData); } else if ((uint)block.BlockType == dataBlockType) { images += 1; blockC = true; data.Add(block.data); } else if ((uint)block.BlockType == mipBlockType) { mipMaps.Add(block.data); } } if (textures.Count != data.Count) { throw new Exception($"Bad size! {textures.Count} {data.Count}"); } int curTex = 0; int curMip = 0; foreach (var node in Nodes) { TextureData tex = (TextureData)node; tex.surface.data = data[curTex]; tex.surface.bpp = GTX.surfaceGetBitsPerPixel(tex.surface.format) >> 3; tex.Format = FTEX.ConvertFromGx2Format((Syroot.NintenTools.Bfres.GX2.GX2SurfaceFormat)tex.surface.format); tex.Width = tex.surface.width; tex.Height = tex.surface.height; if (tex.surface.numMips > 1) { tex.surface.mipData = mipMaps[curMip++]; } else { tex.surface.mipData = new byte[0]; } if (tex.surface.mipData == null) { tex.surface.numMips = 1; } curTex++; } reader.Close(); reader.Dispose(); }