private void FindImages(List <BaseNode> nodes) { foreach (var node in nodes) { if (node is ImageListNode) { currentImageList.Push(node.Name); ImageLists.Add(node.Name, new List <string>()); } else if (node is ImageNode) { var imageNode = (node as ImageNode); string texturePath = Path.Combine(Program.DataPath, imageNode.Texture.Replace('/', '\\')); if (File.Exists(texturePath)) { List <Bitmap> bitmaps; if (!sourceBitmapCache.ContainsKey(texturePath)) { var texture = FileFormat.FromFile <ImageFormat>(texturePath); bitmaps = texture.GetBitmaps(0).ToList(); sourceBitmapCache.Add(texturePath, bitmaps); } else { bitmaps = sourceBitmapCache[texturePath]; } Bitmap image = new Bitmap(imageNode.UVWH.Width, imageNode.UVWH.Height); using (Graphics g = Graphics.FromImage(image)) { g.DrawImage(bitmaps[imageNode.TextureIndex], 0, 0, imageNode.UVWH, GraphicsUnit.Pixel); } Images.Add(imageNode.Name, image); if (currentImageList.Count > 0) { ImageLists[currentImageList.Peek()].Add(imageNode.Name); } } } FindImages(node.Children); if (node is ImageListNode) { currentImageList.Pop(); } } }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (ofdOpenFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) { currentFile = FileFormat.FromFile <FileFormat>(currentFilename = ofdOpenFile.FileName); if (currentFile != null && currentFile is ImageFormat) { pbImage.Image = (currentFile as ImageFormat).GetBitmap(); tsslStatus.Text = string.Format("Loaded file '{0}'", currentFilename); } SetFormTitle(); } }
private static void ProcessInputFile(FileInfo inputFile, DirectoryInfo inputDir, DirectoryInfo outputDir) { try { string displayPath = inputFile.FullName.Replace(inputDir.FullName, string.Empty).TrimStart(directorySeparators); IndentWrite("File '{0}'... ", displayPath); indent++; string relativeDirectory = inputFile.DirectoryName.TrimEnd(directorySeparators).Replace(inputDir.FullName.TrimEnd(directorySeparators), string.Empty).TrimStart(directorySeparators); if (keepFiles) { string existenceCheckPath = Path.Combine(outputDir.FullName, relativeDirectory); string existenceCheckPattern = Path.GetFileNameWithoutExtension(inputFile.Name) + "*"; if (Directory.Exists(existenceCheckPath) && Directory.EnumerateFiles(existenceCheckPath, existenceCheckPattern).Any()) { Console.WriteLine("already exists."); return; } } using (FileStream inputStream = new FileStream(inputFile.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var instance = FileFormat.FromFile <FileFormat>(inputStream); if (instance != null) { if (instance is ImageFormat) { var imageInstance = (instance as ImageFormat); int imageCount = imageInstance.GetImageCount(); int paletteCount = imageInstance.GetPaletteCount(); int blockCount = ((imageInstance is GXT && (imageInstance as GXT).BUVChunk != null) ? (imageInstance as GXT).BUVChunk.Entries.Length : -1); List <string> contentStrings = new List <string>(); contentStrings.Add(string.Format("{0} image{1}", imageCount, (imageCount != 1 ? "s" : string.Empty))); if (paletteCount > 0) { contentStrings.Add(string.Format("{0} palette{1}", paletteCount, (paletteCount != 1 ? "s" : string.Empty))); } if (blockCount > 0) { contentStrings.Add(string.Format("{0} block{1}", blockCount, (blockCount != 1 ? "s" : string.Empty))); } Console.WriteLine(string.Format("{0} found.", string.Join(", ", contentStrings))); for (int i = 0; i < imageCount; i++) { string imageName = imageInstance.GetImageName(i); string outputFilename; FileInfo outputFile; if (paletteCount < 2) { Bitmap image = imageInstance.GetBitmap(i, 0); if (imageName == null) { outputFilename = string.Format("{0} (Image {1}).png", Path.GetFileNameWithoutExtension(inputFile.Name), i); outputFile = new FileInfo(Path.Combine(outputDir.FullName, relativeDirectory, outputFilename)); } else { outputFilename = string.Format("{0}.png", Path.GetFileNameWithoutExtension(imageName)); outputFile = new FileInfo(Path.Combine(outputDir.FullName, relativeDirectory, Path.GetFileNameWithoutExtension(inputFile.Name), outputFilename)); } Directory.CreateDirectory(outputFile.Directory.FullName); image.Save(outputFile.FullName, System.Drawing.Imaging.ImageFormat.Png); } else { for (int p = 0; p < paletteCount; p++) { Bitmap image = imageInstance.GetBitmap(i, p); if (imageName == null) { outputFilename = string.Format("{0} (Image {1}, Palette {2}).png", Path.GetFileNameWithoutExtension(inputFile.Name), i, p); outputFile = new FileInfo(Path.Combine(outputDir.FullName, relativeDirectory, outputFilename)); } else { outputFilename = string.Format("{0} (Palette {1}).png", Path.GetFileNameWithoutExtension(imageName), p); outputFile = new FileInfo(Path.Combine(outputDir.FullName, relativeDirectory, Path.GetFileNameWithoutExtension(inputFile.Name), outputFilename)); } Directory.CreateDirectory(outputFile.Directory.FullName); image.Save(outputFile.FullName, System.Drawing.Imaging.ImageFormat.Png); } } } if (imageInstance is GXT && (imageInstance as GXT).BUVChunk != null) { var gxtInstance = (imageInstance as GXT); List <Bitmap> buvImages = gxtInstance.GetBUVBitmaps().ToList(); for (int b = 0; b < buvImages.Count; b++) { Bitmap image = buvImages[b]; string outputFilename = string.Format("{0} (Block {1}).png", Path.GetFileNameWithoutExtension(inputFile.Name), b); FileInfo outputFile = new FileInfo(Path.Combine(outputDir.FullName, relativeDirectory, outputFilename)); Directory.CreateDirectory(outputFile.Directory.FullName); image.Save(outputFile.FullName, System.Drawing.Imaging.ImageFormat.Png); } } } else if (instance is ContainerFormat) { var containerInstance = (instance as ContainerFormat); int elementCount = containerInstance.GetElementCount(); Console.WriteLine("{0} element{1} found.", elementCount, (elementCount != 1 ? "s" : string.Empty)); foreach (var element in containerInstance.GetElements(inputStream)) { string outputFilename = element.GetName(); FileInfo outputFile = new FileInfo(Path.Combine(outputDir.FullName, relativeDirectory, Path.GetFileNameWithoutExtension(inputFile.Name), outputFilename)); Directory.CreateDirectory(outputFile.Directory.FullName); using (FileStream outputStream = new FileStream(outputFile.FullName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { using (Stream elementStream = element.GetStream(inputStream)) { elementStream.CopyTo(outputStream); } } ProcessInputFile(outputFile, outputFile.Directory, new DirectoryInfo(outputFile.Directory.FullName + " " + defaultOutputDir)); } } else if (instance is CompressionFormat) { var compressedInstance = (instance as CompressionFormat); Console.WriteLine("decompressed {0}.", compressedInstance.GetType().Name); // TODO: less naive way of determining target filename; see also CompressionFormat class in Scarlet.IO.CompressionFormats string outputFilename; string nameOrExtension = compressedInstance.GetNameOrExtension(); if (nameOrExtension != string.Empty) { bool isFullName = nameOrExtension.Contains('.'); outputFilename = (isFullName ? nameOrExtension : Path.GetFileNameWithoutExtension(inputFile.Name) + "." + nameOrExtension).TrimEnd('.'); } else { outputFilename = Path.GetFileName(inputFile.Name); } FileInfo outputFile = new FileInfo(Path.Combine(outputDir.FullName, relativeDirectory, outputFilename)); Directory.CreateDirectory(outputFile.Directory.FullName); using (FileStream outputStream = new FileStream(outputFile.FullName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { using (Stream decompressedStream = compressedInstance.GetDecompressedStream()) { decompressedStream.CopyTo(outputStream); } } // TODO: make nicer? ProcessInputFile(outputFile, outputFile.Directory, outputFile.Directory); } else { Console.WriteLine("unhandled file."); } } else { Console.WriteLine("unsupported file."); } } } #if !DEBUG catch (Exception ex) { IndentWriteLine("Exception occured: {0}.", ex.Message); } #endif finally { indent--; } }