test/testoverlay2.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 06 May 2004 15:55:06 +0000
changeset 886 05c551e5bc64
parent 753 b14fdadd8311
child 1030 6b8f0543337c
permissions -rw-r--r--
Date: Sat, 24 Apr 2004 15:13:32 +0300
From: "Mike Gorchak"
Subject: SDL updates for the QNX6

1. Updated the README.QNX
2. Updated libtool scripts, which are shipped with SDL for QNX6 support.
3. Added some code to support the new QNX 6.3.0, which is in beta now.
4. Added code to detect the hw features, which driver supports.
5. Added hw alpha blits code.
6. Fixed bug when application switches to fullscreen more the 2 times. (afte\
r that window becames always stay on top).
7. Updated a bit README for the tests.
8. Added information about acceleration show in the testalpha.c test.
9. Added small fixes to the testoverlay2.c test.
10. Added alpha and cc+alpha blits benchmarks to the testvidinfo.c test.
     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, "\n");
   258     fprintf(stderr, "Where 'arg' is any of the following options:\n");
   259     fprintf(stderr, "\n");
   260     fprintf(stderr, "	-fps <frames per second>\n");
   261     fprintf(stderr, "	-format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
   262     fprintf(stderr, "	-scale <scale factor> (initial scale of the overlay)\n");
   263     fprintf(stderr, "	-help (shows this help)\n");
   264     fprintf(stderr, "\n");
   265     fprintf(stderr, "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
   266     fprintf(stderr, "\n");
   267 }
   268 
   269 int main(int argc, char **argv)
   270 {
   271     Uint8* RawMooseData;
   272     SDL_RWops* handle;
   273     SDL_Surface* screen;
   274     SDL_Surface* MooseFrame[MOOSEFRAMES_COUNT];
   275     SDL_Overlay* overlay;
   276     SDL_Rect overlayrect;
   277     SDL_Event event;
   278     Uint32 lastftick;
   279     int paused=0;
   280     int resized=0;
   281     int i;
   282     int fps=12;
   283     int fpsdelay;
   284     int overlay_format=SDL_YUY2_OVERLAY;
   285     int scale=5;
   286 
   287     while ( argc > 1 )
   288     {
   289         if (strcmp(argv[1], "-fps")== 0)
   290         {
   291             if (argv[2])
   292             {
   293                 fps = atoi(argv[2]);
   294                 if (fps==0)
   295                 {
   296                     fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   297                     return -1;
   298                 }
   299                 if ((fps<0) || (fps>1000))
   300                 {
   301                     fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n");
   302                     return -1;
   303                 }
   304                 argv += 2;
   305                 argc -= 2;
   306             }
   307             else
   308             {
   309                 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   310                 return -1;
   311             }
   312         } else
   313         if (strcmp(argv[1], "-format") == 0)
   314         {
   315             if (argv[2])
   316             {
   317                 if (!strcmp(argv[2],"YV12"))
   318                     overlay_format = SDL_YV12_OVERLAY;
   319                 else if(!strcmp(argv[2],"IYUV"))
   320                     overlay_format = SDL_IYUV_OVERLAY;
   321                 else if(!strcmp(argv[2],"YUY2"))
   322                     overlay_format = SDL_YUY2_OVERLAY;
   323                 else if(!strcmp(argv[2],"UYVY"))
   324                     overlay_format = SDL_UYVY_OVERLAY;
   325                 else if(!strcmp(argv[2],"YVYU"))
   326                     overlay_format = SDL_YVYU_OVERLAY;
   327                 else
   328                 {
   329                     fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]);
   330                     return -1;
   331                 }
   332                 argv += 2;
   333                 argc -= 2;
   334             }
   335             else
   336             {
   337                 fprintf(stderr, "The -format option requires an argument, default is YUY2.\n");
   338                 return -1;
   339             }
   340         } else
   341         if (strcmp(argv[1], "-scale") == 0)
   342         {
   343             if (argv[2])
   344             {
   345                 scale = atoi(argv[2]);
   346                 if (scale==0)
   347                 {
   348                     fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n");
   349                     return -1;
   350                 }
   351                 if ((scale<0) || (scale>50))
   352                 {
   353                     fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n");
   354                     return -1;
   355                 }
   356                 argv += 2;
   357                 argc -= 2;
   358             }
   359             else
   360             {
   361                 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   362                 return -1;
   363             }
   364         } else
   365         if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0))
   366         {
   367             PrintUsage(argv[0]);
   368             return 0;
   369         } else
   370         {
   371             fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
   372             return -1;
   373         }
   374         break;
   375     }
   376    
   377     RawMooseData=(Uint8*)malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
   378     if (RawMooseData==NULL)
   379     {
   380         fprintf(stderr, "Can't allocate memory for movie !\n");
   381         free(RawMooseData);
   382         return 1;
   383     }
   384 
   385     /* load the trojan moose images */
   386     handle=SDL_RWFromFile("moose.dat", "rb");
   387     if (handle==NULL)
   388     {
   389         fprintf(stderr, "Can't find the file moose.dat !\n");
   390         free(RawMooseData);
   391         return 2;
   392     }
   393    
   394     SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
   395 
   396     SDL_RWclose(handle);
   397 
   398     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
   399     {
   400         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   401         free(RawMooseData);
   402         return 3;
   403     }
   404     atexit(SDL_Quit);
   405 
   406     /* Set video mode */
   407     if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL )
   408     {
   409         fprintf(stderr, "Couldn't set video mode: %s\n", 0, SDL_GetError());
   410         free(RawMooseData);
   411         return 4;
   412     }
   413 
   414     /* Set the window manager title bar */
   415     SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2");
   416 
   417     for (i=0; i<MOOSEFRAMES_COUNT; i++)
   418     {
   419         MooseFrame[i]=SDL_CreateRGBSurfaceFrom(RawMooseData+i*MOOSEFRAME_SIZE, MOOSEPIC_W,
   420                                                MOOSEPIC_H, 8, MOOSEPIC_W, 0, 0, 0, 0);
   421         if (MooseFrame[i]==NULL)
   422         {
   423             fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", 0, SDL_GetError());
   424             free(RawMooseData);
   425             return 5;
   426         }
   427         SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);
   428 
   429 	{
   430 		SDL_Surface *newsurf;
   431 		SDL_PixelFormat format;
   432 
   433 		format.palette=NULL;
   434 		format.BitsPerPixel=32;
   435 		format.BytesPerPixel=4;
   436 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   437 		format.Rshift=0;
   438 		format.Gshift=8;
   439 		format.Bshift=16;
   440 #else
   441 		format.Rshift=24;
   442 		format.Gshift=16;
   443 		format.Bshift=8;
   444 #endif
   445 		format.Ashift=0;
   446 		format.Rmask=0xff<<format.Rshift;
   447 		format.Gmask=0xff<<format.Gshift;
   448 		format.Bmask=0xff<<format.Bshift;
   449 		format.Amask=0;
   450 		format.Rloss=0;
   451 		format.Gloss=0;
   452 		format.Bloss=0;
   453 		format.Aloss=8;
   454 		format.colorkey=0;
   455 		format.alpha=0;
   456 
   457 		newsurf=SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE);
   458 		if(!newsurf)
   459 		{
   460                     fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError());
   461                     return 6;
   462 		}
   463 		SDL_FreeSurface(MooseFrame[i]);
   464 		MooseFrame[i]=newsurf;
   465 	}
   466     }
   467 
   468     free(RawMooseData);
   469 
   470     overlay=SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen);
   471     if (!overlay)
   472     {
   473         fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
   474         return 7;
   475     }
   476 
   477     printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
   478            overlay->hw_overlay?"hardware":"software",
   479            overlay->format==SDL_YV12_OVERLAY?"YV12":
   480            overlay->format==SDL_IYUV_OVERLAY?"IYUV":
   481            overlay->format==SDL_YUY2_OVERLAY?"YUY2":
   482            overlay->format==SDL_UYVY_OVERLAY?"UYVY":
   483            overlay->format==SDL_YVYU_OVERLAY?"YVYU":
   484            "Unknown");
   485 
   486     for(i=0; i<overlay->planes; i++)
   487     {
   488         printf("  plane %d: pitch=%d\n", i, overlay->pitches[i]);
   489     }
   490 
   491     overlayrect.x=0;
   492     overlayrect.y=0;
   493     overlayrect.w=MOOSEPIC_W*scale;
   494     overlayrect.h=MOOSEPIC_H*scale;
   495 
   496     /* set the start frame */
   497     i=0;
   498     fpsdelay=1000/fps;
   499 
   500     /* Ignore key up events, they don't even get filtered */
   501     SDL_EventState(SDL_KEYUP, SDL_IGNORE);
   502 
   503     lastftick=SDL_GetTicks();
   504 
   505     /* Loop, waiting for QUIT or RESIZE */
   506     while (1)
   507     {
   508         if (SDL_PollEvent(&event))
   509         {
   510             switch (event.type)
   511             {
   512                 case SDL_VIDEORESIZE:
   513                      screen=SDL_SetVideoMode(event.resize.w, event.resize.h, 0, SDL_RESIZABLE | SDL_SWSURFACE);
   514                      overlayrect.w=event.resize.w;
   515                      overlayrect.h=event.resize.h;
   516                      if (paused)
   517                      {
   518                          resized=1;
   519                      }
   520                      break;
   521                 case SDL_KEYDOWN:
   522                      if (event.key.keysym.sym == SDLK_SPACE)
   523                      {
   524                          paused=!paused;
   525                          break;
   526                      }
   527                      if (event.key.keysym.sym != SDLK_ESCAPE)
   528                      {
   529                          break;
   530                      }
   531                 case SDL_QUIT:
   532                      SDL_FreeYUVOverlay(overlay);
   533                      for (i=0; i<MOOSEFRAMES_COUNT; i++)
   534                      {
   535                          SDL_FreeSurface(MooseFrame[i]);
   536                      }
   537                      return 0;
   538             }
   539         }
   540 
   541         if ((!paused)||(resized))
   542         {
   543             if (((SDL_GetTicks()-lastftick)>fpsdelay)||(resized))
   544             {
   545                 lastftick=SDL_GetTicks();
   546 
   547                 switch (overlay_format)
   548                 {
   549                     case SDL_YUY2_OVERLAY:
   550                          ConvertRGBtoYUY2(MooseFrame[i], overlay, 0, 100);
   551                          break;
   552                     case SDL_YV12_OVERLAY:
   553                          ConvertRGBtoYV12(MooseFrame[i], overlay, 0, 100);
   554                          break;
   555                     case SDL_UYVY_OVERLAY:
   556                          ConvertRGBtoUYVY(MooseFrame[i], overlay, 0, 100);
   557                          break;
   558                     case SDL_YVYU_OVERLAY:
   559                          ConvertRGBtoYVYU(MooseFrame[i], overlay, 0, 100);
   560                          break;
   561                     case SDL_IYUV_OVERLAY:
   562                          ConvertRGBtoIYUV(MooseFrame[i], overlay, 0, 100);
   563                          break;
   564                 }
   565 
   566                 SDL_DisplayYUVOverlay(overlay, &overlayrect);
   567                 if (!resized)
   568                 {
   569                     i++;
   570                     if (i==10)
   571                     {
   572                         i=0;
   573                     }
   574                 }
   575                 else
   576                 {
   577                     resized=0;
   578                 }
   579             }
   580         }
   581         /* kind of timeslice to OS */
   582         SDL_Delay(1);
   583     }
   584 
   585     return 0;
   586 }
   587