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