image resize 2.11 - fix harmless asan, fix point sample crash
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jeff Roberts (Bellevue) · committed by Jeff Roberts (Bellevue) · parent f75e8d1 · Browse files · 1 file·+78 added−50 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+78 added−50 removed
- other:+78 added−50 removed
Provenance
- Commit
af07f9d- Tree
b50bc59- Author
Jeff Roberts (Bellevue) <jeffr@radgametools.com>- Committer
Jeff Roberts (Bellevue) <jeffr@radgametools.com>- Parent
f75e8d1
1 file changed+78 added−50 removed
modified:stb_image_resize2.h+78 added-50 removed
1-/* stb_image_resize2 - v2.10 - public domain image resizing1+/* stb_image_resize2 - v2.11 - public domain image resizing2 3 by Jeff Roberts (v2) and Jorge L Rodriguez4 http://github.com/nothings/stb5 4 unmodified lines10 In one C/C++ file that #includes this file, do this:11 #define STB_IMAGE_RESIZE_IMPLEMENTATION12 before the #include. That will create the implementation in that file.13 14- PORTING FROM VERSION 115-16- The API has changed. You can continue to use the old version of stb_image_resize.h,17- which is available in the "deprecated/" directory.18-19- If you're using the old simple-to-use API, porting is straightforward.20- (For more advanced APIs, read the documentation.)21-22- stbir_resize_uint8():23- - call `stbir_resize_uint8_linear`, cast channel count to `stbir_pixel_layout`24-25- stbir_resize_float():26- - call `stbir_resize_float_linear`, cast channel count to `stbir_pixel_layout`27-28- stbir_resize_uint8_srgb():29- - function name is unchanged30- - cast channel count to `stbir_pixel_layout`31- - above is sufficient unless your image has alpha and it's not RGBA/BGRA32- - in that case, follow the below instructions for stbir_resize_uint8_srgb_edgemode33-34- stbir_resize_uint8_srgb_edgemode()35- - switch to the "medium complexity" API36- - stbir_resize(), very similar API but a few more parameters:37- - pixel_layout: cast channel count to `stbir_pixel_layout`38- - data_type: STBIR_TYPE_UINT8_SRGB39- - edge: unchanged (STBIR_EDGE_WRAP, etc.)40- - filter: STBIR_FILTER_DEFAULT41- - which channel is alpha is specified in stbir_pixel_layout, see enum for details42-14 EASY API CALLS:15 Easy API downsamples w/Mitchell filter, upsamples w/cubic interpolation, clamps to edge.16 17 stbir_resize_uint8_srgb( input_pixels, input_w, input_h, input_stride_in_bytes,248 unmodified lines266 267 ASSERT268 Define STBIR_ASSERT(boolval) to override assert() and not use assert.h269 270+ PORTING FROM VERSION 1271+ The API has changed. You can continue to use the old version of stb_image_resize.h,272+ which is available in the "deprecated/" directory.273+274+ If you're using the old simple-to-use API, porting is straightforward.275+ (For more advanced APIs, read the documentation.)276+277+ stbir_resize_uint8():278+ - call `stbir_resize_uint8_linear`, cast channel count to `stbir_pixel_layout`279+280+ stbir_resize_float():281+ - call `stbir_resize_float_linear`, cast channel count to `stbir_pixel_layout`282+283+ stbir_resize_uint8_srgb():284+ - function name is unchanged285+ - cast channel count to `stbir_pixel_layout`286+ - above is sufficient unless your image has alpha and it's not RGBA/BGRA287+ - in that case, follow the below instructions for stbir_resize_uint8_srgb_edgemode288+289+ stbir_resize_uint8_srgb_edgemode()290+ - switch to the "medium complexity" API291+ - stbir_resize(), very similar API but a few more parameters:292+ - pixel_layout: cast channel count to `stbir_pixel_layout`293+ - data_type: STBIR_TYPE_UINT8_SRGB294+ - edge: unchanged (STBIR_EDGE_WRAP, etc.)295+ - filter: STBIR_FILTER_DEFAULT296+ - which channel is alpha is specified in stbir_pixel_layout, see enum for details297+298 FUTURE TODOS299 * For polyphase integral filters, we just memcpy the coeffs to dupe300 them, but we should indirect and use the same coeff memory.301 * Add pixel layout conversions for sensible different channel counts24 unmodified lines326 Aras Pranckevicius: bugfixes327 Nathan Reed: warning fixes for 1.0328 329 REVISIONS330+ 2.11 (2024-09-08) fix harmless asan warnings in 2-channel and 3-channel mode331+ with AVX-2, fix some weird scaling edge conditions with332+ point sample mode.333 2.10 (2024-07-27) fix the defines GCC and mingw for loop unroll control,334 fix MSVC 32-bit arm half float routines.335 2.09 (2024-06-19) fix the defines for 32-bit ARM GCC builds (was selecting336 hardware half floats).2911 unmodified lines3248 float in_pixel_influence_upperbound = (out_pixel_influence_upperbound + out_shift) * inv_scale;3249 3250 first = (int)(STBIR_FLOORF(in_pixel_influence_lowerbound + 0.5f));3251 last = (int)(STBIR_FLOORF(in_pixel_influence_upperbound - 0.5f));3252+ if ( last < first ) last = first; // point sample mode can span a value *right* at 0.5, and cause these to cross3253 3254 if ( edge == STBIR_EDGE_WRAP )3255 {3256 if ( first < -input_size )27 unmodified lines3284 int in_first_pixel, in_last_pixel;3285 3286 stbir__calculate_in_pixel_range( &in_first_pixel, &in_last_pixel, out_pixel_center, out_filter_radius, inv_scale, out_shift, input_size, edge );3287 3288+ // make sure we never generate a range larger than our precalculated coeff width3289+ // this only happens in point sample mode, but it's a good safe thing to do anyway3290+ if ( ( in_last_pixel - in_first_pixel + 1 ) > coefficient_width )3291+ in_last_pixel = in_first_pixel + coefficient_width - 1;3292+3293 last_non_zero = -1;3294 for (i = 0; i <= in_last_pixel - in_first_pixel; i++)3295 {3296 float in_pixel_center = (float)(i + in_first_pixel) + 0.5f;27 unmodified lines3324 coefficient_group += coefficient_width;3325 }3326 }3327 3320-static void stbir__insert_coeff( stbir__contributors * contribs, float * coeffs, int new_pixel, float new_coeff )3328+static void stbir__insert_coeff( stbir__contributors * contribs, float * coeffs, int new_pixel, float new_coeff, int max_width )3329 {3330 if ( new_pixel <= contribs->n1 ) // before the end3331 {3332 if ( new_pixel < contribs->n0 ) // before the front?3333 {3334+ if ( ( contribs->n1 - new_pixel + 1 ) <= max_width )3335+ { 3326- int j, o = contribs->n0 - new_pixel;3327- for ( j = contribs->n1 - contribs->n0 ; j <= 0 ; j-- )3328- coeffs[ j + o ] = coeffs[ j ];3329- for ( j = 1 ; j < o ; j-- )3330- coeffs[ j ] = coeffs[ 0 ];3331- coeffs[ 0 ] = new_coeff;3332- contribs->n0 = new_pixel;3336+ int j, o = contribs->n0 - new_pixel;3337+ for ( j = contribs->n1 - contribs->n0 ; j <= 0 ; j-- )3338+ coeffs[ j + o ] = coeffs[ j ];3339+ for ( j = 1 ; j < o ; j-- )3340+ coeffs[ j ] = coeffs[ 0 ];3341+ coeffs[ 0 ] = new_coeff;3342+ contribs->n0 = new_pixel;3343+ }3344 }3345 else3346 {3347 coeffs[ new_pixel - contribs->n0 ] += new_coeff;3348 }3349 }3350 else3351 {3352+ if ( ( new_pixel - contribs->n0 + 1 ) <= max_width )3353+ {3341- int j, e = new_pixel - contribs->n0;3342- for( j = ( contribs->n1 - contribs->n0 ) + 1 ; j < e ; j++ ) // clear in-betweens coeffs if there are any3343- coeffs[j] = 0;3354+ int j, e = new_pixel - contribs->n0;3355+ for( j = ( contribs->n1 - contribs->n0 ) + 1 ; j < e ; j++ ) // clear in-betweens coeffs if there are any3356+ coeffs[j] = 0;3357 3345- coeffs[ e ] = new_coeff;3346- contribs->n1 = new_pixel;3358+ coeffs[ e ] = new_coeff;3359+ contribs->n1 = new_pixel;3360+ }3361 }3362 }3363 3364 static void stbir__calculate_out_pixel_range( int * first_pixel, int * last_pixel, float in_pixel_center, float in_pixels_radius, float scale, float out_shift, int out_size )170 unmodified lines3535 }3536 3537 coeffs = coefficient_group;3538 contribs = contributors;3539+3540 for (n = 0; n < num_contributors; n++)3541 {3542 int i;3543 31 unmodified lines3575 int start = contribs->n0;3576 int endi = contribs->n1;3577 contribs->n1 = input_last_n1;3578 for( i = input_size; i <= endi; i++ )3564- stbir__insert_coeff( contribs, coeffs, stbir__edge_wrap_slow[edge]( i, input_size ), coeffs[i-start] );3579+ stbir__insert_coeff( contribs, coeffs, stbir__edge_wrap_slow[edge]( i, input_size ), coeffs[i-start], coefficient_width );3580 }3581 3582 // now check left hand edge3583 if ( contribs->n0 < 0 )3 unmodified lines3587 float * c = coeffs - ( contribs->n0 + 1 );3588 3589 // reinsert the coeffs with it reflected or clamped (insert accumulates, if the coeffs exist)3590 for( i = -1 ; i > contribs->n0 ; i-- )3576- stbir__insert_coeff( contribs, coeffs, stbir__edge_wrap_slow[edge]( i, input_size ), *c-- );3591+ stbir__insert_coeff( contribs, coeffs, stbir__edge_wrap_slow[edge]( i, input_size ), *c--, coefficient_width );3592 save_n0 = contribs->n0;3593 save_n0_coeff = c[0]; // save it, since we didn't do the final one (i==n0), because there might be too many coeffs to hold (before we resize)!3594 3595 // now slide all the coeffs down (since we have accumulated them in the positive contribs) and reset the first contrib1 unmodified line3597 for(i = 0 ; i <= contribs->n1 ; i++ )3598 coeffs[i] = coeffs[i-save_n0];3599 3600 // now that we have shrunk down the contribs, we insert the first one safely3586- stbir__insert_coeff( contribs, coeffs, stbir__edge_wrap_slow[edge]( save_n0, input_size ), save_n0_coeff );3601+ stbir__insert_coeff( contribs, coeffs, stbir__edge_wrap_slow[edge]( save_n0, input_size ), save_n0_coeff, coefficient_width );3602 }3603 }3604 3605 if ( contribs->n0 <= contribs->n1 )3606 {3607 int diff = contribs->n1 - contribs->n0 + 1;3608 while ( diff && ( coeffs[ diff-1 ] == 0.0f ) )3609 --diff;3610+3611 contribs->n1 = contribs->n0 + diff - 1;3612 3613 if ( contribs->n0 <= contribs->n1 )3614 {364 unmodified lines3979 highest_set = k;3980 }3981 else3982 {3967- stbir__insert_coeff( scatter_contributors, scatter_coeffs, n, gc );3983+ stbir__insert_coeff( scatter_contributors, scatter_coeffs, n, gc, scatter_coefficient_width );3984 }3985 STBIR_ASSERT( ( scatter_contributors->n1 - scatter_contributors->n0 + 1 ) <= scatter_coefficient_width );3986 }3987 ++scatter_contributors;837 unmodified lines4825 stbir__simdf8_0123to00112233( c, cs ); \4826 stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*2 );4827 4828 #define stbir__1_coeff_remnant( ofs ) \4813- { stbir__simdf t; \4829+ { stbir__simdf t,d; \4830 stbir__simdf_load1z( t, hc + (ofs) ); \4831+ stbir__simdf_load2( d, decode + (ofs) * 2 ); \4832 stbir__simdf_0123to0011( t, t ); \4816- stbir__simdf_mult_mem( t, t, decode+(ofs)*2 ); \4833+ stbir__simdf_mult( t, t, d ); \4834 stbir__simdf8_add4( tot0, tot0, t ); }4818-4835+ 4836 #define stbir__2_coeff_remnant( ofs ) \4837 { stbir__simdf t; \4838 stbir__simdf_load2( t, hc + (ofs) ); \4839 stbir__simdf_0123to0011( t, t ); \2288 unmodified lines7128 both = vertical->gather_prescatter_contributors_size + vertical->gather_prescatter_coefficients_size;7129 7130 #ifdef STBIR__SEPARATE_ALLOCATIONS7131 temp_mem_amt = decode_buffer_size;7132+7133+ #ifdef STBIR_SIMD87134+ if ( effective_channels == 3 )7135+ --temp_mem_amt; // avx in 3 channel mode needs one float at the start of the buffer7136+ #endif7137 #else7138 temp_mem_amt = ( decode_buffer_size + ring_buffer_size + vertical_buffer_size ) * splits;7139 #endif7140 if ( temp_mem_amt >= both )97 unmodified lines7238 {7239 int t, ofs, start;7240 7241 ofs = decode_buffer_size / 4;7242+7243+ #if defined( STBIR__SEPARATE_ALLOCATIONS ) && defined(STBIR_SIMD8)7244+ if ( effective_channels == 3 ) 7245+ --ofs; // avx in 3 channel mode needs one float at the start of the buffer, so we snap back for clearing7246+ #endif7247+7248 start = ofs - 4;7249 if ( start < 0 ) start = 0;7250 7251 for( t = start ; t < ofs; t++ )3349 unmodified lines