separate filter for horizontal and vertical

This commit is contained in:
Sean Barrett 2014-08-31 06:47:45 -07:00
parent 5dfa79fb31
commit 1bd9770e75

View File

@ -304,7 +304,8 @@ typedef struct
int alpha_channel;
stbir_uint32 flags;
stbir_type type;
stbir_filter filter;
stbir_filter horizontal_filter;
stbir_filter vertical_filter;
stbir_edge edge_horizontal;
stbir_edge edge_vertical;
stbir_colorspace colorspace;
@ -504,12 +505,12 @@ stbir__inline static int stbir__get_filter_pixel_width(stbir_filter filter, int
stbir__inline static int stbir__get_filter_pixel_width_horizontal(stbir__info* stbir_info)
{
return stbir__get_filter_pixel_width(stbir_info->filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale);
return stbir__get_filter_pixel_width(stbir_info->horizontal_filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale);
}
stbir__inline static int stbir__get_filter_pixel_width_vertical(stbir__info* stbir_info)
{
return stbir__get_filter_pixel_width(stbir_info->filter, stbir_info->input_h, stbir_info->output_h, stbir_info->vertical_scale);
return stbir__get_filter_pixel_width(stbir_info->vertical_filter, stbir_info->input_h, stbir_info->output_h, stbir_info->vertical_scale);
}
// This is how much to expand buffers to account for filters seeking outside
@ -521,12 +522,12 @@ stbir__inline static int stbir__get_filter_pixel_margin(stbir_filter filter, int
stbir__inline static int stbir__get_filter_pixel_margin_horizontal(stbir__info* stbir_info)
{
return stbir__get_filter_pixel_width(stbir_info->filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale) / 2;
return stbir__get_filter_pixel_width(stbir_info->horizontal_filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale) / 2;
}
stbir__inline static int stbir__get_filter_pixel_margin_vertical(stbir__info* stbir_info)
{
return stbir__get_filter_pixel_width(stbir_info->filter, stbir_info->input_h, stbir_info->output_h, stbir_info->vertical_scale) / 2;
return stbir__get_filter_pixel_width(stbir_info->vertical_filter, stbir_info->input_h, stbir_info->output_h, stbir_info->vertical_scale) / 2;
}
stbir__inline static int stbir__get_horizontal_contributors_noinfo(stbir_filter filter, int input_w, int output_w, float horizontal_scale)
@ -539,17 +540,18 @@ stbir__inline static int stbir__get_horizontal_contributors_noinfo(stbir_filter
stbir__inline static int stbir__get_horizontal_contributors(stbir__info* stbir_info)
{
return stbir__get_horizontal_contributors_noinfo(stbir_info->filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale);
return stbir__get_horizontal_contributors_noinfo(stbir_info->horizontal_filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale);
}
stbir__inline static int stbir__get_total_coefficients_noinfo(stbir_filter filter, int input_w, int output_w, float horizontal_scale)
stbir__inline static int stbir__get_total_coefficients_noinfo(stbir_filter h_filter, int input_w, int output_w, float horizontal_scale)
{
return stbir__get_horizontal_contributors_noinfo(filter, input_w, output_w, horizontal_scale) * stbir__get_filter_pixel_width(filter, input_w, output_w, horizontal_scale);
return stbir__get_horizontal_contributors_noinfo(h_filter, input_w, output_w, horizontal_scale)
* stbir__get_filter_pixel_width (h_filter, input_w, output_w, horizontal_scale);
}
stbir__inline static int stbir__get_total_coefficients(stbir__info* stbir_info)
{
return stbir__get_total_coefficients_noinfo(stbir_info->filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale);
return stbir__get_total_coefficients_noinfo(stbir_info->horizontal_filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale);
}
stbir__inline static stbir__contributors* stbir__get_contributor(stbir__info* stbir_info, int n)
@ -560,7 +562,7 @@ stbir__inline static stbir__contributors* stbir__get_contributor(stbir__info* st
stbir__inline static float* stbir__get_coefficient(stbir__info* stbir_info, int n, int c)
{
return &stbir_info->horizontal_coefficients[stbir__get_filter_pixel_width(stbir_info->filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale)*n + c];
return &stbir_info->horizontal_coefficients[stbir__get_filter_pixel_width(stbir_info->horizontal_filter, stbir_info->input_w, stbir_info->output_w, stbir_info->horizontal_scale)*n + c];
}
static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max)
@ -655,12 +657,11 @@ static void stbir__calculate_sample_range_downsample(int n, float in_pixels_radi
*out_last_pixel = (int)(floor(out_pixel_influence_upperbound - 0.5));
}
static void stbir__calculate_coefficients_upsample(stbir__info* stbir_info, int in_first_pixel, int in_last_pixel, float in_center_of_out, stbir__contributors* contributor, float* coefficient_group)
static void stbir__calculate_coefficients_upsample(stbir__info* stbir_info, stbir_filter filter, int in_first_pixel, int in_last_pixel, float in_center_of_out, stbir__contributors* contributor, float* coefficient_group)
{
int i;
float total_filter = 0;
float filter_scale;
stbir_filter filter = stbir_info->filter;
STBIR__DEBUG_ASSERT(in_last_pixel - in_first_pixel <= stbir__get_filter_pixel_width_horizontal(stbir_info));
@ -685,10 +686,9 @@ static void stbir__calculate_coefficients_upsample(stbir__info* stbir_info, int
coefficient_group[i] *= filter_scale;
}
static void stbir__calculate_coefficients_downsample(stbir__info* stbir_info, float scale_ratio, int out_first_pixel, int out_last_pixel, float out_center_of_in, stbir__contributors* contributor, float* coefficient_group)
static void stbir__calculate_coefficients_downsample(stbir__info* stbir_info, stbir_filter filter, float scale_ratio, int out_first_pixel, int out_last_pixel, float out_center_of_in, stbir__contributors* contributor, float* coefficient_group)
{
int i;
stbir_filter filter = stbir_info->filter;
STBIR__DEBUG_ASSERT(out_last_pixel - out_first_pixel <= stbir__get_filter_pixel_width_horizontal(stbir_info));
@ -740,7 +740,7 @@ static void stbir__calculate_horizontal_filters(stbir__info* stbir_info)
if (stbir__use_width_upsampling(stbir_info))
{
float out_pixels_radius = stbir__filter_info_table[stbir_info->filter].support * scale_ratio;
float out_pixels_radius = stbir__filter_info_table[stbir_info->horizontal_filter].support * scale_ratio;
// Looping through out pixels
for (n = 0; n < total_contributors; n++)
@ -750,12 +750,12 @@ static void stbir__calculate_horizontal_filters(stbir__info* stbir_info)
stbir__calculate_sample_range_upsample(n, out_pixels_radius, scale_ratio, stbir_info->horizontal_shift, &in_first_pixel, &in_last_pixel, &in_center_of_out);
stbir__calculate_coefficients_upsample(stbir_info, in_first_pixel, in_last_pixel, in_center_of_out, stbir__get_contributor(stbir_info, n), stbir__get_coefficient(stbir_info, n, 0));
stbir__calculate_coefficients_upsample(stbir_info, stbir_info->horizontal_filter, in_first_pixel, in_last_pixel, in_center_of_out, stbir__get_contributor(stbir_info, n), stbir__get_coefficient(stbir_info, n, 0));
}
}
else
{
float in_pixels_radius = stbir__filter_info_table[stbir_info->filter].support / scale_ratio;
float in_pixels_radius = stbir__filter_info_table[stbir_info->horizontal_filter].support / scale_ratio;
// Looping through in pixels
for (n = 0; n < total_contributors; n++)
@ -766,7 +766,7 @@ static void stbir__calculate_horizontal_filters(stbir__info* stbir_info)
stbir__calculate_sample_range_downsample(n_adjusted, in_pixels_radius, scale_ratio, stbir_info->horizontal_shift, &out_first_pixel, &out_last_pixel, &out_center_of_in);
stbir__calculate_coefficients_downsample(stbir_info, scale_ratio, out_first_pixel, out_last_pixel, out_center_of_in, stbir__get_contributor(stbir_info, n), stbir__get_coefficient(stbir_info, n, 0));
stbir__calculate_coefficients_downsample(stbir_info, stbir_info->horizontal_filter, scale_ratio, out_first_pixel, out_last_pixel, out_center_of_in, stbir__get_contributor(stbir_info, n), stbir__get_coefficient(stbir_info, n, 0));
}
#ifdef STBIR_DEBUG
@ -1177,7 +1177,7 @@ static void stbir__resample_vertical_upsample(stbir__info* stbir_info, int n, in
int n0,n1, output_row_start;
stbir__calculate_coefficients_upsample(stbir_info, in_first_scanline, in_last_scanline, in_center_of_out, vertical_contributors, vertical_coefficients);
stbir__calculate_coefficients_upsample(stbir_info, stbir_info->vertical_filter, in_first_scanline, in_last_scanline, in_center_of_out, vertical_contributors, vertical_coefficients);
n0 = vertical_contributors->n0;
n1 = vertical_contributors->n1;
@ -1230,7 +1230,7 @@ static void stbir__resample_vertical_downsample(stbir__info* stbir_info, int n,
int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);
int n0,n1,max_n;
stbir__calculate_coefficients_downsample(stbir_info, stbir_info->vertical_scale, in_first_scanline, in_last_scanline, in_center_of_out, vertical_contributors, vertical_coefficients);
stbir__calculate_coefficients_downsample(stbir_info, stbir_info->vertical_filter, stbir_info->vertical_scale, in_first_scanline, in_last_scanline, in_center_of_out, vertical_contributors, vertical_coefficients);
n0 = vertical_contributors->n0;
n1 = vertical_contributors->n1;
@ -1264,7 +1264,7 @@ static void stbir__buffer_loop_upsample(stbir__info* stbir_info)
{
int y;
float scale_ratio = stbir_info->vertical_scale;
float out_scanlines_radius = stbir__filter_info_table[stbir_info->filter].support * scale_ratio;
float out_scanlines_radius = stbir__filter_info_table[stbir_info->vertical_filter].support * scale_ratio;
STBIR__DEBUG_ASSERT(stbir__use_height_upsampling(stbir_info));
@ -1360,7 +1360,7 @@ static void stbir__buffer_loop_downsample(stbir__info* stbir_info)
int y;
float scale_ratio = stbir_info->vertical_scale;
int output_h = stbir_info->output_h;
float in_pixels_radius = stbir__filter_info_table[stbir_info->filter].support / scale_ratio;
float in_pixels_radius = stbir__filter_info_table[stbir_info->vertical_filter].support / scale_ratio;
int max_y = stbir_info->input_h + stbir__get_filter_pixel_margin_vertical(stbir_info);
STBIR__DEBUG_ASSERT(!stbir__use_height_upsampling(stbir_info));
@ -1395,25 +1395,27 @@ static void stbir__buffer_loop_downsample(stbir__info* stbir_info)
stbir__empty_ring_buffer(stbir_info, stbir_info->output_h);
}
static stbir__inline stbir_size_t stbir__calculate_memory(int input_w, int input_h, int output_w, int output_h, float s0, float t0, float s1, float t1, int channels, stbir_filter filter)
static stbir__inline stbir_size_t stbir__calculate_memory(int input_w, int input_h, int output_w, int output_h, float s0, float t0, float s1, float t1, int channels, stbir_filter h_filter, stbir_filter v_filter)
{
float horizontal_scale = ((float)output_w / input_w) / (s1 - s0);
float vertical_scale = ((float)output_h / input_h) / (t1 - t0);
int pixel_margin = stbir__get_filter_pixel_margin(filter, input_w, output_w, horizontal_scale);
int filter_height = stbir__get_filter_pixel_width(filter, input_h, output_h, vertical_scale);
int pixel_margin = stbir__get_filter_pixel_margin(h_filter, input_w, output_w, horizontal_scale);
int filter_height = stbir__get_filter_pixel_width(v_filter, input_h, output_h, vertical_scale);
int info_size = sizeof(stbir__info);
int contributors_size = stbir__get_horizontal_contributors_noinfo(filter, input_w, output_w, horizontal_scale) * sizeof(stbir__contributors);
int horizontal_coefficients_size = stbir__get_total_coefficients_noinfo(filter, input_w, output_w, horizontal_scale) * sizeof(float);
int contributors_size = stbir__get_horizontal_contributors_noinfo(h_filter, input_w, output_w, horizontal_scale) * sizeof(stbir__contributors);
int horizontal_coefficients_size = stbir__get_total_coefficients_noinfo(h_filter, input_w, output_w, horizontal_scale) * sizeof(float);
int vertical_coefficients_size = filter_height * sizeof(float);
int decode_buffer_size = (input_w + pixel_margin*2) * channels * sizeof(float);
int horizontal_buffer_size = output_w * channels * sizeof(float);
int ring_buffer_size = output_w * channels * filter_height * sizeof(float);
int encode_buffer_size = output_w * channels * sizeof(float);
STBIR_ASSERT(filter != 0);
STBIR_ASSERT(filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late
STBIR_ASSERT(h_filter != 0);
STBIR_ASSERT(h_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late
STBIR_ASSERT(v_filter != 0);
STBIR_ASSERT(v_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late
if (stbir__use_upsampling(horizontal_scale))
// The horizontal buffer is for when we're downsampling the height and we
@ -1431,12 +1433,13 @@ static stbir__inline stbir_size_t stbir__calculate_memory(int input_w, int input
static int stbir__resize_allocated(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,
float s0, float t0, float s1, float t1,
int channels, int alpha_channel, stbir_uint32 flags, stbir_type type, stbir_filter filter, stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace,
int channels, int alpha_channel, stbir_uint32 flags, stbir_type type, stbir_filter h_filter, stbir_filter v_filter,
stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace,
void* tempmem, stbir_size_t tempmem_size_in_bytes)
{
stbir__info* stbir_info = (stbir__info*)tempmem;
stbir_size_t memory_required = stbir__calculate_memory(input_w, input_h, output_w, output_h, s0, t0, s1, t1, channels, filter);
stbir_size_t memory_required = stbir__calculate_memory(input_w, input_h, output_w, output_h, s0, t0, s1, t1, channels, h_filter, v_filter);
int width_stride_input = input_stride_in_bytes ? input_stride_in_bytes : channels * input_w * stbir__type_size[type];
int width_stride_output = output_stride_in_bytes ? output_stride_in_bytes : channels * output_w * stbir__type_size[type];
@ -1455,10 +1458,14 @@ static int stbir__resize_allocated(const void* input_data, int input_w, int inpu
memcpy(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE);
#endif
STBIR_ASSERT(filter != 0);
STBIR_ASSERT(filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));
STBIR_ASSERT(h_filter != 0);
STBIR_ASSERT(h_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));
STBIR_ASSERT(v_filter != 0);
STBIR_ASSERT(v_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));
if (!filter || filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))
if (!h_filter || h_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))
return 0;
if (!v_filter || v_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))
return 0;
STBIR_ASSERT(s1 > s0);
@ -1518,7 +1525,8 @@ static int stbir__resize_allocated(const void* input_data, int input_w, int inpu
stbir_info->alpha_channel = alpha_channel;
stbir_info->flags = flags;
stbir_info->type = type;
stbir_info->filter = filter;
stbir_info->horizontal_filter = h_filter;
stbir_info->vertical_filter = v_filter;
stbir_info->edge_horizontal = edge_horizontal;
stbir_info->edge_vertical = edge_vertical;
stbir_info->colorspace = colorspace;
@ -1579,13 +1587,13 @@ STBRDEF int stbir_resize_arbitrary(const void* input_data, int input_w, int inpu
int channels, int alpha_channel, stbir_uint32 flags, stbir_type type, stbir_filter filter, stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace)
{
int result;
size_t memory_required = stbir__calculate_memory(input_w, input_h, output_w, output_h, 0, 0, 1, 1, channels, filter);
size_t memory_required = stbir__calculate_memory(input_w, input_h, output_w, output_h, 0, 0, 1, 1, channels, filter, filter);
void* extra_memory = STBIR_MALLOC(memory_required);
if (!extra_memory)
return 0;
result = stbir__resize_allocated(input_data, input_w, input_h, input_stride_in_bytes, output_data, output_w, output_h, output_stride_in_bytes, s0, t0, s1, t1, channels, alpha_channel, flags, type, filter, edge_horizontal, edge_vertical, colorspace, extra_memory, memory_required);
result = stbir__resize_allocated(input_data, input_w, input_h, input_stride_in_bytes, output_data, output_w, output_h, output_stride_in_bytes, s0, t0, s1, t1, channels, alpha_channel, flags, type, filter, filter, edge_horizontal, edge_vertical, colorspace, extra_memory, memory_required);
STBIR_FREE(extra_memory);