Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace SNES_palette_importer_exporter
- {
- public partial class Form1 : Form
- {
- byte[] rom;
- string fileName;
- int maxFileNameLength = 108;
- List<List<Color>> palette = new List<List<Color>>();
- public Form1()
- {
- InitializeComponent();
- }
- private void loadToolStripMenuItem_Click(object sender, EventArgs e)
- {
- OpenFileDialog d = new OpenFileDialog();
- d.Filter = "ROM file (*.smc;*.sfc)|*.smc;*.sfc";
- d.Title = "Select an SNES ROM";
- if (d.ShowDialog() == DialogResult.OK)
- {
- panel1.Visible = true;
- fileName = d.FileName;
- this.Text = (fileName.Length > maxFileNameLength) ? fileName.Substring(fileName.Length - maxFileNameLength) : fileName;
- rom = File.ReadAllBytes(fileName);
- textBox_palette.Focus();
- }
- }
- private void button_export_Click(object sender, EventArgs e)
- {
- if (textBox_palette.Text.Length == 0)
- {
- MessageBox.Show("Error!");
- return;
- }
- //pictureBox_test = new PictureBox();
- // Always add 1
- palette.Add(new List<Color>());
- if (!radioButton_object.Checked)
- {
- // Add enough colors for bg palette
- for (int i = 1; i < 8; i++)
- {
- palette.Add(new List<Color>());
- }
- }
- int address = Convert.ToInt32(textBox_palette.Text, 16);
- // Loop through every color in ROM
- for (int i = 0; i < palette.Count; i++)
- {
- for (int j = 0; j < 16; j++)
- {
- // Value at address
- int value = Read16(ref address);
- // = _bbbbbgg gggrrrrr
- int r = value >> 0 & 0x1f;
- int g = value >> 5 & 0x1f;
- int b = value >> 10 & 0x1f;
- // Put in current format
- r <<= 3;
- g <<= 3;
- b <<= 3;
- // Add color to palette
- palette[i].Add(Color.FromArgb(r, g, b));
- }
- }
- CreateBitmapAtRuntime();
- SaveFileDialog dialog = new SaveFileDialog();
- dialog.Filter = "Image Files (*.bmp)|*.bmp;";
- if (dialog.ShowDialog() == DialogResult.OK)
- {
- int width = Convert.ToInt32(pictureBox1.Image.Width);
- int height = Convert.ToInt32(pictureBox1.Image.Height);
- Bitmap bmp = new Bitmap(width, height);
- bmp = (Bitmap)pictureBox1.Image;
- bmp.Save(dialog.FileName, ImageFormat.Bmp);
- }
- // Reset to default
- palette = new List<List<Color>>();
- }
- private void button_import_Click(object sender, EventArgs e)
- {
- if (textBox_palette.Text.Length == 0)
- {
- MessageBox.Show("Error!");
- return;
- }
- OpenFileDialog d = new OpenFileDialog();
- d.Filter = "Bitmap (*.bmp;)|*.bmp;";
- d.Title = "Select a Bitmap to use";
- if (d.ShowDialog() == DialogResult.OK)
- {
- Bitmap bmp = new Bitmap(d.FileName);
- Import(bmp);
- }
- }
- private void Import (Bitmap bmp)
- {
- int address = Convert.ToInt32(textBox_palette.Text, 16);
- pictureBox1.Image = bmp;
- List<Color> import = new List<Color>();
- // Loop through selected bmp, getting color of each square
- for (int i = 0; i < bmp.Height; i += 20)
- {
- for (int j = 0; j < bmp.Width; j += 20)
- {
- import.Add(bmp.GetPixel(j, i));
- }
- }
- foreach (var color in import)
- {
- // Break down into the 3 component colors snes uses
- int r = color.R >> 3 << 0;
- int g = color.G >> 3 << 5;
- int b = color.B >> 3 << 10;
- int value = r | g | b;
- Write16(value, ref address);
- }
- System.IO.File.WriteAllBytes(fileName, rom); //Include date and random number
- }
- public void CreateBitmapAtRuntime()
- {
- // Create bitmap based on selected option
- Bitmap thisPalette = new Bitmap(320, palette.Count * 20);
- //Bitmap thisPalette = new Bitmap(20, 20);
- //using (Graphics g = Graphics.FromImage(thisPalette))
- //g.FillRectangle(new SolidBrush(palette[0][3]), 0, 0, 20, 20);
- //pictureBox1.Image = Image.FromFile("E:\\OneDrive\\Pictures\\bananas.png");
- //pictureBox1.Image = (Image)thisPalette;
- //return;
- using (Graphics g = Graphics.FromImage(thisPalette))
- {
- g.FillRectangle(new SolidBrush(Color.FromArgb(0,0,0)), 0, 0, 20, 20);
- // Loop through all colors in palette
- for (int i = 0; i < palette.Count; i++)
- {
- for (int j = 0; j < palette[i].Count; j++)
- {
- g.FillRectangle(new SolidBrush(palette[i][j]), j * 20, i * 20, 20, 20);
- }
- }
- }
- pictureBox1.Image = thisPalette;
- //return (Bitmap)thisPalette.Clone();
- }
- public UInt16 Read16(ref Int32 address)
- {
- address &= 0x3fffff;
- return (UInt16)(
- (rom[address++] << 0) |
- (rom[address++] << 8));
- }
- public void Write16(Int32 value, ref Int32 address)
- {
- address &= 0x3fffff;
- rom[address++] = (byte)(value >> 0);
- rom[address++] = (byte)(value >> 8);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement