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
Provenance
- Commit
43fb994- Tree
76dd70b- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent 1
8252a94- Parent 2
891f6d7
2 files changed+3 added−214 removed
deleted:docs/stb_resample_ideas.txt-201 removed
1-1.2-3-Consider just porting this C++ public domain4-library back to C:5- https://code.google.com/p/imageresampler/source/browse/#svn%2Ftrunk6-(recommended by @castano)7-8-9-2.10-11-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 pixels16-17-b) cubic upsample: each output pixel is a weighted sum18-of 16 input pixels19-20-c) downsample by N with box filter: each output pixel21-is a weighted sum of NxN input pixels, N can be very large22-23-Now, suppose you want to handle 8-bit input, 16-bit24-input, and float input, and you want to do sRGB correction25-or not.26-27-Suppose you create a temporary buffer of float pixels, say28-one scanline tall. Actually two temp buffers, one for the29-input and one for the output. You decode a scanline of the30-input into the temp buffer which is always linear floats. This31-isolates the handling of 8/16/float and sRGB to one place32-(and still allows you to make optimized 8-bit-sRGB-to-float33-lookup tables). This also allows you to put wrap logic here,34-explicitly wrapping, reflecting, or replicating-from-edge35-pixels that would come from off-edge.36-37-You then do whatever the appropriate weighted sums are38-into the output buffer, and you move on to the next39-scanline of the input.40-41-The algorithm just described works directly for case (c).42-Suppose you're downsampling by 2.5; then output scanline 043-sums from input scanlines 0, 1, and 2; output scanline 144-sums from 2,3,4; output 2 from 5,6,7; output 3 from 7,8,9.45-Note how 2 & 7 get reused, but we don't have to recompute46-them because we can do things in a single linear pass47-through the input and output at the same time.48-49-Now, consider case (a). When upsampling, the same two input50-scanlines will get sampled-from for multiple output scanlines.51-So, to avoid recomputing the input scanlines, we need either52-multiple input or multiple output temp buffer lines. Since53-the number of output lines a given pair of input scanlines54-might touch scales with the upsample amount, it makes more55-sense to use two input scanline buffers. For cubic, you'll56-need four scanline buffers, and in general the number of57-buffers will be limited by the max filter width, which is58-presumably hardcoded.59-60-It turns out to be slightly different for two reasons:61-62- 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)65-66- 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)72-73-(The above approach isn't optimal for non-uniform resampling;74-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 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 input100-strips), but a downsample by 8 computes ~32-pixel-width101-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-114- 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);118-119-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,128- 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 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.151-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 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)164-165- ordering of src & dest:166- put these in whatever order you like, i just chose one arbitrarily167-168- 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 differently172-173- 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.)181-182-183-184-185-Reference:186-187-Cubic 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 )192-193-194-195-Wish 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 kernels200-201-modified:stb_image_resize.h+3 added-13 removed
29 unmodified lines30 31 ADDITIONAL DOCUMENTATION32 33 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.37 38 MEMORY ALLOCATION39 The resize functions here perform a single memory allocation using40 malloc. To control the memory allocation, before the #include that609 unmodified lines650 typedef 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;655 656 static const stbir_uint32 fp32_to_srgb8_tab4[104] = {657 0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d,1928 unmodified lines