1 /*
2 
3 TestRotozoom.c: test program for rotozoom routines
4 
5 (C) A. Schiffler, 2001-2011, zlib License
6 
7 */
8 
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 
13 #include "SDL.h"
14 
15 #ifdef WIN32
16 #include <windows.h>
17 #include "SDL_gfxPrimitives.h"
18 #include "SDL_rotozoom.h"
19 #else
20 #include "SDL/SDL_gfxPrimitives.h"
21 #include "SDL/SDL_rotozoom.h"
22 #endif
23 
24 /* Pause flag */
25 int pause = 0;
26 
27 /* Custom rotation setup */
28 double custom_angle=0.0;
29 double custom_fx=1.0;
30 double custom_fy=1.0;
31 int custom_smooth=0;
32 
33 /* Delay between frames */
34 int delay;
35 
36 /* Curren message */
37 char *messageText;
38 
HandleEvent()39 void HandleEvent()
40 {
41 	SDL_Event event;
42 
43 	/* Check for events */
44 	while ( SDL_PollEvent(&event) || pause ) {
45 		switch (event.type) {
46 			 case SDL_KEYDOWN:
47 			        /* Space pauses/unpauses */
48 			 	if ((event.key.state==SDL_PRESSED) &&
49 			 	    (event.key.keysym.sym==SDLK_SPACE)) {
50 			 		pause = !pause;
51 			 		if (pause) {
52 			 			printf ("Paused ...\n");
53 			 		}
54 			 	} else {
55 			 		exit(0);
56 			 	}
57 			 	break;
58 			 case SDL_QUIT:
59 				 exit(0);
60 				 break;
61 		}
62 
63 		if (pause) {
64 			SDL_Delay(100);
65 		}
66 	}
67 }
68 
ClearScreen(SDL_Surface * screen)69 void ClearScreen(SDL_Surface *screen)
70 {
71 	int i;
72 	/* Set the screen to black */
73 	if ( SDL_LockSurface(screen) == 0 ) {
74 		Uint8 *pixels;
75 		pixels = (Uint8 *)screen->pixels;
76 		for ( i=0; i<screen->h; ++i ) {
77 			memset(pixels, 0,
78 				screen->w*screen->format->BytesPerPixel);
79 			pixels += screen->pitch;
80 		}
81 		SDL_UnlockSurface(screen);
82 	}
83 }
84 
85 #define POSITION_CENTER		1
86 #define POSITION_BOTTOMRIGHT	2
87 
RotatePicture(SDL_Surface * screen,SDL_Surface * picture,int rotate,int flip,int smooth,int position)88 void RotatePicture (SDL_Surface *screen, SDL_Surface *picture, int rotate, int flip, int smooth, int position)
89 {
90 	SDL_Surface *rotozoom_picture;
91 	SDL_Rect dest;
92 	int framecount, framemax, frameinc;
93 	double angle, zoomf, zoomfx, zoomfy;
94 
95 	printf("%s\n", messageText);
96 
97 	/* Rotate and display the picture */
98 	framemax=4*360;
99 	frameinc=1;
100 	for (framecount=-360; framecount<framemax; framecount += frameinc) {
101 		if ((framecount % 360)==0) frameinc++;
102 		HandleEvent();
103 		ClearScreen(screen);
104 		zoomf=(float)(framecount+2*360)/(float)framemax;
105 		zoomf=1.5*zoomf*zoomf;
106 		/* Are we in flipping mode? */
107 		if (flip) {
108 			/* Flip X factor */
109 			if (flip & 1) {
110 				zoomfx=-zoomf;
111 			} else {
112 				zoomfx=zoomf;
113 			}
114 			/* Flip Y factor */
115 			if (flip & 2) {
116 				zoomfy=-zoomf;
117 			} else {
118 				zoomfy=zoomf;
119 			}
120 			angle=framecount*rotate;
121 			if (((framecount % 120)==0) || (delay>0)) {
122 				printf ("  Frame: %i   Rotate: angle=%.2f  Zoom: x=%.2f y=%.2f\n",framecount,angle,zoomfx,zoomfy);
123 			}
124 			if ((rotozoom_picture=rotozoomSurfaceXY (picture, angle, zoomfx, zoomfy, smooth))!=NULL) {
125 				switch (position) {
126 					case POSITION_CENTER:
127 						dest.x = (screen->w - rotozoom_picture->w)/2;
128 						dest.y = (screen->h - rotozoom_picture->h)/2;
129 						break;
130 					case POSITION_BOTTOMRIGHT:
131 						dest.x = (screen->w/2) - rotozoom_picture->w;
132 						dest.y = (screen->h/2) - rotozoom_picture->h;
133 						break;
134 				}
135 				dest.w = rotozoom_picture->w;
136 				dest.h = rotozoom_picture->h;
137 				if ( SDL_BlitSurface(rotozoom_picture, NULL, screen, &dest) < 0 ) {
138 					fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
139 					break;
140 				}
141 				SDL_FreeSurface(rotozoom_picture);
142 		 }
143 		} else {
144 			angle=framecount*rotate;
145 			if ((framecount % 120)==0) {
146 				printf ("  Frame: %i   Rotate: angle=%.2f  Zoom: f=%.2f \n",framecount,angle,zoomf);
147 			}
148 			if ((rotozoom_picture=rotozoomSurface (picture, angle, zoomf, smooth))!=NULL) {
149 				switch (position) {
150 					case POSITION_CENTER:
151 						dest.x = (screen->w - rotozoom_picture->w)/2;
152 						dest.y = (screen->h - rotozoom_picture->h)/2;
153 						break;
154 					case POSITION_BOTTOMRIGHT:
155 						dest.x = (screen->w/2) - rotozoom_picture->w;
156 						dest.y = (screen->h/2) - rotozoom_picture->h;
157 						break;
158 				}
159 				dest.w = rotozoom_picture->w;
160 				dest.h = rotozoom_picture->h;
161 				if ( SDL_BlitSurface(rotozoom_picture, NULL, screen, &dest) < 0 ) {
162 					fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
163 					break;
164 				}
165 				SDL_FreeSurface(rotozoom_picture);
166 		 }
167 		}
168 
169 		stringRGBA(screen, 8, 8, messageText, 255, 255, 255, 255);
170 
171 		/* Display by flipping screens */
172 		SDL_Flip(screen);
173 
174 		/* Maybe delay */
175 		if (delay>0) {
176 			SDL_Delay(delay);
177 		}
178 	}
179 
180 	if (rotate) {
181 		/* Final display with angle=0 */
182 		HandleEvent();
183 		ClearScreen(screen);
184 		if (flip) {
185 			if ((rotozoom_picture=rotozoomSurfaceXY (picture, 0.01, zoomfx, zoomfy, smooth))!=NULL) {
186 				dest.x = (screen->w - rotozoom_picture->w)/2;;
187 				dest.y = (screen->h - rotozoom_picture->h)/2;
188 				dest.w = rotozoom_picture->w;
189 				dest.h = rotozoom_picture->h;
190 				if ( SDL_BlitSurface(rotozoom_picture, NULL, screen, &dest) < 0 ) {
191 					fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
192 					return;
193 				}
194 				SDL_FreeSurface(rotozoom_picture);
195 			}
196 		} else {
197 			if ((rotozoom_picture=rotozoomSurface (picture, 0.01, zoomf, smooth))!=NULL) {
198 				dest.x = (screen->w - rotozoom_picture->w)/2;;
199 				dest.y = (screen->h - rotozoom_picture->h)/2;
200 				dest.w = rotozoom_picture->w;
201 				dest.h = rotozoom_picture->h;
202 				if ( SDL_BlitSurface(rotozoom_picture, NULL, screen, &dest) < 0 ) {
203 					fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
204 					return;
205 				}
206 				SDL_FreeSurface(rotozoom_picture);
207 			}
208 		}
209 
210 		stringRGBA(screen, 8, 8, messageText, 255, 255, 255, 255);
211 
212 		/* Display by flipping screens */
213 		SDL_Flip(screen);
214 
215 		/* Maybe delay */
216 		if (delay>0) {
217 			SDL_Delay(delay);
218 		}
219 	}
220 
221 	/* Pause for a sec */
222 	SDL_Delay(1000);
223 }
224 
ZoomPicture(SDL_Surface * screen,SDL_Surface * picture,int smooth)225 void ZoomPicture (SDL_Surface *screen, SDL_Surface *picture, int smooth)
226 {
227 	SDL_Surface *rotozoom_picture;
228 	SDL_Rect dest;
229 	int framecount, framemax, frameinc;
230 	double zoomxf,zoomyf;
231 
232 	printf("%s\n", messageText);
233 
234 	/* Zoom and display the picture */
235 	framemax=4*360; frameinc=1;
236 	for (framecount=360; framecount<framemax; framecount += frameinc) {
237 		if ((framecount % 360)==0) frameinc++;
238 		HandleEvent();
239 		ClearScreen(screen);
240 		zoomxf=(float)framecount/(float)framemax;
241 		zoomxf=1.5*zoomxf*zoomxf;
242 		zoomyf=0.5+fabs(1.0*sin((double)framecount/80.0));
243 		if ((framecount % 120)==0) {
244 			printf ("  Frame: %i   Zoom: x=%.2f y=%.2f\n",framecount,zoomxf,zoomyf);
245 		}
246 		if ((rotozoom_picture=zoomSurface (picture, zoomxf, zoomyf, smooth))!=NULL) {
247 			dest.x = (screen->w - rotozoom_picture->w)/2;;
248 			dest.y = (screen->h - rotozoom_picture->h)/2;
249 			dest.w = rotozoom_picture->w;
250 			dest.h = rotozoom_picture->h;
251 			if ( SDL_BlitSurface(rotozoom_picture, NULL, screen, &dest) < 0 ) {
252 				fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
253 				break;
254 			}
255 			SDL_FreeSurface(rotozoom_picture);
256 		}
257 
258 		stringRGBA(screen, 8, 8, messageText, 255, 255, 255, 255);
259 
260 		/* Display by flipping screens */
261 		SDL_Flip(screen);
262 
263 		/* Maybe delay */
264 		if (delay>0) {
265 			SDL_Delay(delay);
266 		}
267 	}
268 
269 	/* Pause for a sec */
270 	SDL_Delay(1000);
271 }
272 
RotatePicture90Degrees(SDL_Surface * screen,SDL_Surface * picture)273 void RotatePicture90Degrees (SDL_Surface *screen, SDL_Surface *picture)
274 {
275 	SDL_Surface *rotozoom_picture;
276 	SDL_Rect dest;
277 	int framecount, framemax, frameinc;
278 	int numClockwiseTurns;
279 
280 	printf("%s\n", messageText);
281 
282 	/* Rotate and display the picture */
283 	framemax = 21;
284 	frameinc = 1;
285 	numClockwiseTurns = -4;
286 	for (framecount=0; framecount<framemax; framecount += frameinc) {
287 		HandleEvent();
288 		ClearScreen(screen);
289 		printf ("  Frame: %i   Rotate90: %i clockwise turns\n",framecount,numClockwiseTurns+4);
290 		if ((rotozoom_picture=rotateSurface90Degrees(picture, numClockwiseTurns))!=NULL) {
291 			dest.x = (screen->w - rotozoom_picture->w)/2;;
292 			dest.y = (screen->h - rotozoom_picture->h)/2;
293 			dest.w = rotozoom_picture->w;
294 			dest.h = rotozoom_picture->h;
295 			if (SDL_BlitSurface(rotozoom_picture, NULL, screen, &dest) < 0 ) {
296 				fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
297 				break;
298 			}
299 			SDL_FreeSurface(rotozoom_picture);
300 		}
301 
302 		stringRGBA(screen, 8, 8, messageText, 255, 255, 255, 255);
303 
304 		/* Display by flipping screens */
305 		SDL_Flip(screen);
306 
307 		/* Always delay */
308 		SDL_Delay(333);
309 		if (delay>0) {
310 			SDL_Delay(delay);
311 		}
312 
313 		numClockwiseTurns++;
314 	}
315 
316 	/* Pause for a sec */
317 	SDL_Delay(1000);
318 }
319 
320 #define ROTATE_OFF	0
321 #define ROTATE_ON	1
322 
323 #define FLIP_OFF	0
324 #define FLIP_X		1
325 #define FLIP_Y		2
326 #define FLIP_XY		3
327 
CustomTest(SDL_Surface * screen,SDL_Surface * picture,double a,double x,double y,int smooth)328 void CustomTest(SDL_Surface *screen, SDL_Surface *picture, double a, double x, double y, int smooth){
329 	SDL_Surface *rotozoom_picture;
330 	SDL_Rect dest;
331 
332 	printf("%s\n", messageText);
333 	printf ("  Frame: C   Rotate: angle=%.2f  Zoom: fx=%.2f fy=%.2f \n",a,x,y);
334 
335 	HandleEvent();
336 	ClearScreen(screen);
337 	if ((rotozoom_picture=rotozoomSurfaceXY (picture, a, x, y, smooth))!=NULL) {
338 		dest.x = (screen->w - rotozoom_picture->w)/2;;
339 		dest.y = (screen->h - rotozoom_picture->h)/2;
340 		dest.w = rotozoom_picture->w;
341 		dest.h = rotozoom_picture->h;
342 		if ( SDL_BlitSurface(rotozoom_picture, NULL, screen, &dest) < 0 ) {
343 			fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
344 			return;
345 		}
346 		SDL_FreeSurface(rotozoom_picture);
347 	}
348 
349 	/* Display by flipping screens */
350 	SDL_Flip(screen);
351 
352 	/* Maybe delay */
353 	if (delay>0) {
354 		SDL_Delay(delay);
355 	}
356 
357 	SDL_Delay(1000);
358 }
359 
360 
AccuracyTest1(SDL_Surface * screen)361 void AccuracyTest1(SDL_Surface *screen)
362 {
363 	SDL_Surface* testx1;
364 	SDL_Surface* testx2;
365 	SDL_Rect r;
366 	SDL_Rect target;
367 	SDL_Surface* ref;
368 	int size, halfsize, doublesize;
369 
370 	printf("%s\n", messageText);
371 	for (size = 10; size < 200; size += 2)
372 	{
373 		HandleEvent();
374 		ClearScreen(screen);
375 
376 		stringRGBA(screen, 8, 8, messageText, 255, 255, 255, 255);
377 
378 		halfsize = size / 2;
379 		doublesize = size * 2;
380 		printf ("  zoom from %i to %i\n", size, doublesize);
381 
382 		// Set up test surfaces
383 		testx1 = SDL_CreateRGBSurface(SDL_SWSURFACE, size, size, 24, 0, 0, 0, 0);
384 
385 		r.x = 0;
386 		r.y = 0;
387 		r.w = halfsize;
388 		r.h = halfsize;
389 		SDL_FillRect(testx1, &r, 0x339933);
390 
391 		r.x = halfsize;
392 		r.y = halfsize;
393 		SDL_FillRect(testx1, &r, 0x993399);
394 
395 		r.x = 0;
396 		r.y = halfsize;
397 		SDL_FillRect(testx1, &r, 0x999933);
398 
399 		r.x = halfsize;
400 		r.y = 0;
401 		SDL_FillRect(testx1, &r, 0x333399);
402 
403 		testx2 = zoomSurface(testx1, 2.0, 2.0, 0);
404 
405 		ref = SDL_CreateRGBSurface(SDL_SWSURFACE, size, size, 24, 0, 0, 0, 0);
406 		r.w = size;
407 		r.h = size;
408 		r.x = 0;
409 		r.y = 0;
410 		SDL_FillRect(ref, &r, 0xFFFFFF);
411 
412 		/* Validation display */
413 
414 		target.x = 0;
415 		target.y = 20;
416 		target.w = 0;
417 		target.h = 0;
418 		SDL_BlitSurface(testx1, 0, screen, &target);
419 
420 		target.x = size;
421 		target.y = 20;
422 		SDL_BlitSurface(ref, 0, screen, &target);
423 
424 		target.x = 0;
425 		target.y = 20 + size;
426 		SDL_BlitSurface(ref, 0, screen, &target);
427 
428 		target.x = doublesize + 20;
429 		target.y = 20;
430 		SDL_BlitSurface(testx2, 0, screen, &target);
431 
432 		target.x = doublesize + doublesize + 20;
433 		target.y = 20;
434 		SDL_BlitSurface(ref, 0, screen, &target);
435 
436 		target.x = doublesize + 20;
437 		target.y = 20 + doublesize;
438 		SDL_BlitSurface(ref, 0, screen, &target);
439 
440 		SDL_FreeSurface(testx1);
441 		SDL_FreeSurface(testx2);
442 		SDL_FreeSurface(ref);
443 
444 		/* Display by flipping screens */
445 		SDL_Flip(screen);
446 
447 		/* Always delay */
448 		SDL_Delay(250);
449 
450 		/* Maybe add extra delay */
451 		if (delay>0) {
452 			SDL_Delay(delay);
453 		}
454 	}
455 
456 	SDL_Delay(1000);
457 }
458 
AccuracyTest2(SDL_Surface * screen,SDL_Surface * picture)459 void AccuracyTest2(SDL_Surface *screen, SDL_Surface *picture)
460 {
461 	SDL_Surface *zoomed1, *zoomed2, *zoomed3, *zoomed4;
462 	int factor;
463 	int neww, newh;
464 	SDL_Rect target;
465 
466 	printf("%s\n", messageText);
467 	for (factor = 2; factor < 64; factor += 1)
468 	{
469 		HandleEvent();
470 		ClearScreen(screen);
471 
472 		stringRGBA(screen, 8, 8, messageText, 255, 255, 255, 255);
473 
474 		neww = picture->w * factor;
475 		newh = picture->h * factor;
476 		printf ("  zoom %ix%i to %ix%i\n", picture->w, picture->h, neww, newh);
477 
478 		zoomed1 = zoomSurface(picture,  (float)factor,  (float)factor, 0);
479 		zoomed2 = zoomSurface(picture,  (float)factor,  (float)factor, 1);
480 		zoomed3 = zoomSurface(picture,  (float)factor, -(float)factor, 1);
481 		zoomed4 = zoomSurface(picture, -(float)factor,  (float)factor, 1);
482 
483 		target.x = screen->w/2 - zoomed1->w;
484 		target.y = screen->h/2 - zoomed1->h;
485 		target.w = zoomed1->w;
486 		target.h = zoomed1->h;
487 		SDL_BlitSurface(zoomed1, 0, screen, &target);
488 		target.x = screen->w/2;
489 		target.y = screen->h/2;
490 		SDL_BlitSurface(zoomed2, 0, screen, &target);
491 		target.x = screen->w/2 - zoomed3->w;
492 		target.y = screen->h/2;
493 		SDL_BlitSurface(zoomed4, 0, screen, &target);
494 		target.x = screen->w/2;
495 		target.y = screen->h/2 - zoomed4->h;
496 		SDL_BlitSurface(zoomed3, 0, screen, &target);
497 
498 		SDL_FreeSurface(zoomed1);
499 		SDL_FreeSurface(zoomed2);
500 		SDL_FreeSurface(zoomed3);
501 		SDL_FreeSurface(zoomed4);
502 
503 		/* Display by flipping screens */
504 		SDL_Flip(screen);
505 
506 		/* Always delay */
507 		SDL_Delay(250);
508 
509 		/* Maybe add extra delay */
510 		if (delay>0) {
511 			SDL_Delay(delay);
512 		}
513 	}
514 
515 	SDL_Delay(1000);
516 }
517 
518 
Draw(SDL_Surface * screen,int start,int end)519 void Draw (SDL_Surface *screen, int start, int end)
520 {
521 	SDL_Surface *picture, *picture_again;
522 	char *bmpfile;
523 
524 	/* Define masking bytes */
525 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
526 	Uint32 rmask = 0xff000000;
527 	Uint32 gmask = 0x00ff0000;
528 	Uint32 bmask = 0x0000ff00;
529 	Uint32 amask = 0x000000ff;
530 #else
531 	Uint32 amask = 0xff000000;
532 	Uint32 bmask = 0x00ff0000;
533 	Uint32 gmask = 0x0000ff00;
534 	Uint32 rmask = 0x000000ff;
535 #endif
536 
537 	/* --------- 8 bit tests -------- */
538 
539 	if (start<=6) {
540 
541 		/* Message */
542 		printf("8 bit tests ...\n");
543 
544 		/* Load the image into a surface */
545 		bmpfile = "sample8.bmp";
546 		printf("Loading picture: %s\n", bmpfile);
547 		picture = SDL_LoadBMP(bmpfile);
548 		if ( picture == NULL ) {
549 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
550 			return;
551 		}
552 
553 		/* Add white frame */
554 		rectangleColor(picture, 0, 0, picture->w-1, picture->h-1, 0xffffffff);
555 
556                 if (start <= 1) {
557 			sprintf(messageText, "1.  rotozoom: Rotating and zooming");
558 			RotatePicture(screen,picture,ROTATE_ON,FLIP_OFF,SMOOTHING_OFF,POSITION_CENTER);
559 		}
560 		if (end == 1) goto done8bit;
561 
562                 if (start <= 2) {
563 			sprintf(messageText, "2.  rotozoom: Just zooming (angle=0)");
564 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_OFF,SMOOTHING_OFF,POSITION_CENTER);
565 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_OFF,SMOOTHING_OFF,POSITION_BOTTOMRIGHT);
566 		}
567 		if (end == 2) goto done8bit;
568 
569                 if (start <= 3) {
570 			sprintf(messageText, "3.  zoom: Just zooming");
571 			ZoomPicture(screen,picture,SMOOTHING_OFF);
572 		}
573 		if (end == 3) goto done8bit;
574 
575                 if (start <= 4) {
576 			sprintf(messageText, "4.  rotozoom: Rotating and zooming, interpolation on but unused");
577 			RotatePicture(screen,picture,ROTATE_ON,FLIP_OFF,SMOOTHING_ON,POSITION_CENTER);
578 		}
579 		if (end == 4) goto done8bit;
580 
581                 if (start <= 5) {
582 			sprintf(messageText, "5.  rotozoom: Just zooming (angle=0), interpolation on but unused");
583 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_OFF,SMOOTHING_ON,POSITION_CENTER);
584 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_OFF,SMOOTHING_ON,POSITION_BOTTOMRIGHT);
585 		}
586 		if (end == 5) goto done8bit;
587 
588                 if (start <= 6) {
589 			sprintf(messageText, "6.  zoom: Just zooming, interpolation on but unused");
590 			ZoomPicture(screen,picture,SMOOTHING_ON);
591 		}
592 		if (end == 6) goto done8bit;
593 
594 		done8bit:
595 
596 		/* Free the picture */
597 		SDL_FreeSurface(picture);
598 
599 		if (end <= 6) return;
600 	}
601 
602 	/* -------- 24 bit test --------- */
603 
604 	if (start<=12) {
605 
606 		/* Message */
607 		printf("24 bit tests ...\n");
608 
609 		/* Load the image into a surface */
610 		bmpfile = "sample24.bmp";
611 		printf("Loading picture: %s\n", bmpfile);
612 		picture = SDL_LoadBMP(bmpfile);
613 		if ( picture == NULL ) {
614 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
615 			return;
616 		}
617 
618 		/* Add white frame */
619 		rectangleColor(picture, 0, 0, picture->w-1, picture->h-1, 0xffffffff);
620 
621                 if (start <= 7) {
622 			sprintf(messageText, "7.  rotozoom: Rotating and zooming, no interpolation");
623 		  	RotatePicture(screen,picture,ROTATE_ON,FLIP_OFF,SMOOTHING_OFF,POSITION_CENTER);
624 		}
625 		if (end == 7) goto done24bit;
626 
627                 if (start <= 8) {
628 			sprintf(messageText, "8a.  rotozoom: Just zooming (angle=0), no interpolation, centered");
629 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_OFF,SMOOTHING_OFF,POSITION_CENTER);
630 			sprintf(messageText, "8b.  rotozoom: Just zooming (angle=0), no interpolation, corner");
631 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_OFF,SMOOTHING_OFF,POSITION_BOTTOMRIGHT);
632 			sprintf(messageText, "8c.  rotozoom: Just zooming (angle=0), X flip, no interpolation, centered");
633 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_X,SMOOTHING_OFF,POSITION_CENTER);
634 			sprintf(messageText, "8d.  rotozoom: Just zooming (angle=0), Y flip, no interpolation, centered");
635 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_Y,SMOOTHING_OFF,POSITION_CENTER);
636 			sprintf(messageText, "8e.  rotozoom: Just zooming (angle=0), XY flip, no interpolation, centered");
637 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_XY,SMOOTHING_OFF,POSITION_CENTER);
638 		}
639 		if (end == 8) goto done24bit;
640 
641                 if (start <= 9) {
642   			sprintf(messageText, "9.  zoom: Just zooming, no interpolation");
643   			ZoomPicture(screen,picture,SMOOTHING_OFF);
644 		}
645 		if (end == 9) goto done24bit;
646 
647                 if (start <= 10) {
648 			sprintf(messageText, "10. rotozoom: Rotating and zooming, with interpolation");
649 			RotatePicture(screen,picture,ROTATE_ON,FLIP_OFF,SMOOTHING_ON,POSITION_CENTER);
650 		}
651 		if (end == 10) goto done24bit;
652 
653                 if (start <= 11) {
654 			sprintf(messageText, "11a. rotozoom: Just zooming (angle=0), with interpolation, centered");
655 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_OFF,SMOOTHING_ON,POSITION_CENTER);
656 			sprintf(messageText, "11b. rotozoom: Just zooming (angle=0), with interpolation, corner");
657 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_OFF,SMOOTHING_ON,POSITION_BOTTOMRIGHT);
658 			sprintf(messageText, "11c. rotozoom: Just zooming (angle=0), X flip, with interpolation, corner");
659 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_X,SMOOTHING_ON,POSITION_CENTER);
660 			sprintf(messageText, "11d. rotozoom: Just zooming (angle=0), Y flip, with interpolation, corner");
661 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_Y,SMOOTHING_ON,POSITION_CENTER);
662 			sprintf(messageText, "11e. rotozoom: Just zooming (angle=0), XY flip, with interpolation, corner");
663 			RotatePicture(screen,picture,ROTATE_OFF,FLIP_XY,SMOOTHING_ON,POSITION_CENTER);
664 		}
665 		if (end == 11) goto done24bit;
666 
667                 if (start <= 12) {
668 			sprintf(messageText, "12. zoom: Just zooming, with interpolation");
669 			ZoomPicture(screen,picture,SMOOTHING_ON);
670 		}
671 		if (end == 12) goto done24bit;
672 
673 		done24bit:
674 
675 		/* Free the picture */
676 		SDL_FreeSurface(picture);
677 
678 		if (end <= 12) return;
679 	}
680 
681 	/* -------- 32 bit test --------- */
682 
683 	if (start<=16) {
684 
685 		/* Message */
686 		printf("32 bit tests ...\n");
687 
688 		/* Load the image into a surface */
689 		bmpfile = "sample24.bmp";
690 		printf("Loading picture: %s\n", bmpfile);
691 		picture = SDL_LoadBMP(bmpfile);
692 		if ( picture == NULL ) {
693 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
694 			return;
695 		}
696 
697 		/* Add white frame */
698 		rectangleColor(picture, 0, 0, picture->w-1, picture->h-1, 0xffffffff);
699 
700 		/* New source surface is 32bit with defined RGBA ordering */
701 		/* Much faster to do this once rather than the routine on the fly */
702 		fprintf(stderr,"Converting 24bit image into 32bit RGBA surface ...\n");
703 		picture_again = SDL_CreateRGBSurface(SDL_SWSURFACE, picture->w, picture->h, 32, rmask, gmask, bmask, amask);
704 		if (picture_again == NULL) goto done32bit;
705 		SDL_BlitSurface(picture,NULL,picture_again,NULL);
706 
707                 if (start <= 13) {
708 			sprintf(messageText, "13. Rotating and zooming, with interpolation (RGBA source)");
709 			RotatePicture(screen,picture_again,ROTATE_ON,FLIP_OFF,SMOOTHING_ON,POSITION_CENTER);
710 		}
711 		if (end == 13) goto done32bit;
712 
713                 if (start <= 14) {
714 			sprintf(messageText, "14. Just zooming (angle=0), with interpolation (RGBA source)");
715 			RotatePicture(screen,picture_again,ROTATE_OFF,FLIP_OFF,SMOOTHING_ON,POSITION_CENTER);
716 			RotatePicture(screen,picture_again,ROTATE_OFF,FLIP_OFF,SMOOTHING_ON,POSITION_BOTTOMRIGHT);
717 		}
718 		if (end == 14) goto done32bit;
719 
720 		SDL_FreeSurface(picture_again);
721 		picture_again=NULL;
722 
723 		/* New source surface is 32bit with defined ABGR ordering */
724 		/* Much faster to do this once rather than the routine on the fly */
725 		fprintf(stderr,"Converting 24bit image into 32bit ABGR surface ...\n");
726 		picture_again = SDL_CreateRGBSurface(SDL_SWSURFACE, picture->w, picture->h, 32, amask, bmask, gmask, rmask);
727 		if (picture_again == NULL) goto done32bit;
728 		SDL_BlitSurface(picture,NULL,picture_again,NULL);
729 
730                 if (start <= 14) {
731 			sprintf(messageText, "15. Rotating and zooming, with interpolation (ABGR source)");
732 			RotatePicture(screen,picture_again,ROTATE_ON,FLIP_OFF,SMOOTHING_ON,POSITION_CENTER);
733 		}
734 		if (end == 14) goto done32bit;
735 
736                 if (start <= 14) {
737 			sprintf(messageText, "16. Just zooming (angle=0), with interpolation (ABGR source)");
738 			RotatePicture(screen,picture_again,ROTATE_OFF,FLIP_OFF,SMOOTHING_ON,POSITION_CENTER);
739 			RotatePicture(screen,picture_again,ROTATE_OFF,FLIP_OFF,SMOOTHING_ON,POSITION_BOTTOMRIGHT);
740 		}
741 		if (end == 14) goto done32bit;
742 
743 
744 		done32bit:
745 
746 		/* Free the picture */
747 		SDL_FreeSurface(picture);
748 		if (picture_again) SDL_FreeSurface(picture_again);
749 
750 		if (end <= 16) return;
751 	}
752 
753 	/* -------- 32 bit flip test --------- */
754 
755 	if (start<=22) {
756 
757 		/* Message */
758 		printf("32 bit flip tests ...\n");
759 
760 		/* Load the image into a surface */
761 		bmpfile = "sample24.bmp";
762 		printf("Loading picture: %s\n", bmpfile);
763 		picture = SDL_LoadBMP(bmpfile);
764 		if ( picture == NULL ) {
765 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
766 			return;
767 		}
768 
769 		/* Add white frame */
770 		rectangleColor(picture, 0, 0, picture->w-1, picture->h-1, 0xffffffff);
771 
772 		/* Excercise flipping functions on 32bit RGBA */
773 		printf("Converting 24bit image into 32bit RGBA surface ...\n");
774 		picture_again = SDL_CreateRGBSurface(SDL_SWSURFACE, picture->w, picture->h, 32, rmask, gmask, bmask, amask);
775 		if (picture_again == NULL) goto doneflip;
776 		SDL_BlitSurface(picture,NULL,picture_again,NULL);
777 
778                 if (start <= 17) {
779 			sprintf(messageText, "17. Rotating with x-flip, no interpolation (RGBA source)");
780 			RotatePicture(screen,picture_again,ROTATE_ON,FLIP_X,SMOOTHING_OFF,POSITION_CENTER);
781 		}
782 		if (end == 17) goto doneflip;
783 
784                 if (start <= 18) {
785 			sprintf(messageText, "18. Rotating with y-flip, no interpolation");
786 			RotatePicture(screen,picture_again,ROTATE_ON,FLIP_Y,SMOOTHING_OFF,POSITION_CENTER);
787 		}
788 		if (end == 18) goto doneflip;
789 
790                 if (start <= 19) {
791 			sprintf(messageText, "19. Rotating with xy-flip, no interpolation");
792 			RotatePicture(screen,picture_again,ROTATE_ON,FLIP_XY,SMOOTHING_OFF,POSITION_CENTER);
793 		}
794 		if (end == 19) goto doneflip;
795 
796                 if (start <= 20) {
797 			sprintf(messageText, "20. Rotating with x-flip, with interpolation");
798 			RotatePicture(screen,picture_again,ROTATE_ON,FLIP_X,SMOOTHING_ON,POSITION_CENTER);
799 		}
800 		if (end == 20) goto doneflip;
801 
802                 if (start <= 21) {
803 			sprintf(messageText, "21. Rotating with y-flip, with interpolation");
804 			RotatePicture(screen,picture_again,ROTATE_ON,FLIP_Y,SMOOTHING_ON,POSITION_CENTER);
805 		}
806 		if (end == 21) goto doneflip;
807 
808                 if (start <= 22) {
809 			sprintf(messageText, "22. Rotating with xy-flip, with interpolation");
810 			RotatePicture(screen,picture_again,ROTATE_ON,FLIP_XY,SMOOTHING_ON,POSITION_CENTER);
811 		}
812 		if (end == 22) goto doneflip;
813 
814 		doneflip:
815 
816 		/* Free the pictures */
817 		SDL_FreeSurface(picture);
818 		if (picture_again) SDL_FreeSurface(picture_again);
819 
820 		if (end <= 22) return;
821 	}
822 
823 	if (start<=24) {
824 
825 		/* Message */
826 		printf("Loading 24bit image\n");
827 
828 		/* Load the image into a surface */
829 		bmpfile = "sample24.bmp";
830 		printf("Loading picture: %s\n", bmpfile);
831 		picture = SDL_LoadBMP(bmpfile);
832 		if ( picture == NULL ) {
833 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
834 			return;
835 		}
836 
837 		/* Add white frame */
838 		rectangleColor(picture, 0, 0, picture->w-1, picture->h-1, 0xffffffff);
839 
840 		/* Excercise flipping functions on 32bit RGBA */
841 		fprintf(stderr,"Converting 24bit image into 32bit RGBA surface ...\n");
842 		picture_again = SDL_CreateRGBSurface(SDL_SWSURFACE, picture->w, picture->h, 32, rmask, gmask, bmask, amask);
843 		SDL_BlitSurface(picture,NULL,picture_again,NULL);
844 
845 		sprintf(messageText, "23. CustomTest, values from commandline (32bit)");
846 		CustomTest(screen, picture_again, custom_angle, custom_fx, custom_fy, custom_smooth);
847 
848 		SDL_FreeSurface(picture_again);
849 
850 		/* Free the picture */
851 		SDL_FreeSurface(picture);
852 
853 		/* Message */
854 		printf("Loading 8bit image\n");
855 
856 		/* Load the image into a surface */
857 		bmpfile = "sample8.bmp";
858 		printf("Loading picture: %s\n", bmpfile);
859 		picture = SDL_LoadBMP(bmpfile);
860 		if ( picture == NULL ) {
861 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
862 			return;
863 		}
864 
865 		sprintf(messageText, "24. CustomTest, values from commandline (8bit)");
866 		CustomTest(screen, picture, custom_angle, custom_fx, custom_fy, custom_smooth);
867 
868 		/* Free the picture */
869 		SDL_FreeSurface(picture);
870 
871 		if (end <= 24) return;
872 	}
873 
874 	if (start<=25) {
875 
876 		/* Message */
877 		printf("Loading 24bit image\n");
878 
879 		/* Load the image into a surface */
880 		bmpfile = "sample24.bmp";
881 		printf("Loading picture: %s\n", bmpfile);
882 		picture = SDL_LoadBMP(bmpfile);
883 		if ( picture == NULL ) {
884 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
885 			return;
886 		}
887 
888 		/* Add white frame */
889 		rectangleColor(picture, 0, 0, picture->w-1, picture->h-1, 0xffffffff);
890 
891 		/* New source surface is 32bit with defined RGBA ordering */
892 		printf("Converting 24bit image into 32bit RGBA surface ...\n");
893 		picture_again = SDL_CreateRGBSurface(SDL_SWSURFACE, picture->w, picture->h, 32, rmask, gmask, bmask, amask);
894 		if (picture_again == NULL) goto donerotate90;
895 		SDL_BlitSurface(picture,NULL,picture_again,NULL);
896 
897 		/* Excercise rotate90 function on 32bit RGBA */
898 		sprintf(messageText, "25.  rotate90: Rotate 90 degrees clockwise (32bit)");
899 		RotatePicture90Degrees(screen, picture_again);
900 
901 		donerotate90:
902 
903 		/* Free the pictures */
904 		SDL_FreeSurface(picture);
905 		if (picture_again) SDL_FreeSurface(picture_again);
906 
907 		if (end <= 25) return;
908 	}
909 
910 	if (start<=26) {
911 		/* Run accuracy test */
912 		sprintf(messageText, "26.  accuracy: zoom by factor of 2");
913 		AccuracyTest1(screen);
914 
915 		if (end <= 26) return;
916 	}
917 
918 	if (start <= 27) {
919 		/* Load the image into a surface */
920 		bmpfile = "sample2x2.bmp";
921 		printf("Loading picture: %s\n", bmpfile);
922 		picture = SDL_LoadBMP(bmpfile);
923 		if ( picture == NULL ) {
924 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
925 			return;
926 		}
927 
928 		sprintf(messageText, "27a.  zoom accuracy: zoom 2x2 bitmap");
929 		AccuracyTest2(screen, picture);
930 
931 		/* Free the pictures */
932 		SDL_FreeSurface(picture);
933 
934 		/* Load the image into a surface */
935 		bmpfile = "sample3x3.bmp";
936 		printf("Loading picture: %s\n", bmpfile);
937 		picture = SDL_LoadBMP(bmpfile);
938 		if ( picture == NULL ) {
939 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
940 			return;
941 		}
942 
943 		sprintf(messageText, "27b.  zoom accuracy: zoom 3x3 bitmap");
944 		AccuracyTest2(screen, picture);
945 
946 		/* Free the pictures */
947 		SDL_FreeSurface(picture);
948 
949 		/* Load the image into a surface */
950 		bmpfile = "sample16x16.bmp";
951 		printf("Loading picture: %s\n", bmpfile);
952 		picture = SDL_LoadBMP(bmpfile);
953 		if ( picture == NULL ) {
954 			fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
955 			return;
956 		}
957 
958 		sprintf(messageText, "27c.  zoom accuracy: zoom 16x16 bitmap");
959 		AccuracyTest2(screen, picture);
960 
961 		/* Free the pictures */
962 		SDL_FreeSurface(picture);
963 
964 		if (end <= 27) return;
965 	}
966 
967 	return;
968 }
969 
970 /*!
971  \brief SDL_rotozoom test
972 */
main(int argc,char * argv[])973 int main ( int argc, char *argv[] )
974 {
975 	SDL_Surface *screen;
976 	int w, h;
977 	int desired_bpp;
978 	Uint32 video_flags;
979 	int start, end;
980 
981 	/* Title */
982 	fprintf(stderr,"SDL_rotozoom test\n");
983 	messageText = (char *)malloc(128);
984 
985 	/* Set default options and check command-line */
986 	w = 640;
987 	h = 480;
988 	desired_bpp = 0;
989 	video_flags = 0;
990 	start = 1;
991 	end = 9999;
992 	delay = 0;
993 	while ( argc > 1 ) {
994 		if ( strcmp(argv[1], "-start") == 0 ) {
995 			if ( argv[2] && ((start = atoi(argv[2])) > 0) ) {
996 				argv += 2;
997 				argc -= 2;
998 			} else {
999 				fprintf(stderr,
1000 					"The -start option requires a numeric argument\n");
1001 				exit(1);
1002 			}
1003 		}
1004 		else if ( strcmp(argv[1], "-end") == 0 ) {
1005 			if ( argv[2] && ((end = atoi(argv[2])) > 0) ) {
1006 				argv += 2;
1007 				argc -= 2;
1008 			} else {
1009 				fprintf(stderr,
1010 					"The -end option requires a numeric argument\n");
1011 				exit(1);
1012 			}
1013 
1014 		}
1015 		else
1016 		if ( strcmp(argv[1], "-delay") == 0 ) {
1017 				if ( argv[2] && ((delay = atoi(argv[2])) > 0) ) {
1018 					argv += 2;
1019 					argc -= 2;
1020 				} else {
1021 					fprintf(stderr,
1022 						"The -delay option requires an argument\n");
1023 					exit(1);
1024 				}
1025 		} else
1026 		if ( strcmp(argv[1], "-width") == 0 ) {
1027 					if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
1028 						argv += 2;
1029 						argc -= 2;
1030 					} else {
1031 						fprintf(stderr,
1032 							"The -width option requires an argument\n");
1033 						exit(1);
1034 					}
1035 		} else
1036 		if ( strcmp(argv[1], "-height") == 0 ) {
1037 						if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
1038 							argv += 2;
1039 							argc -= 2;
1040 						} else {
1041 							fprintf(stderr,
1042 								"The -height option requires an argument\n");
1043 							exit(1);
1044 						}
1045 					} else
1046 						if ( strcmp(argv[1], "-bpp") == 0 ) {
1047 							if ( argv[2] ) {
1048 								desired_bpp = atoi(argv[2]);
1049 								argv += 2;
1050 								argc -= 2;
1051 							} else {
1052 								fprintf(stderr,
1053 									"The -bpp option requires an argument\n");
1054 								exit(1);
1055 							}
1056 						} else
1057 							if ( strcmp(argv[1], "-warp") == 0 ) {
1058 								video_flags |= SDL_HWPALETTE;
1059 								argv += 1;
1060 								argc -= 1;
1061 							} else
1062 								if ( strcmp(argv[1], "-hw") == 0 ) {
1063 									video_flags |= SDL_HWSURFACE;
1064 									argv += 1;
1065 									argc -= 1;
1066 								} else
1067 									if ( strcmp(argv[1], "-fullscreen") == 0 ) {
1068 										video_flags |= SDL_FULLSCREEN;
1069 										argv += 1;
1070 										argc -= 1;
1071 									} else
1072 										if ( strcmp(argv[1], "-custom") == 0 ) {
1073 											if (( argv[2] ) && ( argv[3] ) && ( argv[4] ) && (argv[5] )) {
1074 												custom_angle = atof(argv[2]);
1075 												custom_fx = atof(argv[3]);
1076 												custom_fy = atof(argv[4]);
1077 												custom_smooth = atoi(argv[5]);
1078 												argv += 5;
1079 												argc -= 5;
1080 											} else {
1081 												fprintf(stderr,
1082 													"The -custom option requires 4 arguments\n");
1083 												exit(1);
1084 											}
1085 										} else
1086 											if (( strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "--help") == 0)) {
1087 												printf ("Usage:\n");
1088 												printf (" -start #	  Set starting test number\n");
1089 												printf ("             1=8bit, 7=24bit, 13=32bit, 19=32bit flip, 23=custom, 25=rotate90\n");
1090 												printf ("             26=zoom accuracy\n");
1091 												printf (" -delay #        Set delay between frames in ms (default: 0=off)\n");
1092 												printf ("                 (if >0, enables verbose frame logging\n");
1093 												printf (" -width #	  Screen width (Default: %i)\n",w);
1094 												printf (" -height #	  Screen height (Default: %i)\n",h);
1095 												printf (" -bpp #	  Screen bpp\n");
1096 												printf (" -warp		  Enable hardware palette\n");
1097 												printf (" -hw		  Enable hardware surface\n");
1098 												printf (" -fullscreen	  Enable fullscreen mode\n");
1099 												printf (" -custom # # #	# Custom: angle scalex scaley smooth\n");
1100 												printf ("                  scalex/scaley<0, enables flip on axis\n");
1101 												printf ("                  smooth=0/1\n");
1102 												exit(0);
1103 											} else
1104 												break;
1105 	}
1106 
1107 	/* Force double buffering */
1108 	video_flags |= SDL_DOUBLEBUF;
1109 
1110 	/* Initialize SDL */
1111 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
1112 		fprintf(stderr,
1113 			"Couldn't initialize SDL: %s\n", SDL_GetError());
1114 		exit(1);
1115 	}
1116 	atexit(SDL_Quit);			/* Clean up on exit */
1117 
1118 	/* Initialize the display */
1119 	screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
1120 	if ( screen == NULL ) {
1121 		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
1122 			w, h, desired_bpp, SDL_GetError());
1123 		exit(1);
1124 	}
1125 
1126 	/* Show some info */
1127 	printf("Set %dx%dx%d mode\n",
1128 		screen->w, screen->h, screen->format->BitsPerPixel);
1129 	printf("Video surface located in %s memory.\n",
1130 		(screen->flags&SDL_HWSURFACE) ? "video" : "system");
1131 
1132 	/* Check for double buffering */
1133 	if ( screen->flags & SDL_DOUBLEBUF ) {
1134 		printf("Double-buffering enabled - good!\n");
1135 	}
1136 
1137 	/* Set the window manager title bar */
1138 	SDL_WM_SetCaption("SDL_rotozoom test", "rotozoom");
1139 
1140 	/* Do all the drawing work */
1141 	Draw(screen, start, end);
1142 	free(messageText);
1143 
1144 	return(0);
1145 }
1146