@floatToInt, @intToFloat gone

I agree that simplifying the cast syntax would help reduce the clutter a lot.
I implemented this function, which needs to compute some values using mixed types, and it’s a bit hard to see the actual operation I am doing.
Maybe there’s a better way to write this function, though…

/// Blur an image by applying a box filter using an integral image.
fn applyBoxFilter(integral: [*]i32, blur: [*]u8, rows: usize, cols: usize, radius: usize) void {
    for (0..rows) |r| {
        for (0..cols) |c| {
            const pos = r * cols + c;
            const r1 = @max(@as(i32, @intCast(r)) - @as(i32, @intCast(radius)), 0);
            const c1 = @max(@as(i32, @intCast(c)) - @as(i32, @intCast(radius)), 0);
            const r2 = @min(@as(i32, @intCast(r)) + @as(i32, @intCast(radius)), @as(i32, @intCast(rows)) - 1);
            const c2 = @min(@as(i32, @intCast(c)) + @as(i32, @intCast(radius)), @as(i32, @intCast(cols)) - 1);
            const pos11: usize = @intCast(r1 * @as(i32, @intCast(cols)) + c1);
            const pos12: usize = @intCast(r1 * @as(i32, @intCast(cols)) + c2);
            const pos21: usize = @intCast(r2 * @as(i32, @intCast(cols)) + c1);
            const pos22: usize = @intCast(r2 * @as(i32, @intCast(cols)) + c2);
            const area: f32 = @floatFromInt((r2 - r1) * (c2 - c1));
            const sum: f32 = @floatFromInt(integral[pos22] - integral[pos21] - integral[pos12] + integral[pos11]);
            blur[pos] = @intFromFloat(@round(sum / area));
        }
    }
}

For the curious, this function blurs an image using a box filter, but does it really fast by using an integral image, which was computed beforehand.

1 Like