-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgd.c
64 lines (53 loc) · 1.25 KB
/
gd.c
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
// compile with
// gcc -Wall gd.c `pkg-config gdlib --cflags --libs` -o gd
#include <stdio.h>
#include <stdlib.h>
#include <gd.h>
int
main( int argc, char **argv )
{
FILE *fp;
gdImagePtr original, cropped,resized;
gdRect crop;
if( argc != 3 ) {
printf( "usage: %s in-jpeg out-jpeg\n", argv[0] );
exit( 1 );
}
if( !(fp = fopen( argv[1], "r" )) ) {
printf( "unable to open \"%s\"\n", argv[1] );
exit( 1 );
}
if( !(original = gdImageCreateFromJpeg( fp )) ) {
printf( "unable to load \"%s\"\n", argv[1] );
exit( 1 );
}
fclose( fp );
crop.x = 100;
crop.y = 100;
crop.width = original->sx - 200;
crop.height = original->sy - 200;
cropped = gdImageCrop( original, &crop );
gdImageDestroy( original );
original = 0;
if( !cropped ) {
printf( "unable to crop image\n" );
exit( 1 );
}
resized = gdImageScale( cropped, crop.width * 0.9, crop.height * 0.9 );
gdImageDestroy( cropped );
cropped = 0;
if( !resized ) {
printf( "unable to resize image\n" );
exit( 1 );
}
//gdImageSharpen is extremely slow
gdImageSharpen( resized, 75 );
if( !(fp = fopen( argv[2], "w" )) ) {
printf( "unable to open \"%s\"\n", argv[2] );
exit( 1 );
}
gdImageJpeg( resized, fp, -1 );
fclose( fp );
gdImageDestroy( resized );
return( 0 );
}