Skip to content

Commit 899f703

Browse files
authored
(#470) Only use slice of size width*height*channels of image buffer (#491)
1 parent 0420e95 commit 899f703

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ describe("libnut screen action", () => {
3535
bytesPerPixel: 0,
3636
colorAt: jest.fn(),
3737
height: screenShotSize.height,
38-
image: new ArrayBuffer(0),
38+
image: Buffer.from(
39+
new Array(screenShotSize.width * screenShotSize.height * 4 + 10).fill(
40+
0
41+
)
42+
),
3943
width: screenShotSize.width,
4044
}));
4145
libnut.getScreenSize = jest.fn(() => ({
@@ -51,6 +55,9 @@ describe("libnut screen action", () => {
5155
expect(image.height).toEqual(screenShotSize.height);
5256
expect(image.pixelDensity.scaleX).toEqual(2);
5357
expect(image.pixelDensity.scaleY).toEqual(2);
58+
expect(image.data.length).toEqual(
59+
screenShotSize.width * screenShotSize.height * 4
60+
);
5461
expect(libnut.screen.capture).toBeCalledTimes(1);
5562
});
5663

@@ -64,7 +71,11 @@ describe("libnut screen action", () => {
6471
bytesPerPixel: 0,
6572
colorAt: jest.fn(),
6673
height: screenShotSize.height,
67-
image: new ArrayBuffer(0),
74+
image: Buffer.from(
75+
new Array(screenShotSize.width * screenShotSize.height * 4 + 10).fill(
76+
0
77+
)
78+
),
6879
width: screenShotSize.width,
6980
}));
7081

@@ -76,6 +87,9 @@ describe("libnut screen action", () => {
7687
expect(image.height).toEqual(screenShotSize.height);
7788
expect(image.pixelDensity.scaleX).toEqual(20);
7889
expect(image.pixelDensity.scaleY).toEqual(20);
90+
expect(image.data.length).toEqual(
91+
screenShotSize.width * screenShotSize.height * 4
92+
);
7993
expect(libnut.screen.capture).toBeCalledTimes(1);
8094
});
8195

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default class ScreenAction implements ScreenProviderInterface {
3030
new Image(
3131
screenShot.width,
3232
screenShot.height,
33-
screenShot.image,
33+
screenShot.image.slice(0, screenShot.width * screenShot.height * 4),
3434
4,
3535
"grabScreenResult",
3636
screenShot.bitsPerPixel,
@@ -62,11 +62,11 @@ export default class ScreenAction implements ScreenProviderInterface {
6262
new Image(
6363
screenShot.width,
6464
screenShot.height,
65-
screenShot.image,
65+
screenShot.image.slice(0, screenShot.width * screenShot.height * 4),
6666
4,
6767
"grabScreenRegionResult",
68-
4,
69-
screenShot.width * 4,
68+
screenShot.bitsPerPixel,
69+
screenShot.byteWidth,
7070
ColorMode.BGR,
7171
pixelScaling
7272
)

0 commit comments

Comments
 (0)