summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorpajoye <none@none>2008-02-28 00:03:59 +0000
committerpajoye <none@none>2008-02-28 00:03:59 +0000
commit51f2fe893427c24ea4e7875beff0df9354809221 (patch)
tree36c7a5f54f65847cedd6b15142bd11f1b7177e72 /examples
parent41d830e0c110e378fc4473948390674f4ea65305 (diff)
downloadlibgd-51f2fe893427c24ea4e7875beff0df9354809221.tar.gz
- add copyRotated example
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt8
-rw-r--r--examples/copyrotated.c84
2 files changed, 88 insertions, 4 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 0388d4c..36f44c0 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -6,13 +6,13 @@ SET(TESTS_FILES
tiffread
resize
copyrotated
- ellipseaa
- ellipse
+# ellipseaa
+# ellipse
arc
- ellipsearc
+# ellipsearc
flip
crop
- ellfullaa
+# ellfullaa
tgaread
nnquant
gif
diff --git a/examples/copyrotated.c b/examples/copyrotated.c
new file mode 100644
index 0000000..9a89b3a
--- /dev/null
+++ b/examples/copyrotated.c
@@ -0,0 +1,84 @@
+/* $Id$ */
+#include "gd.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
+gdImagePtr loadImage(const char *name)
+{
+ FILE *fp;
+ gdImagePtr im;
+
+ fp = fopen(name, "rb");
+ if (!fp) {
+ fprintf(stderr, "Can't open jpeg file\n");
+ gdImageDestroy(im);
+ return NULL;
+ }
+
+ im = gdImageCreateFromJpeg(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(int argc, char **arg)
+{
+ gdImagePtr im, im2;
+ int new_width, new_height;
+ double angle;
+
+ if (argc < 3) {
+ fprintf(stderr, "Usage: copyrotated [angle in degree] [filename.png]\n");
+ return 1;
+ }
+ angle = strtod(arg[1], 0);
+ im = loadImage(arg[2]);
+
+ if (!im) {
+ fprintf(stderr, "Can't load PNG file <%s>", arg[1]);
+ return 1;
+ }
+
+ /*
+ cos adj hyp (cos = adjacent / hypothenus)
+ sin op hyp (sin adjacent / hypothenus)
+ */
+ new_width = cos(angle * .0174532925) * gdImageSX(im)
+ - sin(angle * .0174532925) * gdImageSY(im) + 1;
+
+ new_height = - sin(angle * .0174532925) * gdImageSX(im)
+ + cos(angle * .0174532925) * gdImageSY(im) + 1;
+
+
+ im2 = gdImageCreateTrueColor(new_width, new_height);
+ if (!im2) {
+ fprintf(stderr, "Can't create a new image");
+ gdImageDestroy(im);
+ return 1;
+ }
+
+ gdImageCopyRotated(im2, im, new_width/2, new_height/2, 0, 0, gdImageSX(im), gdImageSY(im), angle);
+ if (!savePngImage(im2, "rotated.png")) {
+ fprintf(stderr, "Can't save PNG file rotated.png");
+ gdImageDestroy(im);
+ gdImageDestroy(im2);
+ return 1;
+ }
+
+ gdImageDestroy(im2);
+ gdImageDestroy(im);
+ return 0;
+}