Merge branch 'test' into working
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parents efdfbb3, 4ff6723 · Browse files · 2 files·+150 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+150 added−17 removed
- tests:+148 added−15 removed
- other:+2 added−2 removed
Provenance
- Commit
cbc1e7c- Tree
78b46dc- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent 1
efdfbb3- Parent 2
4ff6723
2 files changed+150 added−17 removed
modified:tests/resample_test.cpp+148 added-15 removed
1-#include <malloc.h>1+#include <stdlib.h>2+#include <stdio.h>3 4 #if defined(_WIN32) && _MSC_VER > 12005 #define STBIR_ASSERT(x) \6 if (!(x)) { \349 unmodified lines356 357 free(output_data);358 }359 360+void test_subpixel_region(const char* file, float width_percent, float height_percent, float s0, float t0, float s1, float t1)361+{362+ int w, h, n;363+ unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);364+365+ if (input_data == NULL)366+ return;367+368+ int new_w = (int)(w * width_percent);369+ int new_h = (int)(h * height_percent);370+371+ unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));372+373+ stbir_resize_region(input_data, w, h, 0, output_data, new_w, new_h, 0, STBIR_TYPE_UINT8, n, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, s0, t0, s1, t1);374+375+ stbi_image_free(input_data);376+377+ char output[200];378+ sprintf(output, "test-output/subpixel-region-%d-%d-%f-%f-%f-%f-%s", new_w, new_h, s0, t0, s1, t1, file);379+ stbi_write_png(output, new_w, new_h, n, output_data, 0);380+381+ free(output_data);382+}383+384+void test_subpixel_command(const char* file, float width_percent, float height_percent, float x_scale, float y_scale, float x_offset, float y_offset)385+{386+ int w, h, n;387+ unsigned char* input_data = stbi_load(file, &w, &h, &n, 0);388+389+ if (input_data == NULL)390+ return;391+392+ int new_w = (int)(w * width_percent);393+ int new_h = (int)(h * height_percent);394+395+ unsigned char* output_data = (unsigned char*)malloc(new_w * new_h * n * sizeof(unsigned char));396+397+ stbir_resize_subpixel(input_data, w, h, 0, output_data, new_w, new_h, 0, STBIR_TYPE_UINT8, n, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, x_scale, y_scale, x_offset, y_offset);398+399+ stbi_image_free(input_data);400+401+ char output[200];402+ sprintf(output, "test-output/subpixel-command-%d-%d-%f-%f-%f-%f-%s", new_w, new_h, x_scale, y_scale, x_offset, y_offset, file);403+ stbi_write_png(output, new_w, new_h, n, output_data, 0);404+405+ free(output_data);406+}407+408 unsigned int* pixel(unsigned int* buffer, int x, int y, int c, int w, int n)409 {410 return &buffer[y*w*n + x*n + c];411 }97 unmodified lines509 STBIR_ASSERT(output_data[y * 16 + x] == output_left[y * 8 + x]);510 STBIR_ASSERT(output_data[y * 16 + x + 8] == output_right[y * 8 + x]);511 }512 }513+514+ stbir_resize_subpixel(image, 8, 8, 0, output_left, 8, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, 2, 2, 0, 0);515+ stbir_resize_subpixel(image, 8, 8, 0, output_right, 8, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, 2, 2, 8, 0);516+517+ {for (int x = 0; x < 8; x++)518+ {519+ for (int y = 0; y < 16; y++)520+ {521+ STBIR_ASSERT(output_data[y * 16 + x] == output_left[y * 8 + x]);522+ STBIR_ASSERT(output_data[y * 16 + x + 8] == output_right[y * 8 + x]);523+ }524+ }}525 }526 527 // test that replicating an image and using a subtile of it produces same results as wraparound528 void test_subpixel_2()29 unmodified lines558 {559 for (int y = 0; y < 16; y++)560 STBIR_ASSERT(output_data_1[y * 16 + x] == output_data_2[y * 16 + x]);561 }}562+563+ stbir_resize_subpixel(large_image, 32, 32, 0, output_data_2, 16, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_WRAP, STBIR_EDGE_WRAP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context, 2, 2, 16, 16);564+565+ {for (int x = 0; x < 16; x++)566+ {567+ for (int y = 0; y < 16; y++)568+ STBIR_ASSERT(output_data_1[y * 16 + x] == output_data_2[y * 16 + x]);569+ }}570 }571 572 // test that 0,0,1,1 subpixel produces same result as no-rect573 void test_subpixel_3()15 unmodified lines589 {590 for (int y = 0; y < 32; y++)591 STBIR_ASSERT(output_data_1[y * 32 + x] == output_data_2[y * 32 + x]);592 }593+594+ stbir_resize_subpixel(image, 8, 8, 0, output_data_1, 32, 32, 0, STBIR_TYPE_UINT8, 1, 0, STBIR_ALPHA_CHANNEL_NONE, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_LINEAR, NULL, 4, 4, 0, 0);595+596+ {for (int x = 0; x < 32; x++)597+ {598+ for (int y = 0; y < 32; y++)599+ STBIR_ASSERT(output_data_1[y * 32 + x] == output_data_2[y * 32 + x]);600+ }}601 }602 603 // test that 1:1 resample using s,t=0,0,1,1 with bilinear produces original image604 void test_subpixel_4()8 unmodified lines613 unsigned char output[8 * 8];614 615 stbir_resize_region(image, 8, 8, 0, output, 8, 8, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_TRIANGLE, STBIR_FILTER_TRIANGLE, STBIR_COLORSPACE_LINEAR, &g_context, 0, 0, 1, 1);616 STBIR_ASSERT(memcmp(image, output, 8 * 8) == 0);617+618+ stbir_resize_subpixel(image, 8, 8, 0, output, 8, 8, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_TRIANGLE, STBIR_FILTER_TRIANGLE, STBIR_COLORSPACE_LINEAR, &g_context, 1, 1, 0, 0);619+ STBIR_ASSERT(memcmp(image, output, 8 * 8) == 0);620 }621 622 static unsigned int image88_int[8][8];623 static unsigned char image88 [8][8];335 unmodified lines959 stbir_resize(image88, 8, 8, 0, output88, 4, 16, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_BOX, STBIR_COLORSPACE_SRGB, &g_context);960 stbir_resize(image88, 8, 8, 0, output88, 16, 4, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_FILTER_CATMULLROM, STBIR_COLORSPACE_SRGB, &g_context);961 stbir_resize(image88, 8, 8, 0, output88, 16, 4, 0, STBIR_TYPE_UINT8, 1, STBIR_ALPHA_CHANNEL_NONE, 0, STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_FILTER_CATMULLROM, STBIR_FILTER_BOX, STBIR_COLORSPACE_SRGB, &g_context);962 963+ int barbara_width, barbara_height, barbara_channels;964+ stbi_image_free(stbi_load(barbara, &barbara_width, &barbara_height, &barbara_channels, 0));965+966+ int res = 10;967+ // Downscaling968+ {for (int i = 0; i <= res; i++)969+ {970+ float t = (float)i/res;971+ float scale = 0.5;972+ float out_scale = 2.0f/3;973+ float x_shift = (barbara_width*out_scale - barbara_width*scale) * t;974+ float y_shift = (barbara_height*out_scale - barbara_height*scale) * t;975+976+ test_subpixel_command(barbara, scale, scale, out_scale, out_scale, x_shift, y_shift);977+ }}978+979+ // Upscaling980+ {for (int i = 0; i <= res; i++)981+ {982+ float t = (float)i/res;983+ float scale = 2;984+ float out_scale = 3;985+ float x_shift = (barbara_width*out_scale - barbara_width*scale) * t;986+ float y_shift = (barbara_height*out_scale - barbara_height*scale) * t;987+988+ test_subpixel_command(barbara, scale, scale, out_scale, out_scale, x_shift, y_shift);989+ }}990+991+ // Downscaling992+ {for (int i = 0; i <= res; i++)993+ {994+ float t = (float)i/res / 2;995+ test_subpixel_region(barbara, 0.25f, 0.25f, t, t, t+0.5f, t+0.5f);996+ }}997+998+ // No scaling999+ {for (int i = 0; i <= res; i++)1000+ {1001+ float t = (float)i/res / 2;1002+ test_subpixel_region(barbara, 0.5f, 0.5f, t, t, t+0.5f, t+0.5f);1003+ }}1004+1005+ // Upscaling1006+ {for (int i = 0; i <= res; i++)1007+ {1008+ float t = (float)i/res / 2;1009+ test_subpixel_region(barbara, 1, 1, t, t, t+0.5f, t+0.5f);1010+ }}1011+883- for (i = 0; i < 10; i++)1012+ {for (i = 0; i < 10; i++)1013 test_subpixel(barbara, 0.5f, 0.5f, (float)i / 10, 1);1014+ }1015 886- for (i = 0; i < 10; i++)1016+ {for (i = 0; i < 10; i++)1017 test_subpixel(barbara, 0.5f, 0.5f, 1, (float)i / 10);1018+ }1019 889- for (i = 0; i < 10; i++)1020+ {for (i = 0; i < 10; i++)1021 test_subpixel(barbara, 2, 2, (float)i / 10, 1);1022+ }1023 892- for (i = 0; i < 10; i++)1024+ {for (i = 0; i < 10; i++)1025 test_subpixel(barbara, 2, 2, 1, (float)i / 10);1026+ }1027 1028 // Channels test1029 test_channels(barbara, 0.5f, 0.5f, 1);1030 test_channels(barbara, 0.5f, 0.5f, 2);17 unmodified lines1048 resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CUBICBSPLINE, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-bicubic.png");1049 resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_CATMULLROM , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-catmullrom.png");1050 resize_image(barbara, 0.5f, 0.5f, STBIR_FILTER_MITCHELL , STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, "test-output/barbara-downsample-mitchell.png");1051 919- for (i = 10; i < 100; i++)1052+ {for (i = 10; i < 100; i++)1053 {1054 char outname[200];1055 sprintf(outname, "test-output/barbara-width-%d.jpg", i);1056 resize_image(barbara, (float)i / 100, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);924- }1057+ }}1058 926- for (i = 110; i < 500; i += 10)1059+ {for (i = 110; i < 500; i += 10)1060 {1061 char outname[200];1062 sprintf(outname, "test-output/barbara-width-%d.jpg", i);1063 resize_image(barbara, (float)i / 100, 1, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);931- }1064+ }}1065 933- for (i = 10; i < 100; i++)1066+ {for (i = 10; i < 100; i++)1067 {1068 char outname[200];1069 sprintf(outname, "test-output/barbara-height-%d.jpg", i);1070 resize_image(barbara, 1, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);938- }1071+ }}1072 940- for (i = 110; i < 500; i += 10)1073+ {for (i = 110; i < 500; i += 10)1074 {1075 char outname[200];1076 sprintf(outname, "test-output/barbara-height-%d.jpg", i);1077 resize_image(barbara, 1, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);945- }1078+ }}1079 947- for (i = 50; i < 200; i += 10)1080+ {for (i = 50; i < 200; i += 10)1081 {1082 char outname[200];1083 sprintf(outname, "test-output/barbara-width-height-%d.jpg", i);1084 resize_image(barbara, 100 / (float)i, (float)i / 100, STBIR_FILTER_CATMULLROM, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB, outname);952- }1085+ }}1086 1087 test_format<unsigned short>(barbara, 0.5, 2.0, STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB);1088 test_format<unsigned short>(barbara, 0.5, 2.0, STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR);1089 test_format<unsigned short>(barbara, 2.0, 0.5, STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB);27 unmodified linesmodified:stb_image_resize.h+2 added-2 removed
2227 unmodified lines2228 {2229 info->horizontal_scale = ((float)info->output_w / info->input_w) / (s1 - s0);2230 info->vertical_scale = ((float)info->output_h / info->input_h) / (t1 - t0);2231 2232- info->horizontal_shift = s0 * info->input_w / (s1 - s0);2233- info->vertical_shift = t0 * info->input_h / (t1 - t0);2232+ info->horizontal_shift = s0 * info->output_w / (s1 - s0);2233+ info->vertical_shift = t0 * info->output_h / (t1 - t0);2234 }2235 }2236 2237 static void stbir__choose_filter(stbir__info *info, stbir_filter h_filter, stbir_filter v_filter)349 unmodified lines