test/testoverlay2.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 29 Sep 2005 09:43:00 +0000
changeset 1152 51a8702d8ecd
parent 1151 be9c9c8f6d53
child 1439 4d3bb026cd16
permissions -rw-r--r--
Updates to PocketPC (WinCE) support, thanks to Dmitry Yakimov at
activekitten.com.
     1 /********************************************************************************
     2  *                                                                              *
     3  * Test of the overlay used for moved pictures, test more closed to real life.  *
     4  * Running trojan moose :) Coded by Mike Gorchak.                               *
     5  *                                                                              *
     6  ********************************************************************************/
     7 
     8 #include <stdlib.h>
     9 #include <stdio.h>
    10 #include <string.h>
    11 
    12 #include "SDL.h"
    13 
    14 #define MOOSEPIC_W 64
    15 #define MOOSEPIC_H 88
    16 
    17 #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
    18 #define MOOSEFRAMES_COUNT 10
    19 
    20 SDL_Color MooseColors[84]={
    21     { 49,  49,  49}, { 66,  24,   0}, { 66,  33,   0}, { 66,  66,  66},
    22     { 66, 115,  49}, { 74,  33,   0}, { 74,  41,  16}, { 82,  33,   8},
    23     { 82,  41,   8}, { 82,  49,  16}, { 82,  82,  82}, { 90,  41,   8},
    24     { 90,  41,  16}, { 90,  57,  24}, { 99,  49,  16}, { 99,  66,  24},
    25     { 99,  66,  33}, { 99,  74,  33}, {107,  57,  24}, {107,  82,  41},
    26     {115,  57,  33}, {115,  66,  33}, {115,  66,  41}, {115,  74,   0},
    27     {115,  90,  49}, {115, 115, 115}, {123,  82,   0}, {123,  99,  57},
    28     {132,  66,  41}, {132,  74,  41}, {132,  90,   8}, {132,  99,  33},
    29     {132,  99,  66}, {132, 107,  66}, {140,  74,  49}, {140,  99,  16},
    30     {140, 107,  74}, {140, 115,  74}, {148, 107,  24}, {148, 115,  82},
    31     {148, 123,  74}, {148, 123,  90}, {156, 115,  33}, {156, 115,  90},
    32     {156, 123,  82}, {156, 132,  82}, {156, 132,  99}, {156, 156, 156},
    33     {165, 123,  49}, {165, 123,  90}, {165, 132,  82}, {165, 132,  90},
    34     {165, 132,  99}, {165, 140,  90}, {173, 132,  57}, {173, 132,  99},
    35     {173, 140, 107}, {173, 140, 115}, {173, 148,  99}, {173, 173, 173},
    36     {181, 140,  74}, {181, 148, 115}, {181, 148, 123}, {181, 156, 107},
    37     {189, 148, 123}, {189, 156,  82}, {189, 156, 123}, {189, 156, 132},
    38     {189, 189, 189}, {198, 156, 123}, {198, 165, 132}, {206, 165,  99},
    39     {206, 165, 132}, {206, 173, 140}, {206, 206, 206}, {214, 173, 115},
    40     {214, 173, 140}, {222, 181, 148}, {222, 189, 132}, {222, 189, 156},
    41     {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173}
    42 };
    43 
    44 
    45 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    46 static void quit(int rc)
    47 {
    48 	SDL_Quit();
    49 	exit(rc);
    50 }
    51 
    52 /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
    53 
    54 /* NOTE: These RGB conversion functions are not intended for speed,
    55          only as examples.
    56 */
    57 
    58 void RGBtoYUV(Uint8 *rgb, int *yuv, int monochrome, int luminance)
    59 {
    60     int i;
    61 
    62     if (monochrome)
    63     {
    64 #if 1 /* these are the two formulas that I found on the FourCC site... */
    65         yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
    66         yuv[1] = 128;
    67         yuv[2] = 128;
    68 #else
    69         yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
    70         yuv[1] = 128;
    71         yuv[2] = 128;
    72 #endif
    73     }
    74     else
    75     {
    76 #if 1 /* these are the two formulas that I found on the FourCC site... */
    77         yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
    78         yuv[1] = (rgb[2]-yuv[0])*0.565 + 128;
    79         yuv[2] = (rgb[0]-yuv[0])*0.713 + 128;
    80 #else
    81         yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
    82         yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
    83         yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
    84 #endif
    85     }
    86 
    87     if (luminance!=100)
    88     {
    89         yuv[0]=yuv[0]*luminance/100;
    90         if (yuv[0]>255)
    91             yuv[0]=255;
    92     }
    93 }
    94 
    95 ConvertRGBtoYV12(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
    96 {
    97 	int x,y;
    98 	int yuv[3];
    99 	Uint8 *p,*op[3];
   100 
   101 	SDL_LockSurface(s);
   102 	SDL_LockYUVOverlay(o);
   103 
   104 	/* Convert */
   105 	for(y=0; y<s->h && y<o->h; y++)
   106 	{
   107 		p=((Uint8 *) s->pixels)+s->pitch*y;
   108 		op[0]=o->pixels[0]+o->pitches[0]*y;
   109 		op[1]=o->pixels[1]+o->pitches[1]*(y/2);
   110 		op[2]=o->pixels[2]+o->pitches[2]*(y/2);
   111 		for(x=0; x<s->w && x<o->w; x++)
   112 		{
   113 			RGBtoYUV(p, yuv, monochrome, luminance);
   114 			*(op[0]++)=yuv[0];
   115 			if(x%2==0 && y%2==0)
   116 			{
   117 				*(op[1]++)=yuv[2];
   118 				*(op[2]++)=yuv[1];
   119 			}
   120 			p+=s->format->BytesPerPixel;
   121 		}
   122 	}
   123 
   124 	SDL_UnlockYUVOverlay(o);
   125 	SDL_UnlockSurface(s);
   126 }
   127 
   128 ConvertRGBtoIYUV(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
   129 {
   130 	int x,y;
   131 	int yuv[3];
   132 	Uint8 *p,*op[3];
   133 
   134 	SDL_LockSurface(s);
   135 	SDL_LockYUVOverlay(o);
   136 
   137 	/* Convert */
   138 	for(y=0; y<s->h && y<o->h; y++)
   139 	{
   140 		p=((Uint8 *) s->pixels)+s->pitch*y;
   141 		op[0]=o->pixels[0]+o->pitches[0]*y;
   142 		op[1]=o->pixels[1]+o->pitches[1]*(y/2);
   143 		op[2]=o->pixels[2]+o->pitches[2]*(y/2);
   144 		for(x=0; x<s->w && x<o->w; x++)
   145 		{
   146 			RGBtoYUV(p,yuv, monochrome, luminance);
   147 			*(op[0]++)=yuv[0];
   148 			if(x%2==0 && y%2==0)
   149 			{
   150 				*(op[1]++)=yuv[1];
   151 				*(op[2]++)=yuv[2];
   152 			}
   153 			p+=s->format->BytesPerPixel;
   154 		}
   155 	}
   156 
   157 	SDL_UnlockYUVOverlay(o);
   158 	SDL_UnlockSurface(s);
   159 }
   160 
   161 ConvertRGBtoUYVY(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
   162 {
   163 	int x,y;
   164 	int yuv[3];
   165 	Uint8 *p,*op;
   166 
   167 	SDL_LockSurface(s);
   168 	SDL_LockYUVOverlay(o);
   169 
   170 	for(y=0; y<s->h && y<o->h; y++)
   171 	{
   172 		p=((Uint8 *) s->pixels)+s->pitch*y;
   173 		op=o->pixels[0]+o->pitches[0]*y;
   174 		for(x=0; x<s->w && x<o->w; x++)
   175 		{
   176 			RGBtoYUV(p, yuv, monochrome, luminance);
   177 			if(x%2==0)
   178 			{
   179 				*(op++)=yuv[1];
   180 				*(op++)=yuv[0];
   181 				*(op++)=yuv[2];
   182 			}
   183 			else
   184 				*(op++)=yuv[0];
   185 
   186 			p+=s->format->BytesPerPixel;
   187 		}
   188 	}
   189 
   190 	SDL_UnlockYUVOverlay(o);
   191 	SDL_UnlockSurface(s);
   192 }
   193 
   194 ConvertRGBtoYVYU(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
   195 {
   196 	int x,y;
   197 	int yuv[3];
   198 	Uint8 *p,*op;
   199 
   200 	SDL_LockSurface(s);
   201 	SDL_LockYUVOverlay(o);
   202 
   203 	for(y=0; y<s->h && y<o->h; y++)
   204 	{
   205 		p=((Uint8 *) s->pixels)+s->pitch*y;
   206 		op=o->pixels[0]+o->pitches[0]*y;
   207 		for(x=0; x<s->w && x<o->w; x++)
   208 		{
   209 			RGBtoYUV(p,yuv, monochrome, luminance);
   210 			if(x%2==0)
   211 			{
   212 				*(op++)=yuv[0];
   213 				*(op++)=yuv[2];
   214 				op[1]=yuv[1];
   215 			}
   216 			else
   217 			{
   218 				*op=yuv[0];
   219 				op+=2;
   220 			}
   221 
   222 			p+=s->format->BytesPerPixel;
   223 		}
   224 	}
   225 
   226 	SDL_UnlockYUVOverlay(o);
   227 	SDL_UnlockSurface(s);
   228 }
   229 
   230 ConvertRGBtoYUY2(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
   231 {
   232 	int x,y;
   233 	int yuv[3];
   234 	Uint8 *p,*op;
   235 
   236 	SDL_LockSurface(s);
   237 	SDL_LockYUVOverlay(o);
   238         
   239 	for(y=0; y<s->h && y<o->h; y++)
   240 	{
   241 		p=((Uint8 *) s->pixels)+s->pitch*y;
   242 		op=o->pixels[0]+o->pitches[0]*y;
   243 		for(x=0; x<s->w && x<o->w; x++)
   244 		{
   245 			RGBtoYUV(p,yuv, monochrome, luminance);
   246 			if(x%2==0)
   247 			{
   248 				*(op++)=yuv[0];
   249 				*(op++)=yuv[1];
   250 				op[1]=yuv[2];
   251 			}
   252 			else
   253 			{
   254 				*op=yuv[0];
   255 				op+=2;
   256 			}
   257 
   258 			p+=s->format->BytesPerPixel;
   259 		}
   260 	}
   261 
   262 	SDL_UnlockYUVOverlay(o);
   263 	SDL_UnlockSurface(s);
   264 }
   265 
   266 static void PrintUsage(char *argv0)
   267 {
   268     fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
   269     fprintf(stderr, "\n");
   270     fprintf(stderr, "Where 'arg' is any of the following options:\n");
   271     fprintf(stderr, "\n");
   272     fprintf(stderr, "	-fps <frames per second>\n");
   273     fprintf(stderr, "	-format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
   274     fprintf(stderr, "	-scale <scale factor> (initial scale of the overlay)\n");
   275     fprintf(stderr, "	-help (shows this help)\n");
   276     fprintf(stderr, "\n");
   277     fprintf(stderr, "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
   278     fprintf(stderr, "\n");
   279 }
   280 
   281 int main(int argc, char **argv)
   282 {
   283     Uint8* RawMooseData;
   284     SDL_RWops* handle;
   285     SDL_Surface* screen;
   286     SDL_Surface* MooseFrame[MOOSEFRAMES_COUNT];
   287     SDL_Overlay* overlay;
   288     SDL_Rect overlayrect;
   289     SDL_Event event;
   290     Uint32 lastftick;
   291     int paused=0;
   292     int resized=0;
   293     int i;
   294     int fps=12;
   295     int fpsdelay;
   296     int overlay_format=SDL_YUY2_OVERLAY;
   297     int scale=5;
   298 
   299     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
   300     {
   301         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   302         return 3;
   303     }
   304 
   305     while ( argc > 1 )
   306     {
   307         if (strcmp(argv[1], "-fps")== 0)
   308         {
   309             if (argv[2])
   310             {
   311                 fps = atoi(argv[2]);
   312                 if (fps==0)
   313                 {
   314                     fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   315                     quit(10);
   316                 }
   317                 if ((fps<0) || (fps>1000))
   318                 {
   319                     fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n");
   320                     quit(10);
   321                 }
   322                 argv += 2;
   323                 argc -= 2;
   324             }
   325             else
   326             {
   327                 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   328                 quit(10);
   329             }
   330         } else
   331         if (strcmp(argv[1], "-format") == 0)
   332         {
   333             if (argv[2])
   334             {
   335                 if (!strcmp(argv[2],"YV12"))
   336                     overlay_format = SDL_YV12_OVERLAY;
   337                 else if(!strcmp(argv[2],"IYUV"))
   338                     overlay_format = SDL_IYUV_OVERLAY;
   339                 else if(!strcmp(argv[2],"YUY2"))
   340                     overlay_format = SDL_YUY2_OVERLAY;
   341                 else if(!strcmp(argv[2],"UYVY"))
   342                     overlay_format = SDL_UYVY_OVERLAY;
   343                 else if(!strcmp(argv[2],"YVYU"))
   344                     overlay_format = SDL_YVYU_OVERLAY;
   345                 else
   346                 {
   347                     fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]);
   348                     quit(10);
   349                 }
   350                 argv += 2;
   351                 argc -= 2;
   352             }
   353             else
   354             {
   355                 fprintf(stderr, "The -format option requires an argument, default is YUY2.\n");
   356                 quit(10);
   357             }
   358         } else
   359         if (strcmp(argv[1], "-scale") == 0)
   360         {
   361             if (argv[2])
   362             {
   363                 scale = atoi(argv[2]);
   364                 if (scale==0)
   365                 {
   366                     fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n");
   367                     quit(10);
   368                 }
   369                 if ((scale<0) || (scale>50))
   370                 {
   371                     fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n");
   372                     quit(10);
   373                 }
   374                 argv += 2;
   375                 argc -= 2;
   376             }
   377             else
   378             {
   379                 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   380                 quit(10);
   381             }
   382         } else
   383         if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0))
   384         {
   385             PrintUsage(argv[0]);
   386             quit(0);
   387         } else
   388         {
   389             fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
   390             quit(10);
   391         }
   392         break;
   393     }
   394    
   395     RawMooseData=(Uint8*)malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
   396     if (RawMooseData==NULL)
   397     {
   398         fprintf(stderr, "Can't allocate memory for movie !\n");
   399         free(RawMooseData);
   400         quit(1);
   401     }
   402 
   403     /* load the trojan moose images */
   404     handle=SDL_RWFromFile("moose.dat", "rb");
   405     if (handle==NULL)
   406     {
   407         fprintf(stderr, "Can't find the file moose.dat !\n");
   408         free(RawMooseData);
   409         quit(2);
   410     }
   411    
   412     SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
   413 
   414     SDL_RWclose(handle);
   415 
   416     /* Set video mode */
   417     if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL )
   418     {
   419         fprintf(stderr, "Couldn't set video mode: %s\n", 0, SDL_GetError());
   420         free(RawMooseData);
   421         quit(4);
   422     }
   423 
   424     /* Set the window manager title bar */
   425     SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2");
   426 
   427     for (i=0; i<MOOSEFRAMES_COUNT; i++)
   428     {
   429         MooseFrame[i]=SDL_CreateRGBSurfaceFrom(RawMooseData+i*MOOSEFRAME_SIZE, MOOSEPIC_W,
   430                                                MOOSEPIC_H, 8, MOOSEPIC_W, 0, 0, 0, 0);
   431         if (MooseFrame[i]==NULL)
   432         {
   433             fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", 0, SDL_GetError());
   434             free(RawMooseData);
   435             quit(5);
   436         }
   437         SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);
   438 
   439 	{
   440 		SDL_Surface *newsurf;
   441 		SDL_PixelFormat format;
   442 
   443 		format.palette=NULL;
   444 		format.BitsPerPixel=32;
   445 		format.BytesPerPixel=4;
   446 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   447 		format.Rshift=0;
   448 		format.Gshift=8;
   449 		format.Bshift=16;
   450 #else
   451 		format.Rshift=24;
   452 		format.Gshift=16;
   453 		format.Bshift=8;
   454 #endif
   455 		format.Ashift=0;
   456 		format.Rmask=0xff<<format.Rshift;
   457 		format.Gmask=0xff<<format.Gshift;
   458 		format.Bmask=0xff<<format.Bshift;
   459 		format.Amask=0;
   460 		format.Rloss=0;
   461 		format.Gloss=0;
   462 		format.Bloss=0;
   463 		format.Aloss=8;
   464 		format.colorkey=0;
   465 		format.alpha=0;
   466 
   467 		newsurf=SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE);
   468 		if(!newsurf)
   469 		{
   470                     fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError());
   471                     quit(6);
   472 		}
   473 		SDL_FreeSurface(MooseFrame[i]);
   474 		MooseFrame[i]=newsurf;
   475 	}
   476     }
   477 
   478     free(RawMooseData);
   479 
   480     overlay=SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen);
   481     if (!overlay)
   482     {
   483         fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
   484         quit(7);
   485     }
   486 
   487     printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
   488            overlay->hw_overlay?"hardware":"software",
   489            overlay->format==SDL_YV12_OVERLAY?"YV12":
   490            overlay->format==SDL_IYUV_OVERLAY?"IYUV":
   491            overlay->format==SDL_YUY2_OVERLAY?"YUY2":
   492            overlay->format==SDL_UYVY_OVERLAY?"UYVY":
   493            overlay->format==SDL_YVYU_OVERLAY?"YVYU":
   494            "Unknown");
   495 
   496     for(i=0; i<overlay->planes; i++)
   497     {
   498         printf("  plane %d: pitch=%d\n", i, overlay->pitches[i]);
   499     }
   500 
   501     overlayrect.x=0;
   502     overlayrect.y=0;
   503     overlayrect.w=MOOSEPIC_W*scale;
   504     overlayrect.h=MOOSEPIC_H*scale;
   505 
   506     /* set the start frame */
   507     i=0;
   508     fpsdelay=1000/fps;
   509 
   510     /* Ignore key up events, they don't even get filtered */
   511     SDL_EventState(SDL_KEYUP, SDL_IGNORE);
   512 
   513     lastftick=SDL_GetTicks();
   514 
   515     /* Loop, waiting for QUIT or RESIZE */
   516     while (1)
   517     {
   518         if (SDL_PollEvent(&event))
   519         {
   520             switch (event.type)
   521             {
   522                 case SDL_VIDEORESIZE:
   523                      screen=SDL_SetVideoMode(event.resize.w, event.resize.h, 0, SDL_RESIZABLE | SDL_SWSURFACE);
   524                      overlayrect.w=event.resize.w;
   525                      overlayrect.h=event.resize.h;
   526                      if (paused)
   527                      {
   528                          resized=1;
   529                      }
   530                      break;
   531                 case SDL_KEYDOWN:
   532                      if (event.key.keysym.sym == SDLK_SPACE)
   533                      {
   534                          paused=!paused;
   535                          break;
   536                      }
   537                      if (event.key.keysym.sym != SDLK_ESCAPE)
   538                      {
   539                          break;
   540                      }
   541                 case SDL_QUIT:
   542                      SDL_FreeYUVOverlay(overlay);
   543                      for (i=0; i<MOOSEFRAMES_COUNT; i++)
   544                      {
   545                          SDL_FreeSurface(MooseFrame[i]);
   546                      }
   547                      quit(0);
   548             }
   549         }
   550 
   551         if ((!paused)||(resized))
   552         {
   553             if (((SDL_GetTicks()-lastftick)>fpsdelay)||(resized))
   554             {
   555                 lastftick=SDL_GetTicks();
   556 
   557                 switch (overlay_format)
   558                 {
   559                     case SDL_YUY2_OVERLAY:
   560                          ConvertRGBtoYUY2(MooseFrame[i], overlay, 0, 100);
   561                          break;
   562                     case SDL_YV12_OVERLAY:
   563                          ConvertRGBtoYV12(MooseFrame[i], overlay, 0, 100);
   564                          break;
   565                     case SDL_UYVY_OVERLAY:
   566                          ConvertRGBtoUYVY(MooseFrame[i], overlay, 0, 100);
   567                          break;
   568                     case SDL_YVYU_OVERLAY:
   569                          ConvertRGBtoYVYU(MooseFrame[i], overlay, 0, 100);
   570                          break;
   571                     case SDL_IYUV_OVERLAY:
   572                          ConvertRGBtoIYUV(MooseFrame[i], overlay, 0, 100);
   573                          break;
   574                 }
   575 
   576                 SDL_DisplayYUVOverlay(overlay, &overlayrect);
   577                 if (!resized)
   578                 {
   579                     i++;
   580                     if (i==10)
   581                     {
   582                         i=0;
   583                     }
   584                 }
   585                 else
   586                 {
   587                     resized=0;
   588                 }
   589             }
   590         }
   591         /* kind of timeslice to OS */
   592         SDL_Delay(1);
   593     }
   594 
   595 	SDL_Quit();
   596     return 0;
   597 }
   598