Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DrawingTest.cs « DrawingTestHelper « DrawingTest « Test « System.Drawing « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8f9baacf2be8d679ab06113b276d2c3e775c4340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
using System;
using System.Drawing;
using System.Diagnostics;
using System.IO;
using System.Text;
using Exocortex.DSP;
using System.Reflection;
using System.Xml.Serialization;
using System.Collections;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;

#if MONOTOUCH
#else
#if TARGET_JVM
using awt = java.awt;
using javax.imageio;
using java.lang;
using java.security;
using java.awt.image;
#else
using System.Windows.Forms;
#endif
#endif

using NUnit.Framework;

namespace DrawingTestHelper
{
	#region Results serialization classes
	public sealed class ExpectedResult {
		public ExpectedResult(){}
		public ExpectedResult(string testName, double norm) {
			TestName = testName;
			Norm = norm;
		}
		public string TestName;
		public double Norm;
	}

	public sealed class ExpectedResults {
		[XmlArrayItem(typeof(ExpectedResult))]
		public ArrayList Tests = new ArrayList();
	}

	public sealed class ExpectedResultsHash {
		Hashtable _hash;
		ExpectedResults _suite;

		public ExpectedResultsHash () {
			try {
				using (StreamReader s = new StreamReader (FileName)) {
					_suite = (ExpectedResults)TestSuiteSerializer.Deserialize(s);
				}
			}
			catch {
				_suite = new ExpectedResults ();
			}
			_hash = new Hashtable(_suite.Tests.Count);
			foreach (ExpectedResult res in _suite.Tests)
				_hash[res.TestName] = res.Norm;
		}

		public const string FileName = "ExpectedResults.xml";
		public readonly static XmlSerializer TestSuiteSerializer = new XmlSerializer(typeof(ExpectedResults));

		public double GetNorm(string testName) {
			object res = _hash[testName];
			if (res != null)
				return (double)res;
			return double.NaN;
		}

		public void WriteNorm (string testName, double myNorm) {
			if (_hash.Contains (testName)) {
				for (int i = 0; i < _suite.Tests.Count; i++) {
					ExpectedResult cur = (ExpectedResult) _suite.Tests[i];
					if (cur.TestName == testName) {
						cur.Norm = myNorm;
						break;
					}
				}
			}
			else
				_suite.Tests.Add(new ExpectedResult(testName, myNorm));

			_hash[testName] = myNorm;
			using(StreamWriter w = new StreamWriter(FileName))
				TestSuiteSerializer.Serialize(w, _suite);
		}
	}

	public sealed class CachedResult {
		public CachedResult (){}
		public CachedResult (string testName, string sha1, double norm) {
			TestName = testName;
			SHA1 = sha1;
			Norm = norm;
			DateTime = DateTime.Now;
		}

		public string TestName;
		public string SHA1;
		public double Norm;
		public DateTime DateTime;
	}

	public sealed class CachedResults {
		[XmlArrayItem(typeof(CachedResult))]
		public ArrayList Tests = new ArrayList();
	}

	public class Cache {
		Hashtable _hash;
		CachedResults _results;

#if TARGET_JVM
		public const string FileName = "CachedResults.xml";
		public const string NewFileName = "NewCachedResults.xml";
#else
		public const string FileName = "dotnet.CachedResults.xml";
		public const string NewFileName = "dotnet.NewCachedResults.xml";
#endif
		public readonly static XmlSerializer TestSuiteSerializer =
			new XmlSerializer(typeof(CachedResults));

		public Cache () {
			try {
				using (StreamReader r = new StreamReader(FileName))
					_results = (CachedResults)TestSuiteSerializer.Deserialize(r);
			}
			catch {
				_results = new CachedResults ();
			}
			
			_hash = new Hashtable(_results.Tests.Count);
			foreach (CachedResult res in _results.Tests)
				_hash[res.SHA1] = res.Norm;
		}

		public double GetNorm (string sha1) {
			if (_hash.ContainsKey (sha1))
				return (double)_hash[sha1];
			else
				return double.NaN;
		}

