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
|
/* $Id$ */
#include "gd.h"
#include <stdio.h>
#include <stdlib.h>
void save_png(gdImagePtr im, const char *filename)
{
FILE *fp;
fp = fopen(filename, "wb");
if (!fp) {
fprintf(stderr, "Can't save png image %s\n", filename);
return;
}
#ifdef HAVE_LIBPNG
gdImagePng(im, fp);
#else
printf("No PNG support. Cannot save image.\n");
#endif
fclose(fp);
}
gdImagePtr read_png(const char *filename)
{
FILE * fp;
gdImagePtr im;
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Can't read png image %s\n", filename);
return NULL;
}
#ifdef HAVE_LIBPNG
im = gdImageCreateFromPng(fp);
#else
im = NULL;
printf("No PNG support. Cannot read image.\n");
#endif
fclose(fp);
return im;
}
int main()
{
gdImagePtr im, im2;
im = gdImageCreateTrueColor(400, 400);
if (!im) {
fprintf(stderr, "Can't create 400x400 TC image\n");
return 1;
}
gdImageFilledRectangle(im, 19, 29, 390, 390, 0xFFFFFF);
gdImageRectangle(im, 19, 29, 390, 390, 0xFF0000);
save_png(im, "a1.png");
im2 = gdImageCropAuto(im, GD_CROP_SIDES);
if (im2) {
save_png(im2, "a2.png");
gdImageDestroy(im2);
}
gdImageDestroy(im);
im = read_png("test_crop_threshold.png");
if (!im) {
return 1;
}
im2 = gdImageCropThreshold(im, 0xFFFFFF, 0.6);
if (im2) {
save_png(im2, "a4.png");
gdImageDestroy(im2);
}
gdImageDestroy(im);
return 0;
}
|