boxfilter

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: BSD-3-Clause Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

boxfilter

Pure-Go SIMD kernels for the two inner loops of an area-averaging (box) downscale of an RGBA8 picture on premultiplied alpha, in exact integer arithmetic.

import "github.com/go-simd/boxfilter"

// vertical pass: acc += w * premultiply(row), four uint32 per pixel
boxfilter.AccumPremul(acc, srcRow, w)

// horizontal pass: dst pixel i = sum of src pixels i*n .. i*n+n-1
boxfilter.SumGroups(dst, acc, n)
  • AccumPremul(acc []uint32, src []byte, w uint32): for every pixel, acc += w*(R*A, G*A, B*A, A).

  • SumGroups(dst, src []uint32, n int): channel-wise sum of runs of n pixels.

  • SumBlocks(dst []uint32, src []byte, stride, n, m int): the whole integer-factor downscale in one pass: destination pixel j is the sum of the premultiplied n x m block at pixel j*n of row 0, rows stride bytes apart. The sums stay in vector registers; no accumulator row is written or read back. A kernel reads no byte outside the blocks.

Sums wrap modulo 2^32; the caller keeps 65025 times the total weight below 2^32 (a total weight of at most 66051). Integer arithmetic is exact, so every kernel is bit-identical to the scalar reference, checked by differential tests, fuzzing and a 100% coverage gate.

Architectures

target kernel
arm64, Go 1.27+ NEON (VTBL premultiplier, VUMULL, VUMLAL)
amd64 AVX2 (VPSHUFB, VPMULLW, VPMULLD), detected at run time
everything else portable scalar

On arm64 the weight must fit 16 bits for the kernel (larger weights take the scalar path). The amd64 kernel was validated on CI runners, not on the development machine.

Performance

Apple M4 Max, -cpu 1, one row of N pixels, AccumPremul(acc, src, 1):

pixels scalar NEON speed-up
64 70.9 ns 18.2 ns 3.9x
256 277.6 ns 47.5 ns 5.8x
1000 1096 ns 170.9 ns 6.4x

Used by go-widgets/painter for its downscaling DrawImage.

Documentation

Overview

Package boxfilter holds the two inner loops of an area-averaging (box) downscale of an RGBA8 picture on PREMULTIPLIED alpha, in exact integer arithmetic, with SIMD kernels (NEON on arm64, AVX2 on amd64) and a portable scalar fallback that every kernel is bit-identical to.

A box downscale is two separable passes. The vertical pass adds the rows a destination row covers into a row of uint32 sums, each source pixel premultiplied on the way in: that is AccumPremul, and it is where nearly all the time goes (one multiply per colour channel per source pixel). The horizontal pass then sums runs of those accumulated pixels, which for an integer ratio is a plain sum of n neighbours: SumGroups.

The arithmetic wraps modulo 2^32 like any uint32 sum. The CALLER bounds it: a premultiplied channel is at most 255*255 = 65025, so the sums stay exact while 65025 times the total weight stays below 2^32, i.e. a total weight of at most 66051.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccumPremul

func AccumPremul(acc []uint32, src []byte, w uint32)

AccumPremul adds w times the premultiplied form of every pixel of src (RGBA, 8 bits per channel, straight alpha) to acc, four uint32 per pixel in the same R, G, B, A order:

acc[4i+0] += w * (R*A)   acc[4i+1] += w * (G*A)
acc[4i+2] += w * (B*A)   acc[4i+3] += w * A

It handles min(len(src), len(acc))/4 pixels and ignores the rest.

func SumBlocks added in v0.2.0

func SumBlocks(dst []uint32, src []byte, stride, n, m int)

SumBlocks is the whole of a box downscale by an INTEGER factor in one pass over the source: destination pixel j (four uint32, R, G, B, A order) is the sum of the premultiplied pixels of the n x m block whose top-left pixel is pixel j*n of row 0 of src, rows being stride bytes apart:

dst[4j+0] = sum over the block of R*A   dst[4j+1] = sum of G*A
dst[4j+2] = sum over the block of B*A   dst[4j+3] = sum of A

It is AccumPremul over m rows (every weight 1) followed by SumGroups with the same n, but the sums stay in vector registers for the whole block and only the result is stored: nothing is written to, or read back from, an accumulator row. A kernel reads no byte outside the blocks.

It fills as many whole destination pixels as dst holds, as each of the m rows holds (stride/(4n) per row) and as src covers (the last row included); n < 1, m < 1 or stride < 4n does nothing. Like the other functions the sums wrap modulo 2^32: exact while 65025*n*m < 2^32.

func SumGroups

func SumGroups(dst, src []uint32, n int)

SumGroups sums runs of n pixels: dst pixel i (four uint32) is the channel-wise sum of src pixels i*n .. i*n+n-1. It fills as many whole destination pixels as both slices hold, and does nothing for n < 1.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址