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
Provenance
- Commit
63cce5c- Tree
4bff2b6- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
bcefca1
1 file changed+62 added−0 removed
added:docs/stb_resample_ideas.txt+62 added
1+Consider three cases just to suggest the spectrum2+of possiblities:3+4+a) linear upsample: each output pixel is a weighted sum5+of 4 input pixels6+7+b) cubic upsample: each output pixel is a weighted sum8+of 16 input pixels9+10+c) downsample by N with box filter: each output pixel11+is a weighted sum of NxN input pixels, N can be very large12+13+Now, suppose you want to handle 8-bit input, 16-bit14+input, and float input, and you want to do sRGB correction15+or not.16+17+Suppose you create a temporary buffer of float pixels, say18+one scanline tall. Actually two temp buffers, one for the19+input and one for the output. You decode a scanline of the20+input into the temp buffer which is always linear floats. This21+isolates the handling of 8/16/float and sRGB to one place22+(and still allows you to make optimized 8-bit-sRGB-to-float23+lookup tables). This also allows you to put wrap logic here,24+explicitly wrapping, reflecting, or replicating-from-edge25+pixels that would come from off-edge.26+27+You then do whatever the appropriate weighted sums are28+into the output buffer, and you move on to the next29+scanline of the input.30+31+The algorithm just described works directly for case (c).32+Suppose you're downsampling by 2.5; then output scanline 033+sums from input scanlines 0, 1, and 2; output scanline 134+sums from 2,3,4; output 2 from 5,6,7; output 3 from 7,8,9.35+Note how 2 & 7 get reused, but we don't have to recompute36+them because we can do things in a single linear pass37+through the input and output at the same time.38+39+Now, consider case (a). When upsampling, the same two input40+scanlines will get sampled-from for multiple output scanlines.41+So, to avoid recomputing the input scanlines, we need either42+multiple input or multiple output temp buffer lines. Since43+the number of output lines a given pair of input scanlines44+might touch scales with the upsample amount, it makes more45+sense to use two input scanline buffers. For cubic, you'll46+need four scanline buffers, and in general the number of47+buffers will be limited by the max filter width, which is48+presumably hardcoded.49+50+You want to avoid memory allocations (since you're passing51+in the target buffer already), so instead of using a scanline-width52+temp buffer, use some fixed-width temp buffer that's W pixels,53+and scale the image in vertical stripes that are that wide.54+Suppose you make the temp buffers 256 wide; then an upsample55+by 8 computes 256-pixel-width strips (from ~32-pixel-wide input56+strips), but a downsample by 8 computes ~32-pixel-width57+strips (from a 256-pixel width strip). Note this limits58+the max down/upsampling to be ballpark 256x along the59+horizontal axis.60+61+62+