Skip to content

Commit e501a14

Browse files
authored
fix: Ignore area incorrectly calculated if goes over image size (#147)
Visual-Regression-Tracker/Visual-Regression-Tracker#279
1 parent d8b284a commit e501a14

File tree

4 files changed

+209
-2
lines changed

4 files changed

+209
-2
lines changed

src/_data_/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Baseline, Build, ImageComparison, Project, TestRun, TestVariation } from '@prisma/client';
2+
import { PNG } from 'pngjs';
23

34
export const TEST_PROJECT: Project = {
45
id: '1',
@@ -91,3 +92,17 @@ export const generateTestRun = (testRun?: Partial<TestRun>): TestRun => {
9192
...testRun,
9293
};
9394
};
95+
96+
export const generatePng = (width: number, height: number): PNG => {
97+
const png = new PNG({ width, height });
98+
for (var y = 0; y < png.height; y++) {
99+
for (var x = 0; x < png.width; x++) {
100+
var idx = (png.width * y + x) << 2;
101+
png.data[idx] = 255; // red
102+
png.data[idx + 1] = 255; // green
103+
png.data[idx + 2] = 255; // blue
104+
png.data[idx + 3] = 255; // alpha (0 is transparent)
105+
}
106+
}
107+
return png.pack();
108+
};
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`utils applyIgnoreAreas 1`] = `
4+
Object {
5+
"data": Array [
6+
255,
7+
255,
8+
255,
9+
255,
10+
255,
11+
255,
12+
255,
13+
255,
14+
255,
15+
255,
16+
255,
17+
255,
18+
255,
19+
255,
20+
255,
21+
255,
22+
255,
23+
255,
24+
255,
25+
255,
26+
255,
27+
255,
28+
255,
29+
255,
30+
255,
31+
255,
32+
255,
33+
255,
34+
255,
35+
255,
36+
255,
37+
255,
38+
255,
39+
255,
40+
255,
41+
255,
42+
],
43+
"type": "Buffer",
44+
}
45+
`;
46+
47+
exports[`utils applyIgnoreAreas 2`] = `
48+
Object {
49+
"data": Array [
50+
0,
51+
0,
52+
0,
53+
0,
54+
0,
55+
0,
56+
0,
57+
0,
58+
0,
59+
0,
60+
0,
61+
0,
62+
0,
63+
0,
64+
0,
65+
0,
66+
0,
67+
0,
68+
0,
69+
0,
70+
0,
71+
0,
72+
0,
73+
0,
74+
0,
75+
0,
76+
0,
77+
0,
78+
0,
79+
0,
80+
0,
81+
0,
82+
0,
83+
0,
84+
0,
85+
0,
86+
],
87+
"type": "Buffer",
88+
}
89+
`;
90+
91+
exports[`utils applyIgnoreAreas 3`] = `
92+
Object {
93+
"data": Array [
94+
255,
95+
255,
96+
255,
97+
255,
98+
255,
99+
255,
100+
255,
101+
255,
102+
255,
103+
255,
104+
255,
105+
255,
106+
255,
107+
255,
108+
255,
109+
255,
110+
255,
111+
255,
112+
255,
113+
255,
114+
255,
115+
255,
116+
255,
117+
255,
118+
255,
119+
255,
120+
255,
121+
255,
122+
255,
123+
255,
124+
255,
125+
255,
126+
255,
127+
255,
128+
255,
129+
255,
130+
],
131+
"type": "Buffer",
132+
}
133+
`;
134+
135+
exports[`utils applyIgnoreAreas 4`] = `
136+
Object {
137+
"data": Array [
138+
255,
139+
255,
140+
255,
141+
255,
142+
255,
143+
255,
144+
255,
145+
255,
146+
255,
147+
255,
148+
255,
149+
255,
150+
255,
151+
255,
152+
255,
153+
255,
154+
0,
155+
0,
156+
0,
157+
0,
158+
0,
159+
0,
160+
0,
161+
0,
162+
255,
163+
255,
164+
255,
165+
255,
166+
0,
167+
0,
168+
0,
169+
0,
170+
0,
171+
0,
172+
0,
173+
0,
174+
],
175+
"type": "Buffer",
176+
}
177+
`;

src/compare/utils/index.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PNG } from 'pngjs';
2+
import { IgnoreAreaDto } from 'src/test-runs/dto/ignore-area.dto';
3+
import { generatePng } from 'src/_data_';
4+
import { applyIgnoreAreas } from '.';
5+
6+
describe('utils', () => {
7+
it.each<[PNG, IgnoreAreaDto[]]>([
8+
[generatePng(3, 3), []],
9+
[generatePng(3, 3), [{ x: 0, y: 0, width: 3, height: 3 }]],
10+
[generatePng(3, 3), [{ x: -3, y: -3, width: 3, height: 3 }]],
11+
[generatePng(3, 3), [{ x: 1, y: 1, width: 5, height: 5 }]],
12+
])('applyIgnoreAreas', (image, ignoreAreas) => {
13+
expect(applyIgnoreAreas(image, ignoreAreas).data).toMatchSnapshot();
14+
});
15+
});

src/compare/utils/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export function scaleImageToSize(image: PNG, width: number, height: number): PNG
1212

1313
export function applyIgnoreAreas(image: PNG, ignoreAreas: IgnoreAreaDto[]): PNG {
1414
ignoreAreas.forEach((area) => {
15-
for (let y = area.y; y < area.y + area.height; y++) {
16-
for (let x = area.x; x < area.x + area.width; x++) {
15+
for (let y = area.y; y < Math.min(area.y + area.height, image.height); y++) {
16+
for (let x = area.x; x < Math.min(area.x + area.width, image.width); x++) {
1717
const k = 4 * (image.width * y + x);
1818
image.data[k + 0] = 0;
1919
image.data[k + 1] = 0;

0 commit comments

Comments
 (0)