Skip to content

Commit cec1114

Browse files
committed
Implemented universal method of searching of image in topmost window to snap.
Added NUnit tests (using Nuget package) for FlatImage class methods
1 parent 5f82b9a commit cec1114

14 files changed

Lines changed: 918 additions & 87 deletions

File tree

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ Dist/*.exe
33

44
Src/*.suo
55
Src/**/[Bb]in/
6-
Src/**/[Oo]bj/
6+
Src/**/[Oo]bj/
7+
8+
Src/**/StyleCop.Cache
9+
10+
Src/packages/
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
namespace ScreenGrid.Models.Tests
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Drawing;
6+
7+
using NUnit.Framework;
8+
9+
[TestFixture]
10+
public class FlatImageTests
11+
{
12+
[Test]
13+
public void GetHorizontalStripeTest()
14+
{
15+
const int width = 64;
16+
const int height = 48;
17+
18+
var flatImage = LoadFlatImageFromResource("ScreenGrid.Models.Tests.Resources.ImageSimple.png");
19+
20+
Assert.AreEqual(width, flatImage.Width);
21+
Assert.AreEqual(height, flatImage.Height);
22+
23+
var stripeTop = flatImage.GetHorizontalStripe(0);
24+
Assert.AreEqual(width, stripeTop.Length);
25+
26+
var stripeMiddle = flatImage.GetHorizontalStripe(height / 2);
27+
Assert.AreEqual(width, stripeMiddle.Length);
28+
}
29+
30+
[Test]
31+
public void GetDerivativeTest()
32+
{
33+
var flatImage = LoadFlatImageFromResource("ScreenGrid.Models.Tests.Resources.ImageSimple.png");
34+
var stripeMiddle = flatImage.GetHorizontalStripe(flatImage.Height / 2);
35+
36+
var dMiddle = FlatImage.GetDerivative(stripeMiddle);
37+
Assert.AreEqual(stripeMiddle.Length, dMiddle.Length);
38+
39+
// Border detection
40+
Assert.AreEqual(0, dMiddle[11]);
41+
Assert.Greater(dMiddle[12], 0);
42+
}
43+
44+
[Test]
45+
public void FindBordersTest()
46+
{
47+
var flatImage = LoadFlatImageFromResource("ScreenGrid.Models.Tests.Resources.ImageSimple.png");
48+
49+
const int minimalSegmentLength = 8;
50+
var step = flatImage.Height / 8;
51+
52+
var list = new List<Tuple<int, int>>();
53+
for (var iy = minimalSegmentLength + step; iy < flatImage.Height - (minimalSegmentLength + step); iy += step)
54+
{
55+
var stripe = flatImage.GetHorizontalStripe(iy);
56+
var derivative = FlatImage.GetDerivative(stripe);
57+
var segments = FlatImage.FindZeroSegments(derivative, minimalSegmentLength);
58+
59+
Assert.AreEqual(2, segments.Count, String.Format("iy={0}", iy));
60+
}
61+
}
62+
63+
[Test]
64+
public void FindBoundingsOfInnerImage()
65+
{
66+
var flatImage = LoadFlatImageFromResource("ScreenGrid.Models.Tests.Resources.ImageSimple.png");
67+
68+
var result = flatImage.FindBoundingsOfInnerImage();
69+
70+
Assert.AreEqual(12, result.X);
71+
Assert.AreEqual(10, result.Y);
72+
Assert.AreEqual(40, result.Width);
73+
Assert.AreEqual(28, result.Height);
74+
}
75+
76+
[Test]
77+
public void FindZeroSegmentsTest1()
78+
{
79+
var array = new UInt32[] { 1, 0, 0, 0, 0, 0, 1, 2, 1, 0, 3, 4, 0, 3, 5 };
80+
var result = FlatImage.FindZeroSegments(array, 4);
81+
Assert.AreEqual(1, result.Count);
82+
Assert.AreEqual(1, result[0].Item1);
83+
Assert.AreEqual(5, result[0].Item2);
84+
}
85+
86+
[Test]
87+
public void FindZeroSegmentsTestThreeSegments()
88+
{
89+
var array = new UInt32[] { 1, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 3, 4, 0, 0, 3, 0, 5 };
90+
var result = FlatImage.FindZeroSegments(array, 2);
91+
Assert.AreEqual(3, result.Count);
92+
Assert.AreEqual(1, result[0].Item1);
93+
Assert.AreEqual(5, result[0].Item2);
94+
Assert.AreEqual(9, result[1].Item1);
95+
Assert.AreEqual(10, result[1].Item2);
96+
Assert.AreEqual(13, result[2].Item1);
97+
Assert.AreEqual(14, result[2].Item2);
98+
}
99+
100+
[Test]
101+
public void FindZeroSegmentsTestNoSegments()
102+
{
103+
var array = new UInt32[] { 1, 0, 0, 1, 2, 3 };
104+
var result = FlatImage.FindZeroSegments(array, 3);
105+
Assert.AreEqual(0, result.Count);
106+
}
107+
108+
[Test]
109+
public void FindZeroSegmentsTestShortAllZeroSegment()
110+
{
111+
var array = new UInt32[] { 0, 0, 0, 0, };
112+
var result = FlatImage.FindZeroSegments(array, 4);
113+
Assert.AreEqual(1, result.Count);
114+
Assert.AreEqual(0, result[0].Item1);
115+
Assert.AreEqual(3, result[0].Item2);
116+
}
117+
118+
[Test]
119+
public void FindZeroSegmentsTestAllZeroSegment()
120+
{
121+
var array = new UInt32[] { 0, 0, 0, 0, 0, 0 };
122+
var result = FlatImage.FindZeroSegments(array, 3);
123+
Assert.AreEqual(1, result.Count);
124+
Assert.AreEqual(0, result[0].Item1);
125+
Assert.AreEqual(5, result[0].Item2);
126+
}
127+
128+
[Test]
129+
public void FindZeroSegmentsTestSimpleSegment1()
130+
{
131+
var array = new UInt32[] { 1, 0, 0, 0, 0, 0 };
132+
var result = FlatImage.FindZeroSegments(array, 3);
133+
Assert.AreEqual(1, result.Count);
134+
Assert.AreEqual(1, result[0].Item1);
135+
Assert.AreEqual(5, result[0].Item2);
136+
}
137+
138+
[Test]
139+
public void FindZeroSegmentsTestSimpleSegment2()
140+
{
141+
var array = new UInt32[] { 0, 0, 0, 0, 0, 1 };
142+
var result = FlatImage.FindZeroSegments(array, 3);
143+
Assert.AreEqual(1, result.Count);
144+
Assert.AreEqual(0, result[0].Item1);
145+
Assert.AreEqual(4, result[0].Item2);
146+
}
147+
148+
[Test]
149+
public void IntersectionOfSegmentsFullIntersection()
150+
{
151+
var result = FlatImage.IntersectionOfSegments(
152+
new Tuple<int, int>(1, 2),
153+
new Tuple<int, int>(1, 2));
154+
Assert.AreEqual(new Tuple<int, int>(1, 2), result);
155+
}
156+
157+
[Test]
158+
public void IntersectionOfSegmentsPartialIntersection()
159+
{
160+
var a = new Tuple<int, int>(1, 4);
161+
var b = new Tuple<int, int>(2, 8);
162+
163+
Assert.AreEqual(new Tuple<int, int>(2, 4), FlatImage.IntersectionOfSegments(a, b));
164+
Assert.AreEqual(new Tuple<int, int>(2, 4), FlatImage.IntersectionOfSegments(b, a));
165+
}
166+
167+
[Test]
168+
public void IntersectionOfSegmentsNoIntersection()
169+
{
170+
var result = FlatImage.IntersectionOfSegments(
171+
new Tuple<int, int>(1, 2),
172+
new Tuple<int, int>(4, 5));
173+
Assert.IsNull(result);
174+
}
175+
176+
[Test]
177+
public void IntersectionOfSegments()
178+
{
179+
var result = FlatImage.IntersectionOfSegments(new[]
180+
{
181+
new[] { new Tuple<int, int>(1, 2), new Tuple<int, int>(4, 6) },
182+
new[] { new Tuple<int, int>(1, 2), new Tuple<int, int>(5, 10) },
183+
});
184+
185+
Assert.AreEqual(2, result.Count);
186+
Assert.AreEqual(new Tuple<int, int>(1, 2), result[0]);
187+
Assert.AreEqual(new Tuple<int, int>(5, 6), result[1]);
188+
}
189+
190+
private static FlatImage LoadFlatImageFromResource(string resourceName)
191+
{
192+
FlatImage flatImage;
193+
194+
var imageData = ReadResource(resourceName);
195+
196+
using (var ms = new System.IO.MemoryStream(imageData))
197+
{
198+
using (var original = new Bitmap(ms))
199+
{
200+
using (var bitmap = new Bitmap(original.Width, original.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
201+
{
202+
using (var g = Graphics.FromImage(bitmap))
203+
{
204+
g.DrawImage(original, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
205+
}
206+
207+
flatImage = new FlatImage(bitmap);
208+
}
209+
}
210+
}
211+
212+
return flatImage;
213+
}
214+
215+
private static byte[] ReadResource(string resourceName)
216+
{
217+
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
218+
219+
byte[] data = null;
220+
using (var stream = assembly.GetManifestResourceStream(resourceName))
221+
{
222+
data = new byte[stream.Length];
223+
stream.Read(data, 0, (int)stream.Length);
224+
}
225+
226+
return data;
227+
}
228+
}
229+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("ScreenGrid.Models.Tests")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ScreenGrid.Models.Tests")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("5d17b4a7-87b9-4daf-91c0-8d76aabad502")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Src/ScreenGrid.Models.Tests/Properties/Resources.Designer.cs

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)