Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace SNES_palette_importer_exporter
  14. {
  15. public partial class Form1 : Form
  16. {
  17. byte[] rom;
  18. string fileName;
  19. int maxFileNameLength = 108;
  20. List<List<Color>> palette = new List<List<Color>>();
  21.  
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. }
  26.  
  27. private void loadToolStripMenuItem_Click(object sender, EventArgs e)
  28. {
  29. OpenFileDialog d = new OpenFileDialog();
  30. d.Filter = "ROM file (*.smc;*.sfc)|*.smc;*.sfc";
  31. d.Title = "Select an SNES ROM";
  32.  
  33. if (d.ShowDialog() == DialogResult.OK)
  34. {
  35. panel1.Visible = true;
  36. fileName = d.FileName;
  37. this.Text = (fileName.Length > maxFileNameLength) ? fileName.Substring(fileName.Length - maxFileNameLength) : fileName;
  38. rom = File.ReadAllBytes(fileName);
  39. textBox_palette.Focus();
  40. }
  41. }
  42.  
  43. private void button_export_Click(object sender, EventArgs e)
  44. {
  45. if (textBox_palette.Text.Length == 0)
  46. {
  47. MessageBox.Show("Error!");
  48. return;
  49. }
  50.  
  51. //pictureBox_test = new PictureBox();
  52.  
  53. // Always add 1
  54. palette.Add(new List<Color>());
  55.  
  56. if (!radioButton_object.Checked)
  57. {
  58. // Add enough colors for bg palette
  59. for (int i = 1; i < 8; i++)
  60. {
  61. palette.Add(new List<Color>());
  62. }
  63. }
  64.  
  65. int address = Convert.ToInt32(textBox_palette.Text, 16);
  66.  
  67. // Loop through every color in ROM
  68. for (int i = 0; i < palette.Count; i++)
  69. {
  70. for (int j = 0; j < 16; j++)
  71. {
  72. // Value at address
  73. int value = Read16(ref address);
  74. // = _bbbbbgg gggrrrrr
  75. int r = value >> 0 & 0x1f;
  76. int g = value >> 5 & 0x1f;
  77. int b = value >> 10 & 0x1f;
  78.  
  79. // Put in current format
  80. r <<= 3;
  81. g <<= 3;
  82. b <<= 3;
  83.  
  84. // Add color to palette
  85. palette[i].Add(Color.FromArgb(r, g, b));
  86. }
  87. }
  88.  
  89. CreateBitmapAtRuntime();
  90.  
  91. SaveFileDialog dialog = new SaveFileDialog();
  92. dialog.Filter = "Image Files (*.bmp)|*.bmp;";
  93. if (dialog.ShowDialog() == DialogResult.OK)
  94. {
  95. int width = Convert.ToInt32(pictureBox1.Image.Width);
  96. int height = Convert.ToInt32(pictureBox1.Image.Height);
  97. Bitmap bmp = new Bitmap(width, height);
  98. bmp = (Bitmap)pictureBox1.Image;
  99. bmp.Save(dialog.FileName, ImageFormat.Bmp);
  100. }
  101.  
  102. // Reset to default
  103. palette = new List<List<Color>>();
  104. }
  105.  
  106. private void button_import_Click(object sender, EventArgs e)
  107. {
  108. if (textBox_palette.Text.Length == 0)
  109. {
  110. MessageBox.Show("Error!");
  111. return;
  112. }
  113.  
  114. OpenFileDialog d = new OpenFileDialog();
  115. d.Filter = "Bitmap (*.bmp;)|*.bmp;";
  116. d.Title = "Select a Bitmap to use";
  117.  
  118. if (d.ShowDialog() == DialogResult.OK)
  119. {
  120. Bitmap bmp = new Bitmap(d.FileName);
  121. Import(bmp);
  122. }
  123.  
  124. }
  125. private void Import (Bitmap bmp)
  126. {
  127. int address = Convert.ToInt32(textBox_palette.Text, 16);
  128.  
  129.  
  130. pictureBox1.Image = bmp;
  131. List<Color> import = new List<Color>();
  132.  
  133. // Loop through selected bmp, getting color of each square
  134. for (int i = 0; i < bmp.Height; i += 20)
  135. {
  136. for (int j = 0; j < bmp.Width; j += 20)
  137. {
  138. import.Add(bmp.GetPixel(j, i));
  139. }
  140. }
  141. foreach (var color in import)
  142. {
  143. // Break down into the 3 component colors snes uses
  144. int r = color.R >> 3 << 0;
  145. int g = color.G >> 3 << 5;
  146. int b = color.B >> 3 << 10;
  147.  
  148. int value = r | g | b;
  149.  
  150. Write16(value, ref address);
  151.  
  152. }
  153. System.IO.File.WriteAllBytes(fileName, rom); //Include date and random number
  154.  
  155. }
  156.  
  157. public void CreateBitmapAtRuntime()
  158. {
  159. // Create bitmap based on selected option
  160. Bitmap thisPalette = new Bitmap(320, palette.Count * 20);
  161. //Bitmap thisPalette = new Bitmap(20, 20);
  162.  
  163. //using (Graphics g = Graphics.FromImage(thisPalette))
  164. //g.FillRectangle(new SolidBrush(palette[0][3]), 0, 0, 20, 20);
  165.  
  166. //pictureBox1.Image = Image.FromFile("E:\\OneDrive\\Pictures\\bananas.png");
  167. //pictureBox1.Image = (Image)thisPalette;
  168. //return;
  169. using (Graphics g = Graphics.FromImage(thisPalette))
  170. {
  171. g.FillRectangle(new SolidBrush(Color.FromArgb(0,0,0)), 0, 0, 20, 20);
  172. // Loop through all colors in palette
  173. for (int i = 0; i < palette.Count; i++)
  174. {
  175. for (int j = 0; j < palette[i].Count; j++)
  176. {
  177. g.FillRectangle(new SolidBrush(palette[i][j]), j * 20, i * 20, 20, 20);
  178. }
  179. }
  180. }
  181.  
  182. pictureBox1.Image = thisPalette;
  183.  
  184. //return (Bitmap)thisPalette.Clone();
  185. }
  186.  
  187. public UInt16 Read16(ref Int32 address)
  188. {
  189. address &= 0x3fffff;
  190. return (UInt16)(
  191. (rom[address++] << 0) |
  192. (rom[address++] << 8));
  193. }
  194.  
  195. public void Write16(Int32 value, ref Int32 address)
  196. {
  197. address &= 0x3fffff;
  198. rom[address++] = (byte)(value >> 0);
  199. rom[address++] = (byte)(value >> 8);
  200.  
  201. }
  202.  
  203. }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement