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.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Drawing.Imaging;
- namespace smbx_npc_editor
- {
- public partial class Form1 : Form
- {
- Bitmap originalImage;
- Bitmap originalMaskImage;
- Bitmap maskImage;
- Bitmap maskedImage;
- Bitmap currentFrame;
- Bitmap animatedImage;
- Timer frameSpeedTimer = new Timer();
- int frameHeight;
- int frameWidth;
- int frames;
- int curFrame = 0;
- Rectangle SR;
- public Form1()
- {
- Font = SystemFonts.MessageBoxFont;
- this.frameSpeedTimer.Tick += new EventHandler(frameSpeedTimer_Tickk);
- InitializeComponent();
- }
- void frameSpeedTimer_Tickk(object sender, EventArgs e)
- {
- spritePreview.Image = null;
- curFrame += 1;
- if (curFrame >= frames)
- {
- curFrame = 0;
- }
- SR = new Rectangle(0, frameHeight * curFrame, frameWidth, frameHeight);
- Graphics g = Graphics.FromImage(currentFrame);
- g.DrawImage(maskedImage, new Rectangle(0, 0, frameWidth, frameHeight), SR, GraphicsUnit.Pixel);
- spritePreview.Image = currentFrame;
- spritePreview.Update();
- currentFrame = new Bitmap(frameWidth, frameHeight, PixelFormat.Format32bppArgb);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- try
- {
- spritePreview.BackgroundImage = Image.FromFile(Environment.CurrentDirectory + @"\bg.gif");
- }
- catch
- {
- MessageBox.Show("No bg.gif in exe's directory! Closing...", "Runtime Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- Environment.Exit(1);
- }
- }
- private void menuItem2_Click(object sender, EventArgs e)
- {
- OpenFileDialog of = new OpenFileDialog();
- of.Filter = "SMBX Sprite Files (*.gif)|*.gif";
- of.Title = "Open an NPC Sprite";
- if (of.ShowDialog() == DialogResult.OK)
- {
- switch (getScenario(of.FileName))
- {
- case(0):
- break;
- case(1):
- drawGifWMask(of.FileName);
- break;
- case(2):
- break;
- case(3):
- MessageBox.Show("Couldn't access file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- break;
- }
- }
- }
- void drawGifWMask(string file)
- {
- spritePreview.Image = null;
- frameSpeedTimer.Stop();
- string fileNoMask = Path.GetFileNameWithoutExtension(file);
- string fullPath = Path.GetDirectoryName(file);
- string nameWMask = fullPath + @"\" + fileNoMask + "m" + ".gif";
- originalImage = Create32bppImageAndClearAlpha(new Bitmap(file));
- originalMaskImage = Create32bppImageAndClearAlpha(new Bitmap(nameWMask));
- //originalSprite.Image = originalImage;
- //spriteMask.Image = originalMaskImage;
- PrepareMaskImage();
- }
- #region This is ugly stuff
- private void PrepareMaskedImage()
- {
- this.Cursor = Cursors.WaitCursor;
- if (this.originalImage != null && this.maskImage != null)
- {
- if (this.originalImage.Width != this.maskImage.Width || this.originalImage.Height != this.maskImage.Height)
- {
- MessageBox.Show("Error: mask and image must have the same size", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- this.spritePreview.Image = null;
- }
- else
- {
- //allocate the Masked image in ARGB format
- this.maskedImage = Create32bppImageAndClearAlpha(this.originalImage);
- BitmapData bmpData1 = maskedImage.LockBits(new Rectangle(0, 0, maskedImage.Width, maskedImage.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, maskedImage.PixelFormat);
- byte[] maskedImageRGBAData = new byte[bmpData1.Stride * bmpData1.Height];
- System.Runtime.InteropServices.Marshal.Copy(bmpData1.Scan0, maskedImageRGBAData, 0, maskedImageRGBAData.Length);
- BitmapData bmpData2 = maskImage.LockBits(new Rectangle(0, 0, maskImage.Width, maskImage.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, maskImage.PixelFormat);
- byte[] maskImageRGBAData = new byte[bmpData2.Stride * bmpData2.Height];
- System.Runtime.InteropServices.Marshal.Copy(bmpData2.Scan0, maskImageRGBAData, 0, maskImageRGBAData.Length);
- //copy the mask to the Alpha layer
- for (int i = 0; i + 2 < maskedImageRGBAData.Length; i += 4)
- {
- maskedImageRGBAData[i + 3] = maskImageRGBAData[i];
- }
- System.Runtime.InteropServices.Marshal.Copy(maskedImageRGBAData, 0, bmpData1.Scan0, maskedImageRGBAData.Length);
- this.maskedImage.UnlockBits(bmpData1);
- this.maskImage.UnlockBits(bmpData2);
- this.spritePreview.Image = maskedImage;
- animatedImage = new Bitmap(maskedImage.Width, maskedImage.Height);
- }
- this.Cursor = Cursors.Default;
- }
- }
- private void PrepareMaskImage()
- {
- if (originalMaskImage != null)
- {
- this.Cursor = Cursors.WaitCursor;
- this.maskImage = Create32bppImageAndClearAlpha(originalMaskImage);
- BitmapData bmpData = maskImage.LockBits(new Rectangle(0, 0, maskImage.Width, maskImage.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, maskImage.PixelFormat);
- byte[] maskImageRGBData = new byte[bmpData.Stride * bmpData.Height];
- System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, maskImageRGBData, 0, maskImageRGBData.Length);
- byte greyLevel;
- bool opaque = false;
- //int OpacityThreshold = this.trackBar1.Value;
- bool invertedMask = true;//this.checkBoxInvertMask.Checked;
- for (int i = 0; i + 2 < maskImageRGBData.Length; i += 4)
- {
- //convert to gray scale R:0.30 G=0.59 B 0.11
- greyLevel = (byte)(0.3 * maskImageRGBData[i + 2] + 0.59 * maskImageRGBData[i + 1] + 0.11 * maskImageRGBData[i]);
- if (opaque)
- {
- greyLevel = (greyLevel < 420/*OpacityThreshold*/) ? byte.MinValue : byte.MaxValue;
- }
- if (invertedMask)
- {
- greyLevel = (byte)(255 - (int)greyLevel);
- }
- maskImageRGBData[i] = greyLevel;
- maskImageRGBData[i + 1] = greyLevel;
- maskImageRGBData[i + 2] = greyLevel;
- }
- System.Runtime.InteropServices.Marshal.Copy(maskImageRGBData, 0, bmpData.Scan0, maskImageRGBData.Length);
- this.maskImage.UnlockBits(bmpData);
- //this.spriteMask.Image = maskImage;
- this.Cursor = Cursors.Default;
- // if the loaded image is available, we have everything to compute the masked image
- if (this.originalImage != null)
- {
- PrepareMaskedImage();
- }
- }
- }
- private Bitmap Create32bppImageAndClearAlpha(Bitmap tmpImage)
- {
- // declare the new image that will be returned by the function
- Bitmap returnedImage = new Bitmap(tmpImage.Width, tmpImage.Height, PixelFormat.Format32bppArgb);
- // create a graphics instance to draw the original image in the new one
- Rectangle rect = new Rectangle(0, 0, tmpImage.Width, tmpImage.Height);
- Graphics g = Graphics.FromImage(returnedImage);
- // create an image attribe to force a clearing of the alpha layer
- ImageAttributes imageAttributes = new ImageAttributes();
- float[][] colorMatrixElements = {
- new float[] {1,0,0,0,0},
- new float[] {0,1,0,0,0},
- new float[] {0,0,1,0,0},
- new float[] {0,0,0,0,0},
- new float[] {0,0,0,1,1}};
- ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
- imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
- // draw the original image
- g.DrawImage(tmpImage, rect, 0, 0, tmpImage.Width, tmpImage.Height, GraphicsUnit.Pixel, imageAttributes);
- g.Dispose();
- return returnedImage;
- }
- #endregion
- /// <summary>
- /// 0. Gif, no mask
- /// 1. Gif, with mask
- /// 2. Png, no mask needed
- /// 3. File doesn't exist, show error
- /// </summary>
- /// <param name="file">Full path of the file to check</param>
- int getScenario(string file)
- {
- if (File.Exists(file))
- {
- if (file.Contains("png"))
- {
- return 2;
- }
- else
- {
- string fileNoMask = Path.GetFileNameWithoutExtension(file);
- string fullPath = Path.GetDirectoryName(file);
- string nameWMask = fullPath + @"\" + fileNoMask + "m" + ".gif";
- if (File.Exists(nameWMask))
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- }
- else
- {
- return 3;
- }
- }
- private void linkValButton_Click(object sender, EventArgs e)
- {
- frameWidthVal.Text = frameHeightVal.Text;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- frameHeight = int.Parse(frameHeightVal.Text);
- frameWidth = int.Parse(frameWidthVal.Text);
- frames = int.Parse(framesValue.Text);
- currentFrame = new Bitmap(frameWidth, frameHeight);
- int fs = int.Parse(frameSpeedVal.Text);
- switch (fs)
- {
- case 8:
- frameSpeedTimer.Interval = 100;
- break;
- }
- frameSpeedTimer.Start();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- }
- //
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement