From 3095176edf0950d368fefe2ca8294eddb2b448e6 Mon Sep 17 00:00:00 2001
From: Nicolai Kozel <kozeln@mcmaster.ca>
Date: Tue, 15 Nov 2016 12:29:51 -0500
Subject: [PATCH] restoring project to old state

---
 src/Gifitti/Gifitti/Form1.Designer.cs |  21 ++++++
 src/Gifitti/Gifitti/Form1.cs          |  56 ++++++++++++++
 src/Gifitti/Gifitti/Form1.resx        |   6 ++
 src/Gifitti/Gifitti/GifImage.cs       |  82 ++++++++++++++++++++
 src/Gifitti/Gifitti/GifModel.cs       | 105 ++++++++++++++++++++++++++
 src/Gifitti/Gifitti/Gifitti.csproj    |   7 ++
 src/Gifitti/Gifitti/packages.config   |   4 +
 7 files changed, 281 insertions(+)
 create mode 100644 src/Gifitti/Gifitti/GifImage.cs
 create mode 100644 src/Gifitti/Gifitti/GifModel.cs
 create mode 100644 src/Gifitti/Gifitti/packages.config

diff --git a/src/Gifitti/Gifitti/Form1.Designer.cs b/src/Gifitti/Gifitti/Form1.Designer.cs
index 2a7645f..cbc453b 100644
--- a/src/Gifitti/Gifitti/Form1.Designer.cs
+++ b/src/Gifitti/Gifitti/Form1.Designer.cs
@@ -30,6 +30,10 @@
         {
             this.button1 = new System.Windows.Forms.Button();
             this.button2 = new System.Windows.Forms.Button();
+            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
+            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
+            this.gifView = new System.Windows.Forms.PictureBox();
+            ((System.ComponentModel.ISupportInitialize)(this.gifView)).BeginInit();
             this.SuspendLayout();
             // 
             // button1
@@ -54,17 +58,31 @@
             this.button2.UseVisualStyleBackColor = true;
             this.button2.Click += new System.EventHandler(this.button2_Click);
             // 
+            // openFileDialog1
+            // 
+            this.openFileDialog1.FileName = "openFileDialog1";
+            // 
+            // gifView
+            // 
+            this.gifView.Location = new System.Drawing.Point(195, 197);
+            this.gifView.Name = "gifView";
+            this.gifView.Size = new System.Drawing.Size(100, 50);
+            this.gifView.TabIndex = 2;
+            this.gifView.TabStop = false;
+            // 
             // Form1
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(582, 553);
+            this.Controls.Add(this.gifView);
             this.Controls.Add(this.button2);
             this.Controls.Add(this.button1);
             this.Name = "Form1";
             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
             this.Text = "Form1";
             this.Load += new System.EventHandler(this.Form1_Load);
+            ((System.ComponentModel.ISupportInitialize)(this.gifView)).EndInit();
             this.ResumeLayout(false);
 
         }
@@ -73,6 +91,9 @@
 
         private System.Windows.Forms.Button button1;
         private System.Windows.Forms.Button button2;
+        private System.Windows.Forms.OpenFileDialog openFileDialog1;
+        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
+        private System.Windows.Forms.PictureBox gifView;
     }
 }
 
