Skip to content

Commit aeac4e6

Browse files
committed
(#68) Moved scaling of search regions into separate file
1 parent 8fd761a commit aeac4e6

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { mockPartial } from "sneer";
2+
import { Image } from "../../image.class";
3+
import { MatchRequest } from "../../match-request.class";
4+
import { Region } from "../../region.class";
5+
import { determineScaledSearchRegion } from "./determine-searchregion.function";
6+
7+
describe("determineSearchRegion", () => {
8+
it("should return a search region adopted to pixel density", () => {
9+
// GIVEN
10+
const imageMock = mockPartial<Image>({
11+
pixelDensity: {
12+
scaleX: 1.5,
13+
scaleY: 2.0
14+
}
15+
});
16+
const needlePath = "/path/to/needle";
17+
const inputSearchRegion = new Region(0, 0, 100, 100);
18+
const expectedSearchRegion = new Region(0, 0, 150, 200);
19+
20+
const matchRequest = new MatchRequest(
21+
imageMock,
22+
needlePath,
23+
inputSearchRegion,
24+
0.99
25+
);
26+
27+
// WHEN
28+
const result = determineScaledSearchRegion(matchRequest);
29+
30+
// THEN
31+
expect(result).toEqual(expectedSearchRegion);
32+
});
33+
34+
it.each([[0, 1], [1, 0]])("should not adjust searchregion for factor 0: scaleX: %i scaleY: %i",
35+
(scaleX: number, scaleY: number) => {
36+
// GIVEN
37+
const imageMock = mockPartial<Image>({
38+
pixelDensity: {
39+
scaleX,
40+
scaleY
41+
}
42+
});
43+
const needlePath = "/path/to/needle";
44+
const inputSearchRegion = new Region(0, 0, 100, 100);
45+
const expectedSearchRegion = new Region(0, 0, 100, 100);
46+
47+
const matchRequest = new MatchRequest(
48+
imageMock,
49+
needlePath,
50+
inputSearchRegion,
51+
0.99
52+
);
53+
54+
// WHEN
55+
const result = determineScaledSearchRegion(matchRequest);
56+
57+
// THEN
58+
expect(result).toEqual(expectedSearchRegion);
59+
});
60+
});
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { MatchRequest } from "../../match-request.class";
2+
import { Region } from "../../region.class";
3+
4+
export const determineScaledSearchRegion = (matchRequest: MatchRequest): Region => {
5+
const searchRegion = matchRequest.searchRegion;
6+
const scaleX = matchRequest.haystack.pixelDensity.scaleX || 1.0;
7+
const scaleY = matchRequest.haystack.pixelDensity.scaleY || 1.0;
8+
searchRegion.width *= scaleX;
9+
searchRegion.height *= scaleY;
10+
return searchRegion;
11+
};

Diff for: package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"@types/clipboardy": "^1.1.0",
6363
"@types/jest": "^24.0.9",
6464
"jest": "^24.7.1",
65+
"sneer": "^1.0.1",
6566
"ts-jest": "^24.0.2",
6667
"tslint": "^5.16.0",
6768
"typescript": "^3.4.3",

0 commit comments

Comments
 (0)