From e0ee0de26cfb142cb113f90948d05a8d2e9f69c0 Mon Sep 17 00:00:00 2001 From: Fabian Giesen Date: Sat, 23 Feb 2019 05:18:14 -0800 Subject: [PATCH] tests: Add simple image_write_test smoke test --- tests/Makefile | 3 +++ tests/image_write_test.c | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/image_write_test.c diff --git a/tests/Makefile b/tests/Makefile index e972cfd..487eaf0 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -2,6 +2,9 @@ INCLUDES = -I.. CFLAGS = -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast -DSTB_DIVIDE_TEST CPPFLAGS = -Wno-write-strings -DSTB_DIVIDE_TEST +#CFLAGS += -O -fsanitize=address + all: $(CC) $(INCLUDES) $(CFLAGS) ../stb_vorbis.c test_c_compilation.c test_c_lexer.c test_dxt.c test_easyfont.c test_image.c test_image_write.c test_perlin.c test_sprintf.c test_truetype.c test_voxel.c -lm $(CC) $(INCLUDES) $(CPPFLAGS) test_cpp_compilation.cpp -lm -lstdc++ + $(CC) $(INCLUDES) $(CFLAGS) image_write_test.c -lm -o image_write_test diff --git a/tests/image_write_test.c b/tests/image_write_test.c new file mode 100644 index 0000000..c40b044 --- /dev/null +++ b/tests/image_write_test.c @@ -0,0 +1,50 @@ +#define STB_IMAGE_WRITE_IMPLEMENTATION +#include "stb_image_write.h" + +// using an 'F' since it has no rotational symmetries, and 6x5 +// because it's a small, atypical size likely to trigger edge cases. +// +// conveniently, it's also small enough to fully fit inside a typical +// directory listing thumbnail, which simplifies checking at a glance. +static const char img6x5_template[] = + ".****." + ".*...." + ".***.." + ".*...." + ".*...."; + +int main(int argc, char **argv) +{ + // make a RGB version of the template image + // use red on blue to detect R<->B swaps + unsigned char img6x5_rgb[6*5*3]; + float img6x5_rgbf[6*5*3]; + int i; + + for (i = 0; i < 6*5; i++) { + int on = img6x5_template[i] == '*'; + img6x5_rgb[i*3 + 0] = on ? 255 : 0; + img6x5_rgb[i*3 + 1] = 0; + img6x5_rgb[i*3 + 2] = on ? 0 : 255; + + img6x5_rgbf[i*3 + 0] = on ? 1.0f : 0.0f; + img6x5_rgbf[i*3 + 1] = 0.0f; + img6x5_rgbf[i*3 + 2] = on ? 0.0f : 1.0f; + } + + stbi_write_png("wr6x5_regular.png", 6, 5, 3, img6x5_rgb, 6*3); + stbi_write_bmp("wr6x5_regular.bmp", 6, 5, 3, img6x5_rgb); + stbi_write_tga("wr6x5_regular.tga", 6, 5, 3, img6x5_rgb); + stbi_write_jpg("wr6x5_regular.jpg", 6, 5, 3, img6x5_rgb, 95); + stbi_write_hdr("wr6x5_regular.hdr", 6, 5, 3, img6x5_rgbf); + + stbi_flip_vertically_on_write(1); + + stbi_write_png("wr6x5_flip.png", 6, 5, 3, img6x5_rgb, 6*3); + stbi_write_bmp("wr6x5_flip.bmp", 6, 5, 3, img6x5_rgb); + stbi_write_tga("wr6x5_flip.tga", 6, 5, 3, img6x5_rgb); + stbi_write_jpg("wr6x5_flip.jpg", 6, 5, 3, img6x5_rgb, 95); + stbi_write_hdr("wr6x5_flip.hdr", 6, 5, 3, img6x5_rgbf); + + return 0; +}