summaryrefslogtreecommitdiff
path: root/examples/imagescale.c
blob: d23e172cb810d1e45b95d9ca8412d70a2d9ba283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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;
}