summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorPierre Joye <pierre.php@gmail.com>2021-08-16 13:47:59 +0700
committerPierre Joye <pierre.php@gmail.com>2021-08-16 13:47:59 +0700
commit668f6b5fcffddf40550e2d85c53a249f0e1457e0 (patch)
treeca2bf7f4563c565fc0ee1247e0e894a0b703054a /examples
parentc483535fc991e9f5b1c96e269b0d7bb8f0b6952f (diff)
downloadlibgd-668f6b5fcffddf40550e2d85c53a249f0e1457e0.tar.gz
Add image scale example
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt2
-rw-r--r--examples/imagescale.c82
2 files changed, 83 insertions, 1 deletions
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 <stdio.h>
+
+#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;
+}