		public void Add (string testName, string sha1, double norm) {
			if (_hash.ContainsKey (sha1))
				throw new ArgumentException ("This SHA1 is already in the cache", "sha1");

			_results.Tests.Add (new CachedResult(testName, sha1, norm));
			_hash.Add (sha1, norm);

			using(StreamWriter w = new StreamWriter(NewFileName))
				TestSuiteSerializer.Serialize(w, _results);
		}
	}
	#endregion

	/// <summary>
	/// Summary description for DrawingTest.
	/// </summary>
	public abstract class DrawingTest : IDisposable {

		public const float DEFAULT_FLOAT_TOLERANCE = 1e-5f; 
		public const int DEFAULT_IMAGE_TOLERANCE = 2; 

		Graphics _graphics;
		protected Bitmap _bitmap;
		static string _callingFunction;
		//static int _counter;
		static Hashtable _mpFuncCount = new Hashtable();
		static bool _showForms = false;
		static bool _createResults = true;
		protected string _ownerClass = "";
		protected Hashtable _specialTolerance = null;

		protected readonly static ExpectedResultsHash ExpectedResults = new ExpectedResultsHash ();
		protected readonly static Cache cache = new Cache ();

		public Graphics Graphics {get {return _graphics;}}
		public Bitmap Bitmap {get { return _bitmap; }}

		public Hashtable SpecialTolerance 
		{
			get {return _specialTolerance;}
			set {_specialTolerance = value;}
		}

		public string OwnerClass 
		{
			get {return _ownerClass;}
			set {_ownerClass = value;}
		}

		public static bool ShowForms 
		{
			get {return _showForms;}
			set {_showForms = value;}
		}

		public static bool CreateResults {
			get {return _createResults;}
			set {_createResults = value;}
		}

		protected DrawingTest() {}
		
		private void Init (int width, int height) {
			Init (new Bitmap (width, height));
		}

		private void Init (Bitmap bitmap) {
			_bitmap = bitmap;
			_graphics = Graphics.FromImage (_bitmap);
		}

		protected abstract string DetermineCallingFunction ();

		protected interface IMyForm {
			void Show ();
		}

		protected abstract IMyForm CreateForm (string title);

		public void Show () {
			CheckCounter ();
			if (!ShowForms)
				return;
			IMyForm form = CreateForm(_callingFunction + _mpFuncCount[_callingFunction]);
			form.Show ();
		}
	
		static protected string TestName {
			get {
				return _callingFunction + ":" + _mpFuncCount[_callingFunction]/* + ".dat"*/;
			}
		}

		#region GetImageFFTArray
		private static ComplexF[] GetImageFFTArray(Bitmap bitmap) {
			float scale = 1F / (float) System.Math.Sqrt(bitmap.Width * bitmap.Height);
			ComplexF[] data = new ComplexF [bitmap.Width * bitmap.Height * 4];

			int offset = 0;
			for( int y = 0; y < bitmap.Height; y ++ )
				for( int x = 0; x < bitmap.Width; x ++ ) {
					Color c = bitmap.GetPixel (x, y);
					float s = 1F;
					if( (( x + y ) & 0x1 ) != 0 ) {
						s = -1F;
					}

					data [offset++] = new ComplexF( c.A * s / 256F, 0);
					data [offset++] = new ComplexF( c.R * s / -256F, 0);
					data [offset++] = new ComplexF( c.G * s / 256F, 0);
					data [offset++] = new ComplexF( c.B * s / -256F, 0);
				}
			

			Fourier.FFT3( data, 4, bitmap.Width, bitmap.Height, FourierDirection.Forward );
			
			for( int i = 0; i < data.Length; i ++ ) {
				data[i] *= scale;
			}

			return data;
		}
		#endregion

		abstract public string CalculateSHA1 ();
		
		public static double CalculateNorm (Bitmap bitmap) {
			ComplexF[] matrix = GetImageFFTArray(bitmap);

			double norm = 0;
			int size_x = 4; //ARGB values
			int size_y = bitmap.Width;
			int size_z = bitmap.Height;
			for (int x=1; x<=size_x; x++) {
				double norm_y = 0;
				for (int y=1; y<=size_y; y++) {
					double norm_z = 0;
					for (int z=1; z<=size_z; z++) {
						ComplexF cur = matrix[(size_x-x)+size_x*(size_y-y)+size_x*size_y*(size_z-z)];
						norm_z += cur.GetModulusSquared ();// * z;
					}
					norm_y += norm_z;// * y;
				}
				norm += norm_y;// * x;
			}
			return norm;
		}

