Skip to content

Commit af07f9d

Browse files
committed
image resize 2.11 - fix harmless asan, fix point sample crash
1 parent f75e8d1 commit af07f9d

File tree

1 file changed

+78
-50
lines changed

1 file changed

+78
-50
lines changed

stb_image_resize2.h

+78-50
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* stb_image_resize2 - v2.10 - public domain image resizing
1+
/* stb_image_resize2 - v2.11 - public domain image resizing
22
33
by Jeff Roberts (v2) and Jorge L Rodriguez
44
http://github.com/nothings/stb
@@ -11,35 +11,6 @@
1111
#define STB_IMAGE_RESIZE_IMPLEMENTATION
1212
before the #include. That will create the implementation in that file.
1313
14-
PORTING FROM VERSION 1
15-
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 unchanged
30-
- cast channel count to `stbir_pixel_layout`
31-
- above is sufficient unless your image has alpha and it's not RGBA/BGRA
32-
- in that case, follow the below instructions for stbir_resize_uint8_srgb_edgemode
33-
34-
stbir_resize_uint8_srgb_edgemode()
35-
- switch to the "medium complexity" API
36-
- 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_SRGB
39-
- edge: unchanged (STBIR_EDGE_WRAP, etc.)
40-
- filter: STBIR_FILTER_DEFAULT
41-
- which channel is alpha is specified in stbir_pixel_layout, see enum for details
42-
4314
EASY API CALLS:
4415
Easy API downsamples w/Mitchell filter, upsamples w/cubic interpolation, clamps to edge.
4516
@@ -296,6 +267,34 @@
296267
ASSERT
297268
Define STBIR_ASSERT(boolval) to override assert() and not use assert.h
298269
270+
PORTING FROM VERSION 1
271+
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 unchanged
285+
- cast channel count to `stbir_pixel_layout`
286+
- above is sufficient unless your image has alpha and it's not RGBA/BGRA
287+
- in that case, follow the below instructions for stbir_resize_uint8_srgb_edgemode
288+
289+
stbir_resize_uint8_srgb_edgemode()
290+
- switch to the "medium complexity" API
291+
- 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_SRGB
294+
- edge: unchanged (STBIR_EDGE_WRAP, etc.)
295+
- filter: STBIR_FILTER_DEFAULT
296+
- which channel is alpha is specified in stbir_pixel_layout, see enum for details
297+
299298
FUTURE TODOS
300299
* For polyphase integral filters, we just memcpy the coeffs to dupe
301300
them, but we should indirect and use the same coeff memory.
@@ -328,6 +327,9 @@
328327
Nathan Reed: warning fixes for 1.0
329328
330329
REVISIONS
330+
2.11 (2024-09-08) fix harmless asan warnings in 2-channel and 3-channel mode
331+
with AVX-2, fix some weird scaling edge conditions with
332+
point sample mode.
331333
2.10 (2024-07-27) fix the defines GCC and mingw for loop unroll control,
332334
fix MSVC 32-bit arm half float routines.
333335
2.09 (2024-06-19) fix the defines for 32-bit ARM GCC builds (was selecting
@@ -3247,6 +3249,7 @@ static void stbir__calculate_in_pixel_range( int * first_pixel, int * last_pixel
32473249

32483250
first = (int)(STBIR_FLOORF(in_pixel_influence_lowerbound + 0.5f));
32493251
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 cross
32503253

32513254
if ( edge == STBIR_EDGE_WRAP )
32523255
{
@@ -3282,6 +3285,11 @@ static void stbir__calculate_coefficients_for_gather_upsample( float out_filter_
32823285

32833286
stbir__calculate_in_pixel_range( &in_first_pixel, &in_last_pixel, out_pixel_center, out_filter_radius, inv_scale, out_shift, input_size, edge );
32843287

3288+
// make sure we never generate a range larger than our precalculated coeff width
3289+
// this only happens in point sample mode, but it's a good safe thing to do anyway
3290+
if ( ( in_last_pixel - in_first_pixel + 1 ) > coefficient_width )
3291+
in_last_pixel = in_first_pixel + coefficient_width - 1;
3292+
32853293
last_non_zero = -1;
32863294
for (i = 0; i <= in_last_pixel - in_first_pixel; i++)
32873295
{
@@ -3317,19 +3325,22 @@ static void stbir__calculate_coefficients_for_gather_upsample( float out_filter_
33173325
}
33183326
}
33193327

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 )
33213329
{
33223330
if ( new_pixel <= contribs->n1 ) // before the end
33233331
{
33243332
if ( new_pixel < contribs->n0 ) // before the front?
33253333
{
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;
3334+
if ( ( contribs->n1 - new_pixel + 1 ) <= max_width )
3335+
{
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+
}
33333344
}
33343345
else
33353346
{
@@ -3338,12 +3349,15 @@ static void stbir__insert_coeff( stbir__contributors * contribs, float * coeffs,
33383349
}
33393350
else
33403351
{
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 any
3343-
coeffs[j] = 0;
3352+
if ( ( new_pixel - contribs->n0 + 1 ) <= max_width )
3353+
{
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 any
3356+
coeffs[j] = 0;
33443357

3345-
coeffs[ e ] = new_coeff;
3346-
contribs->n1 = new_pixel;
3358+
coeffs[ e ] = new_coeff;
3359+
contribs->n1 = new_pixel;
3360+
}
33473361
}
33483362
}
33493363

@@ -3522,6 +3536,7 @@ static void stbir__cleanup_gathered_coefficients( stbir_edge edge, stbir__filter
35223536

35233537
coeffs = coefficient_group;
35243538
contribs = contributors;
3539+
35253540
for (n = 0; n < num_contributors; n++)
35263541
{
35273542
int i;
@@ -3561,7 +3576,7 @@ static void stbir__cleanup_gathered_coefficients( stbir_edge edge, stbir__filter
35613576
int endi = contribs->n1;
35623577
contribs->n1 = input_last_n1;
35633578
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 );
35653580
}
35663581

35673582
// now check left hand edge
@@ -3573,7 +3588,7 @@ static void stbir__cleanup_gathered_coefficients( stbir_edge edge, stbir__filter
35733588

35743589
// reinsert the coeffs with it reflected or clamped (insert accumulates, if the coeffs exist)
35753590
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 );
35773592
save_n0 = contribs->n0;
35783593
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)!
35793594

@@ -3583,7 +3598,7 @@ static void stbir__cleanup_gathered_coefficients( stbir_edge edge, stbir__filter
35833598
coeffs[i] = coeffs[i-save_n0];
35843599

35853600
// now that we have shrunk down the contribs, we insert the first one safely
3586-
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 );
35873602
}
35883603
}
35893604

@@ -3592,6 +3607,7 @@ static void stbir__cleanup_gathered_coefficients( stbir_edge edge, stbir__filter
35923607
int diff = contribs->n1 - contribs->n0 + 1;
35933608
while ( diff && ( coeffs[ diff-1 ] == 0.0f ) )
35943609
--diff;
3610+
35953611
contribs->n1 = contribs->n0 + diff - 1;
35963612

35973613
if ( contribs->n0 <= contribs->n1 )
@@ -3964,7 +3980,7 @@ static void stbir__calculate_filters( stbir__sampler * samp, stbir__sampler * ot
39643980
}
39653981
else
39663982
{
3967-
stbir__insert_coeff( scatter_contributors, scatter_coeffs, n, gc );
3983+
stbir__insert_coeff( scatter_contributors, scatter_coeffs, n, gc, scatter_coefficient_width );
39683984
}
39693985
STBIR_ASSERT( ( scatter_contributors->n1 - scatter_contributors->n0 + 1 ) <= scatter_coefficient_width );
39703986
}
@@ -4810,12 +4826,13 @@ static void stbir__decode_scanline(stbir__info const * stbir_info, int n, float
48104826
stbir__simdf8_madd_mem( tot0, tot0, c, decode+(ofs)*2 );
48114827

48124828
#define stbir__1_coeff_remnant( ofs ) \
4813-
{ stbir__simdf t; \
4829+
{ stbir__simdf t,d; \
48144830
stbir__simdf_load1z( t, hc + (ofs) ); \
4831+
stbir__simdf_load2( d, decode + (ofs) * 2 ); \
48154832
stbir__simdf_0123to0011( t, t ); \
4816-
stbir__simdf_mult_mem( t, t, decode+(ofs)*2 ); \
4833+
stbir__simdf_mult( t, t, d ); \
48174834
stbir__simdf8_add4( tot0, tot0, t ); }
4818-
4835+
48194836
#define stbir__2_coeff_remnant( ofs ) \
48204837
{ stbir__simdf t; \
48214838
stbir__simdf_load2( t, hc + (ofs) ); \
@@ -7112,6 +7129,11 @@ static stbir__info * stbir__alloc_internal_mem_and_build_samplers( stbir__sample
71127129

71137130
#ifdef STBIR__SEPARATE_ALLOCATIONS
71147131
temp_mem_amt = decode_buffer_size;
7132+
7133+
#ifdef STBIR_SIMD8
7134+
if ( effective_channels == 3 )
7135+
--temp_mem_amt; // avx in 3 channel mode needs one float at the start of the buffer
7136+
#endif
71157137
#else
71167138
temp_mem_amt = ( decode_buffer_size + ring_buffer_size + vertical_buffer_size ) * splits;
71177139
#endif
@@ -7217,6 +7239,12 @@ static stbir__info * stbir__alloc_internal_mem_and_build_samplers( stbir__sample
72177239
int t, ofs, start;
72187240

72197241
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 clearing
7246+
#endif
7247+
72207248
start = ofs - 4;
72217249
if ( start < 0 ) start = 0;
72227250

0 commit comments

Comments
 (0)