cvs: gd /libgd/examples CMakeLists.txt copyrotated.c

[email protected] ("Pierre-Alain Joye") Thu, 28 Feb 2008 00:03:59 -0000
Newsgroups php.gd.cvs
Message-ID <cvspajoye1204157039@cvsserver>
pajoye		Thu Feb 28 00:03:59 2008 UTC

  Added files:                 
    /gd/libgd/examples	copyrotated.c 

  Modified files:              
    /gd/libgd/examples	CMakeLists.txt 
  Log:
  - add copyRotated example
  
  
http://cvs.php.net/viewvc.cgi/gd/libgd/examples/CMakeLists.txt?r1=1.8&r2=1.9&diff_format=u
Index: gd/libgd/examples/CMakeLists.txt
diff -u gd/libgd/examples/CMakeLists.txt:1.8 gd/libgd/examples/CMakeLists.txt:1.9
--- gd/libgd/examples/CMakeLists.txt:1.8	Wed Feb 27 23:25:00 2008
+++ gd/libgd/examples/CMakeLists.txt	Thu Feb 28 00:03:59 2008
@@ -6,13 +6,13 @@
   tiffread
   resize
   copyrotated
-  ellipseaa
-  ellipse
+#  ellipseaa
+#  ellipse
   arc
-  ellipsearc
+#  ellipsearc
   flip
   crop
-  ellfullaa
+#  ellfullaa
   tgaread
   nnquant
   gif

http://cvs.php.net/viewvc.cgi/gd/libgd/examples/copyrotated.c?view=markup&rev=1.1
Index: gd/libgd/examples/copyrotated.c
+++ gd/libgd/examples/copyrotated.c
/* $Id: copyrotated.c,v 1.1 2008/02/28 00:03:59 pajoye Exp $ */
#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;
}