even more resampling notes
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 92b08aa · Browse files · 1 file·+29 added−10 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+29 added−10 removed
- other:+29 added−10 removed
Provenance
- Commit
ee8e926- Tree
4bd783e- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
92b08aa
1 file changed+29 added−10 removed
modified:docs/stb_resample_ideas.txt+29 added-10 removed
6 unmodified lines7 8 9 2.10 11-@VinoBS Another option is to just port @richgel999's C++ library to C/stb: https://code.google.com/p/imageresampler/source/browse/#svn%2FtrunkConsider three cases just to suggest the spectrum11+Consider three cases just to suggest the spectrum12 of possiblities:13 14 a) linear upsample: each output pixel is a weighted sum15 of 4 input pixels58 unmodified lines74 optimal is to do whichever axis is smaller first, but I don't75 think we have to care about doing that right.)76 77 78-Now, you probably want to avoid memory allocations (since you're passing79-in the target buffer already), so instead of using a scanline-width78+Now, you can either:79+80+ 1. malloc the temp memory81+ 2. alloca it82+ 3. allocate a fixed amount on the stack83+ 4. let the user pass it in84+85+I forbid #2 in stb libraries for portability.86+87+If you're not allocating the output image, but rather requiring88+the user to pass it in, it's probably worth trying to avoid #189+because people always want to use stb libs without any memory90+allocations for various reason. (Note that most stb libs go91+crazy with memory allocations--you shouldn't use stb_image92+in a console game--but I've tried to avoid it more in newer93+libs.)94+95+The way #3 would work is instead of using a scanline-width96 temp buffer, use some fixed-width temp buffer that's W pixels,97 and scale the image in vertical stripes that are that wide.98 Suppose you make the temp buffers 256 wide; then an upsample99 by 8 computes 256-pixel-width strips (from ~32-pixel-wide input1 unmodified line101 strips (from a 256-pixel width strip). Note this limits102 the max down/upsampling to be ballpark 256x along the103 horizontal axis.104 105+In the following, I do #3 and allow #4 for cases where #3 is106+too small, but it's not the only possibility:107 108 109+110 Function prototypes:111 112 the highest-level one could be:113 5 unmodified lines119 the lowest-level one could be:120 121 stb_resample_arbitrary(void *dst, stbr_type dst_type, int dst_width, int dst_height, int dst_stride_in_bytes,122 void const *src, stbr_type src_type, int src_width, int src_height, int src_stride_in_bytes,123+ float s0, float t0, float s1, float t1, // range of source to use, 0..1 in GPU texture-coordinate style124 int channels,125 int nonpremul_alpha_channel_index,126 stbr_wrapmode wrap, // clamp, wrap, mirror127 stbr_filter filter,108- float s0, float t0, float s1, float t1, // range of source to use, 0..1 in GPU texture-coordinate style128 void *tempmem, size_t tempmem_size_in_bytes);129 130 And there would be a bunch of convenience functions in-between those two levels.131 132 133 Some notes:134 135+ s0,t0,s1,t1:136+ this allows fine subpixel-positioning and subpixel-resizing in an explicit way without137+ things having to be exact pixel multiples. it allows people to pseudo-stream138+ images by computing "tiles" of images a bit at a time without forcing those139+ tiles to quantize their source data.140+141 nonpremul_alpha_channel_index:142 if this is negative, no channels are processed specially143 if this is non-negative, then it's the index of the alpha channel,144 and the image should be treated as non-premultiplied alpha that3 unmodified lines148 are scaled by it; an alternative would be to find some way to149 pass in which channels serve as alpha channels for which other150 channels, but eh.151 127- s0,t0,s1,t1:128- this allows fine subpixel-positioning and subpixel-resizing in an explicit way without129- things having to be exact pixel multiples. it allows people to pseudo-stream130- images by computing "tiles" of images a bit at a time without forcing those131- tiles to quantize their source data.132-152 tempmem, tempmem_size:153 all functions will needed tempmem, but they can allocate a fixed tempmem buffer154 on the stack. providing an API that allows overriding the amount of tempmem155 available allows people to process arbitrarily large images. the return37 unmodified lines