public void load_image_data(byte_image img) { this.image_filepath = img.image_filepath; this.image_file = img.image_file; this.image_extension = img.image_extension; this.csv_filepath = img.out_filepath; this.csv_file = img.out_file; }
/// <summary> /// Builds the output filename from a formatted string and image information /// </summary> /// <param name="image">Image</param> /// <param name="format">Format string -- defines order of variables and other static characters</param> /// <param name="mode">dig, build, place, query?</param> /// <param name="levels">how many images are being stiched together into one template</param> /// <param name="etc">anything else</param> /// <returns></returns> public static string csv_name_format(byte_image image, string format, string name = "", string mode = "", string levels = "", string etc = "") { if (format == "") { format = "[mode] - [name].csv"; } //sort in order from least likely to contain bad stuff to most likely to contain bad stuff format = format.Replace("[mode]", mode); format = format.Replace("[levels]", levels); format = format.Replace("[size]", string.Format("({0}x{1})", image.image.Width, image.image.Height)); format = format.Replace("[name]", name); //format = format.Replace("etc", etc); return(format); }
/// <summary> /// Creates a multi-z-level template with the provided list of images. /// </summary> /// <param name="images">List of images -- one for each z-level. Must be the same dimensions</param> /// <param name="output_path">Output file path for the template</param> /// <param name="filename">name of desired output file.</param> /// <param name="mode">what kind of template which is being generated</param> /// <param name="description">Image settings which have deisgnation info</param> /// <param name="progress">ProgressBar object to track the function's progress</param> /// <param name="status">Label object to provide a visual indicator of the function's status</param> /// <returns></returns> public bool build_csv(List <byte_image> images, string output_path, string filename_format, string mode, string description, ProgressBar progress = null, Label status = null) { if (images == null || images.Count == 0) { return(false); } StringBuilder csv_builder = new StringBuilder(); int image_index = 1; int prediction = images[0].image.Width * images[0].image.Height; //check images to make sure they are the same size, prepare progressbar for multiple images #region if multiple images if (images.Count > 1) { byte_image first_image = null; prediction = 0; if (progress != null) { progress.Maximum = images.Count; progress.Value = 0; } foreach (byte_image current in images) { if (progress != null) { progress.Value++; prediction += current.image.Width * current.image.Height; progress.Refresh(); } if (first_image == null) { first_image = current; continue; } if (first_image.image.Size != current.image.Size) { MessageBox.Show("Images must be the same size for multilevel templates"); Debug.Log("images do not match dimensions"); if (progress != null) { progress.Value = 0; } return(false); } } } #endregion #region generate csv //set progressbar estimate if (progress != null) { progress.Maximum = prediction; progress.Value = 0; progress.Refresh(); } //generate csv -- use stringbuilder to create file contents before writing to file update_status(status, "Generating CSV"); //write first line -> template type, start position, comments csv_builder.Append(string.Format("{0}\n", description)); foreach (byte_image current in images) { update_status(status, string.Format("Creating level ({0}/{1})", image_index, images.Count)); Queue <Color> color_queue = new Queue <Color>(current.image_array); for (int y = 0; y < current.image.Height; y++) { for (int x = 0; x < current.image.Width; x++) { if (progress != null) { try { progress.Value++; progress.Refresh(); } catch (Exception ex) { Debug.Log(string.Format("wrong value for progressbar: {0}", progress.Value, ex)); } } //write the setting value of the color based on the template type //handle unkown values by writing blanks string designation = " "; string key = color_string(color_queue.Dequeue()); if (settings[key] != null && settings[key].ToString() != "") { string[] colorcodes = settings[key].ToString().Split('|'); if (colorcodes.Length == 4) { switch (mode) { case "dig": designation = colorcodes[0]; break; case "build": designation = colorcodes[1]; break; case "place": designation = colorcodes[2]; break; case "query": designation = colorcodes[3]; break; default: break; } } else { //they have the wrong format, just grab the first string item designation = colorcodes[0]; } if (designation == "") { designation = " "; } } csv_builder.Append(string.Format("{0},", designation)); } //remove last , csv_builder = csv_builder.Remove(csv_builder.Length - 1, 1); //append # to the end of the line csv_builder.Append("#\n"); } if (image_index < images.Count) { //if there are other images, write #> to indicate the start of the next level down csv_builder.Append("#>\n"); } else { //if this is the last image, write # to indicate end of csv csv_builder.Append("#"); } image_index++; } //Debug.log(csv_builder.ToString()); #endregion generate csv #region write file //write file update_status(status, "Writing File"); string filename = images[0].out_file; try { ////if file is nothing, use the file name of the first image //if (string.IsNullOrEmpty(filename)) filename = images[0].csv_file; //if path is nothing, use the path of the first image if (string.IsNullOrEmpty(output_path)) { output_path = images[0].out_path; } //format filename filename = csv_name_format(images[0], filename_format, name: filename.Replace(byte_image.out_extension, ""), mode: mode, levels: images.Count.ToString()); //save output path in settings set_setting(setting.output_path, output_path); string csv_filepath = string.Format("{0}/{1}", output_path, filename); //if user put / to make subfolders, set the path to subfolder output_path = csv_filepath.Substring(0, csv_filepath.LastIndexOf('/')); //if path doesn't exist, create it if (!Directory.Exists(output_path)) { Directory.CreateDirectory(output_path); } using (StreamWriter f = new StreamWriter(csv_filepath)) f.Write(csv_builder.ToString()); update_status(status, "File Written"); Debug.Log(string.Format("file written to: {0}", csv_filepath)); } catch (Exception e) { string status_message = string.Format("Error. File:{0}/{1} could not be written", output_path, filename); update_status(status, status_message); MessageBox.Show(status_message); Debug.Log(e); return(false); } #endregion write file return(true); }