Skip to content
Myr

created stb_resample_ideas.txt

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sean Barrett · committed by Sean Barrett · parent bcefca1 · Browse files · 1 file+62 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+62 added−0 removed

  • other:+62 added−0 removed

1 file+62 added−0 removed

added:docs/stb_resample_ideas.txt+62 added
1Consider three cases just to suggest the spectrum2of possiblities:34a) linear upsample: each output pixel is a weighted sum5of 4 input pixels67b) cubic upsample: each output pixel is a weighted sum8of 16 input pixels910c) downsample by N with box filter: each output pixel11is a weighted sum of NxN input pixels, N can be very large1213Now, suppose you want to handle 8-bit input, 16-bit14input, and float input, and you want to do sRGB correction15or not.1617Suppose you create a temporary buffer of float pixels, say18one scanline tall. Actually two temp buffers, one for the19input and one for the output. You decode a scanline of the20input into the temp buffer which is always linear floats. This21isolates the handling of 8/16/float and sRGB to one place22(and still allows you to make optimized 8-bit-sRGB-to-float23lookup tables). This also allows you to put wrap logic here,24explicitly wrapping, reflecting, or replicating-from-edge25pixels that would come from off-edge.2627You then do whatever the appropriate weighted sums are28into the output buffer, and you move on to the next29scanline of the input.3031The algorithm just described works directly for case (c).32Suppose you're downsampling by 2.5; then output scanline 033sums from input scanlines 0, 1, and 2; output scanline 134sums from 2,3,4; output 2 from 5,6,7; output 3 from 7,8,9.35Note how 2 & 7 get reused, but we don't have to recompute36them because we can do things in a single linear pass37through the input and output at the same time.3839Now, consider case (a). When upsampling, the same two input40scanlines will get sampled-from for multiple output scanlines.41So, to avoid recomputing the input scanlines, we need either42multiple input or multiple output temp buffer lines. Since43the number of output lines a given pair of input scanlines44might touch scales with the upsample amount, it makes more45sense to use two input scanline buffers. For cubic, you'll46need four scanline buffers, and in general the number of47buffers will be limited by the max filter width, which is48presumably hardcoded.4950You want to avoid memory allocations (since you're passing51in the target buffer already), so instead of using a scanline-width52temp buffer, use some fixed-width temp buffer that's W pixels,53and scale the image in vertical stripes that are that wide.54Suppose you make the temp buffers 256 wide; then an upsample55by 8 computes 256-pixel-width strips (from ~32-pixel-wide input56strips), but a downsample by 8 computes ~32-pixel-width57strips (from a 256-pixel width strip). Note this limits58the max down/upsampling to be ballpark 256x along the59horizontal axis.606162