Skip to content
Myr

stb_image_resize 2.16 - fixed the easy-to-use APIs to allow inverted bitmaps (negative strides), fix vertical filter kernel callback, fix threaded gather buffer priming (and assert)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Jeff Roberts (LA) · committed by Jeff Roberts (LA) · parent fede005 · Browse files · 1 file+127 added−107 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+127 added−107 removed

  • other:+127 added−107 removed

1 file+127 added−107 removed

modified:stb_image_resize2.h+127 added-107 removed
1/* stb_image_resize2 - v2.15 - public domain image resizing1/* stb_image_resize2 - v2.16 - public domain image resizing23 by Jeff Roberts (v2) and Jorge L Rodriguez4 http://github.com/nothings/stb5
322 unmodified lines
328 Aras Pranckevicius: bugfixes329 Nathan Reed: warning fixes for 1.0330331 REVISIONS332 2.16 (2025-10-21) fixed the easy-to-use APIs to allow inverted bitmaps (negative333 strides), fix vertical filter kernel callback, fix threaded334 gather buffer priming (and assert).335 (thanks adipose, TainZerL, and Harrison Green)336 2.15 (2025-07-17) fixed an assert in debug mode when using floats with input337 callbacks, work around GCC warning when adding to null ptr338 (thanks Johannes Spohr and Pyry Kovanen).339 2.14 (2025-05-09) fixed a bug using downsampling gather horizontal first, and
6374 unmodified lines
6714 range->n1 = input_full_size - 1;6715 }6716}67176714static void stbir__get_split_info( stbir__per_split_info* split_info, int splits, int output_height, int vertical_pixel_margin, int input_full_height )6718static void stbir__get_split_info( stbir__per_split_info* split_info, int splits, int output_height, int vertical_pixel_margin, int input_full_height, int is_gather, stbir__contributors * contribs )6719{6720 int i, cur;6721 int left = output_height;67226723 cur = 0;6724 for( i = 0 ; i < splits ; i++ )6725 {6726 int each;67276728 split_info[i].start_output_y = cur;6729 each = left / ( splits - i );6730 split_info[i].end_output_y = cur + each;67316732 // ok, when we are gathering, we need to make sure we are starting on a y offset that doesn't have6733 // a "special" set of coefficients. Basically, with exactly the right filter at exactly the right6734 // resize at exactly the right phase, some of the coefficents can be zero. When they are zero, we6735 // don't process them at all. But this leads to a tricky thing with the thread splits, where we6736 // might have a set of two coeffs like this for example: (4,4) and (3,6). The 4,4 means there was6737 // just one single coeff because things worked out perfectly (normally, they all have 4 coeffs6738 // like the range 3,6. The problem is that if we start right on the (4,4) on a brand new thread,6739 // then when we get to (3,6), we don't have the "3" sample in memory (because we didn't load6740 // it on the initial (4,4) range because it didn't have a 3 (we only add new samples that are 6741 // larger than our existing samples - it's just how the eviction works). So, our solution here6742 // is pretty simple, if we start right on a range that has samples that start earlier, then we 6743 // simply bump up our previous thread split range to include it, and then start this threads6744 // range with the smaller sample. It just moves one scanline from one thread split to another,6745 // so that we end with the unusual one, instead of start with it. To do this, we check 2-4 6746 // sample at each thread split start and then occassionally move them.6747 6748 if ( ( is_gather ) && ( i ) )6749 {6750 stbir__contributors * small_contribs;6751 int j, smallest, stop, start_n0;6752 stbir__contributors * split_contribs = contribs + cur;67536754 // scan for a max of 3x the filter width or until the next thread split6755 stop = vertical_pixel_margin * 3;6756 if ( each < stop )6757 stop = each;67586759 // loops a few times before early out6760 smallest = 0;6761 small_contribs = split_contribs;6762 start_n0 = small_contribs->n0;6763 for( j = 1 ; j <= stop ; j++ )6764 {6765 ++split_contribs;6766 if ( split_contribs->n0 > start_n0 )6767 break;6768 if ( split_contribs->n0 < small_contribs->n0 )6769 {6770 small_contribs = split_contribs;6771 smallest = j;6772 }6773 }67746775 split_info[i-1].end_output_y += smallest;6776 split_info[i].start_output_y += smallest;6777 }67786779 cur += each;6780 left -= each;67816782 // scatter range (updated to minimum as you run it)
538 unmodified lines
7321 STBIR_PROFILE_BUILD_END( vertical );7322 }73237324 // setup the vertical split ranges7272 stbir__get_split_info( info->split_info, info->splits, info->vertical.scale_info.output_sub_size, info->vertical.filter_pixel_margin, info->vertical.scale_info.input_full_size );7325 stbir__get_split_info( info->split_info, info->splits, info->vertical.scale_info.output_sub_size, info->vertical.filter_pixel_margin, info->vertical.scale_info.input_full_size, info->vertical.is_gather, info->vertical.contributors );73267327 // now we know precisely how many entries we need7328 info->ring_buffer_num_entries = info->vertical.extent_info.widest;7329
555 unmodified lines
7885 return 0;78867887 stbir__set_sampler(&horizontal, resize->horizontal_filter, resize->horizontal_filter_kernel, resize->horizontal_filter_support, resize->horizontal_edge, &horizontal.scale_info, 1, resize->user_data );7888 stbir__get_conservative_extents( &horizontal, &conservative, resize->user_data );7836 stbir__set_sampler(&vertical, resize->vertical_filter, resize->horizontal_filter_kernel, resize->vertical_filter_support, resize->vertical_edge, &vertical.scale_info, 0, resize->user_data );7889 stbir__set_sampler(&vertical, resize->vertical_filter, resize->vertical_filter_kernel, resize->vertical_filter_support, resize->vertical_edge, &vertical.scale_info, 0, resize->user_data );78907891 if ( ( vertical.scale_info.output_sub_size / splits ) < STBIR_FORCE_MINIMUM_SCANLINES_FOR_SPLITS ) // each split should be a minimum of 4 scanlines (handwavey choice)7892 {7893 splits = vertical.scale_info.output_sub_size / STBIR_FORCE_MINIMUM_SCANLINES_FOR_SPLITS;
116 unmodified lines
8010 // do resize8011 return stbir__perform_resize( resize->samplers, split_start, split_count );8012}80137961static int stbir__check_output_stuff( void ** ret_ptr, int * ret_pitch, void * output_pixels, int type_size, int output_w, int output_h, int output_stride_in_bytes, stbir_internal_pixel_layout pixel_layout )80148015static void * stbir_quick_resize_helper( const void *input_pixels , int input_w , int input_h, int input_stride_in_bytes,8016 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,8017 stbir_pixel_layout pixel_layout, stbir_datatype data_type, stbir_edge edge, stbir_filter filter )8018{7963 size_t size;7964 int pitch;7965 void * ptr;8019 STBIR_RESIZE resize;8020 int scanline_output_in_bytes;8021 int positive_output_stride_in_bytes;8022 void * start_ptr;8023 void * free_ptr;80247967 pitch = output_w * type_size * stbir__pixel_channels[ pixel_layout ];7968 if ( pitch == 0 )8025 scanline_output_in_bytes = output_w * stbir__type_size[ data_type ] * stbir__pixel_channels[ pixel_layout ];8026 if ( scanline_output_in_bytes == 0 )8027 return 0;80288029 // if zero stride, use scanline output8030 if ( output_stride_in_bytes == 0 )7972 output_stride_in_bytes = pitch;8031 output_stride_in_bytes = scanline_output_in_bytes;80327974 if ( output_stride_in_bytes < pitch )7975 return 0;8033 // abs value for inverted images (negative pitches)8034 positive_output_stride_in_bytes = output_stride_in_bytes;8035 if ( positive_output_stride_in_bytes < 0 )8036 positive_output_stride_in_bytes = -positive_output_stride_in_bytes;80377977 size = (size_t)output_stride_in_bytes * (size_t)output_h;7978 if ( size == 0 )8038 // is the requested stride smaller than the scanline output? if so, just fail8039 if ( positive_output_stride_in_bytes < scanline_output_in_bytes )8040 return 0;80417981 *ret_ptr = 0;7982 *ret_pitch = output_stride_in_bytes;8042 start_ptr = output_pixels;8043 free_ptr = 0; // no free pointer, since they passed buffer to use80448045 // did they pass a zero for the dest? if so, allocate the buffer8046 if ( output_pixels == 0 )8047 {8048 size_t size;8049 char * ptr;8050 8051 size = (size_t)positive_output_stride_in_bytes * (size_t)output_h;8052 if ( size == 0 )8053 return 0;80547986 ptr = STBIR_MALLOC( size, 0 );8055 ptr = (char*) STBIR_MALLOC( size, 0 );8056 if ( ptr == 0 )8057 return 0;80587990 *ret_ptr = ptr;7991 *ret_pitch = pitch;8059 free_ptr = ptr;80608061 // point at the last scanline, if they requested a flipped image8062 if ( output_stride_in_bytes < 0 )8063 start_ptr = ptr + ( (size_t)positive_output_stride_in_bytes * (size_t)( output_h - 1 ) );8064 else8065 start_ptr = ptr;8066 }80677994 return 1;7995}799679977998STBIRDEF unsigned char * stbir_resize_uint8_linear( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes,7999 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,8000 stbir_pixel_layout pixel_layout )8001{8002 STBIR_RESIZE resize;8003 unsigned char * optr;8004 int opitch;80058006 if ( !stbir__check_output_stuff( (void**)&optr, &opitch, output_pixels, sizeof( unsigned char ), output_w, output_h, output_stride_in_bytes, stbir__pixel_layout_convert_public_to_internal[ pixel_layout ] ) )8007 return 0;80088068 // ok, now do the resize8069 stbir_resize_init( &resize,8070 input_pixels, input_w, input_h, input_stride_in_bytes,8011 (optr) ? optr : output_pixels, output_w, output_h, opitch,8012 pixel_layout, STBIR_TYPE_UINT8 );8071 start_ptr, output_w, output_h, output_stride_in_bytes,8072 pixel_layout, data_type );80738074 resize.horizontal_edge = edge;8075 resize.vertical_edge = edge;8076 resize.horizontal_filter = filter;8077 resize.vertical_filter = filter;80788079 if ( !stbir_resize_extended( &resize ) )8080 {8016 if ( optr )8017 STBIR_FREE( optr, 0 );8081 if ( free_ptr )8082 STBIR_FREE( free_ptr, 0 );8083 return 0;8084 }80858021 return (optr) ? optr : output_pixels;8086 return (free_ptr) ? free_ptr : start_ptr;8087}8088808980908091STBIRDEF unsigned char * stbir_resize_uint8_linear( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes,8092 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,8093 stbir_pixel_layout pixel_layout )8094{8095 return (unsigned char *) stbir_quick_resize_helper( input_pixels , input_w , input_h, input_stride_in_bytes, 8096 output_pixels, output_w, output_h, output_stride_in_bytes, 8097 pixel_layout, STBIR_TYPE_UINT8, STBIR_EDGE_CLAMP, STBIR_FILTER_DEFAULT );8098}80998100STBIRDEF unsigned char * stbir_resize_uint8_srgb( const unsigned char *input_pixels , int input_w , int input_h, int input_stride_in_bytes,8101 unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,8102 stbir_pixel_layout pixel_layout )8103{8028 STBIR_RESIZE resize;8029 unsigned char * optr;8030 int opitch;80318032 if ( !stbir__check_output_stuff( (void**)&optr, &opitch, output_pixels, sizeof( unsigned char ), output_w, output_h, output_stride_in_bytes, stbir__pixel_layout_convert_public_to_internal[ pixel_layout ] ) )8033 return 0;80348035 stbir_resize_init( &resize,8036 input_pixels, input_w, input_h, input_stride_in_bytes,8037 (optr) ? optr : output_pixels, output_w, output_h, opitch,8038 pixel_layout, STBIR_TYPE_UINT8_SRGB );80398040 if ( !stbir_resize_extended( &resize ) )8041 {8042 if ( optr )8043 STBIR_FREE( optr, 0 );8044 return 0;8045 }80468047 return (optr) ? optr : output_pixels;8104 return (unsigned char *) stbir_quick_resize_helper( input_pixels , input_w , input_h, input_stride_in_bytes, 8105 output_pixels, output_w, output_h, output_stride_in_bytes, 8106 pixel_layout, STBIR_TYPE_UINT8_SRGB, STBIR_EDGE_CLAMP, STBIR_FILTER_DEFAULT );8107}810881098110STBIRDEF float * stbir_resize_float_linear( const float *input_pixels , int input_w , int input_h, int input_stride_in_bytes,8111 float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,8112 stbir_pixel_layout pixel_layout )8113{8055 STBIR_RESIZE resize;8056 float * optr;8057 int opitch;80588059 if ( !stbir__check_output_stuff( (void**)&optr, &opitch, output_pixels, sizeof( float ), output_w, output_h, output_stride_in_bytes, stbir__pixel_layout_convert_public_to_internal[ pixel_layout ] ) )8060 return 0;80618062 stbir_resize_init( &resize,8063 input_pixels, input_w, input_h, input_stride_in_bytes,8064 (optr) ? optr : output_pixels, output_w, output_h, opitch,8065 pixel_layout, STBIR_TYPE_FLOAT );80668067 if ( !stbir_resize_extended( &resize ) )8068 {8069 if ( optr )8070 STBIR_FREE( optr, 0 );8071 return 0;8072 }80738074 return (optr) ? optr : output_pixels;8114 return (float *) stbir_quick_resize_helper( input_pixels , input_w , input_h, input_stride_in_bytes, 8115 output_pixels, output_w, output_h, output_stride_in_bytes, 8116 pixel_layout, STBIR_TYPE_FLOAT, STBIR_EDGE_CLAMP, STBIR_FILTER_DEFAULT );8117}811881198120STBIRDEF void * stbir_resize( const void *input_pixels , int input_w , int input_h, int input_stride_in_bytes,8121 void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,8080 stbir_pixel_layout pixel_layout, stbir_datatype data_type,8081 stbir_edge edge, stbir_filter filter )8122 stbir_pixel_layout pixel_layout, stbir_datatype data_type,8123 stbir_edge edge, stbir_filter filter )8124{8083 STBIR_RESIZE resize;8084 float * optr;8085 int opitch;80868087 if ( !stbir__check_output_stuff( (void**)&optr, &opitch, output_pixels, stbir__type_size[data_type], output_w, output_h, output_stride_in_bytes, stbir__pixel_layout_convert_public_to_internal[ pixel_layout ] ) )8088 return 0;80898090 stbir_resize_init( &resize,8091 input_pixels, input_w, input_h, input_stride_in_bytes,8092 (optr) ? optr : output_pixels, output_w, output_h, output_stride_in_bytes,8093 pixel_layout, data_type );8125 return (void *) stbir_quick_resize_helper( input_pixels , input_w , input_h, input_stride_in_bytes, 8126 output_pixels, output_w, output_h, output_stride_in_bytes, 8127 pixel_layout, data_type, edge, filter );80948095 resize.horizontal_edge = edge;8096 resize.vertical_edge = edge;8097 resize.horizontal_filter = filter;8098 resize.vertical_filter = filter;80998100 if ( !stbir_resize_extended( &resize ) )8101 {8102 if ( optr )8103 STBIR_FREE( optr, 0 );8104 return 0;8105 }81068107 return (optr) ? optr : output_pixels;8128}81298130#ifdef STBIR_PROFILE8131
2519 unmodified lines