Advertisement
MrMiketheripper

asdfasdfasdf

Aug 14th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.37 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.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Runtime.InteropServices;
  11. using System.Drawing.Imaging;
  12.  
  13. namespace smbx_npc_editor
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         Bitmap originalImage;
  18.         Bitmap originalMaskImage;
  19.         Bitmap maskImage;
  20.         Bitmap maskedImage;
  21.         Bitmap currentFrame;
  22.         Bitmap animatedImage;
  23.         Timer frameSpeedTimer = new Timer();
  24.         int frameHeight;
  25.         int frameWidth;
  26.         int frames;
  27.         int curFrame = 0;
  28.         Rectangle SR;
  29.  
  30.         public Form1()
  31.         {
  32.             Font = SystemFonts.MessageBoxFont;
  33.  
  34.             this.frameSpeedTimer.Tick += new EventHandler(frameSpeedTimer_Tickk);
  35.  
  36.             InitializeComponent();
  37.         }
  38.  
  39.         void frameSpeedTimer_Tickk(object sender, EventArgs e)
  40.         {
  41.             spritePreview.Image = null;
  42.             curFrame += 1;
  43.             if (curFrame >= frames)
  44.             {
  45.                 curFrame = 0;
  46.             }
  47.             SR = new Rectangle(0, frameHeight * curFrame, frameWidth, frameHeight);
  48.             Graphics g = Graphics.FromImage(currentFrame);
  49.             g.DrawImage(maskedImage, new Rectangle(0, 0, frameWidth, frameHeight), SR, GraphicsUnit.Pixel);
  50.            
  51.             spritePreview.Image = currentFrame;
  52.             spritePreview.Update();
  53.             currentFrame = new Bitmap(frameWidth, frameHeight, PixelFormat.Format32bppArgb);
  54.         }
  55.  
  56.         private void Form1_Load(object sender, EventArgs e)
  57.         {
  58.             try
  59.             {
  60.                 spritePreview.BackgroundImage = Image.FromFile(Environment.CurrentDirectory + @"\bg.gif");
  61.        
  62.             }
  63.             catch
  64.             {
  65.                 MessageBox.Show("No bg.gif in exe's directory! Closing...", "Runtime Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  66.                
  67.                 Environment.Exit(1);
  68.             }
  69.            
  70.         }
  71.         private void menuItem2_Click(object sender, EventArgs e)
  72.         {
  73.             OpenFileDialog of = new OpenFileDialog();
  74.             of.Filter = "SMBX Sprite Files (*.gif)|*.gif";
  75.             of.Title = "Open an NPC Sprite";
  76.             if (of.ShowDialog() == DialogResult.OK)
  77.             {
  78.                 switch (getScenario(of.FileName))
  79.                 {
  80.                     case(0):
  81.                         break;
  82.                     case(1):
  83.                         drawGifWMask(of.FileName);
  84.                         break;
  85.                     case(2):
  86.                         break;
  87.                     case(3):
  88.                         MessageBox.Show("Couldn't access file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  89.                         break;
  90.                 }
  91.             }
  92.         }
  93.  
  94.         void drawGifWMask(string file)
  95.         {
  96.             spritePreview.Image = null;
  97.             frameSpeedTimer.Stop();
  98.             string fileNoMask = Path.GetFileNameWithoutExtension(file);
  99.             string fullPath = Path.GetDirectoryName(file);
  100.             string nameWMask = fullPath + @"\" + fileNoMask + "m" + ".gif";
  101.             originalImage = Create32bppImageAndClearAlpha(new Bitmap(file));
  102.             originalMaskImage = Create32bppImageAndClearAlpha(new Bitmap(nameWMask));
  103.            
  104.             //originalSprite.Image = originalImage;
  105.             //spriteMask.Image = originalMaskImage;
  106.             PrepareMaskImage();
  107.            
  108.         }
  109.  
  110.  
  111.         #region This is ugly stuff
  112.         private void PrepareMaskedImage()
  113.         {
  114.             this.Cursor = Cursors.WaitCursor;
  115.  
  116.             if (this.originalImage != null && this.maskImage != null)
  117.             {
  118.                 if (this.originalImage.Width != this.maskImage.Width || this.originalImage.Height != this.maskImage.Height)
  119.                 {
  120.                     MessageBox.Show("Error: mask and image must have the same size", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  121.                     this.spritePreview.Image = null;
  122.  
  123.                 }
  124.                 else
  125.                 {
  126.  
  127.                     //allocate the Masked image in ARGB format
  128.                     this.maskedImage = Create32bppImageAndClearAlpha(this.originalImage);
  129.  
  130.                     BitmapData bmpData1 = maskedImage.LockBits(new Rectangle(0, 0, maskedImage.Width, maskedImage.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, maskedImage.PixelFormat);
  131.                     byte[] maskedImageRGBAData = new byte[bmpData1.Stride * bmpData1.Height];
  132.                     System.Runtime.InteropServices.Marshal.Copy(bmpData1.Scan0, maskedImageRGBAData, 0, maskedImageRGBAData.Length);
  133.  
  134.                     BitmapData bmpData2 = maskImage.LockBits(new Rectangle(0, 0, maskImage.Width, maskImage.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, maskImage.PixelFormat);
  135.                     byte[] maskImageRGBAData = new byte[bmpData2.Stride * bmpData2.Height];
  136.                     System.Runtime.InteropServices.Marshal.Copy(bmpData2.Scan0, maskImageRGBAData, 0, maskImageRGBAData.Length);
  137.  
  138.                     //copy the mask to the Alpha layer
  139.                     for (int i = 0; i + 2 < maskedImageRGBAData.Length; i += 4)
  140.                     {
  141.                         maskedImageRGBAData[i + 3] = maskImageRGBAData[i];
  142.  
  143.                     }
  144.                     System.Runtime.InteropServices.Marshal.Copy(maskedImageRGBAData, 0, bmpData1.Scan0, maskedImageRGBAData.Length);
  145.                     this.maskedImage.UnlockBits(bmpData1);
  146.                     this.maskImage.UnlockBits(bmpData2);
  147.  
  148.                     this.spritePreview.Image = maskedImage;
  149.                     animatedImage = new Bitmap(maskedImage.Width, maskedImage.Height);
  150.                    
  151.                 }
  152.                 this.Cursor = Cursors.Default;
  153.             }
  154.         }
  155.  
  156.         private void PrepareMaskImage()
  157.         {
  158.             if (originalMaskImage != null)
  159.             {
  160.                 this.Cursor = Cursors.WaitCursor;
  161.  
  162.                 this.maskImage = Create32bppImageAndClearAlpha(originalMaskImage);
  163.  
  164.                 BitmapData bmpData = maskImage.LockBits(new Rectangle(0, 0, maskImage.Width, maskImage.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, maskImage.PixelFormat);
  165.  
  166.                 byte[] maskImageRGBData = new byte[bmpData.Stride * bmpData.Height];
  167.  
  168.                 System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, maskImageRGBData, 0, maskImageRGBData.Length);
  169.  
  170.  
  171.                 byte greyLevel;
  172.                 bool opaque = false;
  173.                 //int OpacityThreshold = this.trackBar1.Value;
  174.                 bool invertedMask = true;//this.checkBoxInvertMask.Checked;
  175.                 for (int i = 0; i + 2 < maskImageRGBData.Length; i += 4)
  176.                 {
  177.                     //convert to gray scale R:0.30 G=0.59 B 0.11
  178.                     greyLevel = (byte)(0.3 * maskImageRGBData[i + 2] + 0.59 * maskImageRGBData[i + 1] + 0.11 * maskImageRGBData[i]);
  179.  
  180.                     if (opaque)
  181.                     {
  182.                         greyLevel = (greyLevel < 420/*OpacityThreshold*/) ? byte.MinValue : byte.MaxValue;
  183.                     }
  184.                     if (invertedMask)
  185.                     {
  186.                         greyLevel = (byte)(255 - (int)greyLevel);
  187.                     }
  188.  
  189.                     maskImageRGBData[i] = greyLevel;
  190.                     maskImageRGBData[i + 1] = greyLevel;
  191.                     maskImageRGBData[i + 2] = greyLevel;
  192.  
  193.                 }
  194.                 System.Runtime.InteropServices.Marshal.Copy(maskImageRGBData, 0, bmpData.Scan0, maskImageRGBData.Length);
  195.                 this.maskImage.UnlockBits(bmpData);
  196.                 //this.spriteMask.Image = maskImage;
  197.                 this.Cursor = Cursors.Default;
  198.                 // if the loaded image is available, we have everything to compute the masked image
  199.                 if (this.originalImage != null)
  200.                 {
  201.                     PrepareMaskedImage();
  202.                 }
  203.             }
  204.         }
  205.  
  206.         private Bitmap Create32bppImageAndClearAlpha(Bitmap tmpImage)
  207.         {
  208.             // declare the new image that will be returned by the function
  209.             Bitmap returnedImage = new Bitmap(tmpImage.Width, tmpImage.Height, PixelFormat.Format32bppArgb);
  210.  
  211.             // create a graphics instance to draw the original image in the new one
  212.             Rectangle rect = new Rectangle(0, 0, tmpImage.Width, tmpImage.Height);
  213.             Graphics g = Graphics.FromImage(returnedImage);
  214.  
  215.             // create an image attribe to force a clearing of the alpha layer
  216.             ImageAttributes imageAttributes = new ImageAttributes();
  217.             float[][] colorMatrixElements = {
  218.                         new float[] {1,0,0,0,0},
  219.                         new float[] {0,1,0,0,0},
  220.                         new float[] {0,0,1,0,0},
  221.                         new float[] {0,0,0,0,0},
  222.                         new float[] {0,0,0,1,1}};
  223.  
  224.             ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
  225.             imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  226.  
  227.             // draw the original image
  228.             g.DrawImage(tmpImage, rect, 0, 0, tmpImage.Width, tmpImage.Height, GraphicsUnit.Pixel, imageAttributes);
  229.             g.Dispose();
  230.             return returnedImage;
  231.         }
  232.         #endregion
  233.  
  234.  
  235.         /// <summary>
  236.         /// 0. Gif, no mask
  237.         /// 1. Gif, with mask
  238.         /// 2. Png, no mask needed
  239.         /// 3. File doesn't exist, show error
  240.         /// </summary>
  241.         /// <param name="file">Full path of the file to check</param>
  242.  
  243.         int getScenario(string file)
  244.         {
  245.             if (File.Exists(file))
  246.             {
  247.                 if (file.Contains("png"))
  248.                 {
  249.                     return 2;
  250.                 }
  251.                 else
  252.                 {
  253.                     string fileNoMask = Path.GetFileNameWithoutExtension(file);
  254.                     string fullPath = Path.GetDirectoryName(file);
  255.                     string nameWMask = fullPath + @"\" + fileNoMask + "m" + ".gif";
  256.  
  257.                     if (File.Exists(nameWMask))
  258.                     {
  259.                         return 1;
  260.                     }
  261.                     else
  262.                     {
  263.                         return 0;
  264.                     }
  265.                 }
  266.             }
  267.             else
  268.             {
  269.                 return 3;
  270.             }
  271.         }
  272.  
  273.         private void linkValButton_Click(object sender, EventArgs e)
  274.         {
  275.             frameWidthVal.Text = frameHeightVal.Text;
  276.         }
  277.  
  278.         private void button1_Click(object sender, EventArgs e)
  279.         {
  280.             frameHeight = int.Parse(frameHeightVal.Text);
  281.             frameWidth = int.Parse(frameWidthVal.Text);
  282.             frames = int.Parse(framesValue.Text);
  283.             currentFrame = new Bitmap(frameWidth, frameHeight);
  284.             int fs = int.Parse(frameSpeedVal.Text);
  285.             switch (fs)
  286.             {
  287.                 case 8:
  288.                     frameSpeedTimer.Interval = 100;
  289.                     break;
  290.             }
  291.             frameSpeedTimer.Start();
  292.         }
  293.  
  294.         private void button2_Click(object sender, EventArgs e)
  295.         {
  296.  
  297.         }
  298.         //
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement