Fix malloc context and whitespace.

This commit is contained in:
Jorge Rodriguez 2014-09-06 08:48:46 -07:00
parent ce7aed0fb7
commit 75bdd2da83
2 changed files with 61 additions and 23 deletions

View File

@ -199,7 +199,7 @@ STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int
stbir_datatype datatype,
int num_channels, int alpha_channel, int flags,
stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,
stbir_filter filter_horizontal, stbir_filter filter_vertical,
stbir_filter filter_horizontal, stbir_filter filter_vertical,
stbir_colorspace space, void *alloc_context);
STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
@ -207,7 +207,7 @@ STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int
stbir_datatype datatype,
int num_channels, int alpha_channel, int flags,
stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,
stbir_filter filter_horizontal, stbir_filter filter_vertical,
stbir_filter filter_horizontal, stbir_filter filter_vertical,
stbir_colorspace space, void *alloc_context,
float x_scale, float y_scale,
float x_offset, float y_offset);
@ -217,7 +217,7 @@ STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int
stbir_datatype datatype,
int num_channels, int alpha_channel, int flags,
stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,
stbir_filter filter_horizontal, stbir_filter filter_vertical,
stbir_filter filter_horizontal, stbir_filter filter_vertical,
stbir_colorspace space, void *alloc_context,
float s0, float t0, float s1, float t1);
// (s0, t0) & (s1, t1) are the top-left and bottom right corner (uv addressing style: [0, 1]x[0, 1]) of a region of the input image to use.
@ -1750,7 +1750,7 @@ static int stbir_resize_arbitrary(
stbir__calculate_transform(&info, s0,t0,s1,t1,transform);
stbir__choose_filter(&info, h_filter, v_filter);
memory_required = stbir__calculate_memory(&info);
extra_memory = STBIR_MALLOC(NULL, memory_required);
extra_memory = STBIR_MALLOC(alloc_context, memory_required);
if (!extra_memory)
return 0;
@ -1759,9 +1759,9 @@ static int stbir_resize_arbitrary(
output_data, output_stride_in_bytes,
alpha_channel, flags, type,
edge_horizontal, edge_vertical,
colorspace, extra_memory, memory_required);
colorspace, extra_memory, memory_required);
STBIR_FREE(context, extra_memory);
STBIR_FREE(alloc_context, extra_memory);
return result;
}
@ -1850,7 +1850,7 @@ STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int
stbir_datatype datatype,
int num_channels, int alpha_channel, int flags,
stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,
stbir_filter filter_horizontal, stbir_filter filter_vertical,
stbir_filter filter_horizontal, stbir_filter filter_vertical,
stbir_colorspace space, void *alloc_context)
{
return stbir_resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,
@ -1865,7 +1865,7 @@ STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int
stbir_datatype datatype,
int num_channels, int alpha_channel, int flags,
stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,
stbir_filter filter_horizontal, stbir_filter filter_vertical,
stbir_filter filter_horizontal, stbir_filter filter_vertical,
stbir_colorspace space, void *alloc_context,
float x_scale, float y_scale,
float x_offset, float y_offset)
@ -1886,7 +1886,7 @@ STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int
stbir_datatype datatype,
int num_channels, int alpha_channel, int flags,
stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,
stbir_filter filter_horizontal, stbir_filter filter_vertical,
stbir_filter filter_horizontal, stbir_filter filter_vertical,
stbir_colorspace space, void *alloc_context,
float s0, float t0, float s1, float t1)
{

View File

@ -1,3 +1,5 @@
#include <malloc.h>
#if defined(_WIN32) && _MSC_VER > 1200
#define STBIR_ASSERT(x) \
if (!(x)) { \
@ -8,6 +10,42 @@
#define STBIR_ASSERT(x) assert(x)
#endif
#define STBIR_MALLOC stbir_malloc
#define STBIR_FREE stbir_free
class stbir_context {
public:
stbir_context()
{
size = 1000000;
memory = malloc(size);
}
~stbir_context()
{
free(memory);
}
size_t size;
void* memory;
} g_context;
void* stbir_malloc(void* context, size_t size)
{
if (!context)
return 0;
stbir_context* real_context = (stbir_context*)context;
if (size > real_context->size)
return 0;
return real_context->memory;
}
void stbir_free(void* context, void* memory)
{
}
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#define STB_IMAGE_RESIZE_STATIC
#include "stb_image_resize.h"
@ -129,7 +167,7 @@ int main(int argc, char** argv)
{
ftime(&initial_time_millis);
for (int i = 0; i < 100; i++)
stbir_resize_arbitrary(NULL, input_data + w * border * n + border * n, in_w, in_h, w*n, output_data, out_w, out_h, out_stride, 0, 0, 1, 1, NULL, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
stbir_resize_arbitrary(&g_context, input_data + w * border * n + border * n, in_w, in_h, w*n, output_data, out_w, out_h, out_stride, 0, 0, 1, 1, NULL, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
ftime(&final_time_millis);
long lapsed_ms = (long)(final_time_millis.time - initial_time_millis.time) * 1000 + (final_time_millis.millitm - initial_time_millis.millitm);
printf("Resample: %dms\n", lapsed_ms);
@ -141,7 +179,7 @@ int main(int argc, char** argv)
printf("Average: %dms\n", average);
#else
stbir_resize_arbitrary(NULL, input_data + w * border * n + border * n, in_w, in_h, w*n, output_data, out_w, out_h, out_stride, s0, t0, s1, t1, NULL, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
stbir_resize_arbitrary(&g_context, input_data + w * border * n + border * n, in_w, in_h, w*n, output_data, out_w, out_h, out_stride, s0, t0, s1, t1, NULL, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
#endif
stbi_image_free(input_data);
@ -169,7 +207,7 @@ void resize_image(const char* filename, float width_percent, float height_percen
unsigned char* output_data = (unsigned char*)malloc(out_w * out_h * n);
stbir_resize_arbitrary(NULL, input_data, w, h, 0, output_data, out_w, out_h, 0, 0, 0, 1, 1, NULL, n, -1, 0, STBIR_TYPE_UINT8, filter, filter, edge, edge, colorspace);
stbir_resize_arbitrary(&g_context, input_data, w, h, 0, output_data, out_w, out_h, 0, 0, 0, 1, 1, NULL, n, -1, 0, STBIR_TYPE_UINT8, filter, filter, edge, edge, colorspace);
stbi_image_free(input_data);
@ -204,7 +242,7 @@ void test_format(const char* file, float width_percent, float height_percent, st
T* output_data = (T*)malloc(new_w * new_h * n * sizeof(T));
stbir_resize_arbitrary(NULL, T_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, -1, 0, type, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, colorspace);
stbir_resize_arbitrary(&g_context, T_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, -1, 0, type, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, colorspace);
free(T_data);
stbi_image_free(input_data);
@ -248,7 +286,7 @@ void test_float(const char* file, float width_percent, float height_percent, stb
float* output_data = (float*)malloc(new_w * new_h * n * sizeof(float));
stbir_resize_arbitrary(NULL, T_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, -1, 0, type, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, colorspace);
stbir_resize_arbitrary(&g_context, T_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, -1, 0, type, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, colorspace);
free(T_data);
stbi_image_free(input_data);
@ -316,7 +354,7 @@ void test_subpixel(const char* file, float width_percent, float height_percent,
unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));
stbir_resize_arbitrary(NULL, input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, s1, t1, NULL, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
stbir_resize_arbitrary(&g_context, input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, s1, t1, NULL, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
stbi_image_free(input_data);
@ -351,13 +389,13 @@ void test_premul(const char* file)
unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));
stbir_resize_arbitrary(NULL, input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, 3, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
stbir_resize_arbitrary(&g_context, input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, 3, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
char output[200];
sprintf(output, "test-output/premul-%s", file);
stbi_write_png(output, new_w, new_h, n, output_data, 0);
stbir_resize_arbitrary(NULL, input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
stbir_resize_arbitrary(&g_context, input_data, w, h, 0, output_data, new_w, new_h, 0, 0, 0, 1, 1, NULL, n, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
sprintf(output, "test-output/nopremul-%s", file);
stbi_write_png(output, new_w, new_h, n, output_data, 0);
@ -379,13 +417,13 @@ void test_subpixel_1()
unsigned char output_data[16 * 16];
stbir_resize_arbitrary(NULL, image, 8, 8, 0, output_data, 16, 16, 0, 0, 0, 1, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
stbir_resize_arbitrary(&g_context, image, 8, 8, 0, output_data, 16, 16, 0, 0, 0, 1, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
unsigned char output_left[8 * 16];
unsigned char output_right[8 * 16];
stbir_resize_arbitrary(NULL, image, 8, 8, 0, output_left, 8, 16, 0, 0, 0, 0.5f, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
stbir_resize_arbitrary(NULL, image, 8, 8, 0, output_right, 8, 16, 0, 0.5f, 0, 1, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
stbir_resize_arbitrary(&g_context, image, 8, 8, 0, output_left, 8, 16, 0, 0, 0, 0.5f, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
stbir_resize_arbitrary(&g_context, image, 8, 8, 0, output_right, 8, 16, 0, 0.5f, 0, 1, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
for (int x = 0; x < 8; x++)
{
@ -424,8 +462,8 @@ void test_subpixel_2()
unsigned char output_data_1[16 * 16];
unsigned char output_data_2[16 * 16];
stbir_resize_arbitrary(NULL, image, 8, 8, 0, output_data_1, 16, 16, 0, 0, 0, 1, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_EDGE_WRAP, STBIR_COLORSPACE_SRGB);
stbir_resize_arbitrary(NULL, large_image, 32, 32, 0, output_data_2, 16, 16, 0, 0.25f, 0.25f, 0.5f, 0.5f, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
stbir_resize_arbitrary(&g_context, image, 8, 8, 0, output_data_1, 16, 16, 0, 0, 0, 1, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_EDGE_WRAP, STBIR_COLORSPACE_SRGB);
stbir_resize_arbitrary(&g_context, large_image, 32, 32, 0, output_data_2, 16, 16, 0, 0.25f, 0.25f, 0.5f, 0.5f, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
{for (int x = 0; x < 16; x++)
{
@ -469,7 +507,7 @@ void test_subpixel_4()
unsigned char output[8 * 8];
stbir_resize_arbitrary(NULL, image, 8, 8, 0, output, 8, 8, 0, 0, 0, 1, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_BILINEAR, STBIR_FILTER_BILINEAR, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR);
stbir_resize_arbitrary(&g_context, image, 8, 8, 0, output, 8, 8, 0, 0, 0, 1, 1, NULL, 1, -1, 0, STBIR_TYPE_UINT8, STBIR_FILTER_BILINEAR, STBIR_FILTER_BILINEAR, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR);
STBIR_ASSERT(memcmp(image, output, 8 * 8) == 0);
}