2014-09-06 11:48:46 -04:00
|
|
|
#include <malloc.h>
|
|
|
|
|
2014-08-18 11:56:44 -04:00
|
|
|
#if defined(_WIN32) && _MSC_VER > 1200
|
2014-08-18 12:12:59 -04:00
|
|
|
#define STBIR_ASSERT(x) \
|
2014-08-18 11:56:44 -04:00
|
|
|
if (!(x)) { \
|
|
|
|
__debugbreak(); \
|
|
|
|
} else
|
2014-07-30 20:30:25 -04:00
|
|
|
#else
|
|
|
|
#include <assert.h>
|
2014-08-18 12:12:59 -04:00
|
|
|
#define STBIR_ASSERT(x) assert(x)
|
2014-07-21 03:16:03 -04:00
|
|
|
#endif
|
|
|
|
|
2014-09-06 11:48:46 -04:00
|
|
|
#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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-06 13:57:21 -04:00
|
|
|
void stbir_progress(float p)
|
|
|
|
{
|
|
|
|
STBIR_ASSERT(p >= 0 && p <= 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define STBIR_PROGRESS_REPORT stbir_progress
|
|
|
|
|
2014-08-18 12:12:59 -04:00
|
|
|
#define STB_IMAGE_RESIZE_IMPLEMENTATION
|
|
|
|
#define STB_IMAGE_RESIZE_STATIC
|
|
|
|
#include "stb_image_resize.h"
|
2014-07-21 03:16:03 -04:00
|
|
|
|
|
|
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
|
|
#include "stb_image_write.h"
|
|
|
|
|
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
|
|
#include "stb_image.h"
|
|
|
|
|
2014-07-24 01:17:56 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <sys/timeb.h>
|
|
|
|
#endif
|
|
|
|
|
2014-08-12 15:15:12 -04:00
|
|
|
#define MT_SIZE 624
|
|
|
|
static size_t g_aiMT[MT_SIZE];
|
|
|
|
static size_t g_iMTI = 0;
|
|
|
|
|
|
|
|
// Mersenne Twister implementation from Wikipedia.
|
|
|
|
// Avoiding use of the system rand() to be sure that our tests generate the same test data on any system.
|
|
|
|
void mtsrand(size_t iSeed)
|
|
|
|
{
|
|
|
|
g_aiMT[0] = iSeed;
|
|
|
|
for (size_t i = 1; i < MT_SIZE; i++)
|
|
|
|
{
|
|
|
|
size_t inner1 = g_aiMT[i - 1];
|
|
|
|
size_t inner2 = (g_aiMT[i - 1] >> 30);
|
|
|
|
size_t inner = inner1 ^ inner2;
|
|
|
|
g_aiMT[i] = (0x6c078965 * inner) + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_iMTI = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t mtrand()
|
|
|
|
{
|
|
|
|
if (g_iMTI == 0)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < MT_SIZE; i++)
|
|
|
|
{
|
|
|
|
size_t y = (0x80000000 & (g_aiMT[i])) + (0x7fffffff & (g_aiMT[(i + 1) % MT_SIZE]));
|
|
|
|
g_aiMT[i] = g_aiMT[(i + 397) % MT_SIZE] ^ (y >> 1);
|
|
|
|
if ((y % 2) == 1)
|
|
|
|
g_aiMT[i] = g_aiMT[i] ^ 0x9908b0df;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t y = g_aiMT[g_iMTI];
|
|
|
|
y = y ^ (y >> 11);
|
|
|
|
y = y ^ ((y << 7) & (0x9d2c5680));
|
|
|
|
y = y ^ ((y << 15) & (0xefc60000));
|
|
|
|
y = y ^ (y >> 18);
|
|
|
|
|
|
|
|
g_iMTI = (g_iMTI + 1) % MT_SIZE;
|
|
|
|
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline float mtfrand()
|
|
|
|
{
|
|
|
|
const int ninenine = 999999;
|
|
|
|
return (float)(mtrand() % ninenine)/ninenine;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-30 01:50:06 -04:00
|
|
|
void test_suite();
|
|
|
|
|
2014-07-21 03:16:03 -04:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
unsigned char* input_data;
|
|
|
|
unsigned char* output_data;
|
|
|
|
int w, h;
|
|
|
|
int n;
|
2014-07-21 21:01:05 -04:00
|
|
|
int out_w, out_h, out_stride;
|
2014-07-21 03:16:03 -04:00
|
|
|
|
2014-07-31 19:31:45 -04:00
|
|
|
#if 1
|
2014-07-30 01:50:06 -04:00
|
|
|
test_suite();
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2014-07-21 03:16:03 -04:00
|
|
|
if (argc <= 1)
|
|
|
|
{
|
|
|
|
printf("No input image\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
input_data = stbi_load(argv[1], &w, &h, &n, 0);
|
|
|
|
if (!input_data)
|
|
|
|
{
|
|
|
|
printf("Input image could not be loaded");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:16:36 -04:00
|
|
|
out_w = 512;
|
|
|
|
out_h = 512;
|
2014-07-21 21:01:05 -04:00
|
|
|
out_stride = (out_w + 10) * n;
|
2014-07-21 03:16:03 -04:00
|
|
|
|
2014-07-24 01:17:56 -04:00
|
|
|
output_data = (unsigned char*)malloc(out_stride * out_h);
|
|
|
|
|
|
|
|
int in_w = 512;
|
|
|
|
int in_h = 512;
|
|
|
|
|
2014-07-31 03:39:33 -04:00
|
|
|
float s0 = 0.25f;
|
|
|
|
float t0 = 0.25f;
|
|
|
|
float s1 = 0.75f;
|
|
|
|
float t1 = 0.75f;
|
|
|
|
|
2014-07-21 21:01:05 -04:00
|
|
|
// Cut out the outside 64 pixels all around to test the stride.
|
2014-07-24 01:17:56 -04:00
|
|
|
int border = 64;
|
2014-08-18 12:12:59 -04:00
|
|
|
STBIR_ASSERT(in_w + border <= w);
|
|
|
|
STBIR_ASSERT(in_h + border <= h);
|
2014-07-24 01:17:56 -04:00
|
|
|
|
2014-07-29 20:44:45 -04:00
|
|
|
#ifdef PERF_TEST
|
|
|
|
struct timeb initial_time_millis, final_time_millis;
|
|
|
|
|
|
|
|
long average = 0;
|
|
|
|
for (int j = 0; j < 10; j++)
|
|
|
|
{
|
|
|
|
ftime(&initial_time_millis);
|
|
|
|
for (int i = 0; i < 100; i++)
|
2014-09-06 11:48:46 -04:00
|
|
|
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);
|
2014-07-29 20:44:45 -04:00
|
|
|
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);
|
|
|
|
|
|
|
|
average += lapsed_ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
average /= 10;
|
|
|
|
|
|
|
|
printf("Average: %dms\n", average);
|
|
|
|
#else
|
2014-09-06 11:48:46 -04:00
|
|
|
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);
|
2014-07-29 20:44:45 -04:00
|
|
|
#endif
|
2014-07-24 01:17:56 -04:00
|
|
|
|
2014-07-30 03:16:13 -04:00
|
|
|
stbi_image_free(input_data);
|
2014-07-21 03:16:03 -04:00
|
|
|
|
2014-07-21 21:01:05 -04:00
|
|
|
stbi_write_png("output.png", out_w, out_h, n, output_data, out_stride);
|
2014-07-21 03:16:03 -04:00
|
|
|
|
|
|
|
free(output_data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-30 01:50:06 -04:00
|
|
|
|
2014-08-18 12:12:59 -04:00
|
|
|
void resize_image(const char* filename, float width_percent, float height_percent, stbir_filter filter, stbir_edge edge, stbir_colorspace colorspace, const char* output_filename)
|
2014-07-30 01:50:06 -04:00
|
|
|
{
|
|
|
|
int w, h, n;
|
|
|
|
|
|
|
|
unsigned char* input_data = stbi_load(filename, &w, &h, &n, 0);
|
|
|
|
if (!input_data)
|
|
|
|
{
|
2014-08-31 11:55:41 -04:00
|
|
|
printf("Input image could not be loaded\n");
|
2014-07-30 01:50:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int out_w = (int)(w * width_percent);
|
|
|
|
int out_h = (int)(h * height_percent);
|
|
|
|
|
|
|
|
unsigned char* output_data = (unsigned char*)malloc(out_w * out_h * n);
|
|
|
|
|
2014-09-06 11:48:46 -04:00
|
|
|
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);
|
2014-07-30 01:50:06 -04:00
|
|
|
|
2014-07-30 03:16:13 -04:00
|
|
|
stbi_image_free(input_data);
|
2014-07-30 01:50:06 -04:00
|
|
|
|
|
|
|
stbi_write_png(output_filename, out_w, out_h, n, output_data, 0);
|
|
|
|
|
|
|
|
free(output_data);
|
|
|
|
}
|
|
|
|
|
2014-07-30 03:16:13 -04:00
|
|
|
template <typename F, typename T>
|
|
|
|
void convert_image(const F* input, T* output, int length)
|
|
|
|
{
|
2014-07-30 04:18:23 -04:00
|
|
|
double f = (pow(2.0, 8.0 * sizeof(T)) - 1) / (pow(2.0, 8.0 * sizeof(F)) - 1);
|
2014-07-30 03:16:13 -04:00
|
|
|
for (int i = 0; i < length; i++)
|
2014-07-30 04:18:23 -04:00
|
|
|
output[i] = (T)(((double)input[i]) * f);
|
2014-07-30 03:16:13 -04:00
|
|
|
}
|
|
|
|
|
2014-07-30 03:34:25 -04:00
|
|
|
template <typename T>
|
2014-08-31 10:23:34 -04:00
|
|
|
void test_format(const char* file, float width_percent, float height_percent, stbir_datatype type, stbir_colorspace colorspace)
|
2014-07-30 03:34:25 -04:00
|
|
|
{
|
|
|
|
int w, h, n;
|
|
|
|
unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);
|
|
|
|
|
2014-08-31 11:55:41 -04:00
|
|
|
if (input_data == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
2014-07-30 03:34:25 -04:00
|
|
|
int new_w = (int)(w * width_percent);
|
|
|
|
int new_h = (int)(h * height_percent);
|
|
|
|
|
|
|
|
T* T_data = (T*)malloc(w * h * n * sizeof(T));
|
|
|
|
convert_image<unsigned char, T>(input_data, T_data, w * h * n);
|
|
|
|
|
|
|
|
T* output_data = (T*)malloc(new_w * new_h * n * sizeof(T));
|
|
|
|
|
2014-09-06 11:48:46 -04:00
|
|
|
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);
|
2014-07-30 03:34:25 -04:00
|
|
|
|
|
|
|
free(T_data);
|
|
|
|
stbi_image_free(input_data);
|
|
|
|
|
2014-07-30 04:18:23 -04:00
|
|
|
unsigned char* char_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(char));
|
|
|
|
convert_image<T, unsigned char>(output_data, char_data, new_w * new_h * n);
|
2014-07-30 03:34:25 -04:00
|
|
|
|
|
|
|
char output[200];
|
|
|
|
sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file);
|
|
|
|
stbi_write_png(output, new_w, new_h, n, char_data, 0);
|
|
|
|
|
|
|
|
free(char_data);
|
|
|
|
free(output_data);
|
|
|
|
}
|
|
|
|
|
2014-07-30 11:47:55 -04:00
|
|
|
void convert_image_float(const unsigned char* input, float* output, int length)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
output[i] = ((float)input[i])/255;
|
|
|
|
}
|
|
|
|
|
|
|
|
void convert_image_float(const float* input, unsigned char* output, int length)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
output[i] = (unsigned char)(input[i] * 255);
|
|
|
|
}
|
|
|
|
|
2014-08-31 10:23:34 -04:00
|
|
|
void test_float(const char* file, float width_percent, float height_percent, stbir_datatype type, stbir_colorspace colorspace)
|
2014-07-30 11:47:55 -04:00
|
|
|
{
|
|
|
|
int w, h, n;
|
|
|
|
unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);
|
|
|
|
|
2014-08-31 11:55:41 -04:00
|
|
|
if (input_data == NULL)
|
|
|
|
return;
|
|
|
|
|
2014-07-30 11:47:55 -04:00
|
|
|
int new_w = (int)(w * width_percent);
|
|
|
|
int new_h = (int)(h * height_percent);
|
|
|
|
|
|
|
|
float* T_data = (float*)malloc(w * h * n * sizeof(float));
|
|
|
|
convert_image_float(input_data, T_data, w * h * n);
|
|
|
|
|
|
|
|
float* output_data = (float*)malloc(new_w * new_h * n * sizeof(float));
|
|
|
|
|
2014-09-06 11:48:46 -04:00
|
|
|
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);
|
2014-07-30 11:47:55 -04:00
|
|
|
|
|
|
|
free(T_data);
|
|
|
|
stbi_image_free(input_data);
|
|
|
|
|
|
|
|
unsigned char* char_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(char));
|
|
|
|
convert_image_float(output_data, char_data, new_w * new_h * n);
|
|
|
|
|
|
|
|
char output[200];
|
|
|
|
sprintf(output, "test-output/type-%d-%d-%d-%d-%s", type, colorspace, new_w, new_h, file);
|
|
|
|
stbi_write_png(output, new_w, new_h, n, char_data, 0);
|
|
|
|
|
|
|
|
free(char_data);
|
|
|
|
free(output_data);
|
|
|
|
}
|
|
|
|
|
2014-07-30 20:30:25 -04:00
|
|
|
void test_channels(const char* file, float width_percent, float height_percent, int channels)
|
2014-07-30 12:41:41 -04:00
|
|
|
{
|
|
|
|
int w, h, n;
|
|
|
|
unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);
|
|
|
|
|
2014-08-31 11:55:41 -04:00
|
|
|
if (input_data == NULL)
|
|
|
|
return;
|
|
|
|
|
2014-07-30 12:41:41 -04:00
|
|
|
int new_w = (int)(w * width_percent);
|
|
|
|
int new_h = (int)(h * height_percent);
|
|
|
|
|
|
|
|
unsigned char* channels_data = (unsigned char*)malloc(w * h * channels * sizeof(unsigned char));
|
|
|
|
|
|
|
|
for (int i = 0; i < w * h; i++)
|
|
|
|
{
|
|
|
|
int input_position = i * n;
|
|
|
|
int output_position = i * channels;
|
|
|
|
|
|
|
|
for (int c = 0; c < channels; c++)
|
2014-08-18 12:12:59 -04:00
|
|
|
channels_data[output_position + c] = input_data[input_position + stbir__min(c, n)];
|
2014-07-30 12:41:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * channels * sizeof(unsigned char));
|
|
|
|
|
2014-09-01 19:52:04 -04:00
|
|
|
stbir_resize_uint8_srgb(channels_data, w, h, 0, output_data, new_w, new_h, 0, channels, -1,0);
|
2014-07-30 12:41:41 -04:00
|
|
|
|
|
|
|
free(channels_data);
|
|
|
|
stbi_image_free(input_data);
|
|
|
|
|
|
|
|
char output[200];
|
|
|
|
sprintf(output, "test-output/channels-%d-%d-%d-%s", channels, new_w, new_h, file);
|
|
|
|
stbi_write_png(output, new_w, new_h, channels, output_data, 0);
|
|
|
|
|
|
|
|
free(output_data);
|
|
|
|
}
|
|
|
|
|
2014-07-31 19:31:45 -04:00
|
|
|
void test_subpixel(const char* file, float width_percent, float height_percent, float s1, float t1)
|
|
|
|
{
|
|
|
|
int w, h, n;
|
|
|
|
unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);
|
|
|
|
|
2014-08-31 11:55:41 -04:00
|
|
|
if (input_data == NULL)
|
|
|
|
return;
|
|
|
|
|
2014-07-31 19:31:45 -04:00
|
|
|
s1 = ((float)w - 1 + s1)/w;
|
|
|
|
t1 = ((float)h - 1 + t1)/h;
|
|
|
|
|
|
|
|
int new_w = (int)(w * width_percent);
|
|
|
|
int new_h = (int)(h * height_percent);
|
|
|
|
|
|
|
|
unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));
|
|
|
|
|
2014-09-06 11:48:46 -04:00
|
|
|
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);
|
2014-07-31 19:31:45 -04:00
|
|
|
|
|
|
|
stbi_image_free(input_data);
|
|
|
|
|
|
|
|
char output[200];
|
|
|
|
sprintf(output, "test-output/subpixel-%d-%d-%f-%f-%s", new_w, new_h, s1, t1, file);
|
|
|
|
stbi_write_png(output, new_w, new_h, n, output_data, 0);
|
|
|
|
|
|
|
|
free(output_data);
|
|
|
|
}
|
|
|
|
|
2014-07-31 20:20:00 -04:00
|
|
|
void test_premul(const char* file)
|
|
|
|
{
|
|
|
|
int w, h, n;
|
|
|
|
unsigned char* input_data = stbi_load(file, &w, &h, &n, 4);
|
|
|
|
n = 4;
|
|
|
|
|
2014-08-31 11:55:41 -04:00
|
|
|
if (input_data == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
2014-07-31 21:46:00 -04:00
|
|
|
// Set alpha for the top half.
|
|
|
|
for (int x = 0; x < w; x++)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < h / 2; y++)
|
|
|
|
input_data[(y*w + x)*n + 3] = input_data[(y*w + x)*n + 0];
|
|
|
|
}
|
|
|
|
|
|
|
|
stbi_write_png("test-output/premul-original.png", w, h, n, input_data, 0);
|
2014-07-31 20:20:00 -04:00
|
|
|
|
2014-07-31 22:00:48 -04:00
|
|
|
int new_w = (int)(w * .1);
|
|
|
|
int new_h = (int)(h * .1);
|
2014-07-31 20:20:00 -04:00
|
|
|
|
|
|
|
unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));
|
|
|
|
|
2014-09-06 11:48:46 -04:00
|
|
|
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);
|
2014-07-31 20:20:00 -04:00
|
|
|
|
|
|
|
char output[200];
|
|
|
|
sprintf(output, "test-output/premul-%s", file);
|
|
|
|
stbi_write_png(output, new_w, new_h, n, output_data, 0);
|
|
|
|
|
2014-09-06 11:48:46 -04:00
|
|
|
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);
|
2014-07-31 21:46:00 -04:00
|
|
|
|
|
|
|
sprintf(output, "test-output/nopremul-%s", file);
|
|
|
|
stbi_write_png(output, new_w, new_h, n, output_data, 0);
|
|
|
|
|
|
|
|
stbi_image_free(input_data);
|
|
|
|
|
2014-07-31 20:20:00 -04:00
|
|
|
free(output_data);
|
|
|
|
}
|
|
|
|
|
2014-08-18 12:01:33 -04:00
|
|
|
// test that splitting a pow-2 image into tiles produces identical results
|
2014-08-12 15:15:12 -04:00
|
|
|
void test_subpixel_1()
|
|
|
|
{
|
|
|
|
unsigned char image[8 * 8];
|
|
|
|
|
|
|
|
mtsrand(0);
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(image); i++)
|
|
|
|
image[i] = mtrand() % 255;
|
|
|
|
|
|
|
|
unsigned char output_data[16 * 16];
|
|
|
|
|
2014-09-06 11:48:46 -04:00
|
|
|
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);
|
2014-08-12 15:15:12 -04:00
|
|
|
|
|
|
|
unsigned char output_left[8 * 16];
|
|
|
|
unsigned char output_right[8 * 16];
|
|
|
|
|
2014-09-06 11:48:46 -04:00
|
|
|
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);
|
2014-08-12 15:15:12 -04:00
|
|
|
|
|
|
|
for (int x = 0; x < 8; x++)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < 16; y++)
|
|
|
|
{
|
2014-08-18 12:12:59 -04:00
|
|
|
STBIR_ASSERT(output_data[y * 16 + x] == output_left[y * 8 + x]);
|
|
|
|
STBIR_ASSERT(output_data[y * 16 + x + 8] == output_right[y * 8 + x]);
|
2014-08-12 15:15:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 12:01:33 -04:00
|
|
|
// test that replicating an image and using a subtile of it produces same results as wraparound
|
2014-08-12 15:15:12 -04:00
|
|
|
void test_subpixel_2()
|
|
|
|
{
|
|
|
|
unsigned char image[8 * 8];
|
|
|
|
|
|
|
|
mtsrand(0);
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(image); i++)
|
|
|
|
image[i] = mtrand() % 255;
|
|
|
|
|
|
|
|
unsigned char large_image[32 * 32];
|
|
|
|
|
|
|
|
for (int x = 0; x < 8; x++)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < 8; y++)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < 4; j++)
|
|
|
|
large_image[j*4*8*8 + i*8 + y*4*8 + x] = image[y*8 + x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char output_data_1[16 * 16];
|
|
|
|
unsigned char output_data_2[16 * 16];
|
|
|
|
|
2014-09-06 11:48:46 -04:00
|
|
|
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);
|
2014-08-12 15:15:12 -04:00
|
|
|
|
2014-08-18 11:56:44 -04:00
|
|
|
{for (int x = 0; x < 16; x++)
|
2014-08-12 15:15:12 -04:00
|
|
|
{
|
|
|
|
for (int y = 0; y < 16; y++)
|
2014-08-18 12:12:59 -04:00
|
|
|
STBIR_ASSERT(output_data_1[y * 16 + x] == output_data_2[y * 16 + x]);
|
2014-08-18 11:56:44 -04:00
|
|
|
}}
|
2014-08-12 15:15:12 -04:00
|
|
|
}
|
|
|
|
|
2014-08-18 12:01:33 -04:00
|
|
|
// test that 0,0,1,1 subpixel produces same result as no-rect
|
2014-08-12 15:15:12 -04:00
|
|
|
void test_subpixel_3()
|
|
|
|
{
|
|
|
|
unsigned char image[8 * 8];
|
|
|
|
|
|
|
|
mtsrand(0);
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(image); i++)
|
|
|
|
image[i] = mtrand() % 255;
|
|
|
|
|
|
|
|
unsigned char output_data_1[32 * 32];
|
|
|
|
unsigned char output_data_2[32 * 32];
|
|
|
|
|
2014-08-31 11:55:41 -04:00
|
|
|
stbir_resize_region(image, 8, 8, 0, output_data_1, 32, 32, 0, STBIR_TYPE_UINT8, 1,0,-1, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_LINEAR, NULL, 0, 0, 1, 1);
|
2014-08-31 11:34:05 -04:00
|
|
|
stbir_resize_uint8(image, 8, 8, 0, output_data_2, 32, 32, 0, 1);
|
2014-08-12 15:15:12 -04:00
|
|
|
|
|
|
|
for (int x = 0; x < 32; x++)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < 32; y++)
|
2014-08-18 12:12:59 -04:00
|
|
|
STBIR_ASSERT(output_data_1[y * 32 + x] == output_data_2[y * 32 + x]);
|
2014-08-12 15:15:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 12:01:33 -04:00
|
|
|
// test that 1:1 resample using s,t=0,0,1,1 with bilinear produces original image
|
2014-08-12 15:15:12 -04:00
|
|
|
void test_subpixel_4()
|
|
|
|
{
|
|
|
|
unsigned char image[8 * 8];
|
|
|
|
|
|
|
|
mtsrand(0);
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(image); i++)
|
|
|
|
image[i] = mtrand() % 255;
|
|
|
|
|
|
|
|
unsigned char output[8 * 8];
|
|
|
|
|
2014-09-06 11:48:46 -04:00
|
|
|
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);
|
2014-08-18 12:12:59 -04:00
|
|
|
STBIR_ASSERT(memcmp(image, output, 8 * 8) == 0);
|
2014-08-12 15:15:12 -04:00
|
|
|
}
|
|
|
|
|
2014-07-30 01:50:06 -04:00
|
|
|
void test_suite()
|
|
|
|
{
|
2014-08-18 11:56:44 -04:00
|
|
|
int i;
|
|
|
|
|
2014-09-01 19:52:04 -04:00
|
|
|
#if 0 // linear_to_srgb_uchar table
|
|
|
|
for (i=0; i < 256; ++i) {
|
|
|
|
float f = stbir__srgb_to_linear((i+0.5f)/256.0f);
|
|
|
|
printf("%9d, ", (int) ((f) * (1<<28)));
|
|
|
|
if ((i & 7) == 7)
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-08-12 15:15:12 -04:00
|
|
|
test_subpixel_1();
|
|
|
|
test_subpixel_2();
|
|
|
|
test_subpixel_3();
|
|
|
|
test_subpixel_4();
|
|
|
|
|
2014-07-31 20:20:00 -04:00
|
|
|
test_premul("barbara.png");
|
|
|
|
|
2014-08-18 11:56:44 -04:00
|
|
|
for (i = 0; i < 10; i++)
|
2014-07-31 19:31:45 -04:00
|
|
|
test_subpixel("barbara.png", 0.5f, 0.5f, (float)i / 10, 1);
|
|
|
|
|
2014-08-18 11:56:44 -04:00
|
|
|
for (i = 0; i < 10; i++)
|
2014-07-31 19:31:45 -04:00
|
|
|
test_subpixel("barbara.png", 0.5f, 0.5f, 1, (float)i / 10);
|
|
|
|
|
2014-08-18 11:56:44 -04:00
|
|
|
for (i = 0; i < 10; i++)
|
2014-07-31 19:31:45 -04:00
|
|
|
test_subpixel("barbara.png", 2, 2, (float)i / 10, 1);
|
|
|
|
|
2014-08-18 11:56:44 -04:00
|
|
|
for (i = 0; i < 10; i++)
|
2014-07-31 19:31:45 -04:00
|
|
|
test_subpixel("barbara.png", 2, 2, 1, (float)i / 10);
|
|
|
|
|
2014-07-30 12:41:41 -04:00
|
|
|
// Channels test
|
|
|
|
test_channels("barbara.png", 0.5f, 0.5f, 1);
|
|
|
|
test_channels("barbara.png", 0.5f, 0.5f, 2);
|
|
|
|
test_channels("barbara.png", 0.5f, 0.5f, 3);
|
|
|
|
test_channels("barbara.png", 0.5f, 0.5f, 4);
|
|
|
|
|
|
|
|
test_channels("barbara.png", 2, 2, 1);
|
|
|
|
test_channels("barbara.png", 2, 2, 2);
|
|
|
|
test_channels("barbara.png", 2, 2, 3);
|
|
|
|
test_channels("barbara.png", 2, 2, 4);
|
|
|
|
|
2014-07-30 12:14:38 -04:00
|
|
|
// Edge behavior tests
|
2014-08-18 12:12:59 -04:00
|
|
|
resize_image("hgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/hgradient-clamp.png");
|
|
|
|
resize_image("hgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_COLORSPACE_LINEAR, "test-output/hgradient-wrap.png");
|
2014-07-30 12:14:38 -04:00
|
|
|
|
2014-08-18 12:12:59 -04:00
|
|
|
resize_image("vgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/vgradient-clamp.png");
|
|
|
|
resize_image("vgradient.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_WRAP, STBIR_COLORSPACE_LINEAR, "test-output/vgradient-wrap.png");
|
2014-07-30 12:14:38 -04:00
|
|
|
|
2014-08-18 12:12:59 -04:00
|
|
|
resize_image("1px-border.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_LINEAR, "test-output/1px-border-reflect.png");
|
|
|
|
resize_image("1px-border.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR, "test-output/1px-border-clamp.png");
|
2014-07-30 12:14:38 -04:00
|
|
|
|
2014-07-30 01:50:06 -04:00
|
|
|
// sRGB tests
|
2014-08-18 12:12:59 -04:00
|
|
|
resize_image("gamma_colors.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_colors.jpg");
|
|
|
|
resize_image("gamma_2.2.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_2.2.jpg");
|
|
|
|
resize_image("gamma_dalai_lama_gray.jpg", .5f, .5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_REFLECT, STBIR_COLORSPACE_SRGB, "test-output/gamma_dalai_lama_gray.jpg");
|
2014-07-30 01:50:06 -04:00
|
|
|
|
2014-07-30 12:27:42 -04:00
|
|
|
// filter tests
|
2014-08-18 12:12:59 -04:00
|
|
|
resize_image("barbara.png", 2, 2, STBIR_FILTER_NEAREST, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-nearest.png");
|
|
|
|
resize_image("barbara.png", 2, 2, STBIR_FILTER_BILINEAR, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bilinear.png");
|
|
|
|
resize_image("barbara.png", 2, 2, STBIR_FILTER_BICUBIC, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-bicubic.png");
|
|
|
|
resize_image("barbara.png", 2, 2, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-catmullrom.png");
|
|
|
|
resize_image("barbara.png", 2, 2, STBIR_FILTER_MITCHELL, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-upsample-mitchell.png");
|
|
|
|
|
|
|
|
resize_image("barbara.png", 0.5f, 0.5f, STBIR_FILTER_NEAREST, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-nearest.png");
|
|
|
|
resize_image("barbara.png", 0.5f, 0.5f, STBIR_FILTER_BILINEAR, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bilinear.png");
|
|
|
|
resize_image("barbara.png", 0.5f, 0.5f, STBIR_FILTER_BICUBIC, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bicubic.png");
|
|
|
|
resize_image("barbara.png", 0.5f, 0.5f, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-catmullrom.png");
|
|
|
|
resize_image("barbara.png", 0.5f, 0.5f, STBIR_FILTER_MITCHELL, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-mitchell.png");
|
2014-07-30 12:27:42 -04:00
|
|
|
|
2014-08-18 11:56:44 -04:00
|
|
|
for (i = 10; i < 100; i++)
|
2014-07-30 01:50:06 -04:00
|
|
|
{
|
|
|
|
char outname[200];
|
|
|
|
sprintf(outname, "test-output/barbara-width-%d.jpg", i);
|
2014-08-18 12:12:59 -04:00
|
|
|
resize_image("barbara.png", (float)i / 100, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);
|
2014-07-30 01:50:06 -04:00
|
|
|
}
|
|
|
|
|
2014-08-18 11:56:44 -04:00
|
|
|
for (i = 110; i < 500; i += 10)
|
2014-07-30 01:50:06 -04:00
|
|
|
{
|
|
|
|
char outname[200];
|
|
|
|
sprintf(outname, "test-output/barbara-width-%d.jpg", i);
|
2014-08-18 12:12:59 -04:00
|
|
|
resize_image("barbara.png", (float)i / 100, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);
|
2014-07-30 01:50:06 -04:00
|
|
|
}
|
2014-07-30 02:02:56 -04:00
|
|
|
|
2014-08-18 11:56:44 -04:00
|
|
|
for (i = 10; i < 100; i++)
|
2014-07-30 02:02:56 -04:00
|
|
|
{
|
|
|
|
char outname[200];
|
|
|
|
sprintf(outname, "test-output/barbara-height-%d.jpg", i);
|
2014-08-18 12:12:59 -04:00
|
|
|
resize_image("barbara.png", 1, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);
|
2014-07-30 02:02:56 -04:00
|
|
|
}
|
|
|
|
|
2014-08-18 11:56:44 -04:00
|
|
|
for (i = 110; i < 500; i += 10)
|
2014-07-30 02:02:56 -04:00
|
|
|
{
|
|
|
|
char outname[200];
|
|
|
|
sprintf(outname, "test-output/barbara-height-%d.jpg", i);
|
2014-08-18 12:12:59 -04:00
|
|
|
resize_image("barbara.png", 1, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);
|
2014-07-30 02:02:56 -04:00
|
|
|
}
|
2014-07-30 02:09:41 -04:00
|
|
|
|
2014-08-18 11:56:44 -04:00
|
|
|
for (i = 50; i < 200; i += 10)
|
2014-07-30 02:09:41 -04:00
|
|
|
{
|
|
|
|
char outname[200];
|
|
|
|
sprintf(outname, "test-output/barbara-width-height-%d.jpg", i);
|
2014-08-18 12:12:59 -04:00
|
|
|
resize_image("barbara.png", 100 / (float)i, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);
|
2014-07-30 02:09:41 -04:00
|
|
|
}
|
2014-07-30 03:16:13 -04:00
|
|
|
|
2014-08-18 12:12:59 -04:00
|
|
|
test_format<unsigned short>("barbara.png", 0.5, 2.0, STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB);
|
|
|
|
test_format<unsigned short>("barbara.png", 0.5, 2.0, STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR);
|
|
|
|
test_format<unsigned short>("barbara.png", 2.0, 0.5, STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB);
|
|
|
|
test_format<unsigned short>("barbara.png", 2.0, 0.5, STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR);
|
2014-07-30 03:16:13 -04:00
|
|
|
|
2014-08-18 12:12:59 -04:00
|
|
|
test_format<unsigned int>("barbara.png", 0.5, 2.0, STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB);
|
|
|
|
test_format<unsigned int>("barbara.png", 0.5, 2.0, STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR);
|
|
|
|
test_format<unsigned int>("barbara.png", 2.0, 0.5, STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB);
|
|
|
|
test_format<unsigned int>("barbara.png", 2.0, 0.5, STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR);
|
2014-07-30 03:16:13 -04:00
|
|
|
|
2014-08-18 12:12:59 -04:00
|
|
|
test_float("barbara.png", 0.5, 2.0, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB);
|
|
|
|
test_float("barbara.png", 0.5, 2.0, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR);
|
|
|
|
test_float("barbara.png", 2.0, 0.5, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB);
|
|
|
|
test_float("barbara.png", 2.0, 0.5, STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR);
|
2014-07-30 01:50:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|