Skip to content
Myr

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

1 file+29 added−10 removed

modified:docs/stb_resample_ideas.txt+29 added-10 removed
6 unmodified lines
7892.1011@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 spectrum11Consider three cases just to suggest the spectrum12of possiblities:1314a) linear upsample: each output pixel is a weighted sum15of 4 input pixels
58 unmodified lines
74optimal is to do whichever axis is smaller first, but I don't75think we have to care about doing that right.)767778Now, you probably want to avoid memory allocations (since you're passing79in the target buffer already), so instead of using a scanline-width78Now, you can either:7980 1. malloc the temp memory81 2. alloca it82 3. allocate a fixed amount on the stack83 4. let the user pass it in8485I forbid #2 in stb libraries for portability.8687If you're not allocating the output image, but rather requiring88the user to pass it in, it's probably worth trying to avoid #189because people always want to use stb libs without any memory90allocations for various reason. (Note that most stb libs go91crazy with memory allocations--you shouldn't use stb_image92in a console game--but I've tried to avoid it more in newer93libs.)9495The way #3 would work is instead of using a scanline-width96temp buffer, use some fixed-width temp buffer that's W pixels,97and scale the image in vertical stripes that are that wide.98Suppose you make the temp buffers 256 wide; then an upsample99by 8 computes 256-pixel-width strips (from ~32-pixel-wide input
1 unmodified line
101strips (from a 256-pixel width strip). Note this limits102the max down/upsampling to be ballpark 256x along the103horizontal axis.104105In the following, I do #3 and allow #4 for cases where #3 is106too small, but it's not the only possibility:107108109110Function prototypes:111112the highest-level one could be:113
5 unmodified lines
119the lowest-level one could be:120121 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);129130And there would be a bunch of convenience functions in-between those two levels.131132133Some notes:134135 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.140141 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 that
3 unmodified lines
148 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.151127 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.132152 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 return
37 unmodified lines