Skip to content

Commit 036594b

Browse files
committed
(#451) Adjusted usages of Image class to provide required new data, added type predicate to highlight and grabRegion
1 parent bd82f33 commit 036594b

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

lib/imageResources.function.ts

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export async function fetchFromUrl(url: string | URL): Promise<Image> {
3838
image.bitmap.data,
3939
4,
4040
imageUrl.href,
41+
image.bitmap.data.length / (image.bitmap.width * image.bitmap.height),
42+
image.bitmap.data.length / image.bitmap.height,
4143
ColorMode.RGB
4244
);
4345
})

lib/provider/io/jimp-image-reader.class.ts

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export default class implements ImageReader {
2727
jimpImage.bitmap.data,
2828
jimpImage.hasAlpha() ? 4 : 3,
2929
parameters,
30+
jimpImage.bitmap.data.length /
31+
(jimpImage.bitmap.width * jimpImage.bitmap.height),
32+
jimpImage.bitmap.data.length / jimpImage.bitmap.height,
3033
ColorMode.BGR
3134
)
3235
);

lib/provider/native/libnut-screen.class.ts

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export default class ScreenAction implements ScreenProviderInterface {
3333
screenShot.image,
3434
4,
3535
"grabScreenResult",
36+
screenShot.bitsPerPixel,
37+
screenShot.byteWidth,
3638
ColorMode.BGR,
3739
pixelScaling
3840
)
@@ -63,6 +65,8 @@ export default class ScreenAction implements ScreenProviderInterface {
6365
screenShot.image,
6466
4,
6567
"grabScreenRegionResult",
68+
4,
69+
screenShot.width * 4,
6670
ColorMode.BGR,
6771
pixelScaling
6872
)

lib/screen.class.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export interface ScreenConfig {
7474
resourceDirectory: string;
7575
}
7676

77+
export type FindInput = Image;
78+
7779
/**
7880
* {@link ScreenClass} class provides methods to access screen content of a systems main display
7981
*/
@@ -119,11 +121,11 @@ export class ScreenClass {
119121

120122
/**
121123
* {@link find} will search for a single occurrence of a template image on a systems main screen
122-
* @param template Template {@link Image} instance
124+
* @param searchInput Template {@link Image} instance
123125
* @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence
124126
*/
125127
public async find(
126-
template: Image | Promise<Image>,
128+
searchInput: FindInput | Promise<FindInput>,
127129
params?: OptionalSearchParameters
128130
): Promise<Region> {
129131
const {
@@ -134,7 +136,7 @@ export class ScreenClass {
134136
searchMultipleScales,
135137
} = await this.getFindParameters(params);
136138

137-
const needle = await ScreenClass.getNeedle(template);
139+
const needle = await ScreenClass.getNeedle(searchInput);
138140

139141
const matchRequest = new MatchRequest(
140142
screenImage,
@@ -236,6 +238,13 @@ export class ScreenClass {
236238
regionToHighlight: Region | Promise<Region>
237239
): Promise<Region> {
238240
const highlightRegion = await regionToHighlight;
241+
if (!isRegion(highlightRegion)) {
242+
throw Error(
243+
`highlight requires an Region, but received ${JSON.stringify(
244+
highlightRegion
245+
)}`
246+
);
247+
}
239248
await this.providerRegistry
240249
.getScreen()
241250
.highlightScreenRegion(
@@ -380,9 +389,15 @@ export class ScreenClass {
380389
public async grabRegion(
381390
regionToGrab: Region | Promise<Region>
382391
): Promise<Image> {
383-
return this.providerRegistry
384-
.getScreen()
385-
.grabScreenRegion(await regionToGrab);
392+
const targetRegion = await regionToGrab;
393+
if (!isRegion(targetRegion)) {
394+
throw Error(
395+
`grabRegion requires an Region, but received ${JSON.stringify(
396+
targetRegion
397+
)}`
398+
);
399+
}
400+
return this.providerRegistry.getScreen().grabScreenRegion(targetRegion);
386401
}
387402

388403
/**

0 commit comments

Comments
 (0)