Decode enough scanlines into a ring buffer to make sure that we have enough source scanlines to do a vertical sampling.
This commit is contained in:
parent
158effb62a
commit
152965f334
230
stb_resample.h
230
stb_resample.h
@ -160,6 +160,20 @@ typedef struct
|
|||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
|
const void* input_data;
|
||||||
|
int input_w;
|
||||||
|
int input_h;
|
||||||
|
int input_stride_bytes;
|
||||||
|
|
||||||
|
void* output_data;
|
||||||
|
int output_w;
|
||||||
|
int output_h;
|
||||||
|
int output_stride_bytes;
|
||||||
|
|
||||||
|
int channels;
|
||||||
|
stbr_type type;
|
||||||
|
stbr_filter filter;
|
||||||
|
|
||||||
int total_coefficients;
|
int total_coefficients;
|
||||||
int kernel_texel_width;
|
int kernel_texel_width;
|
||||||
|
|
||||||
@ -167,6 +181,11 @@ typedef struct
|
|||||||
float* horizontal_coefficients;
|
float* horizontal_coefficients;
|
||||||
|
|
||||||
float* decode_buffer;
|
float* decode_buffer;
|
||||||
|
|
||||||
|
int ring_buffer_first_scanline;
|
||||||
|
int ring_buffer_last_scanline;
|
||||||
|
int ring_buffer_begin_index;
|
||||||
|
float* ring_buffer;
|
||||||
} stbr__info;
|
} stbr__info;
|
||||||
|
|
||||||
|
|
||||||
@ -219,7 +238,7 @@ stbr_inline static stbr_size_t stbr__texel_index(int x, int y, int c, int width_
|
|||||||
|
|
||||||
stbr_inline static stbr__contributors* stbr__get_contributor(stbr__info* stbr_info, int n)
|
stbr_inline static stbr__contributors* stbr__get_contributor(stbr__info* stbr_info, int n)
|
||||||
{
|
{
|
||||||
STBR_DEBUG_ASSERT(n >= 0 /*&& n < output_w*/);
|
STBR_DEBUG_ASSERT(n >= 0 && n < stbr_info->output_w);
|
||||||
return &stbr_info->horizontal_contributors[n];
|
return &stbr_info->horizontal_contributors[n];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,37 +247,46 @@ stbr_inline static float* stbr__get_coefficient(stbr__info* stbr_info, int n, in
|
|||||||
return &stbr_info->horizontal_coefficients[stbr_info->kernel_texel_width*n + c];
|
return &stbr_info->horizontal_coefficients[stbr_info->kernel_texel_width*n + c];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// What input texels contribute to this output texel?
|
||||||
|
static void stbr__calculate_sample_range(int n, float out_filter_radius, float scale_ratio, int* in_first_texel, int* in_last_texel, float* in_center_of_out)
|
||||||
|
{
|
||||||
|
// What input texels contribute to this output texel?
|
||||||
|
float out_texel_center = (float)n + 0.5f;
|
||||||
|
float out_texel_influence_lowerbound = out_texel_center - out_filter_radius;
|
||||||
|
float out_texel_influence_upperbound = out_texel_center + out_filter_radius;
|
||||||
|
|
||||||
|
float in_texel_influence_lowerbound = out_texel_influence_lowerbound / scale_ratio;
|
||||||
|
float in_texel_influence_upperbound = out_texel_influence_upperbound / scale_ratio;
|
||||||
|
|
||||||
|
*in_center_of_out = out_texel_center / scale_ratio;
|
||||||
|
*in_first_texel = (int)(floor(in_texel_influence_lowerbound + 0.5));
|
||||||
|
*in_last_texel = (int)(floor(in_texel_influence_upperbound - 0.5));
|
||||||
|
}
|
||||||
|
|
||||||
// Each scan line uses the same kernel values so we should calculate the kernel
|
// Each scan line uses the same kernel values so we should calculate the kernel
|
||||||
// values once and then we can use them for every scan line.
|
// values once and then we can use them for every scan line.
|
||||||
static void stbr__calculate_horizontal_filters(stbr__info* stbr_info, stbr_filter filter, int input_w, int output_w)
|
static void stbr__calculate_horizontal_filters(stbr__info* stbr_info)
|
||||||
{
|
{
|
||||||
int n, i;
|
int n, i;
|
||||||
float scale_ratio = (float)output_w / input_w;
|
float scale_ratio = (float)stbr_info->output_w / stbr_info->input_w;
|
||||||
|
|
||||||
float out_pixels_radius = stbr__filter_info_table[filter].support * scale_ratio;
|
float out_pixels_radius = stbr__filter_info_table[stbr_info->filter].support * scale_ratio;
|
||||||
|
|
||||||
STBR_UNIMPLEMENTED(output_w < input_w);
|
STBR_UNIMPLEMENTED(stbr_info->output_w < stbr_info->input_w);
|
||||||
|
|
||||||
for (n = 0; n < output_w; n++)
|
for (n = 0; n < stbr_info->output_w; n++)
|
||||||
{
|
{
|
||||||
// What input texels contribute to this output texel?
|
float in_center_of_out; // Center of the current out texel in the in texel space
|
||||||
float out_texel_center = (float)n + 0.5f;
|
int in_first_texel, in_last_texel;
|
||||||
float out_texel_influence_lowerbound = out_texel_center - out_pixels_radius;
|
|
||||||
float out_texel_influence_upperbound = out_texel_center + out_pixels_radius;
|
|
||||||
|
|
||||||
float in_center_of_out = out_texel_center / scale_ratio;
|
stbr__calculate_sample_range(n, out_pixels_radius, scale_ratio, &in_first_texel, &in_last_texel, &in_center_of_out);
|
||||||
float in_texel_influence_lowerbound = out_texel_influence_lowerbound / scale_ratio;
|
|
||||||
float in_texel_influence_upperbound = out_texel_influence_upperbound / scale_ratio;
|
|
||||||
|
|
||||||
int in_first_texel = (int)(floor(in_texel_influence_lowerbound + 0.5));
|
|
||||||
int in_last_texel = (int)(floor(in_texel_influence_upperbound - 0.5));
|
|
||||||
|
|
||||||
float total_filter = 0;
|
float total_filter = 0;
|
||||||
float filter_scale;
|
float filter_scale;
|
||||||
|
|
||||||
STBR_DEBUG_ASSERT(in_last_texel - in_first_texel <= stbr_info->kernel_texel_width);
|
STBR_DEBUG_ASSERT(in_last_texel - in_first_texel <= stbr_info->kernel_texel_width);
|
||||||
STBR_DEBUG_ASSERT(in_first_texel >= 0);
|
STBR_DEBUG_ASSERT(in_first_texel >= 0);
|
||||||
STBR_DEBUG_ASSERT(in_last_texel < input_w);
|
STBR_DEBUG_ASSERT(in_last_texel < stbr_info->input_w);
|
||||||
|
|
||||||
stbr__get_contributor(stbr_info, n)->n0 = in_first_texel;
|
stbr__get_contributor(stbr_info, n)->n0 = in_first_texel;
|
||||||
stbr__get_contributor(stbr_info, n)->n1 = in_last_texel;
|
stbr__get_contributor(stbr_info, n)->n1 = in_last_texel;
|
||||||
@ -266,7 +294,7 @@ static void stbr__calculate_horizontal_filters(stbr__info* stbr_info, stbr_filte
|
|||||||
for (i = 0; i <= in_last_texel - in_first_texel; i++)
|
for (i = 0; i <= in_last_texel - in_first_texel; i++)
|
||||||
{
|
{
|
||||||
float in_texel_center = (float)(i + in_first_texel) + 0.5f;
|
float in_texel_center = (float)(i + in_first_texel) + 0.5f;
|
||||||
total_filter += *stbr__get_coefficient(stbr_info, n, i) = stbr__filter_info_table[filter].kernel(in_center_of_out - in_texel_center);
|
total_filter += *stbr__get_coefficient(stbr_info, n, i) = stbr__filter_info_table[stbr_info->filter].kernel(in_center_of_out - in_texel_center);
|
||||||
}
|
}
|
||||||
|
|
||||||
STBR_DEBUG_ASSERT(total_filter > 0);
|
STBR_DEBUG_ASSERT(total_filter > 0);
|
||||||
@ -280,20 +308,112 @@ static void stbr__calculate_horizontal_filters(stbr__info* stbr_info, stbr_filte
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static float* stbr__get_decode_buffer_index(stbr__info* stbr_info, int x, int c)
|
||||||
|
{
|
||||||
|
STBR_DEBUG_ASSERT(x >= 0 && x < stbr_info->input_w);
|
||||||
|
STBR_DEBUG_ASSERT(c >= 0 && c < stbr_info->channels);
|
||||||
|
|
||||||
|
return &stbr_info->decode_buffer[x * stbr_info->channels + c];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void stbr__decode_scanline(stbr__info* stbr_info, int n)
|
||||||
|
{
|
||||||
|
int x, c;
|
||||||
|
int channels = stbr_info->channels;
|
||||||
|
int input_w = stbr_info->input_w;
|
||||||
|
const void* input_data = stbr_info->input_data;
|
||||||
|
float* decode_buffer = stbr_info->decode_buffer;
|
||||||
|
|
||||||
|
STBR_UNIMPLEMENTED(stbr_info->type != STBR_TYPE_UINT8);
|
||||||
|
|
||||||
|
for (x = 0; x < input_w; x++)
|
||||||
|
{
|
||||||
|
for (c = 0; c < channels; c++)
|
||||||
|
{
|
||||||
|
int buffer_index = x * channels + c;
|
||||||
|
decode_buffer[buffer_index] = ((float)((const unsigned char*)input_data)[buffer_index]) / 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void stbr__resample_horizontal(stbr__info* stbr_info, int n)
|
||||||
|
{
|
||||||
|
int x, k, c;
|
||||||
|
int output_w = stbr_info->output_w;
|
||||||
|
int kernel_texel_width = stbr_info->kernel_texel_width;
|
||||||
|
int channels = stbr_info->channels;
|
||||||
|
float* decode_buffer = stbr_info->decode_buffer;
|
||||||
|
stbr__contributors* horizontal_contributors = stbr_info->horizontal_contributors;
|
||||||
|
float* horizontal_coefficients = stbr_info->horizontal_coefficients;
|
||||||
|
|
||||||
|
int ring_buffer_index;
|
||||||
|
float* ring_buffer;
|
||||||
|
|
||||||
|
if (stbr_info->ring_buffer_begin_index < 0)
|
||||||
|
ring_buffer_index = stbr_info->ring_buffer_begin_index = 0;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ring_buffer_index = (stbr_info->ring_buffer_begin_index + (stbr_info->ring_buffer_last_scanline - stbr_info->ring_buffer_first_scanline) + 1) % stbr_info->kernel_texel_width;
|
||||||
|
STBR_DEBUG_ASSERT(ring_buffer_index != stbr_info->ring_buffer_begin_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
ring_buffer = &stbr_info->ring_buffer[ring_buffer_index];
|
||||||
|
|
||||||
|
for (x = 0; x < output_w; x++)
|
||||||
|
{
|
||||||
|
int n0 = horizontal_contributors[x].n0;
|
||||||
|
int n1 = horizontal_contributors[x].n1;
|
||||||
|
|
||||||
|
int out_texel_index = x * channels;
|
||||||
|
int coefficient_group_index = x * kernel_texel_width;
|
||||||
|
int coefficient_counter = 0;
|
||||||
|
|
||||||
|
STBR_DEBUG_ASSERT(n1 >= n0);
|
||||||
|
|
||||||
|
for (k = n0; k <= n1; k++)
|
||||||
|
{
|
||||||
|
int coefficient_index = coefficient_group_index + (coefficient_counter++);
|
||||||
|
int in_texel_index = k * channels;
|
||||||
|
|
||||||
|
if (!horizontal_coefficients[coefficient_index])
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (c = 0; c < channels; c++)
|
||||||
|
ring_buffer[out_texel_index + c] += decode_buffer[in_texel_index + c] * horizontal_coefficients[coefficient_index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stbr_info->ring_buffer_last_scanline = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void stbr__decode_and_resample(stbr__info* stbr_info, int n)
|
||||||
|
{
|
||||||
|
// Decode the nth scanline from the source image into the decode buffer.
|
||||||
|
stbr__decode_scanline(stbr_info, n);
|
||||||
|
|
||||||
|
// Now resample it into the ring buffer.
|
||||||
|
stbr__resample_horizontal(stbr_info, n);
|
||||||
|
|
||||||
|
// Now it's sitting in the ring buffer ready to be used as source for the vertical sampling.
|
||||||
|
}
|
||||||
|
|
||||||
STBRDEF int stbr_resize_arbitrary(const void* input_data, int input_w, int input_h, int input_stride_in_bytes,
|
STBRDEF int stbr_resize_arbitrary(const void* input_data, int input_w, int input_h, int input_stride_in_bytes,
|
||||||
void* output_data, int output_w, int output_h, int output_stride_in_bytes,
|
void* output_data, int output_w, int output_h, int output_stride_in_bytes,
|
||||||
int channels, stbr_type type, stbr_filter filter,
|
int channels, stbr_type type, stbr_filter filter,
|
||||||
void* tempmem, stbr_size_t tempmem_size_in_bytes)
|
void* tempmem, stbr_size_t tempmem_size_in_bytes)
|
||||||
{
|
{
|
||||||
|
int y;
|
||||||
int width_stride_input = input_stride_in_bytes ? input_stride_in_bytes : channels * input_w;
|
int width_stride_input = input_stride_in_bytes ? input_stride_in_bytes : channels * input_w;
|
||||||
int width_stride_output = output_stride_in_bytes ? output_stride_in_bytes : channels * output_w;
|
int width_stride_output = output_stride_in_bytes ? output_stride_in_bytes : channels * output_w;
|
||||||
|
|
||||||
#ifdef STBR_DEBUG_OVERWRITE_TEST
|
#ifdef STBR_DEBUG_OVERWRITE_TEST
|
||||||
#define OVERWRITE_ARRAY_SIZE 64
|
#define OVERWRITE_ARRAY_SIZE 64
|
||||||
unsigned char overwrite_contents_pre[OVERWRITE_ARRAY_SIZE];
|
unsigned char overwrite_output_pre[OVERWRITE_ARRAY_SIZE];
|
||||||
|
unsigned char overwrite_tempmem_pre[OVERWRITE_ARRAY_SIZE];
|
||||||
|
|
||||||
stbr_size_t begin_forbidden = width_stride_output * (output_h - 1) + output_w * channels;
|
stbr_size_t begin_forbidden = width_stride_output * (output_h - 1) + output_w * channels;
|
||||||
memcpy(overwrite_contents_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE);
|
memcpy(overwrite_output_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE);
|
||||||
|
memcpy(overwrite_tempmem_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
STBR_UNIMPLEMENTED(type != STBR_TYPE_UINT8);
|
STBR_UNIMPLEMENTED(type != STBR_TYPE_UINT8);
|
||||||
@ -311,6 +431,20 @@ STBRDEF int stbr_resize_arbitrary(const void* input_data, int input_w, int input
|
|||||||
|
|
||||||
stbr__info* stbr_info = (stbr__info*)tempmem;
|
stbr__info* stbr_info = (stbr__info*)tempmem;
|
||||||
|
|
||||||
|
stbr_info->input_data = input_data;
|
||||||
|
stbr_info->input_w = input_w;
|
||||||
|
stbr_info->input_h = input_h;
|
||||||
|
stbr_info->input_stride_bytes = width_stride_input;
|
||||||
|
|
||||||
|
stbr_info->output_data = output_data;
|
||||||
|
stbr_info->output_w = output_w;
|
||||||
|
stbr_info->output_h = output_h;
|
||||||
|
stbr_info->output_stride_bytes = width_stride_output;
|
||||||
|
|
||||||
|
stbr_info->channels = channels;
|
||||||
|
stbr_info->type = type;
|
||||||
|
stbr_info->filter = filter;
|
||||||
|
|
||||||
stbr_info->total_coefficients = stbr__get_total_coefficients(filter, input_w, output_w);
|
stbr_info->total_coefficients = stbr__get_total_coefficients(filter, input_w, output_w);
|
||||||
stbr_info->kernel_texel_width = stbr__get_filter_texel_width(filter, output_w > input_w ? 1 : 0);
|
stbr_info->kernel_texel_width = stbr__get_filter_texel_width(filter, output_w > input_w ? 1 : 0);
|
||||||
|
|
||||||
@ -319,13 +453,62 @@ STBRDEF int stbr_resize_arbitrary(const void* input_data, int input_w, int input
|
|||||||
stbr_info->horizontal_contributors = STBR__NEXT_MEMPTR(stbr_info, sizeof(stbr__info), stbr__contributors);
|
stbr_info->horizontal_contributors = STBR__NEXT_MEMPTR(stbr_info, sizeof(stbr__info), stbr__contributors);
|
||||||
stbr_info->horizontal_coefficients = STBR__NEXT_MEMPTR(stbr_info->horizontal_contributors, output_w * sizeof(stbr__contributors), float);
|
stbr_info->horizontal_coefficients = STBR__NEXT_MEMPTR(stbr_info->horizontal_contributors, output_w * sizeof(stbr__contributors), float);
|
||||||
stbr_info->decode_buffer = STBR__NEXT_MEMPTR(stbr_info->horizontal_coefficients, stbr_info->total_coefficients * sizeof(stbr__contributors), float);
|
stbr_info->decode_buffer = STBR__NEXT_MEMPTR(stbr_info->horizontal_coefficients, stbr_info->total_coefficients * sizeof(stbr__contributors), float);
|
||||||
|
stbr_info->ring_buffer = STBR__NEXT_MEMPTR(stbr_info->decode_buffer, input_w * channels * sizeof(float), float);
|
||||||
|
|
||||||
#undef STBR__NEXT_MEMPTR
|
#undef STBR__NEXT_MEMPTR
|
||||||
|
|
||||||
stbr__calculate_horizontal_filters(stbr_info, filter, input_w, output_w);
|
// This signals that the ring buffer is empty
|
||||||
|
stbr_info->ring_buffer_begin_index = -1;
|
||||||
|
|
||||||
|
stbr__calculate_horizontal_filters(stbr_info);
|
||||||
|
|
||||||
|
float scale_ratio = (float)output_h / input_h;
|
||||||
|
float out_scanlines_radius = stbr__filter_info_table[filter].support * scale_ratio;
|
||||||
|
|
||||||
|
for (y = 0; y < output_h; y++)
|
||||||
|
{
|
||||||
|
float in_center_of_out; // Center of the current out scanline in the in scanline space
|
||||||
|
int in_first_scanline, in_last_scanline;
|
||||||
|
|
||||||
|
stbr__calculate_sample_range(y, out_scanlines_radius, scale_ratio, &in_first_scanline, &in_last_scanline, &in_center_of_out);
|
||||||
|
|
||||||
|
STBR_DEBUG_ASSERT(in_last_scanline - in_first_scanline <= stbr_info->kernel_texel_width);
|
||||||
|
STBR_DEBUG_ASSERT(in_first_scanline >= 0);
|
||||||
|
STBR_DEBUG_ASSERT(in_last_scanline < input_w);
|
||||||
|
|
||||||
|
if (stbr_info->ring_buffer_begin_index >= 0)
|
||||||
|
{
|
||||||
|
// Get rid of whatever we don't need anymore.
|
||||||
|
while (in_first_scanline > stbr_info->ring_buffer_first_scanline)
|
||||||
|
{
|
||||||
|
if (stbr_info->ring_buffer_first_scanline == stbr_info->ring_buffer_last_scanline)
|
||||||
|
{
|
||||||
|
// We just popped the last scanline off the ring buffer.
|
||||||
|
// Reset it to the empty state.
|
||||||
|
stbr_info->ring_buffer_begin_index = -1;
|
||||||
|
stbr_info->ring_buffer_first_scanline = 0;
|
||||||
|
stbr_info->ring_buffer_last_scanline = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
stbr_info->ring_buffer_first_scanline++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load in new ones.
|
||||||
|
if (stbr_info->ring_buffer_begin_index < 0)
|
||||||
|
stbr__decode_and_resample(stbr_info, 0);
|
||||||
|
|
||||||
|
while (in_last_scanline < stbr_info->ring_buffer_last_scanline)
|
||||||
|
stbr__decode_and_resample(stbr_info, stbr_info->ring_buffer_last_scanline + 1);
|
||||||
|
|
||||||
|
// Now all buffers should be ready to do a row a vertical sampling.
|
||||||
|
//stbr__resample_vertical();
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef STBR_DEBUG_OVERWRITE_TEST
|
#ifdef STBR_DEBUG_OVERWRITE_TEST
|
||||||
STBR_DEBUG_ASSERT(memcmp(overwrite_contents_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE) == 0);
|
STBR_DEBUG_ASSERT(memcmp(overwrite_output_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE) == 0);
|
||||||
|
STBR_DEBUG_ASSERT(memcmp(overwrite_tempmem_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE) == 0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@ -343,8 +526,9 @@ STBRDEF stbr_size_t stbr_calculate_memory(int input_w, int input_h, int input_st
|
|||||||
int decode_buffer_size = input_w * channels * sizeof(float);
|
int decode_buffer_size = input_w * channels * sizeof(float);
|
||||||
int contributors_size = output_w * sizeof(stbr__contributors);
|
int contributors_size = output_w * sizeof(stbr__contributors);
|
||||||
int coefficients_size = stbr__get_total_coefficients(filter, input_w, output_w) * sizeof(float);
|
int coefficients_size = stbr__get_total_coefficients(filter, input_w, output_w) * sizeof(float);
|
||||||
|
int ring_buffer_size = output_w * channels * sizeof(float) * stbr__get_filter_texel_width(filter, output_w > input_w ? 1 : 0);
|
||||||
|
|
||||||
return info_size + decode_buffer_size + contributors_size + coefficients_size;
|
return info_size + decode_buffer_size + contributors_size + coefficients_size + ring_buffer_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // STB_RESAMPLE_IMPLEMENTATION
|
#endif // STB_RESAMPLE_IMPLEMENTATION
|
||||||
|
Loading…
Reference in New Issue
Block a user