Merge pull request #5327 from marek-safar/corefx-bump
[mono.git] / mcs / class / System.Drawing / System.Drawing / ImageAnimator.cs
1 //
2 // System.Drawing.ImageAnimator.cs
3 //
4 // Authors:
5 //      Dennis Hayes (dennish@Raytek.com)
6 //      Sanjay Gupta (gsanjay@novell.com)
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // (C) 2002 Ximian, Inc
10 // Copyright (C) 2004,2006-2007 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Collections;
33 using System.Drawing.Imaging;
34 using System.Threading;
35
36 namespace System.Drawing {
37
38         class AnimateEventArgs : EventArgs {
39
40                 private int frameCount;
41                 private int activeFrame;
42                 private Thread thread;
43
44                 public AnimateEventArgs (Image image)
45                 {
46                         frameCount = image.GetFrameCount (FrameDimension.Time);
47                 }
48       
49                 public Thread RunThread {
50                         get { return thread; }
51                         set { thread = value; }
52                 }
53
54                 public int GetNextFrame ()
55                 {
56                         if (activeFrame < frameCount - 1)
57                                 activeFrame++;
58                         else
59                                 activeFrame = 0;
60
61                         return activeFrame;
62                 }
63         }
64
65         public sealed class ImageAnimator {
66
67                 static Hashtable ht = Hashtable.Synchronized (new Hashtable ());
68
69                 private ImageAnimator ()
70                 {
71                 }
72
73                 public static void Animate (Image image, EventHandler onFrameChangedHandler)
74                 {
75                         // must be non-null and contain animation time frames
76                         if (!CanAnimate (image))
77                                 return;
78
79                         // is animation already in progress ?
80                         if (ht.ContainsKey (image))
81                                 return;
82
83                         PropertyItem item = image.GetPropertyItem (0x5100); // FrameDelay in libgdiplus
84                         byte[] value = item.Value;
85                         int[] delay = new int [(value.Length >> 2)];
86                         for (int i=0, n=0; i < value.Length; i += 4, n++) {
87                                 int d = BitConverter.ToInt32 (value, i) * 10;
88                                 // follow worse case (Opera) see http://news.deviantart.com/article/27613/
89                                 delay [n] = d < 100 ? 100 : d;
90                         }
91
92                         AnimateEventArgs aea = new AnimateEventArgs (image);
93                         WorkerThread wt = new WorkerThread (onFrameChangedHandler, aea, delay);
94                         Thread thread = new Thread (new ThreadStart (wt.LoopHandler));
95                         thread.IsBackground = true;
96                         aea.RunThread = thread;
97                         ht.Add (image, aea);
98                         thread.Start ();
99                 }
100
101                 public static bool CanAnimate (Image image)
102                 {
103                         if (image == null)
104                                 return false;
105
106                         int n = image.FrameDimensionsList.Length;
107                         if (n < 1)
108                                 return false;
109
110                         for (int i = 0; i < n; i++) {
111                                 if (image.FrameDimensionsList [i].Equals (FrameDimension.Time.Guid)) {
112                                         return (image.GetFrameCount (FrameDimension.Time) > 1);
113                                 }
114                         }
115                         return false;
116                 }
117
118                 public static void StopAnimate (Image image, EventHandler onFrameChangedHandler)
119                 {
120                         if (image == null)
121                                 return;
122
123                         if (ht.ContainsKey (image)) {
124                                 AnimateEventArgs evtArgs = (AnimateEventArgs) ht [image];
125                                 evtArgs.RunThread.Abort ();
126                                 ht.Remove (image);
127                         }
128                 }
129
130                 public static void UpdateFrames ()
131                 {
132                         foreach (Image image in ht.Keys)
133                                 UpdateImageFrame (image);
134                 }
135
136
137                 public static void UpdateFrames (Image image)
138                 {
139                         if (image == null)
140                                 return;
141
142                         if (ht.ContainsKey (image))
143                                 UpdateImageFrame (image);
144                 }
145
146                 // this method avoid checks that aren't requied for UpdateFrames()
147                 private static void UpdateImageFrame (Image image)
148                 {
149                         AnimateEventArgs aea = (AnimateEventArgs) ht [image];
150                         image.SelectActiveFrame (FrameDimension.Time, aea.GetNextFrame ());
151                 }
152         }
153
154         class WorkerThread {
155
156                 private EventHandler frameChangeHandler;
157                 private AnimateEventArgs animateEventArgs;
158                 private int[] delay;
159
160                 public WorkerThread (EventHandler frmChgHandler, AnimateEventArgs aniEvtArgs, int[] delay)
161                 {
162                         frameChangeHandler = frmChgHandler;
163                         animateEventArgs = aniEvtArgs;
164                         this.delay = delay;
165                 }
166
167                 public void LoopHandler ()
168                 {
169                         try {
170                                 int n = 0;
171                                 while (true) {
172                                         Thread.Sleep (delay [n++]);
173                                         frameChangeHandler (null, animateEventArgs);
174                                         if (n == delay.Length)
175                                                 n = 0;
176                                 }
177                         }
178                         catch (ThreadAbortException) {
179                                 Thread.ResetAbort (); // we're going to finish anyway
180                         }
181                 }
182         }
183 }