Skip to content
Myr

Merge branch 'master' of https://github.com/nothings/stb

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 · parents 8252a94, 891f6d7 · Browse files · 2 files+3 added−214 removed
This commit

State

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

Diff stats

2 files+3 added−214 removed

  • other:+3 added−214 removed

2 files+3 added−214 removed

deleted:docs/stb_resample_ideas.txt-201 removed
11.23Consider just porting this C++ public domain4library back to C:5 https://code.google.com/p/imageresampler/source/browse/#svn%2Ftrunk6(recommended by @castano)7892.1011Consider three cases just to suggest the spectrum12of possiblities:1314a) linear upsample: each output pixel is a weighted sum15of 4 input pixels1617b) cubic upsample: each output pixel is a weighted sum18of 16 input pixels1920c) downsample by N with box filter: each output pixel21is a weighted sum of NxN input pixels, N can be very large2223Now, suppose you want to handle 8-bit input, 16-bit24input, and float input, and you want to do sRGB correction25or not.2627Suppose you create a temporary buffer of float pixels, say28one scanline tall. Actually two temp buffers, one for the29input and one for the output. You decode a scanline of the30input into the temp buffer which is always linear floats. This31isolates the handling of 8/16/float and sRGB to one place32(and still allows you to make optimized 8-bit-sRGB-to-float33lookup tables). This also allows you to put wrap logic here,34explicitly wrapping, reflecting, or replicating-from-edge35pixels that would come from off-edge.3637You then do whatever the appropriate weighted sums are38into the output buffer, and you move on to the next39scanline of the input.4041The algorithm just described works directly for case (c).42Suppose you're downsampling by 2.5; then output scanline 043sums from input scanlines 0, 1, and 2; output scanline 144sums from 2,3,4; output 2 from 5,6,7; output 3 from 7,8,9.45Note how 2 & 7 get reused, but we don't have to recompute46them because we can do things in a single linear pass47through the input and output at the same time.4849Now, consider case (a). When upsampling, the same two input50scanlines will get sampled-from for multiple output scanlines.51So, to avoid recomputing the input scanlines, we need either52multiple input or multiple output temp buffer lines. Since53the number of output lines a given pair of input scanlines54might touch scales with the upsample amount, it makes more55sense to use two input scanline buffers. For cubic, you'll56need four scanline buffers, and in general the number of57buffers will be limited by the max filter width, which is58presumably hardcoded.5960It turns out to be slightly different for two reasons:6162 1. when using an arbitrary filter and downsampling,63 you actually need N output buffers and 1 input buffer64 (vs 1 output buffer and N input buffers upsampling)6566 2. this approach will be very inefficient as written.67 you want to use separable filters and actually do68 seperable computation: first decode an input scanline69 into a 'decode' buffer, then horizontally resample it70 into the "input" buffer (kind of a misnomer, but71 they're the inputs to the vertical resampler)7273(The above approach isn't optimal for non-uniform resampling;74optimal is to do whichever axis is smaller first, but I don't75think we have to care about doing that right.)767778Now, 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 input100strips), but a downsample by 8 computes ~32-pixel-width101strips (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:113114 stb_resample_8bit(uint8_t *dest, int dest_width, int dest_height,115 uint8_t const *src , int src_width, int src_height,116 int channels,117 stbr_filter filter);118119the 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,128 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 that145 needs to be resampled accounting for this (weight the sampling146 by the alpha channel, i.e. premultiply, filter, unpremultiply).147 this mechanism only allows one alpha channel and ALL channels 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.151152 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 return156 value for the function could be 0 on success or non-0 being the size of157 tempmem needed.158 159 src_stride, dest_stride:160 the stride variables are signed to allow you to describe both traditional161 top-to-bottom images (pass in a pointer to the top-left pixel and162 a positive stride) and bottom-to-top images (pass in a pointer to163 the bottom-left pixel and a negative stride)164165 ordering of src & dest:166 put these in whatever order you like, i just chose one arbitrarily167168 width & height169 these are ints not unsigned ints or size_ts because i personally forbid170 unsigned variables for almost everything to avoid signed/unsigned comparison171 issues, but this is a matter of personal taste and you can do differently172173 Intermediate-level functions should be provided for each source type & same dest type174 so that the code is typesafe; only when people fall back to stb_resample_arbitrary should175 they be at risk for type unsafety. (One way to deal avoid an explosion of functions of176 every possible *combination* of types in a type-safe way would be to define one function177 for each input type, and accept three separate output pointers, one for each type, only178 one of which can be non-NULL. 9 functions isn't that bad, but if you want to have three179 or four intermediate-level functions with fewer parameters, 9*4 gets silly. Could also180 use the same trick for stb_resample_arbitrary, replacing it with three typesafe functions.)181182183184185Reference:186187Cubic sampling function for seperable cubic:188 f(x) = (a+2)*x^3 - (a+3)*x^2 + 1 for 0 <= x <= 1189 f(x) = a*x^3 - 5*a*x^2 + 8*a*x - 4*a for 1 < x <= 2190 f(x) = 0 otherwise191 "a" is configurable, try -1/2 (from http://pixinsight.com/forum/index.php?topic=556.0 )192193194195Wish list:196 s0, t0, s1, t1 vs scale_x, scale_y, offset_x, offset_y - What's the best interface?197 Separate wrap modes and filter modes per axis198 Alpha test coverage respecting resize (FloatImage::alphaTestCoverage and FloatImage::scaleAlphaToCoverage: https://code.google.com/p/nvidia-texture-tools/source/browse/trunk/src/nvimage/FloatImage.cpp)199 Installable filter kernels200201
modified:stb_image_resize.h+3 added-13 removed
29 unmodified lines
3031 ADDITIONAL DOCUMENTATION3233 SRGB & FLOATING POINT REPRESENTATION34 Some srgb-related code in this library relies on floats being 32-bit35 IEEE floating point, and relies on a specific bitpacking order of C36 bitfields. If you are on a system that uses non-IEEE floats or packs37 C bitfields in the opposite order, then you can use a slower fallback38 codepath by defining STBIR_NON_IEEE_FLOAT. (We didn't make this choice39 idly; using mostly-but-not-100%-portable-code for this is a massive40 speedup, especially upsampling where colorspace conversion dominates.)34 The sRGB functions presume IEEE floating point. If you do not have35 IEEE floating point, define STBIR_NON_IEEE_FLOAT. This will use36 a slower implementation.3738 MEMORY ALLOCATION39 The resize functions here perform a single memory allocation using40 malloc. To control the memory allocation, before the #include that
609 unmodified lines
650typedef union651{652 stbir_uint32 u;653 float f;658 struct659 {660 stbir_uint32 Mantissa : 23;661 stbir_uint32 Exponent : 8;662 stbir_uint32 Sign : 1;663 };654} stbir__FP32;655656static const stbir_uint32 fp32_to_srgb8_tab4[104] = {657 0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d,
1928 unmodified lines