19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/********************************************************************************
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *                                                                              *
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Test of the overlay used for moved pictures, test more closed to real life.  *
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Running trojan moose :) Coded by Mike Gorchak.                               *
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *                                                                              *
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ********************************************************************************/
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdlib.h>
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdio.h>
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <string.h>
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h"
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MOOSEPIC_W 64
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MOOSEPIC_H 88
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MOOSEFRAMES_COUNT 10
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Color MooseColors[84]={
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    { 49,  49,  49}, { 66,  24,   0}, { 66,  33,   0}, { 66,  66,  66},
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    { 66, 115,  49}, { 74,  33,   0}, { 74,  41,  16}, { 82,  33,   8},
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    { 82,  41,   8}, { 82,  49,  16}, { 82,  82,  82}, { 90,  41,   8},
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    { 90,  41,  16}, { 90,  57,  24}, { 99,  49,  16}, { 99,  66,  24},
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    { 99,  66,  33}, { 99,  74,  33}, {107,  57,  24}, {107,  82,  41},
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {115,  57,  33}, {115,  66,  33}, {115,  66,  41}, {115,  74,   0},
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {115,  90,  49}, {115, 115, 115}, {123,  82,   0}, {123,  99,  57},
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {132,  66,  41}, {132,  74,  41}, {132,  90,   8}, {132,  99,  33},
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {132,  99,  66}, {132, 107,  66}, {140,  74,  49}, {140,  99,  16},
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {140, 107,  74}, {140, 115,  74}, {148, 107,  24}, {148, 115,  82},
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {148, 123,  74}, {148, 123,  90}, {156, 115,  33}, {156, 115,  90},
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {156, 123,  82}, {156, 132,  82}, {156, 132,  99}, {156, 156, 156},
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {165, 123,  49}, {165, 123,  90}, {165, 132,  82}, {165, 132,  90},
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {165, 132,  99}, {165, 140,  90}, {173, 132,  57}, {173, 132,  99},
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {173, 140, 107}, {173, 140, 115}, {173, 148,  99}, {173, 173, 173},
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {181, 140,  74}, {181, 148, 115}, {181, 148, 123}, {181, 156, 107},
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {189, 148, 123}, {189, 156,  82}, {189, 156, 123}, {189, 156, 132},
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {189, 189, 189}, {198, 156, 123}, {198, 165, 132}, {206, 165,  99},
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {206, 165, 132}, {206, 173, 140}, {206, 206, 206}, {214, 173, 115},
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {214, 173, 140}, {222, 181, 148}, {222, 189, 132}, {222, 189, 156},
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173}
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void quit(int rc)
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Quit();
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	exit(rc);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* NOTE: These RGB conversion functions are not intended for speed,
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         only as examples.
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid RGBtoYUV(Uint8 *rgb, int *yuv, int monochrome, int luminance)
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (monochrome)
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 1 /* these are the two formulas that I found on the FourCC site... */
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[1] = 128;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[2] = 128;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[1] = 128;
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[2] = 128;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 1 /* these are the two formulas that I found on the FourCC site... */
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[1] = (rgb[2]-yuv[0])*0.565 + 128;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[2] = (rgb[0]-yuv[0])*0.713 + 128;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (luminance!=100)
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        yuv[0]=yuv[0]*luminance/100;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (yuv[0]>255)
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            yuv[0]=255;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid ConvertRGBtoYV12(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x,y;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int yuv[3];
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *p,*op[3];
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LockSurface(s);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LockYUVOverlay(o);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Convert */
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(y=0; y<s->h && y<o->h; y++)
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		p=((Uint8 *) s->pixels)+s->pitch*y;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		op[0]=o->pixels[0]+o->pitches[0]*y;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		op[1]=o->pixels[1]+o->pitches[1]*(y/2);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		op[2]=o->pixels[2]+o->pitches[2]*(y/2);
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for(x=0; x<s->w && x<o->w; x++)
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			RGBtoYUV(p, yuv, monochrome, luminance);
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*(op[0]++)=yuv[0];
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(x%2==0 && y%2==0)
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(op[1]++)=yuv[2];
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(op[2]++)=yuv[1];
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			p+=s->format->BytesPerPixel;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockYUVOverlay(o);
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockSurface(s);
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid ConvertRGBtoIYUV(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x,y;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int yuv[3];
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *p,*op[3];
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LockSurface(s);
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LockYUVOverlay(o);
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Convert */
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(y=0; y<s->h && y<o->h; y++)
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		p=((Uint8 *) s->pixels)+s->pitch*y;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		op[0]=o->pixels[0]+o->pitches[0]*y;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		op[1]=o->pixels[1]+o->pitches[1]*(y/2);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		op[2]=o->pixels[2]+o->pitches[2]*(y/2);
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for(x=0; x<s->w && x<o->w; x++)
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			RGBtoYUV(p,yuv, monochrome, luminance);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*(op[0]++)=yuv[0];
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(x%2==0 && y%2==0)
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(op[1]++)=yuv[1];
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(op[2]++)=yuv[2];
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			p+=s->format->BytesPerPixel;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockYUVOverlay(o);
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockSurface(s);
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid ConvertRGBtoUYVY(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x,y;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int yuv[3];
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *p,*op;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LockSurface(s);
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LockYUVOverlay(o);
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(y=0; y<s->h && y<o->h; y++)
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		p=((Uint8 *) s->pixels)+s->pitch*y;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		op=o->pixels[0]+o->pitches[0]*y;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for(x=0; x<s->w && x<o->w; x++)
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			RGBtoYUV(p, yuv, monochrome, luminance);
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(x%2==0)
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(op++)=yuv[1];
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(op++)=yuv[0];
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(op++)=yuv[2];
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			else
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(op++)=yuv[0];
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			p+=s->format->BytesPerPixel;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockYUVOverlay(o);
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockSurface(s);
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid ConvertRGBtoYVYU(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x,y;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int yuv[3];
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *p,*op;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LockSurface(s);
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LockYUVOverlay(o);
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(y=0; y<s->h && y<o->h; y++)
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		p=((Uint8 *) s->pixels)+s->pitch*y;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		op=o->pixels[0]+o->pitches[0]*y;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for(x=0; x<s->w && x<o->w; x++)
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			RGBtoYUV(p,yuv, monochrome, luminance);
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(x%2==0)
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(op++)=yuv[0];
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(op++)=yuv[2];
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				op[1]=yuv[1];
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			else
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*op=yuv[0];
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				op+=2;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			p+=s->format->BytesPerPixel;
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockYUVOverlay(o);
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockSurface(s);
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid ConvertRGBtoYUY2(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x,y;
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int yuv[3];
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *p,*op;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LockSurface(s);
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LockYUVOverlay(o);
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for(y=0; y<s->h && y<o->h; y++)
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		p=((Uint8 *) s->pixels)+s->pitch*y;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		op=o->pixels[0]+o->pitches[0]*y;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for(x=0; x<s->w && x<o->w; x++)
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			RGBtoYUV(p,yuv, monochrome, luminance);
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if(x%2==0)
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(op++)=yuv[0];
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*(op++)=yuv[1];
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				op[1]=yuv[2];
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			else
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*op=yuv[0];
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				op+=2;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			p+=s->format->BytesPerPixel;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockYUVOverlay(o);
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockSurface(s);
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void PrintUsage(char *argv0)
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr, "\n");
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr, "Where 'arg' is any of the following options:\n");
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr, "\n");
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr, "	-fps <frames per second>\n");
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr, "	-format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr, "	-scale <scale factor> (initial scale of the overlay)\n");
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr, "	-help (shows this help)\n");
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr, "\n");
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr, "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr, "\n");
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint main(int argc, char **argv)
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint8* RawMooseData;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_RWops* handle;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Surface* screen;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Surface* MooseFrame[MOOSEFRAMES_COUNT];
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Overlay* overlay;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Rect overlayrect;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_Event event;
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint32 lastftick;
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int paused=0;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int resized=0;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int fps=12;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int fpsdelay;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int overlay_format=SDL_YUY2_OVERLAY;
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int scale=5;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 3;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while ( argc > 1 )
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (strcmp(argv[1], "-fps")== 0)
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (argv[2])
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                fps = atoi(argv[2]);
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if (fps==0)
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    quit(10);
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ((fps<0) || (fps>1000))
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n");
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    quit(10);
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                argv += 2;
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                argc -= 2;
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            else
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                quit(10);
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (strcmp(argv[1], "-format") == 0)
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (argv[2])
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if (!strcmp(argv[2],"YV12"))
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    overlay_format = SDL_YV12_OVERLAY;
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                else if(!strcmp(argv[2],"IYUV"))
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    overlay_format = SDL_IYUV_OVERLAY;
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                else if(!strcmp(argv[2],"YUY2"))
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    overlay_format = SDL_YUY2_OVERLAY;
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                else if(!strcmp(argv[2],"UYVY"))
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    overlay_format = SDL_UYVY_OVERLAY;
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                else if(!strcmp(argv[2],"YVYU"))
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    overlay_format = SDL_YVYU_OVERLAY;
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                else
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]);
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    quit(10);
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                argv += 2;
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                argc -= 2;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            else
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                fprintf(stderr, "The -format option requires an argument, default is YUY2.\n");
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                quit(10);
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (strcmp(argv[1], "-scale") == 0)
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (argv[2])
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                scale = atoi(argv[2]);
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if (scale==0)
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n");
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    quit(10);
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ((scale<0) || (scale>50))
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n");
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    quit(10);
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                argv += 2;
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                argc -= 2;
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            else
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                quit(10);
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0))
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            PrintUsage(argv[0]);
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            quit(0);
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            quit(10);
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    RawMooseData=(Uint8*)malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (RawMooseData==NULL)
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fprintf(stderr, "Can't allocate memory for movie !\n");
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        free(RawMooseData);
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        quit(1);
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* load the trojan moose images */
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    handle=SDL_RWFromFile("moose.dat", "rb");
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (handle==NULL)
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fprintf(stderr, "Can't find the file moose.dat !\n");
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        free(RawMooseData);
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        quit(2);
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_RWclose(handle);
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set video mode */
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL )
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        free(RawMooseData);
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        quit(4);
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set the window manager title bar */
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2");
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (i=0; i<MOOSEFRAMES_COUNT; i++)
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        MooseFrame[i]=SDL_CreateRGBSurfaceFrom(RawMooseData+i*MOOSEFRAME_SIZE, MOOSEPIC_W,
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                               MOOSEPIC_H, 8, MOOSEPIC_W, 0, 0, 0, 0);
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (MooseFrame[i]==NULL)
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", SDL_GetError());
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            free(RawMooseData);
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            quit(5);
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Surface *newsurf;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_PixelFormat format;
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.palette=NULL;
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.BitsPerPixel=32;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.BytesPerPixel=4;
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_BYTEORDER == SDL_LIL_ENDIAN
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Rshift=0;
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Gshift=8;
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Bshift=16;
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Rshift=24;
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Gshift=16;
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Bshift=8;
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Ashift=0;
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Rmask=0xff<<format.Rshift;
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Gmask=0xff<<format.Gshift;
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Bmask=0xff<<format.Bshift;
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Amask=0;
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Rloss=0;
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Gloss=0;
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Bloss=0;
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.Aloss=8;
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.colorkey=0;
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format.alpha=0;
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		newsurf=SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE);
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(!newsurf)
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError());
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    quit(6);
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeSurface(MooseFrame[i]);
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		MooseFrame[i]=newsurf;
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    free(RawMooseData);
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlay=SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen);
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (!overlay)
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        quit(7);
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           overlay->hw_overlay?"hardware":"software",
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           overlay->format==SDL_YV12_OVERLAY?"YV12":
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           overlay->format==SDL_IYUV_OVERLAY?"IYUV":
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           overlay->format==SDL_YUY2_OVERLAY?"YUY2":
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           overlay->format==SDL_UYVY_OVERLAY?"UYVY":
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           overlay->format==SDL_YVYU_OVERLAY?"YVYU":
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           "Unknown");
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for(i=0; i<overlay->planes; i++)
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("  plane %d: pitch=%d\n", i, overlay->pitches[i]);
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlayrect.x=0;
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlayrect.y=0;
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlayrect.w=MOOSEPIC_W*scale;
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    overlayrect.h=MOOSEPIC_H*scale;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* set the start frame */
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    i=0;
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fpsdelay=1000/fps;
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Ignore key up events, they don't even get filtered */
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_EventState(SDL_KEYUP, SDL_IGNORE);
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    lastftick=SDL_GetTicks();
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Loop, waiting for QUIT or RESIZE */
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while (1)
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (SDL_PollEvent(&event))
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            switch (event.type)
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                case SDL_VIDEORESIZE:
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     screen=SDL_SetVideoMode(event.resize.w, event.resize.h, 0, SDL_RESIZABLE | SDL_SWSURFACE);
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     overlayrect.w=event.resize.w;
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     overlayrect.h=event.resize.h;
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     if (paused)
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     {
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         resized=1;
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     }
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     break;
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                case SDL_MOUSEBUTTONDOWN:
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     overlayrect.x = event.button.x - overlayrect.w/2;
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     overlayrect.y = event.button.y - overlayrect.h/2;
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     break;
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                case SDL_KEYDOWN:
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     if (event.key.keysym.sym == SDLK_SPACE)
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     {
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         paused=!paused;
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         break;
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     }
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     if (event.key.keysym.sym != SDLK_ESCAPE)
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     {
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         break;
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     }
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                case SDL_QUIT:
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     SDL_FreeYUVOverlay(overlay);
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     for (i=0; i<MOOSEFRAMES_COUNT; i++)
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     {
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         SDL_FreeSurface(MooseFrame[i]);
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     }
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     quit(0);
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ((!paused)||(resized))
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (((SDL_GetTicks()-lastftick)>fpsdelay)||(resized))
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                lastftick=SDL_GetTicks();
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                switch (overlay_format)
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case SDL_YUY2_OVERLAY:
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         ConvertRGBtoYUY2(MooseFrame[i], overlay, 0, 100);
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         break;
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case SDL_YV12_OVERLAY:
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         ConvertRGBtoYV12(MooseFrame[i], overlay, 0, 100);
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         break;
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case SDL_UYVY_OVERLAY:
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         ConvertRGBtoUYVY(MooseFrame[i], overlay, 0, 100);
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         break;
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case SDL_YVYU_OVERLAY:
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         ConvertRGBtoYVYU(MooseFrame[i], overlay, 0, 100);
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         break;
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    case SDL_IYUV_OVERLAY:
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         ConvertRGBtoIYUV(MooseFrame[i], overlay, 0, 100);
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         break;
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_DisplayYUVOverlay(overlay, &overlayrect);
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if (!resized)
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    i++;
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    if (i==10)
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    {
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        i=0;
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    }
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                else
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    resized=0;
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* kind of timeslice to OS */
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_Delay(1);
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Quit();
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
601