Merge pull request #1913 from jeffrbig2/stb_image_resize_218
Before adoption: this commit came in with the history the project adopted, so no landing records it.
stb_image_resize 2.18
authored by Sean Barrett · committed by GitHub · parents 2fb8c5a, e7fc6c9 · Browse files · 1 file·+68 added−41 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+68 added−41 removed
- other:+68 added−41 removed
Provenance
- Commit
29ac5c4- Tree
1f2d98c- Author
Sean Barrett <sean2@nothings.org>- Committer
GitHub <noreply@github.com>- Parent 1
2fb8c5a- Parent 2
e7fc6c9
1 file changed+68 added−41 removed
modified:stb_image_resize2.h+68 added-41 removed
1-/* stb_image_resize2 - v2.17 - public domain image resizing1+/* stb_image_resize2 - v2.18 - public domain image resizing2 3 by Jeff Roberts (v2) and Jorge L Rodriguez4 http://github.com/nothings/stb5 134 unmodified lines140 images, expecting the input images to be unpremultiplied. This is what the141 COLOR+ALPHA buffer types tell the resizer to do.142 143 When you use the pixel layouts STBIR_RGBA, STBIR_BGRA, STBIR_ARGB,144- STBIR_ABGR, STBIR_RX, or STBIR_XR you are telling us that the pixels are144+ STBIR_ABGR, STBIR_RA, or STBIR_AR you are telling us that the pixels are145 non-premultiplied. In these cases, the resizer will alpha weight the colors146 (effectively creating the premultiplied image), do the filtering, and then147 convert back to non-premult on exit.148 149- When you use the pixel layouts STBIR_RGBA_PM, STBIR_RGBA_PM, STBIR_RGBA_PM,150- STBIR_RGBA_PM, STBIR_RX_PM or STBIR_XR_PM, you are telling that the pixels149+ When you use the pixel layouts STBIR_RGBA_PM, STBIR_BGRA_PM, STBIR_ARGB_PM,150+ STBIR_ABGR_PM, STBIR_RA_PM or STBIR_AR_PM, you are telling that the pixels151 ARE premultiplied. In this case, the resizer doesn't have to do the152 premultipling - it can filter directly on the input. This about twice as153 fast as the non-premultiplied case, so it's the right option if your data is154 already setup correctly.173 unmodified lines328 Aras Pranckevicius: bugfixes329 Nathan Reed: warning fixes for 1.0330 331 REVISIONS332+ 2.18 (2026-03-25) fixed coefficient calculation when skipping a coefficient off 333+ the left side of the window, added non-aligned access safe 334+ memcpy mode for scalar path, fixed various typos, and fixed 335+ define error in the float clamp output mode. 336 2.17 (2025-10-25) silly format bug in easy-to-use APIs.337 2.16 (2025-10-21) fixed the easy-to-use APIs to allow inverted bitmaps (negative338 strides), fix vertical filter kernel callback, fix threaded339 gather buffer priming (and assert).860 unmodified lines1200 #ifndef STBIR_AVX21201 #define STBIR_AVX21202 #endif1203 #if defined( _MSC_VER ) && !defined(__clang__)1200- #ifndef STBIR_FP16C // FP16C instructions are on all AVX2 cpus, so we can autoselect it here on microsoft - clang needs -m16c1204+ #ifndef STBIR_FP16C // FP16C instructions are on all AVX2 cpus, so we can autoselect it here on microsoft - clang needs -mf16c1205 #define STBIR_FP16C1206 #endif1207 #endif1208 #endif1362 unmodified lines2571 static const STBIR__SIMDF_CONST(STBIR_simd_point5, 0.5f);2572 static const STBIR__SIMDF_CONST(STBIR_ones, 1.0f);2573 static const STBIR__SIMDI_CONST(STBIR_almost_zero, (127 - 13) << 23);2574 static const STBIR__SIMDI_CONST(STBIR_almost_one, 0x3f7fffff);2571-static const STBIR__SIMDI_CONST(STBIR_mastissa_mask, 0xff);2575+static const STBIR__SIMDI_CONST(STBIR_mantissa_mask, 0xff);2576 static const STBIR__SIMDI_CONST(STBIR_topscale, 0x02000000);2577 2578 // Basically, in simd mode, we unroll the proper amount, and we don't want2579 // the non-simd remnant loops to be unroll because they only run a few times262 unmodified lines2842 char STBIR_SIMD_STREAMOUT_PTR (*) sd = (char*) src;2843 char STBIR_SIMD_STREAMOUT_PTR( * ) s_end = ((char*) src) + bytes;2844 ptrdiff_t ofs_to_dest = (char*)dest - (char*)src;2845 2842- if ( ofs_to_dest >= 8 ) // is the overlap more than 8 away?2846+ if ( ofs_to_dest >= 8 ) // is the overlap more than 8 away2847 {2848 char STBIR_SIMD_STREAMOUT_PTR( * ) s_end8 = ((char*) src) + (bytes&~7);2845- STBIR_NO_UNROLL_LOOP_START2846- do2849+2850+ if ( ( ( ((ptrdiff_t)dest)|((ptrdiff_t)src) ) & 7 ) == 0 ) // is it 8byte aligned?2851 {2852+ STBIR_NO_UNROLL_LOOP_START2853+ do2854+ {2848- STBIR_NO_UNROLL(sd);2849- *(stbir_uint64*)( sd + ofs_to_dest ) = *(stbir_uint64*) sd;2850- sd += 8;2851- } while ( sd < s_end8 );2855+ STBIR_NO_UNROLL(sd);2856+ *(stbir_uint64*)( sd + ofs_to_dest ) = *(stbir_uint64*) sd;2857+ sd += 8;2858+ } while ( sd < s_end8 );2859+ }2860+ else2861+ {2862+ STBIR_NO_UNROLL_LOOP_START2863+ do2864+ {2865+ STBIR_NO_UNROLL(sd);2866+ int a = ((int*)sd)[0];2867+ int b = ((int*)sd)[1];2868+ ((int*)( sd + ofs_to_dest ))[0] = a;2869+ ((int*)( sd + ofs_to_dest ))[1] = b;2870+ sd += 8;2871+ } while ( sd < s_end8 );2872+ }2873 2874 if ( sd == s_end )2875 return;2876 }384 unmodified lines3261 newspan->n0 = -left_margin;3262 newspan->n1 = ( max_left - min_left ) - left_margin;3263 scanline_extents->edge_sizes[0] = 0; // don't need to copy the left margin, since we are directly decoding into the margin3264 }3244- // if we can't merge the min_left range, add it as a second range3265+ // if we can't merge the min_right range, add it as a second range3266 else 3267 if ( ( right_margin ) && ( min_right != 0x7fffffff ) )3268 {3269 stbir__span * newspan = scanline_extents->spans + 1;107 unmodified lines3377 }3378 3379 static void stbir__insert_coeff( stbir__contributors * contribs, float * coeffs, int new_pixel, float new_coeff, int max_width )3380 {3360- if ( new_pixel <= contribs->n1 ) // before the end3381+ if ( contribs->n1 < contribs->n0 ) // this first clause should never happen, but handle in case3382 {3383+ contribs->n0 = contribs->n1 = new_pixel;3384+ coeffs[0] = new_coeff;3385+ }3386+ else if ( new_pixel <= contribs->n1 ) // before the end3387+ {3388 if ( new_pixel < contribs->n0 ) // before the front?3389 {3390 if ( ( contribs->n1 - new_pixel + 1 ) <= max_width )3391 { 3392 int j, o = contribs->n0 - new_pixel;3367- for ( j = contribs->n1 - contribs->n0 ; j <= 0 ; j-- )3393+ for ( j = contribs->n1 - contribs->n0 ; j >= 0 ; j-- )3394 coeffs[ j + o ] = coeffs[ j ];3369- for ( j = 1 ; j < o ; j-- )3370- coeffs[ j ] = coeffs[ 0 ];3395+ for ( j = 1 ; j < o ; j++ )3396+ coeffs[ j ] = 0;3397 coeffs[ 0 ] = new_coeff;3398 contribs->n0 = new_pixel;3399 }3400 }3401 else3402 {3403+ // add new weight to existing coeff if already there3404 coeffs[ new_pixel - contribs->n0 ] += new_coeff;3405 }3406 }3407 else438 unmodified lines3846 break;3847 }3848 }3849 3823- // some horizontal routines read one float off the end (which is then masked off), so put in a sentinal so we don't read an snan or denormal3850+ // some horizontal routines read one float off the end (which is then masked off), so put in a sentinel so we don't read an snan or denormal3851 coefficents[ widest * num_contributors ] = 8888.0f;3852 3853 // the minimum we might read for unrolled filters widths is 12. So, we need to3854 // make sure we never read outside the decode buffer, by possibly moving3043 unmodified lines6898 { 1.00000f, 1.00000f, 0.31250f, 1.00000f },6899 { 0.56250f, 0.59375f, 0.00000f, 0.96875f },6900 { 1.00000f, 0.06250f, 0.00000f, 1.00000f },6901 { 0.00000f, 0.09375f, 1.00000f, 1.00000f },6875- { 1.00000f, 1.00000f, 1.00000f, 1.00000f },6902+ { 1.00000f, 1.00000f, 0.31250f, 1.00000f },6903 { 0.03125f, 0.12500f, 1.00000f, 1.00000f },6877- { 0.06250f, 0.12500f, 0.00000f, 1.00000f },6904+ { 1.00000f, 1.00000f, 0.06250f, 1.00000f },6905 { 0.00000f, 1.00000f, 0.00000f, 0.03125f },6906 }, {6907 { 0.00000f, 0.84375f, 0.00000f, 0.03125f },6908 { 0.09375f, 0.93750f, 0.00000f, 0.78125f },6909 { 0.87500f, 0.21875f, 0.00000f, 0.96875f },6910 { 0.09375f, 0.09375f, 1.00000f, 1.00000f },6884- { 1.00000f, 1.00000f, 1.00000f, 1.00000f },6911+ { 0.00000f, 0.84375f, 0.00000f, 0.03125f },6912 { 0.03125f, 0.12500f, 1.00000f, 1.00000f },6886- { 0.06250f, 0.12500f, 0.00000f, 1.00000f },6913+ { 1.00000f, 1.00000f, 0.06250f, 1.00000f },6914 { 0.00000f, 1.00000f, 0.00000f, 0.53125f },6915 }, {6916 { 0.00000f, 0.53125f, 0.00000f, 0.03125f },6917 { 0.06250f, 0.96875f, 0.00000f, 0.53125f },6918 { 0.87500f, 0.18750f, 0.00000f, 0.93750f },6919 { 0.00000f, 0.09375f, 1.00000f, 1.00000f },6893- { 1.00000f, 1.00000f, 1.00000f, 1.00000f },6920+ { 0.00000f, 0.53125f, 0.00000f, 0.03125f },6921 { 0.03125f, 0.12500f, 1.00000f, 1.00000f },6895- { 0.06250f, 0.12500f, 0.00000f, 1.00000f },6922+ { 1.00000f, 1.00000f, 0.06250f, 1.00000f },6923 { 0.00000f, 1.00000f, 0.00000f, 0.56250f },6924 }, {6925 { 0.00000f, 0.50000f, 0.00000f, 0.71875f },6926 { 0.06250f, 0.84375f, 0.00000f, 0.87500f },6927 { 1.00000f, 0.50000f, 0.50000f, 0.96875f },6928 { 1.00000f, 0.09375f, 0.31250f, 0.50000f },6902- { 1.00000f, 1.00000f, 1.00000f, 1.00000f },6929+ { 0.00000f, 0.50000f, 0.00000f, 0.71875f },6930 { 1.00000f, 0.03125f, 0.03125f, 0.53125f },6904- { 0.18750f, 0.12500f, 0.00000f, 1.00000f },6931+ { 1.00000f, 1.00000f, 0.06250f, 1.00000f },6932 { 0.00000f, 1.00000f, 0.03125f, 0.18750f },6933 }, {6934 { 0.00000f, 0.59375f, 0.00000f, 0.96875f },6935 { 0.06250f, 0.81250f, 0.06250f, 0.59375f },6936 { 0.75000f, 0.43750f, 0.12500f, 0.96875f },6937 { 0.87500f, 0.06250f, 0.18750f, 0.43750f },6911- { 1.00000f, 1.00000f, 1.00000f, 1.00000f },6938+ { 0.00000f, 0.59375f, 0.00000f, 0.96875f },6939 { 0.15625f, 0.12500f, 1.00000f, 1.00000f },6913- { 0.06250f, 0.12500f, 0.00000f, 1.00000f },6940+ { 1.00000f, 1.00000f, 0.06250f, 1.00000f },6941 { 0.00000f, 1.00000f, 0.03125f, 0.34375f },6942 }6943 };6944 45 unmodified lines6990 6991 // categorize the resize into buckets6992 if ( ( vertical_output_size <= 4 ) || ( horizontal_output_size <= 4 ) )6993 v_classification = ( vertical_output_size < horizontal_output_size ) ? 6 : 7;6994+ else if ( ( !is_gather ) && ( ( vertical_output_size <= 16 ) || ( horizontal_output_size <= 16 ) ) )6995+ v_classification = 4;6996 else if ( vertical_scale <= 1.0f )6997 v_classification = ( is_gather ) ? 1 : 0;6998 else if ( vertical_scale <= 2.0f)6999 v_classification = 2;7000 else if ( vertical_scale <= 3.0f)7001 v_classification = 3;6973- else if ( vertical_scale <= 4.0f)6974- v_classification = 5;6975- else6976- v_classification = 6;7002+ else 7003+ v_classification = 5; // everything bigger than 3x7004 7005 // use the right weights7006 weights = weights_table[ v_classification ];7007 615 unmodified lines7623 numer_last = numer_estimate;7624 numer_estimate = temp;7625 }7626 7600- // we didn't fine anything good enough for float, use a full range estimate7627+ // we didn't find anything good enough for float, use a full range estimate7628 if ( limit_denom )7629 {7630 numer_estimate= (stbir_uint64)( f * (double)limit + 0.5 );7631 denom_estimate = limit;312 unmodified lines7944 7945 return 0;7946 }7947 7921-void stbir_free_samplers( STBIR_RESIZE * resize )7948+STBIRDEF void stbir_free_samplers( STBIR_RESIZE * resize )7949 {7950 if ( resize->samplers )7951 {7952 stbir__free_internal_mem( resize->samplers );205 unmodified lines8158 #ifdef STBIR_PROFILE8159 8160 STBIRDEF void stbir_resize_build_profile_info( STBIR_PROFILE_INFO * info, STBIR_RESIZE const * resize )8161 {8135- static char const * bdescriptions[6] = { "Building", "Allocating", "Horizontal sampler", "Vertical sampler", "Coefficient cleanup", "Coefficient piovot" } ;8162+ static char const * bdescriptions[6] = { "Building", "Allocating", "Horizontal sampler", "Vertical sampler", "Coefficient cleanup", "Coefficient pivot" } ;8163 stbir__info* samp = resize->samplers;8164 int i;8165 8166 typedef int testa[ (STBIR__ARRAY_SIZE( bdescriptions ) == (STBIR__ARRAY_SIZE( samp->profile.array )-1) )?1:-1];42 unmodified lines8209 sum += split_info[s].profile.array[i+1];8210 info->clocks[i] = sum;8211 }8212 8186- info->total_clocks = split_info->profile.named.total;8213+ info->total_clocks = split_info->profile.named.total; 8214 info->descriptions = descriptions;8215 info->count = STBIR__ARRAY_SIZE( descriptions );8216 }8217 539 unmodified lines8757 #define stbir__linear_to_srgb_finish( i, f ) \8758 { \8759 stbir__simdi temp; \8760 stbir__simdi_32shr( temp, stbir_simdi_castf( f ), 12 ) ; \8734- stbir__simdi_and( temp, temp, STBIR__CONSTI(STBIR_mastissa_mask) ); \8761+ stbir__simdi_and( temp, temp, STBIR__CONSTI(STBIR_mantissa_mask) ); \8762 stbir__simdi_or( temp, temp, STBIR__CONSTI(STBIR_topscale) ); \8763 stbir__simdi_16madd( i, i, temp ); \8764 stbir__simdi_32shr( i, i, 16 ); \8765 }232 unmodified lines8998 }8999 decode -= 4;9000 if( decode < decode_end )9001 {8975- decode[0] = stbir__srgb_uchar_to_linear_float[ stbir__decode_order0 ];9002+ decode[0] = stbir__srgb_uchar_to_linear_float[ input[stbir__decode_order0] ];9003 decode[1] = ( (float) input[stbir__decode_order1] ) * stbir__max_uint8_as_float_inverted;9004 }9005 return decode_end;9006 }727 unmodified lines9734 }9735 9736 static void STBIR__CODER_NAME( stbir__encode_float_linear )( void * outputp, int width_times_channels, float const * encode )9737 {9711- #if !defined( STBIR_FLOAT_HIGH_CLAMP ) && !defined(STBIR_FLOAT_LO_CLAMP) && !defined(stbir__decode_swizzle)9738+ #if !defined( STBIR_FLOAT_HIGH_CLAMP ) && !defined(STBIR_FLOAT_LOW_CLAMP) && !defined(stbir__decode_swizzle)9739 9740 if ( (void*)outputp != (void*) encode )9741 STBIR_MEMCPY( outputp, encode, width_times_channels * sizeof( float ) );9742 46 unmodified lines9789 stbir__simdfX_store( output, e0 );9790 stbir__simdfX_store( output+stbir__simdfX_float_count, e1 );9791 encode += stbir__simdfX_float_count * 2;9792 output += stbir__simdfX_float_count * 2;9766- if ( output < end_output )9793+ if ( output <= end_output )9794 continue;9795 if ( output == ( end_output + ( stbir__simdfX_float_count * 2 ) ) )9796 break;9797 output = end_output; // backup and do last couple881 unmodified lines