		public double GetNorm () {
			string sha1 = CalculateSHA1 ();

			double norm = cache.GetNorm (sha1);
			if (double.IsNaN (norm)) {
				norm = CalculateNorm (_bitmap);
				cache.Add (TestName, sha1, norm);
				//_bitmap.Save(TestName.Replace(":", "_"));
			}
			return norm;
		}

		protected abstract double GetExpectedNorm (double myNorm);

		private void CheckCounter () {
			string callFunc = DetermineCallingFunction ();
			_callingFunction = callFunc;
			if (!_mpFuncCount.Contains(_callingFunction)) {
				
				_mpFuncCount[_callingFunction] = 1;
			}
			else {
				int counter = (int)_mpFuncCount[_callingFunction];
				counter ++;
				_mpFuncCount[_callingFunction] = counter;
			}
		}

		public static void AssertAlmostEqual (float expected, float actual)
		{
			AssertAlmostEqual (expected, actual, DEFAULT_FLOAT_TOLERANCE);
		}
		
		public static void AssertAlmostEqual (float expected, float actual, float tolerance)
		{
			string msg = String.Format("\nExpected : {0} \nActual : {1}",expected.ToString(),actual.ToString());
			AssertAlmostEqual (expected, actual, tolerance, msg);
		}

		private static void AssertAlmostEqual (float expected, float actual, float tolerance, string message)
		{
			float error = System.Math.Abs ((expected - actual) / (expected + actual + float.Epsilon));
			Assert.That (error < tolerance, Is.True, message);
		}

		public static void AssertAlmostEqual (PointF expected, PointF actual)
		{
			string msg = String.Format("\nExpected : {0} \n  Actual : {1}",expected.ToString(),actual.ToString());
			AssertAlmostEqual (expected.X, actual.X, DEFAULT_FLOAT_TOLERANCE, msg);
			AssertAlmostEqual (expected.Y, actual.Y, DEFAULT_FLOAT_TOLERANCE, msg);
		}

		/// <summary>
		/// Checks that the given bitmap norm is similar to expected
		/// </summary>
		/// <param name="tolerance">tolerance in percents (0..100)</param>
		/// <returns></returns>
		/// 
		public bool Compare (double tolerance) {
			CheckCounter ();

			double error = CompareToExpectedInternal()*100;

			if (SpecialTolerance != null)
				return error <= GetSpecialTolerance(TestName);

			return error <= tolerance;
		}

		public bool PDCompare (double tolerance) {
			Bitmap ri = GetReferenceImage(TestName);
			if (ri == null)
				return true;

			double error = PDComparer.Compare(ri, _bitmap);
			return error <= tolerance;
		}
		
		public bool Compare () {
			CheckCounter ();

			double error = CompareToExpectedInternal()*100;
			
			if (SpecialTolerance != null)
				return error <= GetSpecialTolerance(TestName);

			return error <= DEFAULT_IMAGE_TOLERANCE;
		}

		public bool PDCompare () {
			Bitmap ri = GetReferenceImage(TestName);
			if (ri == null)
				return true;

			double error = PDComparer.Compare(ri, _bitmap);
			return error <= DEFAULT_IMAGE_TOLERANCE;
		}

		protected abstract Bitmap GetReferenceImage(string testName);

		protected double GetSpecialTolerance(string testName) {
			try	{
				string shortTestName = testName.Substring( testName.LastIndexOf(".") + 1 );
				object o = SpecialTolerance[shortTestName];
				if (o == null)
					return DEFAULT_IMAGE_TOLERANCE;

				return Convert.ToDouble(o);
			}
			catch (System.Exception) {
				return DEFAULT_IMAGE_TOLERANCE;
			}
		}

		public void AssertCompare () {
			CheckCounter ();
			Assert.That ((CompareToExpectedInternal () * 100) < DEFAULT_IMAGE_TOLERANCE, Is.True);
		}

		public void AssertCompare (double tolerance) {
			CheckCounter ();
			Assert.That ((CompareToExpectedInternal () * 100) < tolerance, Is.True);
		}
		
