@@ -248,26 +248,27 @@ pub fn Image(comptime T: type) type {
248
248
var pos : usize = 0 ;
249
249
var rem : usize = size ;
250
250
const simd_len = std .simd .suggestVectorLength (T ) orelse 1 ;
251
- const box_areas : @Vector (simd_len , f32 ) = @splat (2 * radius * 2 * radius );
252
251
while (pos < size ) {
253
252
const r = pos / self .cols ;
254
253
const c = pos % self .cols ;
255
254
const r1 = r - | radius ;
256
255
const r2 = @min (r + radius , self .rows - 1 );
257
256
const r1_offset = r1 * self .cols ;
258
257
const r2_offset = r2 * self .cols ;
258
+ const r2_r1 = r2 - r1 ;
259
259
if (r1 >= radius and r2 <= self .rows - 1 - radius and
260
260
c >= radius and c <= self .cols - 1 - radius - simd_len and
261
261
rem >= simd_len )
262
262
{
263
263
const c1 = c - radius ;
264
264
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 )));
269
270
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 );
271
272
for (vals , 0.. ) | val , i | {
272
273
blurred .data [pos + i ] = as (T , val );
273
274
}
@@ -285,6 +286,7 @@ pub fn Image(comptime T: type) type {
285
286
blurred .data [pos ] = switch (@typeInfo (T )) {
286
287
.Int , .ComptimeInt = > as (T , @round (sum / area )),
287
288
.Float , .ComptimeFloat = > as (T , sum / area ),
289
+ else = > @compileError ("Can't compute the boxBlur image with struct fields of type " ++ @typeName (T ) ++ "." ),
288
290
};
289
291
pos += 1 ;
290
292
rem -= 1 ;
0 commit comments