Skip to content

Commit 54fd4ff

Browse files
author
Adrià Arrufat
committed
image: fix boxBlur with scalar types
1 parent 1543b98 commit 54fd4ff

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/image.zig

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,26 +248,27 @@ pub fn Image(comptime T: type) type {
248248
var pos: usize = 0;
249249
var rem: usize = size;
250250
const simd_len = std.simd.suggestVectorLength(T) orelse 1;
251-
const box_areas: @Vector(simd_len, f32) = @splat(2 * radius * 2 * radius);
252251
while (pos < size) {
253252
const r = pos / self.cols;
254253
const c = pos % self.cols;
255254
const r1 = r -| radius;
256255
const r2 = @min(r + radius, self.rows - 1);
257256
const r1_offset = r1 * self.cols;
258257
const r2_offset = r2 * self.cols;
258+
const r2_r1 = r2 - r1;
259259
if (r1 >= radius and r2 <= self.rows - 1 - radius and
260260
c >= radius and c <= self.cols - 1 - radius - simd_len and
261261
rem >= simd_len)
262262
{
263263
const c1 = c - radius;
264264
const c2 = c + radius;
265-
const int11s: @Vector(simd_len, f32) = integral.data[r1_offset + c1 ..][0..simd_len];
266-
const int12s: @Vector(simd_len, f32) = integral.data[r1_offset + c2 ..][0..simd_len];
267-
const int21s: @Vector(simd_len, f32) = integral.data[r2_offset + c1 ..][0..simd_len];
268-
const int22s: @Vector(simd_len, f32) = integral.data[r2_offset + c2 ..][0..simd_len];
265+
const int11s: @Vector(simd_len, f32) = integral.data[r1_offset + c1 ..][0..simd_len].*;
266+
const int12s: @Vector(simd_len, f32) = integral.data[r1_offset + c2 ..][0..simd_len].*;
267+
const int21s: @Vector(simd_len, f32) = integral.data[r2_offset + c1 ..][0..simd_len].*;
268+
const int22s: @Vector(simd_len, f32) = integral.data[r2_offset + c2 ..][0..simd_len].*;
269+
const areas: @Vector(simd_len, f32) = @splat(@as(f32, @floatFromInt(r2_r1 * 2 * radius)));
269270
const sums = int22s - int21s - int12s + int11s;
270-
const vals: [simd_len]f32 = @round(sums / box_areas);
271+
const vals: [simd_len]f32 = @round(sums / areas);
271272
for (vals, 0..) |val, i| {
272273
blurred.data[pos + i] = as(T, val);
273274
}
@@ -285,6 +286,7 @@ pub fn Image(comptime T: type) type {
285286
blurred.data[pos] = switch (@typeInfo(T)) {
286287
.Int, .ComptimeInt => as(T, @round(sum / area)),
287288
.Float, .ComptimeFloat => as(T, sum / area),
289+
else => @compileError("Can't compute the boxBlur image with struct fields of type " ++ @typeName(T) ++ "."),
288290
};
289291
pos += 1;
290292
rem -= 1;

0 commit comments

Comments
 (0)