		public double CompareToExpected () {
			CheckCounter ();
			return CompareToExpectedInternal ();
		}

		double CompareToExpectedInternal () {
			if (ShowForms)
				return 0;

			double norm = GetNorm ();
			double expNorm = GetExpectedNorm (norm);
			return System.Math.Abs (norm-expNorm)/(norm+expNorm+double.Epsilon);
		}

		public static DrawingTest Create (int width, int height) {
			return Create(width, height, "GraphicsFixture");
		}
		public static DrawingTest Create (int width, int height, string ownerClass) {
			DrawingTest test;
#if TARGET_JVM
			test = new JavaDrawingTest ();
#else
			test = new NetDrawingTest ();
#endif
			test.Init (width, height);
			test.OwnerClass = ownerClass;
			return test;
		}
		#region IDisposable Members

		public void Dispose()
		{
			// TODO:  Add DrawingTest.Dispose implementation
			if (_graphics != null) {
				_graphics.Dispose();
				_graphics = null;
			}
		}

		#endregion
	}

#if TARGET_JVM
	internal class JavaDrawingTest:DrawingTest {
		java.awt.image.BufferedImage _image;
		java.awt.image.BufferedImage Image {
			get {
				if (_image != null)
					return _image;
				Type imageType = typeof (Bitmap);
				PropertyInfo [] props = imageType.GetProperties (
					BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);

				PropertyInfo prop = null;
				foreach (PropertyInfo p in props) {
					if (p.Name == "NativeObject")
						if (p.PropertyType == typeof(java.awt.image.BufferedImage))
							prop = p;
				}

				MethodInfo method = prop.GetGetMethod (true);
				_image = (java.awt.image.BufferedImage) method.Invoke (_bitmap, new object [0]);
				return _image;
			}
		}

		public JavaDrawingTest () {}

		protected override double GetExpectedNorm (double myNorm) {
			return ExpectedResults.GetNorm(TestName);
		}

		protected override Bitmap GetReferenceImage(string testName) {
			try{
				string dotNetResultsFolder = @"";
				string fileName = dotNetResultsFolder + testName.Replace(":", "_") + ".png";
				return new Bitmap(fileName);
			}
			catch(System.Exception e) {
				throw new System.Exception("Error creating .Net reference image");
			}
		}

		private class JavaForm:java.awt.Dialog,IMyForm {
			class EventListener : java.awt.@event.WindowListener {
				#region WindowListener Members

				public void windowOpened(java.awt.@event.WindowEvent arg_0) {
					// TODO:  Add ttt.windowOpened implementation
				}

				public void windowActivated(java.awt.@event.WindowEvent arg_0) {
					// TODO:  Add ttt.windowActivated implementation
				}

				public void windowClosed(java.awt.@event.WindowEvent arg_0) {
					// TODO:  Add ttt.windowClosed implementation
				}

				public void windowDeiconified(java.awt.@event.WindowEvent arg_0) {
					// TODO:  Add ttt.windowDeiconified implementation
				}

				public void windowIconified(java.awt.@event.WindowEvent arg_0) {
					// TODO:  Add ttt.windowIconified implementation
				}

				public void windowClosing(java.awt.@event.WindowEvent arg_0) {
					// TODO:  Add ttt.windowClosing implementation
					java.awt.Window w = arg_0.getWindow();
					java.awt.Window par = w.getOwner ();
					w.dispose();
					par.dispose ();
				}

				public void windowDeactivated(java.awt.@event.WindowEvent arg_0) {
					// TODO:  Add ttt.windowDeactivated implementation
				}

				#endregion
			}

			java.awt.Image _image;
			Size _s;

			public JavaForm (string title, java.awt.Image anImage, Size s)
				: base(new java.awt.Frame(), title, true) {
				_image = anImage;
				_s = s;
				
				addWindowListener(new EventListener());
			}
			public override void paint (java.awt.Graphics g) {
				base.paint (g);
				awt.Insets insets = this.getInsets ();
				g.drawImage (_image, insets.left, insets.top, null);
			}
			void IMyForm.Show () {
				awt.Insets insets = this.getInsets ();
				base.setSize (_s.Width + insets.left + insets.right,
					_s.Width + insets.top + insets.bottom);
				this.show ();
				//save the image
				//ImageIO.write((java.awt.image.RenderedImage)_image, "png", new java.io.File("test.java.png"));
			}
		}