diff --git a/src/Gifitti/Gifitti/Form1.cs b/src/Gifitti/Gifitti/Form1.cs
index 89041a5..2ad2016 100644
--- a/src/Gifitti/Gifitti/Form1.cs
+++ b/src/Gifitti/Gifitti/Form1.cs
@@ -3,6 +3,8 @@ 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;
@@ -12,6 +14,13 @@ namespace Gifitti
 {
     public partial class Form1 : Form
     {
+        GifModel gm;
+
+        // GifImage _currentGif; <- used to encapsulate info later 
+        private const int widthBuffer = 20;
+        private const int heightBuffer = 60;
+        Image globalGif;
+
         public Form1()
         {
             InitializeComponent();
@@ -24,12 +33,59 @@ namespace Gifitti
 
         private void button1_Click(object sender, EventArgs e)
         {
+            openFileDialog1.Filter = "GIF Files|*.gif";
+            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
+            if (result == DialogResult.OK) // Test result.
+            {
+                string file = openFileDialog1.FileName;
+                try
+                {
+
+
+                    gm = new GifModel(file);
+                    Image loadedGif = gm.gifImage.gifImage;
+                    globalGif = loadedGif;
+                    gifView.Image = loadedGif;
+
+                    gifView.Width = loadedGif.Width;
+                    gifView.Height = loadedGif.Height;
+                    ClientSize = new Size(loadedGif.Width + widthBuffer, loadedGif.Height + heightBuffer);
+                }
 
+                catch (IOException)
+                {
+                }
+
+                CenterToScreen();
+
+                // Focus the gif:
+                gifView.Select();
+                gifView.Focus();
+                ActiveControl = gifView;
+            }
         }
 
         private void button2_Click(object sender, EventArgs e)
         {
+            String x = "";
+            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
+            {
+                x = folderBrowserDialog1.SelectedPath;
+            }
+
+            List<Image> IMGs = new List<Image>();
+            Image test;
+            int Length = globalGif.GetFrameCount(FrameDimension.Time);
+
+            for (int i = 0; i < Length; i++)
+            {
+                globalGif.SelectActiveFrame(FrameDimension.Time, i);
+                test = new Bitmap(globalGif);
+                String y = "\\tmp-" + i + ".bmp";
+                String xy = x + y;
+                test.Save(@xy);
 
+            }
         }
     }
 }
diff --git a/src/Gifitti/Gifitti/Form1.resx b/src/Gifitti/Gifitti/Form1.resx
index 1af7de1..1e80402 100644
--- a/src/Gifitti/Gifitti/Form1.resx
+++ b/src/Gifitti/Gifitti/Form1.resx
@@ -117,4 +117,10 @@
   <resheader name="writer">
     <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
   </resheader>
+  <metadata name="openFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>17, 17</value>
+  </metadata>
+  <metadata name="folderBrowserDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>184, 17</value>
+  </metadata>
 </root>
\ No newline at end of file
diff --git a/src/Gifitti/Gifitti/GifImage.cs b/src/Gifitti/Gifitti/GifImage.cs
new file mode 100644
index 0000000..afb4125
--- /dev/null
+++ b/src/Gifitti/Gifitti/GifImage.cs
@@ -0,0 +1,82 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Gifitti
+{
+    class GifImage
+    {
+        public Image gifImage
+        {
+            get; set;
+        }
+        private FrameDimension dimension;
+        private int frameCount;
+        private int currentFrame = -1;
+        private bool reverse;
+        private int step = 1;
+
+        public GifImage(string path)
+        {
+            gifImage = Image.FromFile(path);
+            //initialize
+            dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
+            //gets the GUID
+            //total frames in the animation
+            frameCount = gifImage.GetFrameCount(dimension);
+        }
+
+        private void modifyGifImage(Image gif)
+        {
+            gifImage = gif;
+            //initialize
+            dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
+            //gets the GUID
+            //total frames in the animation
+            frameCount = gifImage.GetFrameCount(dimension);
+        }
+
+        public bool ReverseAtEnd
+        {
+            //whether the gif should play backwards when it reaches the end
+            get { return reverse; }
+            set { reverse = value; }
+        }
+
+        public Image GetNextFrame()
+        {
+
+            currentFrame += step;
+
+            //if the animation reaches a boundary...
+            if (currentFrame >= frameCount || currentFrame < 1)
+            {
+                if (reverse)
+                {
+                    step *= -1;
+                    //...reverse the count
+                    //apply it
+                    currentFrame += step;
+                }
+                else
+                {
+                    currentFrame = 0;
+                    //...or start over
+                }
+            }
+            return GetFrame(currentFrame);
+        }
+
+        public Image GetFrame(int index)
+        {
+            gifImage.SelectActiveFrame(dimension, index);
+            //find the frame
+            return (Image)gifImage.Clone();
+            //return a copy of it
+        }
+    }
+}
diff --git a/src/Gifitti/Gifitti/GifModel.cs b/src/Gifitti/Gifitti/GifModel.cs
new file mode 100644
index 0000000..900c82e
--- /dev/null
+++ b/src/Gifitti/Gifitti/GifModel.cs
@@ -0,0 +1,105 @@
+using ImageMagick;
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Gifitti
+{
+    class GifModel
+    {
+        public GifImage gifImage { get; private set; }
+        public Image originalGif { get; private set; }
+        private int numberOfFrames;
+        private Image[] frames;
+
+        //Build a Gif Model instace
+        //Re create object when a new gif is loaded
+        //Keep when working with same gif
+        public GifModel(GifImage gifImage)
+        {
+            this.gifImage = gifImage;
+            //Save the original Gif
+            originalGif = this.gifImage.gifImage.Clone() as Image;
+            frameConstruction(this.gifImage);
+        }
+
+        public GifModel(String path)
+        {
+            this.gifImage = new GifImage(path);
+            //Save the original Gif
+            originalGif = this.gifImage.gifImage.Clone() as Image;
+            frameConstruction(this.gifImage);
+        }
+
+        //Construct Frames for the entire Image
+        private void frameConstruction(GifImage gifImage)
+        {
+            numberOfFrames = gifImage.gifImage.GetFrameCount(FrameDimension.Time);
+            frames = new Image[numberOfFrames];
+
+            for (int i = 0; i < numberOfFrames; i++)
+            {
+                originalGif.SelectActiveFrame(FrameDimension.Time, i);
+                frames[i] = originalGif.Clone() as Image;
+            }
+        }
+
+        public void resetToOriginalGif()
+        {
+            gifImage.gifImage = originalGif;
+        }
+
+
+        public void resizeGif(int newWidth, int newHeight)
+        {
+            using (MagickImageCollection collection = new MagickImageCollection())
+            {
+                for (int i = 0; i < frames.Length; i++)
+                {
+                    collection.Add(new MagickImage(frames[i] as Bitmap));
+                    collection[i].Resize(newWidth, newHeight);
+                }
+                // Optionally reduce colors
+                QuantizeSettings settings = new QuantizeSettings();
+                settings.Colors = 256;
+                collection.Quantize(settings);
+
+                // Optionally optimize the images (images should have the same size).
+                collection.Optimize();
+
+                // Save gif
+                collection.Write("tempResize.gif");
+            }
+
+
+
+        }
+
+        public void returnSubGif(int start, int stop)
+        {
+            using (MagickImageCollection collection = new MagickImageCollection())
+            {
+                // Add first image and set the animation delay to 100ms
+                for (int i = start; i < stop; i++)
+                {
+                    collection.Add(new ImageMagick.MagickImage(frames[i] as Bitmap));
+                    collection[i - start].AnimationDelay = 1;
+                }
+                // Optionally reduce colors
+                QuantizeSettings settings = new QuantizeSettings();
+                settings.Colors = 256;
+                collection.Quantize(settings);
+
+                // Optionally optimize the images (images should have the same size).
+                collection.Optimize();
+
+                // Save gif
+                collection.Write("temp.gif");
+            }
+        }
+    }
+}
diff --git a/src/Gifitti/Gifitti/Gifitti.csproj b/src/Gifitti/Gifitti/Gifitti.csproj
index b59a4c3..70d710d 100644
--- a/src/Gifitti/Gifitti/Gifitti.csproj
+++ b/src/Gifitti/Gifitti/Gifitti.csproj
@@ -33,6 +33,10 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Magick.NET-Q16-AnyCPU, Version=7.0.0.0, Culture=neutral, PublicKeyToken=2004825badfa91ec, processorArchitecture=MSIL">
+      <HintPath>..\packages\Magick.NET-Q16-AnyCPU.7.0.3.502\lib\net40-client\Magick.NET-Q16-AnyCPU.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Xml.Linq" />
@@ -52,6 +56,8 @@
     <Compile Include="Form1.Designer.cs">
       <DependentUpon>Form1.cs</DependentUpon>
     </Compile>
+    <Compile Include="GifImage.cs" />
+    <Compile Include="GifModel.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <EmbeddedResource Include="Form1.resx">
@@ -66,6 +72,7 @@
       <AutoGen>True</AutoGen>
       <DependentUpon>Resources.resx</DependentUpon>
     </Compile>
+    <None Include="packages.config" />
     <None Include="Properties\Settings.settings">
       <Generator>SettingsSingleFileGenerator</Generator>
       <LastGenOutput>Settings.Designer.cs</LastGenOutput>
diff --git a/src/Gifitti/Gifitti/packages.config b/src/Gifitti/Gifitti/packages.config
new file mode 100644
index 0000000..cec5b1c
--- /dev/null
+++ b/src/Gifitti/Gifitti/packages.config
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="Magick.NET-Q16-AnyCPU" version="7.0.3.502" targetFramework="net452" />
+</packages>
\ No newline at end of file
-- 
GitLab