Skip to content

Commit 6f7d4e5

Browse files
Merge pull request #725 from wahahachan/optimize_separable_apply
2 parents 18c3c16 + 703d5db commit 6f7d4e5

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

lib/src/filter/separable_kernel.dart

+21-13
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ class SeparableKernel {
6161

6262
void _applyCoefficientsLine(Image src, Image dst, int y, int width,
6363
bool horizontal, Image? mask, Channel maskChannel) {
64+
final srcPixel = src.getPixelSafe(0, 0);
65+
final dstPixel = dst.getPixelSafe(0, 0);
66+
if ((!srcPixel.isValid) || (!dstPixel.isValid)) {
67+
return;
68+
}
69+
6470
for (var x = 0; x < width; x++) {
6571
num r = 0.0;
6672
num g = 0.0;
@@ -71,25 +77,27 @@ class SeparableKernel {
7177
final c = coefficients[j2];
7278
final gr = _reflect(width, x + j);
7379

74-
final sc = horizontal ? src.getPixel(gr, y) : src.getPixel(y, gr);
80+
horizontal ? srcPixel.setPosition(gr, y) : srcPixel.setPosition(y, gr);
7581

76-
r += c * sc.r;
77-
g += c * sc.g;
78-
b += c * sc.b;
79-
a += c * sc.a;
82+
r += c * srcPixel.r;
83+
g += c * srcPixel.g;
84+
b += c * srcPixel.b;
85+
a += c * srcPixel.a;
8086
}
8187

82-
final p = horizontal ? dst.getPixel(x, y) : dst.getPixel(y, x);
88+
horizontal ? dstPixel.setPosition(x, y) : dstPixel.setPosition(y, x);
8389

84-
final msk = mask?.getPixel(p.x, p.y).getChannelNormalized(maskChannel);
90+
final msk = mask
91+
?.getPixel(dstPixel.x, dstPixel.y)
92+
.getChannelNormalized(maskChannel);
8593
if (msk == null) {
86-
p.setRgba(r, g, b, a);
94+
dstPixel.setRgba(r, g, b, a);
8795
} else {
88-
p
89-
..r = mix(p.r, r, msk)
90-
..g = mix(p.g, g, msk)
91-
..b = mix(p.b, b, msk)
92-
..a = mix(p.a, a, msk);
96+
dstPixel
97+
..r = mix(dstPixel.r, r, msk)
98+
..g = mix(dstPixel.g, g, msk)
99+
..b = mix(dstPixel.b, b, msk)
100+
..a = mix(dstPixel.a, a, msk);
93101
}
94102
}
95103
}

0 commit comments

Comments
 (0)