Skip to content

Commit

Permalink
image: fix boxBlur with scalar types
Browse files Browse the repository at this point in the history
  • Loading branch information
arrufat committed Jul 24, 2024
1 parent 1543b98 commit 54fd4ff
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/image.zig
Original file line number Diff line number Diff line change
Expand Up @@ -248,26 +248,27 @@ pub fn Image(comptime T: type) type {
var pos: usize = 0;
var rem: usize = size;
const simd_len = std.simd.suggestVectorLength(T) orelse 1;
const box_areas: @Vector(simd_len, f32) = @splat(2 * radius * 2 * radius);
while (pos < size) {
const r = pos / self.cols;
const c = pos % self.cols;
const r1 = r -| radius;
const r2 = @min(r + radius, self.rows - 1);
const r1_offset = r1 * self.cols;
const r2_offset = r2 * self.cols;
const r2_r1 = r2 - r1;
if (r1 >= radius and r2 <= self.rows - 1 - radius and
c >= radius and c <= self.cols - 1 - radius - simd_len and
rem >= simd_len)
{
const c1 = c - radius;
const c2 = c + radius;
const int11s: @Vector(simd_len, f32) = integral.data[r1_offset + c1 ..][0..simd_len];
const int12s: @Vector(simd_len, f32) = integral.data[r1_offset + c2 ..][0..simd_len];
const int21s: @Vector(simd_len, f32) = integral.data[r2_offset + c1 ..][0..simd_len];
const int22s: @Vector(simd_len, f32) = integral.data[r2_offset + c2 ..][0..simd_len];
const int11s: @Vector(simd_len, f32) = integral.data[r1_offset + c1 ..][0..simd_len].*;
const int12s: @Vector(simd_len, f32) = integral.data[r1_offset + c2 ..][0..simd_len].*;
const int21s: @Vector(simd_len, f32) = integral.data[r2_offset + c1 ..][0..simd_len].*;
const int22s: @Vector(simd_len, f32) = integral.data[r2_offset + c2 ..][0..simd_len].*;
const areas: @Vector(simd_len, f32) = @splat(@as(f32, @floatFromInt(r2_r1 * 2 * radius)));
const sums = int22s - int21s - int12s + int11s;
const vals: [simd_len]f32 = @round(sums / box_areas);
const vals: [simd_len]f32 = @round(sums / areas);
for (vals, 0..) |val, i| {
blurred.data[pos + i] = as(T, val);
}
Expand All @@ -285,6 +286,7 @@ pub fn Image(comptime T: type) type {
blurred.data[pos] = switch (@typeInfo(T)) {
.Int, .ComptimeInt => as(T, @round(sum / area)),
.Float, .ComptimeFloat => as(T, sum / area),
else => @compileError("Can't compute the boxBlur image with struct fields of type " ++ @typeName(T) ++ "."),
};
pos += 1;
rem -= 1;
Expand Down

0 comments on commit 54fd4ff

Please sign in to comment.