Skip to content

Commit 8b9083e

Browse files
committed
Implemented additional checks in FlatImage.FindBoundingsOfInnerImage() function, added test
1 parent 5131d22 commit 8b9083e

3 files changed

Lines changed: 69 additions & 19 deletions

File tree

Src/ScreenGrid.Models.Tests/FlatImageTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public void GetDerivativeTest()
4141
Assert.Greater(dMiddle[12], 0);
4242
}
4343

44-
4544
[Test]
4645
public void FindBordersTest()
4746
{
@@ -61,6 +60,19 @@ public void FindBordersTest()
6160
}
6261
}
6362

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+
6476
[Test]
6577
public void FindZeroSegmentsTest1()
6678
{

Src/ScreenGrid.Models/FlatImage.cs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public static IList<Tuple<int, int>> IntersectionOfSegments(IEnumerable<IList<Tu
161161
sum.AddRange(line);
162162
}
163163
else
164-
{
164+
{
165165
// Perform intersection checks
166166
var newSum = new List<Tuple<int, int>>();
167167
foreach (var segmentA in sum)
@@ -211,25 +211,60 @@ public static Tuple<int, int> SegmentsWithMaxDistance(IList<Tuple<int, int>> seg
211211
return new Tuple<int, int>(i1, i2);
212212
}
213213

214+
private IList<Tuple<int, int>> FindHorizontalZeroSegments(int iy, int minimalSegmentLength)
215+
{
216+
return FlatImage.FindZeroSegments(FlatImage.GetDerivative(this.GetHorizontalStripe(iy)), minimalSegmentLength);
217+
}
218+
219+
private IList<Tuple<int, int>> FindVerticalZeroSegments(int ix, int minimalSegmentLength)
220+
{
221+
return FlatImage.FindZeroSegments(FlatImage.GetDerivative(this.GetVerticalStripe(ix)), minimalSegmentLength);
222+
}
223+
214224
public Models.Geometry.Rectangle FindBoundingsOfInnerImage()
215225
{
216226
// TODO: add tests for this method
217227
const int minimalSegmentLength = 8;
218228

219-
// TODO: Very simple method, need to perform more checks and do more searches
220229
var stripeCH = FlatImage.FindZeroSegments(FlatImage.GetDerivative(this.GetHorizontalStripe(this.Height / 2)), minimalSegmentLength);
221230
var stripeCV = FlatImage.FindZeroSegments(FlatImage.GetDerivative(this.GetVerticalStripe(this.Width / 2)), minimalSegmentLength);
222231

223-
var maxH = FlatImage.SegmentsWithMaxDistance(stripeCH);
224-
var maxV = FlatImage.SegmentsWithMaxDistance(stripeCV);
232+
// TODO: optimize code
233+
for (var iy = 1 * this.Height / 4; iy < 3 * this.Height / 4; iy += minimalSegmentLength)
234+
{
235+
stripeCH = FlatImage.IntersectionOfSegments(new[]
236+
{
237+
stripeCH,
238+
FindHorizontalZeroSegments(iy, minimalSegmentLength)
239+
});
240+
}
241+
242+
for (var ix = 1 * this.Width / 4; ix < 3 * this.Width / 4; ix += minimalSegmentLength)
243+
{
244+
stripeCV = FlatImage.IntersectionOfSegments(new[]
245+
{
246+
stripeCV,
247+
FindVerticalZeroSegments(ix, minimalSegmentLength)
248+
});
249+
}
225250

226-
return new Geometry.Rectangle
251+
if ((stripeCH.Count > 1) && (stripeCV.Count > 1))
227252
{
228-
X = maxH.Item1 + 1,
229-
Y = maxV.Item1 + 1,
230-
Width = maxH.Item2 - maxH.Item1 - 2,
231-
Height = maxV.Item2 - maxV.Item1 - 2,
232-
};
253+
var maxH = FlatImage.SegmentsWithMaxDistance(stripeCH);
254+
var maxV = FlatImage.SegmentsWithMaxDistance(stripeCV);
255+
256+
return new Geometry.Rectangle
257+
{
258+
X = maxH.Item1 + 1,
259+
Y = maxV.Item1 + 1,
260+
Width = maxH.Item2 - maxH.Item1 - 2,
261+
Height = maxV.Item2 - maxV.Item1 - 2,
262+
};
263+
}
264+
else
265+
{
266+
return new Geometry.Rectangle();
267+
}
233268
}
234269

235270
public bool CompareWithFragment(FlatImage fragment, int startX, int startY)
@@ -297,7 +332,7 @@ public bool CompareWithFragmentWithTolerance(FlatImage fragment, int startX, int
297332

298333
public unsafe System.Drawing.Bitmap ToBitmap()
299334
{
300-
var bitmap = new System.Drawing.Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
335+
var bitmap = new System.Drawing.Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
301336

302337
const int pixelSize = 3;
303338
var rect = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);

Src/ScreenGrid.ViewModels/ScreenGridViewModel.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,26 +298,29 @@ public void SnapToRenderView()
298298
var bitmap = nativeWindow.GetShot();
299299
var flatImage = new Models.FlatImage(bitmap);
300300

301-
Models.Geometry.Rectangle rectRenderedImage;
301+
Models.Geometry.Rectangle imageBoundings;
302302
if (Models.AppsInterop.OctaneRenderWindow.GetFromAllProcesses().Any(w => w.ClassName == nativeWindow.ClassName))
303303
{
304304
// TODO: remove this Octane Render specific code
305-
rectRenderedImage = Models.AppsInterop.OctaneRenderWindow.FindRenderedImageBorders(flatImage);
305+
imageBoundings = Models.AppsInterop.OctaneRenderWindow.FindRenderedImageBorders(flatImage);
306306
}
307307
else
308308
{
309-
rectRenderedImage = flatImage.FindBoundingsOfInnerImage();
309+
imageBoundings = flatImage.FindBoundingsOfInnerImage();
310310
}
311311

312312
var nativeWindowLocation = new Models.Geometry.Point(nativeWindow.Location.X, nativeWindow.Location.Y);
313-
return new Tuple<Models.Geometry.Rectangle, Models.Geometry.Point>(rectRenderedImage, nativeWindowLocation);
313+
return new Tuple<Models.Geometry.Rectangle, Models.Geometry.Point>(imageBoundings, nativeWindowLocation);
314314
}).ContinueWith((t) =>
315315
{
316-
var rectRenderedImage = t.Result.Item1;
316+
var imageBoundings = t.Result.Item1;
317317
var windowLocation = t.Result.Item2;
318-
if (!rectRenderedImage.IsEmpty)
318+
if (!imageBoundings.IsEmpty)
319319
{
320-
this.PositionWindow(t.Result.Item2, t.Result.Item1);
320+
if ((imageBoundings.Width > 150) && (imageBoundings.Height > 50))
321+
{
322+
this.PositionWindow(t.Result.Item2, t.Result.Item1);
323+
}
321324
}
322325
},
323326
TaskScheduler.FromCurrentSynchronizationContext());

0 commit comments

Comments
 (0)