test/testoverlay2.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 18 Jan 2005 18:32:10 +0000
changeset 1030 6b8f0543337c
parent 886 05c551e5bc64
child 1151 be9c9c8f6d53
permissions -rw-r--r--
Date: Sat, 15 Jan 2005 15:06:01 -0600
From: Tyler Montbriand
Subject: [SDL] minor bugfix for test/testoverlay2.c

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