Allow specifying a stride.

This commit is contained in:
Jorge Rodriguez 2014-07-21 18:01:05 -07:00
parent 06b7b00696
commit ba861fa493
2 changed files with 16 additions and 12 deletions

View File

@ -18,10 +18,11 @@ Initial implementation by Jorge L Rodriguez
#define STBR_INCLUDE_STB_RESAMPLE_H #define STBR_INCLUDE_STB_RESAMPLE_H
// Basic usage: // Basic usage:
// result = stbr_resize(input_data, input_w, input_h, input_components, output_data, output_w, output_h, STBR_FILTER_NEAREST, STBR_EDGE_CLAMP); // result = stbr_resize(input_data, input_w, input_h, input_components, 0, output_data, output_w, output_h, 0, STBR_FILTER_NEAREST, STBR_EDGE_CLAMP);
// //
// input_data is your supplied texels. // input_data is your supplied texels.
// output_data will be the resized texels. It should be of size output_w * output_h * input_components. // output_data will be the resized texels. It should be of size output_w * output_h * input_components (or output_h * output_stride if you provided a stride.)
// If input_stride or output_stride is 0 (as in this example) the stride will be automatically calculated as width*components.
// Returned result is 1 for success or 0 in case of an error. // Returned result is 1 for success or 0 in case of an error.
typedef enum typedef enum
@ -52,7 +53,7 @@ extern "C" {
// PRIMARY API - resize an image // PRIMARY API - resize an image
// //
STBRDEF int stbr_resize(const stbr_uc* input_data, int input_w, int input_h, int input_components, stbr_uc* output_data, int output_w, int output_h, stbr_filter filter, stbr_edge edge); STBRDEF int stbr_resize(const stbr_uc* input_data, int input_w, int input_h, int input_components, int input_stride, stbr_uc* output_data, int output_w, int output_h, int output_stride, stbr_filter filter, stbr_edge edge);
#ifdef __cplusplus #ifdef __cplusplus
@ -159,17 +160,18 @@ static void stbr__filter_nearest_n(const stbr_uc* input_data, stbr_uc* output_da
typedef void (stbr__filter_fn)(const stbr_uc* input_data, stbr_uc* output_data, size_t input_texel_index, size_t output_texel_index, size_t n); typedef void (stbr__filter_fn)(const stbr_uc* input_data, stbr_uc* output_data, size_t input_texel_index, size_t output_texel_index, size_t n);
STBRDEF int stbr_resize(const stbr_uc* input_data, int input_w, int input_h, int input_components, stbr_uc* output_data, int output_w, int output_h, stbr_filter filter, stbr_edge edge) STBRDEF int stbr_resize(const stbr_uc* input_data, int input_w, int input_h, int input_components, int input_stride, stbr_uc* output_data, int output_w, int output_h, int output_stride, stbr_filter filter, stbr_edge edge)
{ {
int x, y; int x, y;
int width_stride_input = input_components * input_w; int width_stride_input = input_stride ? input_stride : input_components * input_w;
int width_stride_output = input_components * output_w; int width_stride_output = output_stride ? output_stride : input_components * 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_contents_pre[OVERWRITE_ARRAY_SIZE];
memcpy(overwrite_contents_pre, &output_data[output_w * output_h * input_components], OVERWRITE_ARRAY_SIZE); size_t begin_forbidden = width_stride_output * (output_h - 1) + output_w * input_components;
memcpy(overwrite_contents_pre, &output_data[begin_forbidden], OVERWRITE_ARRAY_SIZE);
#endif #endif
if (filter == STBR_FILTER_NEAREST) if (filter == STBR_FILTER_NEAREST)
@ -203,7 +205,7 @@ STBRDEF int stbr_resize(const stbr_uc* input_data, int input_w, int input_h, int
return 0; return 0;
#ifdef STBR_DEBUG_OVERWRITE_TEST #ifdef STBR_DEBUG_OVERWRITE_TEST
STBR_ASSERT(memcmp(overwrite_contents_pre, &output_data[output_w * output_h * input_components], OVERWRITE_ARRAY_SIZE) == 0); STBR_ASSERT(memcmp(overwrite_contents_pre, &output_data[begin_forbidden], OVERWRITE_ARRAY_SIZE) == 0);
#endif #endif
return 1; return 1;

View File

@ -19,7 +19,7 @@ int main(int argc, char** argv)
unsigned char* output_data; unsigned char* output_data;
int w, h; int w, h;
int n; int n;
int out_w, out_h; int out_w, out_h, out_stride;
if (argc <= 1) if (argc <= 1)
{ {
@ -36,12 +36,14 @@ int main(int argc, char** argv)
out_w = 512; out_w = 512;
out_h = 512; out_h = 512;
out_stride = (out_w + 10) * n;
output_data = malloc(out_w * out_h * n); output_data = malloc(out_stride * out_h);
stbr_resize(input_data, w, h, n, output_data, out_w, out_h, STBR_FILTER_NEAREST, STBR_EDGE_CLAMP); // Cut out the outside 64 pixels all around to test the stride.
stbr_resize(input_data + w*64*n + 64*n, w - 128, h - 128, n, w*n, output_data, out_w, out_h, out_stride, STBR_FILTER_NEAREST, STBR_EDGE_CLAMP);
stbi_write_png("output.png", out_w, out_h, n, output_data, out_w * n); stbi_write_png("output.png", out_w, out_h, n, output_data, out_stride);
free(output_data); free(output_data);