static string ConvertAig(AlImage img, string path) { foreach (var converter in imageConverters) { if (converter.CanConvert(img.PixelFormat)) { string outPath = Path.ChangeExtension(path, converter.FileExtension); using (FileStream ofs = File.Create(outPath)) { converter.ConvertFromAl(img, ofs); } if (converter.HasAlternativeFile(img)) { outPath = Path.ChangeExtension(path, null); outPath = $"{outPath}_alt{converter.FileExtension}"; using (FileStream ofs = File.Create(outPath)) { converter.ConvertFromAlAlt(img, ofs); } } return(converter.FileExtension); } } throw new Exception($"Cannot find converter for {img.PixelFormat}"); }
public void ConvertFromAl(AlImage image, Stream destStream) { List <byte[]> mips; if (image.PixelFormat == "ETC1") { mips = image.Mipmaps; } else if (image.PixelFormat == "EC1A") { // Give first half of the mips mips = new List <byte[]>(); foreach (var mip in image.Mipmaps) { byte[] newMip = new byte[mip.Length / 2]; Buffer.BlockCopy(mip, 0, newMip, 0, newMip.Length); mips.Add(newMip); } } else { throw new ArgumentException("Pixel format not supported.", nameof(image)); } var ktx = KtxCreator.Create(GlDataType.Compressed, GlPixelFormat.GL_RGB, GlInternalFormat.GL_ETC1_RGB8_OES, image.Width, image.Height, mips, new Dictionary <string, MetadataValue>()); KtxWriter.WriteTo(ktx, destStream); }
static void BulkConvertAig(string inPath, string filter, bool recursive) { foreach (var path in Directory.GetFiles(inPath, filter, recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)) { Console.WriteLine(path); using (var fs = Utils.CheckDecompress(File.OpenRead(path))) { var img = new AlImage(); img.Read(new BinaryReader(fs)); ConvertAig(img, path); } } }
public void ConvertFromAlAlt(AlImage image, Stream destStream) { if (image.PixelFormat != "EC1A") { throw new ArgumentException("Pixel format does not have alternate representation.", nameof(image)); } // Give second half of the mips byte[] newMip = new byte[image.Mipmaps[0].Length / 2]; Buffer.BlockCopy(image.Mipmaps[0], newMip.Length, newMip, 0, newMip.Length); WritePkm(destStream, newMip, image.Width, image.Height); }
public void ConvertFromAl(AlImage image, Stream destStream) { // Only grab the first mip var pixels = image.Mipmaps[0]; using (MemoryStream ms = new MemoryStream(pixels)) { BinaryReader br = new BinaryReader(ms); using (var img = ConvertBgra32(br, (int)image.Width, (int)image.Height)) { img.SaveAsPng(destStream); } } }
static void BulkConvertAtxMulti(string inPath, string filter, bool recursive) { foreach (var path in Directory.GetFiles(inPath, filter, recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)) { using (var fs = Utils.CheckDecompress(File.OpenRead(path))) { var tex = new AlTexture(); tex.Read(new BinaryReader(fs)); if (tex.ChildTextures.Count < 2) { continue; } Console.WriteLine(path); var img = new AlImage(); using (Stream ms = Utils.CheckDecompress(new MemoryStream(tex.ImageData))) { img.Read(new BinaryReader(ms)); if (img.PixelFormat != "BGRA") { Console.WriteLine("Not BGRA, skipping"); continue; } using (MemoryStream ims = new MemoryStream(img.Mipmaps[0])) using (var imgConv = PngConverter.ConvertBgra32(new BinaryReader(ims), (int)img.Width, (int)img.Height)) { foreach (var child in tex.ChildTextures) { // First mip only var bounds = child.Bounds[0]; using (var childImage = new Image <Bgra32>(bounds.W, bounds.H)) { childImage.Mutate(ctx => ctx.DrawImage(imgConv, new Point(-bounds.X, -bounds.Y), 1f)); childImage.SaveAsPng($"{Path.ChangeExtension(path, null)}_{child.Id:x8}.png"); } } } } } } }
public void ConvertFromAlAlt(AlImage image, Stream destStream) { if (image.PixelFormat != "EC1A") { throw new ArgumentException("Pixel format does not have alternate representation.", nameof(image)); } // Give second half of the mips var mips = new List <byte[]>(); foreach (var mip in image.Mipmaps) { byte[] newMip = new byte[mip.Length / 2]; Buffer.BlockCopy(mip, newMip.Length, newMip, 0, newMip.Length); mips.Add(newMip); } var ktx = KtxCreator.Create(GlDataType.Compressed, GlPixelFormat.GL_RGB, GlInternalFormat.GL_ETC1_RGB8_OES, image.Width, image.Height, mips, new Dictionary <string, MetadataValue>()); KtxWriter.WriteTo(ktx, destStream); }
public void ConvertFromAl(AlImage image, Stream destStream) { List <byte[]> mips; if (image.PixelFormat == "ETC1") { mips = image.Mipmaps; } else if (image.PixelFormat == "EC1A") { // Give first half of the mips mips = new List <byte[]>(); byte[] newMip = new byte[image.Mipmaps[0].Length / 2]; Buffer.BlockCopy(image.Mipmaps[0], 0, newMip, 0, newMip.Length); mips.Add(newMip); } else { throw new ArgumentException("Pixel format not supported.", nameof(image)); } WritePkm(destStream, mips[0], image.Width, image.Height); }
static void BulkConvertAtxToPng(string inPath, string filter, bool recursive) { foreach (var path in Directory.GetFiles(inPath, filter, recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)) { Console.WriteLine(path); string fileExtension; // Export the texture (probably KTX) using (var fs = Utils.CheckDecompress(File.OpenRead(path))) { var tex = new AlTexture(); tex.Read(new BinaryReader(fs)); var img = new AlImage(); using (Stream ms = Utils.CheckDecompress(new MemoryStream(tex.ImageData))) { img.Read(new BinaryReader(ms)); fileExtension = ConvertAig(img, path); } } if (fileExtension != ".pkm") { continue; } // Convert main texture to PNG string mainPath = Path.ChangeExtension(path, fileExtension); var startInfo = new ProcessStartInfo(ETC1TOOL_PATH); startInfo.ArgumentList.Add(mainPath); startInfo.ArgumentList.Add("--decode"); startInfo.CreateNoWindow = false; startInfo.UseShellExecute = false; Process.Start(startInfo).WaitForExit(); // Convert alpha texture if present string altPath = $"{Path.ChangeExtension(path, null)}_alt{fileExtension}"; if (File.Exists(altPath)) { startInfo = new ProcessStartInfo(ETC1TOOL_PATH); startInfo.ArgumentList.Add(altPath); startInfo.ArgumentList.Add("--decode"); startInfo.CreateNoWindow = false; startInfo.UseShellExecute = false; Process.Start(startInfo).WaitForExit(); var mainPngPath = Path.ChangeExtension(mainPath, ".png"); var altPngPath = Path.ChangeExtension(altPath, ".png"); using (var mainImg = Image.Load <Rgb24>(mainPngPath)) using (var altImg = Image.Load <Rgb24>(altPngPath)) { using (var mergedImg = MergeAlpha(mainImg, altImg)) using (FileStream newFs = File.Create(mainPngPath)) { mergedImg.SaveAsPng(newFs); } } File.Delete(altPngPath); File.Delete(altPath); } File.Delete(mainPath); } }
public bool HasAlternativeFile(AlImage image) { return(image.PixelFormat == "EC1A"); }
public bool HasAlternativeFile(AlImage image) { return(false); }
public void ConvertFromAlAlt(AlImage image, Stream destStream) { throw new NotSupportedException(); }