private byte[] DecodeASTC(int blocksize) { var buff = new byte[m_Width * m_Height * 4]; if (!TextureDecoder.DecodeASTC(image_data, m_Width, m_Height, blocksize, blocksize, buff)) { return(null); } return(buff); }
private byte[] DecodePVRTC(bool is2bpp) { var buff = new byte[m_Width * m_Height * 4]; if (!TextureDecoder.DecodePVRTC(image_data, m_Width, m_Height, buff, is2bpp)) { return(null); } return(buff); }
private byte[] DecodeBC4() { var buff = new byte[m_Width * m_Height * 4]; if (!TextureDecoder.DecodeBC4(image_data, m_Width, m_Height, buff)) { return(null); } return(buff); }
protected override void Initialize() { RegisterImportHandler <Texture>(filePath => { var texture = TextureEncoder.Encode(filePath); { texture.Id = Data.Textures.Max(x => x.Id) + 1; texture.Name = Path.GetFileNameWithoutExtension(filePath); } Data.Textures.Add(texture); }); RegisterImportHandler <Bitmap>(filePath => { var texture = TextureEncoder.Encode(filePath); { texture.Id = Data.Textures.Max(x => x.Id) + 1; texture.Name = Path.GetFileNameWithoutExtension(filePath); } Data.Textures.Add(texture); }); RegisterExportHandler <TextureSet>(filePath => Data.Save(filePath)); RegisterReplaceHandler <TextureSet>(BinaryFile.Load <TextureSet>); RegisterCustomHandler("Export All", () => { using (var folderBrowseDialog = new VistaFolderBrowserDialog()) { folderBrowseDialog.Description = "Select a folder to save textures to."; folderBrowseDialog.UseDescriptionForTitle = true; if (folderBrowseDialog.ShowDialog() != DialogResult.OK) { return; } foreach (var texture in Data.Textures) { if (!TextureFormatUtilities.IsCompressed(texture.Format) || texture.IsYCbCr) { TextureDecoder.DecodeToPNG(texture, Path.Combine(folderBrowseDialog.SelectedPath, $"{texture.Name}.png")); } else { TextureDecoder.DecodeToDDS(texture, Path.Combine(folderBrowseDialog.SelectedPath, $"{texture.Name}.dds")); } } } }, Keys.Control | Keys.Shift | Keys.E); base.Initialize(); }
public static Bitmap Crop(Sprite sprite, SpriteSet parentSet) { var bitmap = TextureDecoder.Decode( parentSet.TextureSet.Textures[sprite.TextureIndex]); bitmap.RotateFlip(RotateFlipType.Rotate180FlipX); var croppedBitmap = bitmap.Clone( new RectangleF(sprite.X, sprite.Y, sprite.Width, sprite.Height), bitmap.PixelFormat); bitmap.Dispose(); return(croppedBitmap); }
public static void ExportToFile(ObjectSet objectSet, string outputFilePath) { AssimpSceneHelper.Export(ExportToAiScene(objectSet), outputFilePath, Ai.PostProcessSteps.FlipUVs); if (objectSet.TextureSet == null) { return; } string outputDirectoryPath = Path.GetDirectoryName(outputFilePath); foreach (var texture in objectSet.TextureSet.Textures) { string extension = TextureFormatUtilities.IsBlockCompressed(texture.Format) && !texture.IsYCbCr ? ".dds" : ".png"; TextureDecoder.DecodeToFile(texture, Path.Combine(outputDirectoryPath, texture.Name + extension)); } }
private async void button1_Click(object sender, EventArgs e) { var selectedItem = await OpenItemSelector(); if (selectedItem == null) { blockIDbutton.Text = "any"; return; } var sourceImage = TextureDecoder.ConvertRTPACKFile(gamepath + @"\game\" + selectedItem.Texture); blockIDbutton.Image = sourceImage.Clone( new Rectangle(selectedItem.TextureRealX * 32, selectedItem.TextureRealY * 32, 32, 32), sourceImage.PixelFormat); settings.User.BlockID = (short)selectedItem.ItemID; blockIDbutton.Text = ""; }
private bool UnpackCrunch(byte[] image_data, out byte[] result) { if (version[0] > 2017 || (version[0] == 2017 && version[1] >= 3) || //2017.3 and up m_TextureFormat == TextureFormat.ETC_RGB4Crunched || m_TextureFormat == TextureFormat.ETC2_RGBA8Crunched) { result = TextureDecoder.UnpackUnityCrunch(image_data); } else { result = TextureDecoder.UnpackCrunch(image_data); } if (result != null) { return(true); } return(false); }
protected override void InitializeCore() { RegisterExportHandler <Bitmap>((path) => TextureDecoder.Decode(Data).Save(path)); RegisterExportHandler <Texture>((path) => TextureDecoder.DecodeToDDS(Data, path)); RegisterReplaceHandler <Bitmap>((path) => { using (var bitmap = new Bitmap(path)) { if (Data.IsYCbCr) { var format = DDSCodec.HasTransparency(bitmap) ? TextureFormat.RGBA : TextureFormat.RGB; return(TextureEncoder.Encode(bitmap, format, false)); } return(TextureEncoder.Encode(bitmap, Data.Format, Data.MipMapCount != 0)); } }); RegisterReplaceHandler <Texture>(TextureEncoder.Encode); }
private bool UnpackCrunch() { byte[] result; if (version[0] > 2017 || (version[0] == 2017 && version[1] >= 3) //2017.3 and up || m_TextureFormat == TextureFormat.ETC_RGB4Crunched || m_TextureFormat == TextureFormat.ETC2_RGBA8Crunched) { result = TextureDecoder.UnpackUnityCrunch(image_data); } else { result = TextureDecoder.UnpackCrunch(image_data); } if (result != null) { image_data = result; image_data_size = result.Length; return true; } return false; }
public static Dictionary <Sprite, Bitmap> Crop(SpriteSet spriteSet) { var bitmaps = new List <Bitmap>(spriteSet.TextureSet.Textures.Count); foreach (var texture in spriteSet.TextureSet.Textures) { var bitmap = TextureDecoder.Decode(texture); bitmap.RotateFlip(RotateFlipType.Rotate180FlipX); bitmaps.Add(bitmap); } var sprites = new Dictionary <Sprite, Bitmap>(spriteSet.Sprites.Count); foreach (var sprite in spriteSet.Sprites) { var sourceBitmap = bitmaps[sprite.TextureIndex]; var bitmap = sourceBitmap.Clone(sprite.GetSourceRectangle(), sourceBitmap.PixelFormat); sprites.Add(sprite, bitmap); } return(sprites); }
private static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine(Resources.HelpText); Console.ReadLine(); return; } string sourceFileName = null; string destinationFileName = null; foreach (string arg in args) { if (sourceFileName == null) { sourceFileName = arg; } else if (destinationFileName == null) { destinationFileName = arg; } } if (destinationFileName == null) { destinationFileName = sourceFileName; } if (File.GetAttributes(sourceFileName).HasFlag(FileAttributes.Directory)) { destinationFileName = Path.ChangeExtension(destinationFileName, "bin"); var textureSet = new TextureSet(); var textures = new SortedList <int, Texture>(); foreach (string textureFileName in Directory.EnumerateFiles(sourceFileName)) { if (textureFileName.EndsWith(".dds", StringComparison.OrdinalIgnoreCase) || textureFileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) { string cleanFileName = Path.GetFileNameWithoutExtension(textureFileName); if (int.TryParse(cleanFileName, out int index)) { Texture texture; if (textureFileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) { var bitmap = new Bitmap(textureFileName); var format = TextureFormat.RGB; if (DDSCodec.HasTransparency(bitmap)) { format = TextureFormat.RGBA; } texture = TextureEncoder.Encode(new Bitmap(textureFileName), format, false); } else { texture = TextureEncoder.Encode(textureFileName); } textures.Add(index, texture); } else { Console.WriteLine( "WARNING: Skipped '{0}' because it didn't match the expected name format", Path.GetFileName(textureFileName)); } } } textureSet.Textures.Capacity = textures.Count; foreach (var texture in textures.Values) { textureSet.Textures.Add(texture); } textureSet.Save(destinationFileName); } else if (sourceFileName.EndsWith(".bin", StringComparison.OrdinalIgnoreCase) || sourceFileName.EndsWith(".txd", StringComparison.OrdinalIgnoreCase)) { destinationFileName = Path.ChangeExtension(destinationFileName, null); var textureSet = BinaryFile.Load <TextureSet>(sourceFileName); Directory.CreateDirectory(destinationFileName); for (int i = 0; i < textureSet.Textures.Count; i++) { var texture = textureSet.Textures[i]; string name = string.IsNullOrEmpty(texture.Name) ? $"{i}" : texture.Name; if (TextureFormatUtilities.IsCompressed(texture.Format)) { TextureDecoder.DecodeToDDS(texture, Path.Combine(destinationFileName, $"{name}.dds")); } else { TextureDecoder.DecodeToPNG(texture, Path.Combine(destinationFileName, $"{name}.png")); } } } }
public override void Export(Texture model, string filePath) { TextureDecoder.DecodeToFile(model, filePath); }
public Image Decode(int level) { return(TextureDecoder.Decode(this, level)); }
protected override void InitializeCore() { RegisterReplaceHandler <TextureSet>(BinaryFile.Load <TextureSet>); RegisterExportHandler <TextureSet>((path) => { // Assume it's being exported for F2nd PS3 if (BinaryFormatUtilities.IsClassic(Data.Format) && path.EndsWith(".txd", StringComparison.OrdinalIgnoreCase)) { Data.Format = BinaryFormat.F2nd; Data.Endianness = Endianness.BigEndian; } // Or reverse else if (BinaryFormatUtilities.IsModern(Data.Format) && path.EndsWith(".bin", StringComparison.OrdinalIgnoreCase)) { Data.Format = BinaryFormat.DT; Data.Endianness = Endianness.LittleEndian; } Data.Save(path); }); RegisterImportHandler <Texture>((path) => { var texture = TextureEncoder.Encode(path); var node = DataNodeFactory.Create <Texture>(Path.GetFileNameWithoutExtension(path), texture); Textures.Add(node); }); RegisterImportHandler <Bitmap>((path) => { var texture = TextureEncoder.Encode(path); var node = DataNodeFactory.Create <Texture>(Path.GetFileNameWithoutExtension(path), texture); Textures.Add(node); }); RegisterCustomHandler("Export All", () => { using (var saveFileDialog = new SaveFileDialog()) { saveFileDialog.AutoUpgradeEnabled = true; saveFileDialog.CheckPathExists = true; saveFileDialog.Title = "Select a folder to export textures to."; saveFileDialog.FileName = "Enter into a directory and press Save"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { var outputDirectory = Path.GetDirectoryName(saveFileDialog.FileName); foreach (var texture in Data.Textures) { if (!TextureFormatUtilities.IsCompressed(texture.Format) || texture.IsYCbCr) { TextureDecoder.DecodeToPNG(texture, Path.Combine(outputDirectory, texture.Name + ".png")); } else { TextureDecoder.DecodeToDDS(texture, Path.Combine(outputDirectory, texture.Name + ".dds")); } } } } }, Keys.Control | Keys.Shift | Keys.E); RegisterDataUpdateHandler(() => { var data = new TextureSet(); data.Format = Format; data.Endianness = Endianness; data.Textures.AddRange(Textures.Data); return(data); }); }
private bool DecodeASTC(byte[] image_data, byte[] buff, int blocksize) { return(TextureDecoder.DecodeASTC(image_data, m_Width, m_Height, blocksize, blocksize, buff)); }
private bool DecodeETC2A8(byte[] image_data, byte[] buff) { return(TextureDecoder.DecodeETC2A8(image_data, m_Width, m_Height, buff)); }
private bool DecodeEACRGSigned(byte[] image_data, byte[] buff) { return(TextureDecoder.DecodeEACRGSigned(image_data, m_Width, m_Height, buff)); }
protected override Bitmap GetBitmapCore(GNFTexture obj) { return(TextureDecoder.Decode(obj)); }
private static byte[] TextureConverter(Texture2D tex, out int width, out int height) { bool is_dxt_crunch = tex.m_TextureFormat == TextureFormat.DXT1Crunched || tex.m_TextureFormat == TextureFormat.DXT5Crunched; bool is_32 = tex.m_TextureFormat == TextureFormat.RGBA32; int mip_count = tex.m_MipCount, w = tex.m_Width, h = tex.m_Height, ofs = 0, level = 0; int mip_size = tex.m_TextureFormat == TextureFormat.DXT1 || tex.m_TextureFormat == TextureFormat.DXT1Crunched ? w * h / 2 : is_32 ? w * h * 4 : w * h; while (mip_count > 0 && w > 256) { mip_count--; ofs += mip_size; w >>= 1; h >>= 1; mip_size >>= 2; level++; } width = w; height = h; byte[] data; if (is_dxt_crunch) { byte[] hdr = GetDataOfs(tex.m_StreamData, tex.assetsFile, 0, 65536); // start with max 64K header uint dataOfs, dataSize, resultSize; IntPtr context = TextureDecoder.UnpackUnityCrunchInit(hdr, level, out dataOfs, out dataSize, out resultSize); if (context.Equals(IntPtr.Zero)) { if (resultSize == 0) // invalid header { return(null); } // retry with larger header hdr = GetDataOfs(tex.m_StreamData, tex.assetsFile, 0, (int)resultSize); context = TextureDecoder.UnpackUnityCrunchInit(hdr, level, out dataOfs, out dataSize, out resultSize); if (context.Equals(IntPtr.Zero)) { return(null); } } byte[] level_data = GetDataOfs(tex.m_StreamData, tex.assetsFile, (int)dataOfs, (int)dataSize); data = TextureDecoder.UnpackUnityCrunchLevelData(context, level, level_data, resultSize); TextureDecoder.UnpackUnityCrunchDone(context); } else { int len = mip_size; data = GetDataOfs(tex.m_StreamData, tex.assetsFile, ofs, len); } var imageBuff = new byte[w * h * 4]; if (tex.m_TextureFormat == TextureFormat.DXT5 || tex.m_TextureFormat == TextureFormat.DXT5Crunched) { TextureDecoder.DecodeDXT5(data, w, h, imageBuff); } else if (tex.m_TextureFormat == TextureFormat.BC7) { TextureDecoder.DecodeBC7(data, w, h, imageBuff); } else if (tex.m_TextureFormat == TextureFormat.DXT1 || tex.m_TextureFormat == TextureFormat.DXT1Crunched) { TextureDecoder.DecodeDXT1(data, w, h, imageBuff); } else if (tex.m_TextureFormat == TextureFormat.RGBA32) { DecodeRGBA32(data, w, h, imageBuff); } else { System.Diagnostics.Debug.WriteLine(tex.m_Name + ": unknown texture format " + tex.m_TextureFormat); return(null); } return(imageBuff); }
protected override Bitmap GetBitmapCore(FieldTexturePS3 obj) { return(TextureDecoder.Decode(obj)); }
public unsafe GLTexture(Texture texture) { Id = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, Id); switch (texture.Format) { case TextureFormat.DDS: { var ddsHeader = new DDSHeader(new MemoryStream(texture.Data)); // todo: identify and retrieve values from texture // todo: disable mipmaps for now, they often break and show up as black ( eg after replacing a texture ) int mipMapCount = ddsHeader.MipMapCount; if (mipMapCount > 0) { --mipMapCount; } else { mipMapCount = 1; } SetTextureParameters(TextureWrapMode.Repeat, TextureWrapMode.Repeat, TextureMagFilter.Linear, TextureMinFilter.Linear, mipMapCount); var format = (PixelInternalFormat)0; if (ddsHeader.PixelFormat.FourCC != 0 || !ddsHeader.PixelFormat.Flags.HasFlag(DDSPixelFormatFlags.RGB) || ddsHeader.PixelFormat.RGBBitCount != 32) { format = GetPixelInternalFormat(ddsHeader.PixelFormat.FourCC); } else if (ddsHeader.PixelFormat.Flags.HasFlag(DDSPixelFormatFlags.AlphaPixels)) { format = PixelInternalFormat.Rgba; } else { format = PixelInternalFormat.Rgb; } UploadDDSTextureData(ddsHeader.Width, ddsHeader.Height, format, 1, texture.Data, 0x80); } break; default: { // rip hardware acceleration var bitmap = TextureDecoder.Decode(texture); SetTextureParameters(TextureWrapMode.Repeat, TextureWrapMode.Repeat, TextureMagFilter.Linear, TextureMinFilter.Linear, 1); var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var pixels = new byte[bitmap.Width * bitmap.Height * 4]; fixed(byte *pPixels = &pixels[0]) { var pSrc = ( byte * )bitmapData.Scan0; var pDest = pPixels; for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { *pDest++ = pSrc[2]; *pDest++ = pSrc[1]; *pDest++ = pSrc[0]; *pDest++ = pSrc[3]; pSrc += 4; } } GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bitmap.Width, bitmap.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, ( IntPtr )pPixels); } bitmap.UnlockBits(bitmapData); } break; } }
protected override void Initialize() { AddImportHandler <Texture>(filePath => { var texture = TextureEncoder.EncodeFromFile(filePath, TextureFormat.Unknown, Parent.FindParent <SpriteSetNode>() == null); { texture.Name = Path.GetFileNameWithoutExtension(filePath); texture.Id = MurmurHash.Calculate(texture.Name); } Data.Textures.Add(texture); }); AddExportHandler <TextureSet>(filePath => Data.Save(filePath)); AddReplaceHandler <TextureSet>(BinaryFile.Load <TextureSet>); AddCustomHandler("Export All", () => { string filePath = ModuleExportUtilities.SelectModuleExport <Texture>( "Select a folder to export textures to.", "Enter into a directory and press Save"); if (string.IsNullOrEmpty(filePath)) { return; } string directoryPath = Path.GetDirectoryName(filePath); string extension = Path.GetExtension(filePath).Trim('.'); foreach (var texture in Data.Textures) { TextureDecoder.DecodeToFile(texture, Path.Combine(directoryPath, $"{texture.Name}.{extension}")); } }, Keys.Control | Keys.Shift | Keys.E); AddCustomHandler("Export All (Flipped)", () => { string filePath = ModuleExportUtilities.SelectModuleExport <Bitmap>( "Select a folder to export textures to.", "Enter into a directory and press Save"); if (string.IsNullOrEmpty(filePath)) { return; } string directoryPath = Path.GetDirectoryName(filePath); string extension = Path.GetExtension(filePath).Trim('.'); foreach (var texture in Data.Textures) { using (var bitmap = TextureDecoder.DecodeToBitmap(texture)) { bitmap.RotateFlip(RotateFlipType.Rotate180FlipX); bitmap.Save(Path.Combine(directoryPath, $"{texture.Name}.{extension}")); } } }); AddCustomHandlerSeparator(); AddDirtyCustomHandler("Replace All", () => { var fileNames = ModuleImportUtilities.SelectModuleImportMultiselect <Texture>(); if (fileNames == null) { return(false); } bool any = false; foreach (string fileName in fileNames) { string textureName = Path.GetFileNameWithoutExtension(fileName); int textureIndex = Data.Textures.FindIndex(x => x.Name.Equals(textureName, StringComparison.OrdinalIgnoreCase)); if (textureIndex == -1) { continue; } any = true; var texture = Data.Textures[textureIndex]; var newTexture = TextureEncoder.EncodeFromFile(fileName, texture.IsYCbCr ? TextureFormat.RGBA8 : texture.Format, texture.MipMapCount != 1); newTexture.Name = texture.Name; newTexture.Id = texture.Id; Data.Textures[textureIndex] = newTexture; } return(any); }, Keys.Control | Keys.Shift | Keys.R, CustomHandlerFlags.Repopulate | CustomHandlerFlags.ClearMementos); AddDirtyCustomHandler("Replace All (Flipped)", () => { var fileNames = ModuleImportUtilities.SelectModuleImportMultiselect <Bitmap>(); if (fileNames == null) { return(false); } bool any = false; foreach (string fileName in fileNames) { // Boy do I love duplicate code C: string textureName = Path.GetFileNameWithoutExtension(fileName); int textureIndex = Data.Textures.FindIndex(x => x.Name.Equals(textureName, StringComparison.OrdinalIgnoreCase)); if (textureIndex == -1) { continue; } any = true; var texture = Data.Textures[textureIndex]; Texture newTexture; using (var bitmap = new Bitmap(fileName)) { bitmap.RotateFlip(RotateFlipType.Rotate180FlipX); newTexture = TextureEncoder.EncodeFromBitmap(bitmap, texture.IsYCbCr ? TextureFormat.RGBA8 : texture.Format, texture.MipMapCount != 1); } newTexture.Name = texture.Name; newTexture.Id = texture.Id; Data.Textures[textureIndex] = newTexture; } return(any); }, Keys.None, CustomHandlerFlags.Repopulate | CustomHandlerFlags.ClearMementos); base.Initialize(); }
protected override void ExportCore(Texture model, Stream destination, string fileName) { TextureDecoder.DecodeToDDS(model, destination); }
private bool DecodePVRTC(byte[] image_data, byte[] buff, bool is2bpp) { return(TextureDecoder.DecodePVRTC(image_data, m_Width, m_Height, buff, is2bpp)); }