Merge pull request #1609 from jeffrbig2/fix_coeffs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix bug in coeff gen on more than 3x downsamples with w and h equal
authored by Sean Barrett · committed by GitHub · parents b7cf124, 2fb057a · Browse files · 1 file·+39 added−21 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+39 added−21 removed
- other:+39 added−21 removed
Provenance
- Commit
ae721c5- Tree
2c2c797- Author
Sean Barrett <sean2@nothings.org>- Committer
GitHub <noreply@github.com>- Parent 1
b7cf124- Parent 2
2fb057a
1 file changed+39 added−21 removed
modified:stb_image_resize2.h+39 added-21 removed
1-/* stb_image_resize2 - v2.05 - public domain image resizing1+/* stb_image_resize2 - v2.06 - public domain image resizing2 3 by Jeff Roberts (v2) and Jorge L Rodriguez4 http://github.com/nothings/stb5 321 unmodified lines327 Aras Pranckevicius: bugfixes328 Nathan Reed: warning fixes for 1.0329 330 REVISIONS331+ 2.06 (2024-02-10) fix for indentical width/height 3x or more down-scaling 332+ undersampling a single row on rare resize ratios (about 1%)331- 2.05 (2024-02-24) fix for 2 pixel to 1 pixel resizes with wrap (thanks Aras)333+ 2.05 (2024-02-07) fix for 2 pixel to 1 pixel resizes with wrap (thanks Aras)334 fix for output callback (thanks Julien Koenen)335 2.04 (2023-11-17) fix for rare AVX bug, shadowed symbol (thanks Nikola Smiljanic).336 2.03 (2023-11-01) ASAN and TSAN warnings fixed, minor tweaks.337 2.00 (2023-10-10) mostly new source: new api, optimizations, simd, vertical-first, etc3057 unmodified lines3395 }3396 }3397 }3398 3399+#ifdef STBIR_RENORMALIZE_IN_FLOAT3400+#define STBIR_RENORM_TYPE float3401+#else3402+#define STBIR_RENORM_TYPE double3403+#endif3404+3405 static void stbir__cleanup_gathered_coefficients( stbir_edge edge, stbir__filter_extent_info* filter_info, stbir__scale_info * scale_info, int num_contributors, stbir__contributors* contributors, float * coefficient_group, int coefficient_width )3406 {3407 int input_size = scale_info->input_full_size;3408 int input_last_n1 = input_size - 1;13 unmodified lines3422 end = num_contributors; if ( polyphase ) end = numerator;3423 for (n = 0; n < end; n++)3424 {3425 int i;3418- float filter_scale, total_filter = 0;3426+ STBIR_RENORM_TYPE filter_scale, total_filter = 0;3427 int e;3428 3429 // add all contribs3430 e = contribs->n1 - contribs->n0;3431 for( i = 0 ; i <= e ; i++ )3432 {3425- total_filter += coeffs[i];3433+ total_filter += (STBIR_RENORM_TYPE) coeffs[i];3434 STBIR_ASSERT( ( coeffs[i] >= -2.0f ) && ( coeffs[i] <= 2.0f ) ); // check for wonky weights3435 }3436 3437 // rescale7 unmodified lines3445 {3446 // if the total isn't 1.0, rescale everything3447 if ( ( total_filter < (1.0f-stbir__small_float) ) || ( total_filter > (1.0f+stbir__small_float) ) )3448 {3441- filter_scale = 1.0f / total_filter;3449+ filter_scale = ((STBIR_RENORM_TYPE)1.0) / total_filter;3450+3451 // scale them all3452 for (i = 0; i <= e; i++)3444- coeffs[i] *= filter_scale;3453+ coeffs[i] = (float) ( coeffs[i] * filter_scale );3454 }3455 }3456 ++contribs;3457 coeffs += coefficient_width;112 unmodified lines3570 filter_info->highest = highest;3571 filter_info->widest = widest;3572 }3573 3574+#undef STBIR_RENORM_TYPE 3575+3576 static int stbir__pack_coefficients( int num_contributors, stbir__contributors* contributors, float * coefficents, int coefficient_width, int widest, int row0, int row1 ) 3577 {3578 #define STBIR_MOVE_1( dest, src ) { STBIR_NO_UNROLL(dest); ((stbir_uint32*)(dest))[0] = ((stbir_uint32*)(src))[0]; }3579 #define STBIR_MOVE_2( dest, src ) { STBIR_NO_UNROLL(dest); ((stbir_uint64*)(dest))[0] = ((stbir_uint64*)(src))[0]; }299 unmodified lines3879 3880 for (k = gn0 ; k <= gn1 ; k++ )3881 {3882 float gc = *g_coeffs++;3872- if ( ( k > highest_set ) || ( scatter_contributors->n0 > scatter_contributors->n1 ) )3883+ 3884+ // skip zero and denormals - must skip zeros to avoid adding coeffs beyond scatter_coefficient_width3885+ // (which happens when pivoting from horizontal, which might have dummy zeros)3886+ if ( ( ( gc >= stbir__small_float ) || ( gc <= -stbir__small_float ) ) )3887 {3888+ if ( ( k > highest_set ) || ( scatter_contributors->n0 > scatter_contributors->n1 ) )3889 {3875- // if we are skipping over several contributors, we need to clear the skipped ones3876- stbir__contributors * clear_contributors = samp->contributors + ( highest_set + filter_pixel_margin + 1);3877- while ( clear_contributors < scatter_contributors )3890 {3891+ // if we are skipping over several contributors, we need to clear the skipped ones3892+ stbir__contributors * clear_contributors = samp->contributors + ( highest_set + filter_pixel_margin + 1);3893+ while ( clear_contributors < scatter_contributors )3894+ {3879- clear_contributors->n0 = 0;3880- clear_contributors->n1 = -1;3881- ++clear_contributors;3895+ clear_contributors->n0 = 0;3896+ clear_contributors->n1 = -1;3897+ ++clear_contributors;3898+ }3899 }3900+ scatter_contributors->n0 = n;3901+ scatter_contributors->n1 = n;3902+ scatter_coeffs[0] = gc;3903+ highest_set = k;3904 }3884- scatter_contributors->n0 = n;3885- scatter_contributors->n1 = n;3886- scatter_coeffs[0] = gc;3887- highest_set = k;3905+ else3906+ {3907+ stbir__insert_coeff( scatter_contributors, scatter_coeffs, n, gc );3908+ }3909+ STBIR_ASSERT( ( scatter_contributors->n1 - scatter_contributors->n0 + 1 ) <= scatter_coefficient_width );3910 }3889- else3890- {3891- stbir__insert_coeff( scatter_contributors, scatter_coeffs, n, gc );3892- }3911 ++scatter_contributors;3912 scatter_coeffs += scatter_coefficient_width;3913 }3914 6451 unmodified lines