private static void writeCompressedData(BinaryWriterWrapper bw, Bitmap png, bool premultiply) { using (MemoryStream stream = new MemoryStream()) { byte[] uncompressedData; using (BinaryWriterWrapper mw = new BinaryWriterWrapper(new BinaryWriter(stream))) { writeData(png, mw, premultiply); uncompressedData = stream.ToArray(); } byte[] compressedData = XCompress.Compress(uncompressedData); bw.WriteInt(6 + 4 + 4 + compressedData.Length); // compressed file size including headers bw.WriteInt(uncompressedData.Length); // uncompressed data size (exluding headers! only the data) bw.WriteByteArray(compressedData); } }
public static void Main(string[] args) { Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; MainClass.keepRunning = false; }; if (args.Length == 0) { openGUI(); Environment.Exit(0); } if (args.Length == 1 && (args[0] == "-h" || args[0] == "--help" || args[0] == "/?" || args[0] == "/h")) { Console.WriteLine("Save images as XNB."); Console.WriteLine("Usage: " + System.AppDomain.CurrentDomain.FriendlyName + " [-h|--help] [-c] [-u] [-hidef] [-nopre] png_file [xnb_file]"); Console.WriteLine(""); Console.WriteLine("The program reads the image 'png_file' and saves as an XNB file as 'xnb_file'."); Console.WriteLine("Start without any input parameters to launch a GUI."); Console.WriteLine(""); Console.WriteLine("Options:"); Console.WriteLine(" -h Prints this help."); Console.WriteLine(" -c Compress the XNB file. This is the default if xcompress32.dll is"); Console.WriteLine(" available. Note that the compression might take significant time, but"); Console.WriteLine(" of course the result XNB file will be much smaller."); Console.WriteLine(" -u Save uncompressed XNB file, even if xcompress32.dll is available."); Console.WriteLine(" -hidef XNB's can be either 'reach' or 'hidef'. Default is 'reach', so use"); Console.WriteLine(" this -hidef option when necessary. I don't know what 'reach' or"); Console.WriteLine(" 'hidef' means, but for example Terraria cannot load 'hidef' XNB files."); Console.WriteLine(" -nopre RGB channels will not be premultiplied by the alpha. By default, XNB's"); Console.WriteLine(" use premultiplied alpha."); Console.WriteLine(""); Console.WriteLine("png_file This can either be a file or a directory. If this is a directory"); Console.WriteLine(" then it will convert all *.png files in the directory (not recursive)."); Console.WriteLine("xnb_file This can also be a file or a directory. If this is a directory then"); Console.WriteLine(" the filename will be name.xnb if the image file was name.png"); Console.WriteLine(" If this is omitted then it converts the png_file into the same folder."); Environment.Exit(1); } bool compressionAvailable = XCompress.isItAvailable(); bool compressed = compressionAvailable; bool reach = true; bool premultiply = true; string pngFile = null; string xnbFile = null; for (int i = 0; i < args.Length; i++) { string v = args[i]; if (v.Equals("-c")) { compressed = true; } else if (v.Equals("-u")) { compressed = false; } else if (v.Equals("-hidef")) { reach = false; } else if (v.Equals("-nopre")) { premultiply = false; } else if (pngFile == null) { pngFile = v; } else if (xnbFile == null) { xnbFile = v; } else { Console.WriteLine("Invalid command line argument: " + v); Environment.Exit(3); } } if (pngFile != null && xnbFile == null && isFile(pngFile)) { xnbFile = Path.ChangeExtension(pngFile, ".xnb"); } if (!compressionAvailable && compressed) { Console.WriteLine("To write compressed XNB files, you must have 'xcompress32.dll' available."); Environment.Exit(2); } try { execute(pngFile, xnbFile, compressed, reach, premultiply); } catch (Exception ex) { Console.WriteLine(ex); } }
public SimpleGUI() { Text = "PNG to XNB"; Size = new Size(640, 250); MainMenu mainMenu = new MainMenu(); MenuItem file = mainMenu.MenuItems.Add("&File"); file.MenuItems.Add(new MenuItem("E&xit", new EventHandler(this.OnExit), Shortcut.CtrlQ)); Menu = mainMenu; ToolTip toolTip = new ToolTip(); Label labelInputFile = new Label(); labelInputFile.Parent = this; labelInputFile.Text = "Input file:"; labelInputFile.Width = 70; labelInputFile.Location = new Point(10, 18); textBoxInputFile = new TextBox(); textBoxInputFile.Parent = this; textBoxInputFile.Multiline = false; textBoxInputFile.Location = new Point(80, 15); textBoxInputFile.Width = 400; textBoxInputFile.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; Button buttonInputFile = new Button(); buttonInputFile.Parent = this; buttonInputFile.Text = "File..."; buttonInputFile.Width = 60; buttonInputFile.Location = new Point(490, 15); buttonInputFile.Click += new EventHandler(onChooseInputFile); buttonInputFile.Anchor = AnchorStyles.Top | AnchorStyles.Right; Button buttonInputFolder = new Button(); buttonInputFolder.Parent = this; buttonInputFolder.Text = "Folder..."; buttonInputFolder.Width = 60; buttonInputFolder.Location = new Point(560, 15); buttonInputFolder.Click += new EventHandler(onChooseInputFolder); buttonInputFolder.Anchor = AnchorStyles.Top | AnchorStyles.Right; Label labelOutputFile = new Label(); labelOutputFile.Parent = this; labelOutputFile.Text = "Output file:"; labelOutputFile.Width = 70; labelOutputFile.Location = new Point(10, 53); textBoxOutputFile = new TextBox(); textBoxOutputFile.Parent = this; textBoxOutputFile.Multiline = false; textBoxOutputFile.Location = new Point(80, 50); textBoxOutputFile.Width = 400; textBoxOutputFile.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; Button buttonOutputFile = new Button(); buttonOutputFile.Parent = this; buttonOutputFile.Text = "File..."; buttonOutputFile.Width = 60; buttonOutputFile.Location = new Point(490, 50); buttonOutputFile.Click += new EventHandler(onChooseOutputFile); buttonOutputFile.Anchor = AnchorStyles.Top | AnchorStyles.Right; Button buttonOutputFolder = new Button(); buttonOutputFolder.Parent = this; buttonOutputFolder.Text = "Folder..."; buttonOutputFolder.Width = 60; buttonOutputFolder.Location = new Point(560, 50); buttonOutputFolder.Click += new EventHandler(onChooseOutputFolder); buttonOutputFolder.Anchor = AnchorStyles.Top | AnchorStyles.Right; checkBoxCompress = new CheckBox(); checkBoxCompress.Parent = this; checkBoxCompress.Width = 275; checkBoxCompress.Location = new Point(10, 80); checkBoxCompress.Enabled = XCompress.isItAvailable(); checkBoxCompress.Checked = checkBoxCompress.Enabled; if (checkBoxCompress.Enabled) { checkBoxCompress.Text = "Compress XNB file"; } else { checkBoxCompress.Text = "Compress XNB file (xcompress32.dll not found)"; } checkBoxPremultiply = new CheckBox(); checkBoxPremultiply.Parent = this; checkBoxPremultiply.Text = "Premultiply alpha"; checkBoxPremultiply.Checked = true; checkBoxPremultiply.Location = new Point(285, 80); checkBoxPremultiply.Width = 200; toolTip.SetToolTip(checkBoxPremultiply, "RGB channels are multiplied by the alpha channel"); radioReach = new RadioButton(); radioReach.Parent = this; radioReach.Text = "reach"; radioReach.Width = 60; radioReach.Location = new Point(10, 110); radioReach.Checked = true; radioHidef = new RadioButton(); radioHidef.Parent = this; radioHidef.Text = "hidef"; radioHidef.Width = 60; radioHidef.Location = new Point(80, 110); Button buttonConvert = new Button(); buttonConvert.Parent = this; buttonConvert.Text = "Convert"; buttonConvert.Location = new Point(10, 140); buttonConvert.Click += new EventHandler(onConvert); statusBar = new StatusBar(); statusBar.Parent = this; statusBar.Text = "Ready"; CenterToScreen(); }