From 668f6b5fcffddf40550e2d85c53a249f0e1457e0 Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Mon, 16 Aug 2021 13:47:59 +0700 Subject: Add image scale example --- examples/CMakeLists.txt | 2 +- examples/imagescale.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 examples/imagescale.c (limited to 'examples') diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 70282c7..5b0d99d 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -8,7 +8,7 @@ if (PNG_FOUND) LIST(APPEND TEST_FILES arc crop tgaread) if (JPEG_FOUND) - LIST(APPEND TESTS_FILES copyrotated flip nnquant) + LIST(APPEND TESTS_FILES copyrotated flip nnquant imagescale) endif (JPEG_FOUND) endif (PNG_FOUND) diff --git a/examples/imagescale.c b/examples/imagescale.c new file mode 100644 index 0000000..d23e172 --- /dev/null +++ b/examples/imagescale.c @@ -0,0 +1,82 @@ +/* Exercise all scaling with all interpolation modes and ensure that + * at least, something comes back. */ + +#include + +#include "gd.h" + +#define X 100 +#define Y 100 + +#define NX 100 +#define NY 100 + +gdImagePtr loadImage(const char *name) +{ + FILE *fp; + gdImagePtr im; + + fp = fopen(name, "rb"); + if (!fp) { + fprintf(stderr, "Can't open jpeg file\n"); + return NULL; + } + + im = gdImageCreateFromPng(fp); + fclose(fp); + return im; +} + +int savePngImage(gdImagePtr im, const char *name) +{ + FILE *fp; + fp = fopen(name, "wb"); + if (!fp) { + fprintf(stderr, "Can't save png image fromtiff.png\n"); + return 0; + } + gdImagePng(im, fp); + fclose(fp); + return 1; +} + +int main() { + unsigned int method; + gdImagePtr src; + src = loadImage("fillandstroke.png"); + if (!src) { + printf("cannot open src image"); + exit(1); + } + int new_width = gdImageSX(src) *3; + int new_height = gdImageSY(src) *3; + for(method = GD_DEFAULT; method < GD_METHOD_COUNT; method++) { /* GD_WEIGHTED4 is unsupported. */ + gdImagePtr result; + if (method == GD_WEIGHTED4) { + continue; + } + + gdImageSetInterpolationMethod(src, method); + printf("arg %i set: %i get %i\n", method, src->interpolation_id, gdImageGetInterpolationMethod(src)); + + result = gdImageScale(src, new_width, new_height); + if (result == NULL) { + printf("gdImageScale failed (method: %i).\n", method); + break; + } + if (result->sx != new_width || result->sy != new_height) { + printf("missmatch width or height\n"); + gdImageDestroy(result); + gdImageDestroy(src); + exit(1); + } + + char filename[255]; + sprintf(filename, "scale_%i.png", method); + savePngImage(result, filename); + gdImageDestroy(result); + }/* for*/ + gdImageDestroy(src); + + return 0; +} -- cgit v1.2.1