		protected override IMyForm CreateForm(string title) {
			return new JavaForm (title, Image, _bitmap.Size);
		}
		
		protected override string DetermineCallingFunction() {
			System.Exception e = new System.Exception ();
			java.lang.Class c = vmw.common.TypeUtils.ToClass (e);
			java.lang.reflect.Method m = c.getMethod ("getStackTrace",
				new java.lang.Class [0]);
			java.lang.StackTraceElement [] els = (java.lang.StackTraceElement [])
				m.invoke (e, new object [0]);
			java.lang.StackTraceElement el = els [4];
			return el.getClassName () + "." + _ownerClass + "." + el.getMethodName ();
		}

		public override string CalculateSHA1() {
			MessageDigest md = MessageDigest.getInstance ("SHA");
			DataBufferInt dbi = (DataBufferInt) Image.getRaster ().getDataBuffer ();
			for (int i=0; i<dbi.getNumBanks (); i++) {
				int [] curBank = dbi.getData (i);
				for (int j=0; j<curBank.Length; j++) {
					int x = curBank[j];
					md.update ((sbyte) (x & 0xFF));
					md.update ((sbyte) ((x>>8) & 0xFF));
					md.update ((sbyte) ((x>>16) & 0xFF));
					md.update ((sbyte) ((x>>24) & 0xFF));
				}
			}
			byte [] resdata = (byte[])vmw.common.TypeUtils.ToByteArray(md.digest());
			return Convert.ToBase64String (resdata);
		}
	}
#else
	internal class NetDrawingTest:DrawingTest {
		public NetDrawingTest () {}

		protected override double GetExpectedNorm (double myNorm) {
			if (CreateResults)
				ExpectedResults.WriteNorm (TestName, myNorm);

			return myNorm;
		}

		protected override Bitmap GetReferenceImage(string testName) {
			string fileName = testName.Replace(":", "_") + ".png";
			try{
				if (true){
					return new Bitmap("/Developer/MonoTouch/Source/mono/mcs/class/System.Drawing/Test/DrawingTest/Test/PNGs/" + fileName);
				} else {
					_bitmap.Save( fileName );
					GC.Collect();
				}
				return null;
			}
			catch(System.Exception e) {
				throw new System.Exception("Error loading .Net reference image: " + fileName);
			}
		}
		
#if MONOTOUCH
		private class NetForm:MonoTouch.UIKit.UIViewController,IMyForm {
			Image image;
			public NetForm(string title, Image anImage):base() {
				//base.Text = title;		
				image = anImage;
			}
			void IMyForm.Show () {
				this.image.Save("test.net.png");
			}
		}
#else
		private class NetForm:Form,IMyForm {
			Image image;
			public NetForm(string title, Image anImage):base() {
				base.Text = title;
				image = anImage;
			}
			protected override void OnPaint(PaintEventArgs e) {
				e.Graphics.DrawImageUnscaled (image, 0, 0);
			}
			void IMyForm.Show () {
				this.Size = image.Size;
				this.ShowDialog ();
				this.image.Save("test.net.png");
			}
		}
#endif
		protected override IMyForm CreateForm(string title) {
			return new NetForm (title, _bitmap);
		}

		protected override string DetermineCallingFunction() {
			StackFrame sf = new StackFrame (3, true);
			MethodBase mb = sf.GetMethod ();

			string name = mb.DeclaringType.FullName + "." + _ownerClass + "." + mb.Name;
			return name;
		}

		public override string CalculateSHA1() {
			Rectangle r = new Rectangle(0, 0, _bitmap.Width, _bitmap.Height);
			
			BitmapData data = _bitmap.LockBits (r, ImageLockMode.ReadOnly,
				_bitmap.PixelFormat);
			int dataSize = data.Stride * data.Height;
			byte [] bdata = new byte [dataSize];
			Marshal.Copy (data.Scan0, bdata, 0, dataSize);
			_bitmap.UnlockBits (data);

			SHA1 sha1 = new SHA1CryptoServiceProvider ();
			byte [] resdata = sha1.ComputeHash (bdata);
			return Convert.ToBase64String (resdata);
		}

	}
#endif

}