public void ImportFolder(string inFolder) { DirectoryInfo dir = new DirectoryInfo(inFolder); FileInfo[] files; files = dir.GetFiles(); foreach (FileInfo info in files) { string ext = Path.GetExtension(info.FullName).ToUpper(); if (ext == ".PNG" || ext == ".TGA" || ext == ".BMP" || ext == ".JPG" || ext == ".JPEG" || ext == ".GIF" || ext == ".TIF" || ext == ".TIFF") { using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = info.FullName; dlg.ShowDialog(null, this); } } else if (ext == ".TEX0") { TEX0Node node = NodeFactory.FromFile(null, info.FullName) as TEX0Node; GetOrCreateFolder <TEX0Node>().AddChild(node); } else if (ext == ".MDL0") { MDL0Node node = NodeFactory.FromFile(null, info.FullName) as MDL0Node; GetOrCreateFolder <MDL0Node>().AddChild(node); } else if (ext == ".CHR0") { CHR0Node node = NodeFactory.FromFile(null, info.FullName) as CHR0Node; GetOrCreateFolder <CHR0Node>().AddChild(node); } else if (ext == ".CLR0") { CLR0Node node = NodeFactory.FromFile(null, info.FullName) as CLR0Node; GetOrCreateFolder <CLR0Node>().AddChild(node); } else if (ext == ".SRT0") { SRT0Node node = NodeFactory.FromFile(null, info.FullName) as SRT0Node; GetOrCreateFolder <SRT0Node>().AddChild(node); } else if (ext == ".SHP0") { SHP0Node node = NodeFactory.FromFile(null, info.FullName) as SHP0Node; GetOrCreateFolder <SHP0Node>().AddChild(node); } else if (ext == ".PAT0") { PAT0Node node = NodeFactory.FromFile(null, info.FullName) as PAT0Node; GetOrCreateFolder <PAT0Node>().AddChild(node); } else if (ext == ".VIS0") { VIS0Node node = NodeFactory.FromFile(null, info.FullName) as VIS0Node; GetOrCreateFolder <VIS0Node>().AddChild(node); } } }
public void ImportTexture() { string path; int index = Program.OpenFile(ExportFilters.TEX0, out path); if (index == 8) { TEX0Node node = NodeFactory.FromFile(null, path) as TEX0Node; ((BRESNode)_resource).GetOrCreateFolder <TEX0Node>().AddChild(node); BaseWrapper w = this.FindResource(node, true); w.EnsureVisible(); w.TreeView.SelectedNode = w; } else if (index > 0) { using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = path; if (dlg.ShowDialog(MainForm.Instance, ResourceNode as BRESNode) == DialogResult.OK) { BaseWrapper w = this.FindResource(dlg.TextureNode, true); w.EnsureVisible(); w.TreeView.SelectedNode = w; } } } }
private void AddNewTEX0(object sender, string filename) { BRRESNode md80 = sc_selmap.FindChild("MiscData[80]", false) as BRRESNode; string dir = Path.Combine(Path.GetTempPath(), "BrawlStageManager-newimage-" + Guid.NewGuid()); Directory.CreateDirectory(dir); string temp = Path.Combine(dir, GetTexInfoFor(sender).pat0.Texture + Path.GetExtension(filename)); File.Copy(filename, temp); using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = temp; dlg.InitialFormat = sender == prevbase ? WiiPixelFormat.CMPR : sender == frontstname ? WiiPixelFormat.I4 : sender == icon ? WiiPixelFormat.CI8 : sender == seriesicon ? WiiPixelFormat.I4 : sender == selmap_mark ? WiiPixelFormat.IA4 : (WiiPixelFormat?)null; dlg.InitialSize = sender == prevbase ? prevbaseResizeTo : sender == frontstname ? frontstnameResizeTo : sender == selmap_mark ? selmapMarkResizeTo : null; dlg.FormBorderStyle = FormBorderStyle.FixedSingle; dlg.ShowInTaskbar = true; if (dlg.ShowDialog(null, md80) == DialogResult.OK) { md80.IsDirty = true; // do this to be safe UpdateImage(); } } Directory.Delete(dir, true); }
public void ImportTexture() { string path; int index = Program.OpenFile(FileFilters.Images, out path); if (index == 8) { TPLTextureNode t = new TPLTextureNode() { Name = "Texture" }; _resource.AddChild(t); t.Replace(path); BaseWrapper w = this.FindResource(t, true); w.EnsureVisible(); w.TreeView.SelectedNode = w; } else if (index > 0) { using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = path; if (dlg.ShowDialog(MainForm.Instance, Resource as TPLNode) == DialogResult.OK) { BaseWrapper w = this.FindResource(dlg.TPLTextureNode, true); w.EnsureVisible(); w.TreeView.SelectedNode = w; } } } }
public void ImportTexture() { string path; int index = Program.OpenFile(FileFilters.TEX0, out path); if (index == 8) { TEX0Node node = NodeFactory.FromFile(null, path) as TEX0Node; ((BRRESNode)_resource).GetOrCreateFolder <TEX0Node>().AddChild(node); string palette = Path.ChangeExtension(path, ".plt0"); if (File.Exists(palette) && node.HasPalette) { PLT0Node n = NodeFactory.FromFile(null, palette) as PLT0Node; ((BRRESNode)_resource).GetOrCreateFolder <PLT0Node>().AddChild(n); } BaseWrapper w = this.FindResource(node, true); w.EnsureVisible(); w.TreeView.SelectedNode = w; } else if (index > 0) { using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = path; if (dlg.ShowDialog(MainForm.Instance, ResourceNode as BRRESNode) == DialogResult.OK) { BaseWrapper w = this.FindResource(dlg.TEX0TextureNode, true); w.EnsureVisible(); w.TreeView.SelectedNode = w; } } } }
public void ImportTexture() { if (Program.OpenFile(FileFilters.Images, out string path)) { if (path.EndsWith(".tplt", StringComparison.OrdinalIgnoreCase) || !path.Contains(".")) { TPLTextureNode t = new TPLTextureNode { Name = "Texture" }; _resource.AddChild(t); t.Replace(path); BaseWrapper w = FindResource(t, true); w.EnsureVisible(); w.TreeView.SelectedNode = w; } else { using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = path; if (dlg.ShowDialog(MainForm.Instance, Resource as TPLNode) == DialogResult.OK) { BaseWrapper w = FindResource(dlg.TPLTextureNode, true); w.EnsureVisible(); w.TreeView.SelectedNode = w; } } } } }
public void ReEncode() { using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.LoadImages((ResourceNode as REFTEntryNode).GetImage(0)); dlg.ShowDialog(MainForm.Instance, ResourceNode as REFTEntryNode); } }
public void ResizeAllPrevbases(Size newSize) { if (sc_selmap == null) { return; } var prevbases = from c in sc_selmap.FindChild("MiscData[80]/Textures(NW4R)", false).Children where c is TEX0Node && c.Name.Contains("MenSelmapPrevbase") select(TEX0Node) c; int i = 0; foreach (TEX0Node node in prevbases) { Bitmap origImage = node.GetImage(0); if (origImage.Width <= newSize.Width && origImage.Height <= newSize.Height) { continue; } string file = TempFiles.Create(".png"); if (useTextureConverter) { origImage.Save(file); TextureConverterDialog d = new TextureConverterDialog(); d.ImageSource = file; d.InitialSize = newSize; if (d.ShowDialog(null, node) == DialogResult.OK) { node.IsDirty = true; Console.WriteLine("Resized " + node); i++; } else if (MessageBox.Show(this, "Stop resizing textures here?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { break; } } else { Bitmap b = BitmapUtilities.Resize(origImage, newSize); b.Save(file); node.Replace(file); Console.WriteLine("Resized " + node); i++; } File.Delete(file); } MessageBox.Show("Resized " + i + " images."); UpdateImage(); }
public override void OnReplace(string inStream) { if (inStream.EndsWith(".tplt", StringComparison.OrdinalIgnoreCase) || !inStream.Contains(".")) { base.OnReplace(inStream); } else { using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = inStream; dlg.ShowDialog(MainForm.Instance, (TPLTextureNode)_resource); } } }
public override void OnReplace(string inStream, int filterIndex) { if (filterIndex == 8) { base.OnReplace(inStream, filterIndex); } else { using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = inStream; dlg.ShowDialog(MainForm.Instance, (REFTEntryNode)_resource); } } }
public void ImportTexture() { if (Program.OpenFile(FileFilters.Images, out string path)) { using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = path; if (dlg.ShowDialog(MainForm.Instance, Resource as REFTNode) == DialogResult.OK) { BaseWrapper w = FindResource(dlg.REFTTextureNode, true); w.EnsureVisible(); w.TreeView.SelectedNode = w; } } } }
public void ReEncode() { PLT0Node plt = null; if (((TEX0Node)_resource).HasPalette) { plt = ((TEX0Node)_resource).GetPaletteNode(); } using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.LoadImages((Resource as TEX0Node).GetImage(0)); dlg.ShowDialog(MainForm.Instance, Resource as TEX0Node); } if (plt != null && !((TEX0Node)_resource).HasPalette) { plt.Dispose(); plt.Remove(); } }
public static BRESNode FromGIF(string file) { string s = Path.GetFileNameWithoutExtension(file); BRESNode b = new BRESNode() { _name = s }; PAT0Node p = new PAT0Node() { _name = s }; p.CreateEntry(); PAT0TextureNode t = p.Children[0].Children[0] as PAT0TextureNode; GifDecoder d = new GifDecoder(); d.Read(file); int f = d.GetFrameCount(); using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.Source = (Bitmap)d.GetFrame(0); if (dlg.ShowDialog(null, b) == DialogResult.OK) { for (int i = 1; i < f; i++) { dlg.Source = (Bitmap)d.GetFrame(i); dlg.EncodeSource(); } } } return(b); }
private void ImportFile(string file, TreeNode t) { ResourceNode node = null; if (t == null) { Program.Open(file); } else { ResourceNode dest = ((BaseWrapper)t).Resource; try { if ((node = NodeFactory.FromFile(null, file)) != null) { bool ok = false; if (ModifierKeys == Keys.Shift || dest.Parent == null) { ok = TryAddChild(node, dest); } else { ok = TryDrop(node, dest); } if (!ok) { node.Dispose(); node = null; } else { BaseWrapper b = FindResource(node); if (b != null) { b.EnsureVisible(); SelectedNode = b; } } } else if (dest is TEX0Node || dest is REFTEntryNode || dest is TPLTextureNode) { if (ModifierKeys == Keys.Control) { using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = file; if (dest is TEX0Node) { dlg.ShowDialog(MainForm.Instance, dest as TEX0Node); } else if (dest is REFTEntryNode) { dlg.ShowDialog(MainForm.Instance, dest as REFTEntryNode); } else { dlg.ShowDialog(MainForm.Instance, dest as TPLTextureNode); } } } else { dest.Replace(file); } } } catch { } } _timer.Enabled = false; _dragNode = null; }
public void Replace(object sender, string filename) { var ig = StringComparison.CurrentCultureIgnoreCase; if (filename.EndsWith(".tex0", ig)) { TEX0Node tex0 = GetTexInfoFor(sender).tex0; if (tex0 == null) { return; } tex0.Replace(filename); tex0.IsDirty = true; UpdateImage(); } else if (filename.EndsWith(".brres", ig)) { using (ResourceNode node = NodeFactory.FromFile(null, filename)) { TEX0Node tex0; if (node is TEX0Node) { tex0 = (TEX0Node)node; } else { tex0 = (TEX0Node)node.FindChild("Textures(NW4R)", false).Children[0]; } string tempFile = TempFiles.Create(".png"); tex0.Export(tempFile); Replace(sender, tempFile); // call self with new file } } else { TEX0Node tex0 = GetTexInfoFor(sender).tex0; if (tex0 == null) { AddNewTEX0(sender, filename); return; } else if (useTextureConverter) { using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = filename; dlg.InitialSize = sender == prevbase ? prevbaseResizeTo : sender == frontstname ? frontstnameResizeTo : sender == selmap_mark ? selmapMarkResizeTo : null; dlg.FormBorderStyle = FormBorderStyle.FixedSingle; dlg.ShowInTaskbar = true; if (dlg.ShowDialog(null, tex0) == DialogResult.OK) { tex0.IsDirty = true; UpdateImage(); } } } else { Bitmap bmp = new Bitmap(filename); if (sender == prevbase && prevbaseResizeTo != null) { bmp = BitmapUtilities.Resize(bmp, prevbaseResizeTo.Value); } else if (sender == frontstname && frontstnameResizeTo != null) { bmp = BitmapUtilities.Resize(bmp, frontstnameResizeTo.Value); } else if (sender == selmap_mark && selmapMarkResizeTo != null) { bmp = BitmapUtilities.Resize(bmp, selmapMarkResizeTo.Value); } if (sender == selmap_mark) { ReplaceSelmapMark(bmp, tex0, false); } else { int colorCount = BitmapUtilities.CountColors(bmp, 256).Align(16); tex0.Replace(bmp, colorCount); } tex0.IsDirty = true; UpdateImage(); } } }
public void ImportGIF(string file) { Action <object, DoWorkEventArgs> work = (object sender, DoWorkEventArgs e) => { GifDecoder decoder = new GifDecoder(); decoder.Read(file, null); e.Result = decoder; }; Action <object, RunWorkerCompletedEventArgs> completed = (object sender, RunWorkerCompletedEventArgs e) => { GifDecoder decoder = e.Result as GifDecoder; string s = Path.GetFileNameWithoutExtension(file); PAT0Node p = CreateResource <PAT0Node>(s); p._loop = true; p.CreateEntry(); PAT0TextureNode textureNode = p.Children[0].Children[0] as PAT0TextureNode; PAT0TextureEntryNode entry = textureNode.Children[0] as PAT0TextureEntryNode; //Get the number of images in the file int frames = decoder.GetFrameCount(); //The framecount will be created by adding up each image delay. float frameCount = 0; bool resized = false; int w = 0, h = 0; Action <int, int> onResized = (newW, newH) => { if (resized != true) { w = newW; h = newH; resized = true; } }; using (TextureConverterDialog dlg = new TextureConverterDialog()) { using (ProgressWindow progress = new ProgressWindow(RootNode._mainForm, "GIF to PAT0 converter", "Converting, please wait...", true)) { Bitmap prev = null; progress.Begin(0, frames, 0); for (int i = 0; i < frames; i++, entry = new PAT0TextureEntryNode()) { if (progress.Cancelled) { break; } string name = s + "." + i; dlg.ImageSource = name + "."; using (Bitmap img = (Bitmap)decoder.GetFrame(i)) { if (i == 0) { dlg.LoadImages(img.Copy()); dlg.Resized += onResized; if (dlg.ShowDialog(null, this) != DialogResult.OK) { return; } textureNode._hasTex = dlg.TextureData != null; textureNode._hasPlt = dlg.PaletteData != null; prev = img.Copy(); } else { //Draw the current image over the previous //This is because some GIFs use pixels of the previous frame //in order to compress the overall image data using (Graphics graphics = Graphics.FromImage(prev)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.CompositingMode = CompositingMode.SourceOver; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.DrawImage(img, 0, 0, prev.Width, prev.Height); } dlg.LoadImages(prev.Copy()); if (resized) { dlg.ResizeImage(w, h); } else { dlg.UpdatePreview(); } dlg.EncodeSource(); textureNode.AddChild(entry); } } entry._frame = (float)Math.Round(frameCount, 2); frameCount += decoder.GetDelay(i) / 1000.0f * 60.0f; if (textureNode._hasTex) { entry.Texture = name; } if (textureNode._hasPlt) { entry.Palette = name; } progress.Update(progress.CurrentValue + 1); } progress.Finish(); if (prev != null) { prev.Dispose(); } } } p._numFrames = (ushort)(frameCount + 0.5f); }; using (BackgroundWorker b = new BackgroundWorker()) { b.DoWork += new DoWorkEventHandler(work); b.RunWorkerCompleted += new RunWorkerCompletedEventHandler(completed); b.RunWorkerAsync(); } }
public static void ColorSmashTex0(object sender, EventArgs e) { // If this was selected via keycode when it's invalid, return without error if (!CanRunColorSmash || MainForm.Instance.resourceTree.SelectedNodes.Count <= 1) { return; } DirectoryInfo inputDir = Directory.CreateDirectory(Program.AppPath + "\\cs\\"); DirectoryInfo outputDir = Directory.CreateDirectory(Program.AppPath + "\\cs\\out\\"); try { // Clear the directories. If they aren't empty, the implementation WILL NOT work foreach (FileInfo f in outputDir.GetFiles()) { try { f.Delete(); } catch { // ignored } } foreach (FileInfo f in inputDir.GetFiles()) { try { f.Delete(); } catch { // ignored } } // Throw error if color smash directory isn't empty if (inputDir.GetFiles().Length > 0 || outputDir.GetFiles().Length > 0) { MessageBox.Show( "One or more files exist in the required Color Smash folder. Please delete these nodes manually and try again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } short paletteCount = 0; List <TEX0Node> textureList = new List <TEX0Node>(); Dictionary <int, string> names = new Dictionary <int, string>(); BRRESNode b = (((TEX0Wrapper)MainForm.Instance.resourceTree.SelectedNodes[0]).Resource as TEX0Node)? .BRESNode; if (b == null) { MessageBox.Show( "The BRRES could not be found. Color Smashing is only supported for TEX0 nodes in BRRES groups", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int index = int.MaxValue; foreach (TreeNode n in MainForm.Instance.resourceTree.SelectedNodes) { // If this was selected via keycode when it's invalid, return without error if (!(n is TEX0Wrapper)) { return; } if (((TEX0Wrapper)n).Resource is TEX0Node t) { if (t.BRESNode != b) { MessageBox.Show("Color Smash is only supported for nodes in the same BRRES", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } textureList.Add(t); int placement = t.Parent.Children.IndexOf(t); names.Add(placement, t.Name); if (placement < index) { index = placement; } t.Export($"{inputDir.FullName}\\{placement:D5}.png"); if (paletteCount < 256) { if (!t.HasPalette || t.GetPaletteNode() == null) { paletteCount = 256; } else if (t.HasPalette && t.GetPaletteNode() != null && t.GetPaletteNode().Palette.Entries.Length > paletteCount) { paletteCount = (short)Math.Min(t.GetPaletteNode().Palette.Entries.Length, 256); } } } } if (index == int.MaxValue) { MessageBox.Show("Could not properly get the index of the images", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (paletteCount == 0) { paletteCount = 256; } foreach (TEX0Node t in textureList) { if (t.HasPalette && t.GetPaletteNode() != null) { t.GetPaletteNode().Remove(); } t.Remove(); } Process cSmash = Process.Start(new ProcessStartInfo { FileName = Program.AppPath + "\\color_smash.exe", WindowStyle = ProcessWindowStyle.Hidden, Arguments = $"-c RGB5A3 -n {paletteCount}" }); cSmash?.WaitForExit(); textureList = new List <TEX0Node>(); int count = 0; BRESGroupNode texGroup = b.GetOrCreateFolder <TEX0Node>(); foreach (FileInfo f in outputDir.GetFiles()) { FileInfo f2 = new FileInfo($"{inputDir.FullName}\\{f.Name}"); int i = int.Parse(f.Name.Substring(0, f.Name.IndexOf(".", StringComparison.Ordinal))); using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = f.FullName; dlg.ChkImportPalette.Checked = true; dlg.Automatic = true; dlg.StartingFormat = WiiPixelFormat.CI8; if (dlg.ShowDialog(MainForm.Instance, b) != DialogResult.OK) { continue; } TEX0Node t = dlg.TEX0TextureNode; t.Name = names[i]; textureList.Add(t); dlg.Dispose(); t.OriginalPath = ""; if (texGroup.Children.Count > count + 1) { texGroup.RemoveChild(t); texGroup.InsertChild(t, false, index + count); } count++; } f2.Delete(); } if (textureList.Count > 0) { textureList.Remove(textureList.Last()); foreach (TEX0Node t in textureList) { t.SharesData = true; } } if (inputDir.GetFiles().Length > 0) { MessageBox.Show( "One or more files threw an error. Please ensure all relevant textures can be Color Smashed together.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); foreach (FileInfo f in inputDir.GetFiles()) { int i = int.Parse(f.Name.Substring(0, f.Name.IndexOf(".", StringComparison.Ordinal))); using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = f.FullName; dlg.Automatic = true; if (dlg.ShowDialog(MainForm.Instance, b) != DialogResult.OK) { continue; } TEX0Node t = dlg.TEX0TextureNode; t.Name = names[i]; dlg.Dispose(); t.OriginalPath = ""; if (texGroup.Children.Count > count + 1) { texGroup.RemoveChild(t); texGroup.InsertChild(t, false, index + count); } count++; } } } } finally { foreach (FileInfo f in outputDir.GetFiles()) { try { f.Delete(); } catch { // ignored } } try { outputDir.Delete(); } catch { // ignored } foreach (FileInfo f in inputDir.GetFiles()) { try { f.Delete(); } catch { // ignored } } try { inputDir.Delete(); } catch { // ignored } } }
public void Replace(string filename, bool useTextureConverter) { if (Texture == null) { return; } var ig = StringComparison.CurrentCultureIgnoreCase; if (filename.EndsWith(".tex0", ig) || filename.EndsWith(".brres", ig)) { using (ResourceNode node = NodeFactory.FromFile(null, filename)) { TEX0Node tex0; if (node is TEX0Node) { tex0 = (TEX0Node)node; } else { tex0 = (TEX0Node)node.FindChild("Textures(NW4R)", false).Children[0]; } string tempFile = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".png"; tex0.Export(tempFile); Replace(tempFile, useTextureConverter); // call self with new file File.Delete(tempFile); } } else { if (useTextureConverter) { using (TextureConverterDialog dlg = new TextureConverterDialog()) { dlg.ImageSource = filename; if (dlg.ShowDialog(null, Texture) == DialogResult.OK) { if (OnUpdate != null) { OnUpdate(this); } } } } else { if (Texture.Format == WiiPixelFormat.CMPR) { Bitmap bitmap = new Bitmap(filename); UnsafeBuffer buffer = TextureConverter.CMPR.GeneratePreview(bitmap); BrawlLib.IO.FileMap textureData = TextureConverter.CMPR.EncodeTEX0TextureCached(bitmap, Texture.LevelOfDetail, buffer); Texture.ReplaceRaw(textureData); } else { Texture.Replace(filename); } if (OnUpdate != null) { OnUpdate(this); } } } }