test/testoverlay.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 06 Mar 2003 05:57:29 +0000
changeset 603 2e726be3dc08
parent 569 e8063c656626
child 605 6399f4e90211
permissions -rw-r--r--
From: Jonathan Atkins
Subject: testoverlay...

here's a fun one:
./testoverlay -scale -h

it seems to print argv[optind-1] as the program name...
     1 
     2 /* Bring up a window and play with it */
     3 
     4 #include <stdlib.h>
     5 #include <stdio.h>
     6 #include <string.h>
     7 
     8 #define BENCHMARK_SDL
     9 
    10 #define NOTICE(X)	printf("%s", X);
    11 
    12 #define WINDOW_WIDTH  640
    13 #define WINDOW_HEIGHT 480
    14 
    15 #include "SDL.h"
    16 
    17 SDL_Surface *screen, *pic;
    18 SDL_Overlay *overlay;
    19 int scale;
    20 int monochrome;
    21 int luminance;
    22 int w, h;
    23 
    24 /* NOTE: These RGB conversion functions are not intended for speed,
    25          only as examples.
    26 */
    27 
    28 void RGBtoYUV(Uint8 *rgb, int *yuv, int monochrome, int luminance)
    29 {
    30     int i;
    31 
    32     if (monochrome)
    33     {
    34 #if 1 /* these are the two formulas that I found on the FourCC site... */
    35         yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
    36         yuv[1] = 128;
    37         yuv[2] = 128;
    38 #else
    39         yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
    40         yuv[1] = 128;
    41         yuv[2] = 128;
    42 #endif
    43     }
    44     else
    45     {
    46 #if 1 /* these are the two formulas that I found on the FourCC site... */
    47         yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
    48         yuv[1] = (rgb[2]-yuv[0])*0.565 + 128;
    49         yuv[2] = (rgb[0]-yuv[0])*0.713 + 128;
    50 #else
    51         yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
    52         yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
    53         yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
    54 #endif
    55     }
    56 
    57     if (luminance!=100)
    58     {
    59         yuv[0]=yuv[0]*luminance/100;
    60         if (yuv[0]>255)
    61             yuv[0]=255;
    62     }
    63 
    64     /* clamp values...if you need to, we don't seem to have a need */
    65     /*
    66     for(i=0;i<3;i++)
    67     {
    68         if(yuv[i]<0)
    69             yuv[i]=0;
    70         if(yuv[i]>255)
    71             yuv[i]=255;
    72     }
    73     */
    74 }
    75 
    76 ConvertRGBtoYV12(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
    77 {
    78 	int x,y;
    79 	int yuv[3];
    80 	Uint8 *p,*op[3];
    81 
    82 	SDL_LockSurface(s);
    83 	SDL_LockYUVOverlay(o);
    84 
    85 	/* Black initialization */
    86 	/*
    87 	memset(o->pixels[0],0,o->pitches[0]*o->h);
    88 	memset(o->pixels[1],128,o->pitches[1]*((o->h+1)/2));
    89 	memset(o->pixels[2],128,o->pitches[2]*((o->h+1)/2));
    90 	*/
    91 
    92 	/* Convert */
    93 	for(y=0; y<s->h && y<o->h; y++)
    94 	{
    95 		p=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 	/* Black initialization */
   126 	/*
   127 	memset(o->pixels[0],0,o->pitches[0]*o->h);
   128 	memset(o->pixels[1],128,o->pitches[1]*((o->h+1)/2));
   129 	memset(o->pixels[2],128,o->pitches[2]*((o->h+1)/2));
   130 	*/
   131 
   132 	/* Convert */
   133 	for(y=0; y<s->h && y<o->h; y++)
   134 	{
   135 		p=s->pixels+s->pitch*y;
   136 		op[0]=o->pixels[0]+o->pitches[0]*y;
   137 		op[1]=o->pixels[1]+o->pitches[1]*(y/2);
   138 		op[2]=o->pixels[2]+o->pitches[2]*(y/2);
   139 		for(x=0; x<s->w && x<o->w; x++)
   140 		{
   141 			RGBtoYUV(p,yuv, monochrome, luminance);
   142 			*(op[0]++)=yuv[0];
   143 			if(x%2==0 && y%2==0)
   144 			{
   145 				*(op[1]++)=yuv[1];
   146 				*(op[2]++)=yuv[2];
   147 			}
   148 			p+=s->format->BytesPerPixel;
   149 		}
   150 	}
   151 
   152 	SDL_UnlockYUVOverlay(o);
   153 	SDL_UnlockSurface(s);
   154 }
   155 
   156 ConvertRGBtoUYVY(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
   157 {
   158 	int x,y;
   159 	int yuv[3];
   160 	Uint8 *p,*op;
   161 
   162 	SDL_LockSurface(s);
   163 	SDL_LockYUVOverlay(o);
   164 
   165 	for(y=0; y<s->h && y<o->h; y++)
   166 	{
   167 		p=s->pixels+s->pitch*y;
   168 		op=o->pixels[0]+o->pitches[0]*y;
   169 		for(x=0; x<s->w && x<o->w; x++)
   170 		{
   171 			RGBtoYUV(p, yuv, monochrome, luminance);
   172 			if(x%2==0)
   173 			{
   174 				*(op++)=yuv[1];
   175 				*(op++)=yuv[0];
   176 				*(op++)=yuv[2];
   177 			}
   178 			else
   179 				*(op++)=yuv[0];
   180 
   181 			p+=s->format->BytesPerPixel;
   182 		}
   183 	}
   184 
   185 	SDL_UnlockYUVOverlay(o);
   186 	SDL_UnlockSurface(s);
   187 }
   188 
   189 ConvertRGBtoYVYU(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
   190 {
   191 	int x,y;
   192 	int yuv[3];
   193 	Uint8 *p,*op;
   194 
   195 	SDL_LockSurface(s);
   196 	SDL_LockYUVOverlay(o);
   197 
   198 	for(y=0; y<s->h && y<o->h; y++)
   199 	{
   200 		p=s->pixels+s->pitch*y;
   201 		op=o->pixels[0]+o->pitches[0]*y;
   202 		for(x=0; x<s->w && x<o->w; x++)
   203 		{
   204 			RGBtoYUV(p,yuv, monochrome, luminance);
   205 			if(x%2==0)
   206 			{
   207 				*(op++)=yuv[0];
   208 				*(op++)=yuv[2];
   209 				op[1]=yuv[1];
   210 			}
   211 			else
   212 			{
   213 				*op=yuv[0];
   214 				op+=2;
   215 			}
   216 
   217 			p+=s->format->BytesPerPixel;
   218 		}
   219 	}
   220 
   221 	SDL_UnlockYUVOverlay(o);
   222 	SDL_UnlockSurface(s);
   223 }
   224 
   225 ConvertRGBtoYUY2(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
   226 {
   227 	int x,y;
   228 	int yuv[3];
   229 	Uint8 *p,*op;
   230 
   231 	SDL_LockSurface(s);
   232 	SDL_LockYUVOverlay(o);
   233 
   234 	for(y=0; y<s->h && y<o->h; y++)
   235 	{
   236 		p=s->pixels+s->pitch*y;
   237 		op=o->pixels[0]+o->pitches[0]*y;
   238 		for(x=0; x<s->w && x<o->w; x++)
   239 		{
   240 			RGBtoYUV(p,yuv, monochrome, luminance);
   241 			if(x%2==0)
   242 			{
   243 				*(op++)=yuv[0];
   244 				*(op++)=yuv[1];
   245 				op[1]=yuv[2];
   246 			}
   247 			else
   248 			{
   249 				*op=yuv[0];
   250 				op+=2;
   251 			}
   252 
   253 			p+=s->format->BytesPerPixel;
   254 		}
   255 	}
   256 
   257 	SDL_UnlockYUVOverlay(o);
   258 	SDL_UnlockSurface(s);
   259 }
   260 
   261 void Draw()
   262 {
   263 	SDL_Rect rect;
   264 	int i;
   265         int disp;
   266 
   267 	if(!scale)
   268 	{
   269 		rect.w=overlay->w;
   270 		rect.h=overlay->h;
   271 		for(i=0; i<h-rect.h && i<w-rect.w; i++)
   272 		{
   273 			rect.x=i;
   274 			rect.y=i;
   275 			SDL_DisplayYUVOverlay(overlay,&rect);
   276 		}
   277 	}
   278 	else
   279 	{
   280 		rect.w=overlay->w/2;
   281 		rect.h=overlay->h/2;
   282 		rect.x=(w-rect.w)/2;
   283 		rect.y=(h-rect.h)/2;
   284                 disp=rect.y-1;
   285 		for(i=0; i<disp; i++)
   286 		{
   287                         rect.w+=2;
   288                         rect.h+=2;
   289                         rect.x--;
   290                         rect.y--;
   291 			SDL_DisplayYUVOverlay(overlay,&rect);
   292 		}
   293 	}
   294 	printf("Displayed %d times.\n",i);
   295 }
   296 
   297 static void PrintUsage(char *argv0)
   298 {
   299 	fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
   300 	fprintf(stderr, "Where 'arg' is one of:\n");
   301 	fprintf(stderr, "	-delay <seconds>\n");
   302 	fprintf(stderr, "	-width <pixels>\n");
   303 	fprintf(stderr, "	-height <pixels>\n");
   304 	fprintf(stderr, "	-bpp <bits>\n");
   305 	fprintf(stderr, "	-format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
   306 	fprintf(stderr, "	-hw\n");
   307 	fprintf(stderr, "	-flip\n");
   308 	fprintf(stderr, "	-scale (test scaling features, from 50%% upto window size)\n");
   309 	fprintf(stderr, "	-mono (use monochromatic RGB2YUV conversion)\n");
   310 	fprintf(stderr, "	-lum <perc> (use luminance correction during RGB2YUV conversion,\n");
   311 	fprintf(stderr, "	             from 0%% to unlimited, normal is 100%%)\n");
   312 	fprintf(stderr, "	-help (shows this help)\n");
   313 	fprintf(stderr, "	-fullscreen (test overlay in fullscreen mode)\n");
   314 }
   315 
   316 int main(int argc, char **argv)
   317 {
   318 	char *argv0 = argv[0];
   319 	int flip;
   320 	int delay;
   321 	int desired_bpp;
   322 	Uint32 video_flags, overlay_format;
   323 	char *bmpfile;
   324 #ifdef BENCHMARK_SDL
   325 	Uint32 then, now;
   326 #endif
   327 	int i;
   328 
   329 	/* Set default options and check command-line */
   330 	flip = 0;
   331 	scale=0;
   332         monochrome=0;
   333         luminance=100;
   334 	delay = 1;
   335 	w = WINDOW_WIDTH;
   336 	h = WINDOW_HEIGHT;
   337 	desired_bpp = 0;
   338 	video_flags = 0;
   339 	overlay_format = SDL_YV12_OVERLAY;
   340 
   341 	while ( argc > 1 ) {
   342 		if ( strcmp(argv[1], "-delay") == 0 ) {
   343 			if ( argv[2] ) {
   344 				delay = atoi(argv[2]);
   345 				argv += 2;
   346 				argc -= 2;
   347 			} else {
   348 				fprintf(stderr,
   349 				"The -delay option requires an argument\n");
   350 				exit(1);
   351 			}
   352 		} else
   353 		if ( strcmp(argv[1], "-width") == 0 ) {
   354 			if ( argv[2] && ((w = atoi(argv[2])) > 0) ) {
   355 				argv += 2;
   356 				argc -= 2;
   357 			} else {
   358 				fprintf(stderr,
   359 				"The -width option requires an argument\n");
   360 				exit(1);
   361 			}
   362 		} else
   363 		if ( strcmp(argv[1], "-height") == 0 ) {
   364 			if ( argv[2] && ((h = atoi(argv[2])) > 0) ) {
   365 				argv += 2;
   366 				argc -= 2;
   367 			} else {
   368 				fprintf(stderr,
   369 				"The -height option requires an argument\n");
   370 				exit(1);
   371 			}
   372 		} else
   373 		if ( strcmp(argv[1], "-bpp") == 0 ) {
   374 			if ( argv[2] ) {
   375 				desired_bpp = atoi(argv[2]);
   376 				argv += 2;
   377 				argc -= 2;
   378 			} else {
   379 				fprintf(stderr,
   380 				"The -bpp option requires an argument\n");
   381 				exit(1);
   382 			}
   383 		} else
   384 		if ( strcmp(argv[1], "-lum") == 0 ) {
   385 			if ( argv[2] ) {
   386 				luminance = atoi(argv[2]);
   387 				argv += 2;
   388 				argc -= 2;
   389 			} else {
   390 				fprintf(stderr,
   391 				"The -lum option requires an argument\n");
   392 				exit(1);
   393 			}
   394 		} else
   395 		if ( strcmp(argv[1], "-format") == 0 ) {
   396 			if ( argv[2] ) {
   397 				if(!strcmp(argv[2],"YV12"))
   398 					overlay_format = SDL_YV12_OVERLAY;
   399 				else if(!strcmp(argv[2],"IYUV"))
   400 					overlay_format = SDL_IYUV_OVERLAY;
   401 				else if(!strcmp(argv[2],"YUY2"))
   402 					overlay_format = SDL_YUY2_OVERLAY;
   403 				else if(!strcmp(argv[2],"UYVY"))
   404 					overlay_format = SDL_UYVY_OVERLAY;
   405 				else if(!strcmp(argv[2],"YVYU"))
   406 					overlay_format = SDL_YVYU_OVERLAY;
   407 				else
   408 				{
   409 					fprintf(stderr, "The -format option %s is not recognized\n",argv[2]);
   410 					exit(1);
   411 				}
   412 				argv += 2;
   413 				argc -= 2;
   414 			} else {
   415 				fprintf(stderr,
   416 				"The -format option requires an argument\n");
   417 				exit(1);
   418 			}
   419 		} else
   420 		if ( strcmp(argv[1], "-hw") == 0 ) {
   421 			video_flags |= SDL_HWSURFACE;
   422 			argv += 1;
   423 			argc -= 1;
   424 		} else
   425 		if ( strcmp(argv[1], "-flip") == 0 ) {
   426 			video_flags |= SDL_DOUBLEBUF;
   427 			argv += 1;
   428 			argc -= 1;
   429 		} else
   430 		if ( strcmp(argv[1], "-scale") == 0 ) {
   431 			scale = 1;
   432 			argv += 1;
   433 			argc -= 1;
   434 		} else
   435 		if ( strcmp(argv[1], "-mono") == 0 ) {
   436 			monochrome = 1;
   437 			argv += 1;
   438 			argc -= 1;
   439 		} else
   440 		if (( strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) {
   441                         PrintUsage(argv0);
   442                         exit(1);
   443 		} else
   444 		if ( strcmp(argv[1], "-fullscreen") == 0 ) {
   445 			video_flags |= SDL_FULLSCREEN;
   446 			argv += 1;
   447 			argc -= 1;
   448 		} else
   449 			break;
   450 	}
   451 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
   452 		fprintf(stderr,
   453 			"Couldn't initialize SDL: %s\n", SDL_GetError());
   454 		exit(1);
   455 	}
   456 	atexit(SDL_Quit);			/* Clean up on exit */
   457 
   458 	/* Initialize the display */
   459 	screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
   460 	if ( screen == NULL ) {
   461 		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
   462 					w, h, desired_bpp, SDL_GetError());
   463 		exit(1);
   464 	}
   465 	printf("Set%s %dx%dx%d mode\n",
   466 			screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
   467 			screen->w, screen->h, screen->format->BitsPerPixel);
   468 	printf("(video surface located in %s memory)\n",
   469 			(screen->flags&SDL_HWSURFACE) ? "video" : "system");
   470 	if ( screen->flags & SDL_DOUBLEBUF ) {
   471 		printf("Double-buffering enabled\n");
   472 		flip = 1;
   473 	}
   474 
   475 	/* Set the window manager title bar */
   476 	SDL_WM_SetCaption("SDL test overlay", "testoverlay");
   477 
   478 	/* Load picture */
   479 	bmpfile=(argv[1]?argv[1]:"sample.bmp");
   480 	pic = SDL_LoadBMP(bmpfile);
   481 	if ( pic == NULL ) {
   482 		fprintf(stderr, "Couldn't load %s: %s\n", bmpfile,
   483 							SDL_GetError());
   484 		exit(1);
   485 	}
   486 
   487 	/* Convert the picture to 32bits, for easy conversion */
   488 	{
   489 		SDL_Surface *newsurf;
   490 		SDL_PixelFormat format;
   491 
   492 		format.palette=NULL;
   493 		format.BitsPerPixel=32;
   494 		format.BytesPerPixel=4;
   495 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   496 		format.Rshift=0;
   497 		format.Gshift=8;
   498 		format.Bshift=16;
   499 #else
   500 		format.Rshift=24;
   501 		format.Gshift=16;
   502 		format.Bshift=8;
   503 #endif
   504 		format.Ashift=0;
   505 		format.Rmask=0xff<<format.Rshift;
   506 		format.Gmask=0xff<<format.Gshift;
   507 		format.Bmask=0xff<<format.Bshift;
   508 		format.Amask=0;
   509 		format.Rloss=0;
   510 		format.Gloss=0;
   511 		format.Bloss=0;
   512 		format.Aloss=8;
   513 		format.colorkey=0;
   514 		format.alpha=0;
   515 
   516 		newsurf=SDL_ConvertSurface(pic, &format, SDL_SWSURFACE);
   517 		if(!newsurf)
   518 		{
   519 			fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n",
   520 							SDL_GetError());
   521 			exit(1);
   522 		}
   523 		SDL_FreeSurface(pic);
   524 		pic=newsurf;
   525 	}
   526 	
   527 	/* Create the overlay */
   528 	overlay = SDL_CreateYUVOverlay(pic->w, pic->h, overlay_format, screen);
   529 	if ( overlay == NULL ) {
   530 		fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
   531 		exit(1);
   532 	}
   533 	printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
   534 			overlay->hw_overlay?"hardware":"software",
   535 			overlay->format==SDL_YV12_OVERLAY?"YV12":
   536 			overlay->format==SDL_IYUV_OVERLAY?"IYUV":
   537 			overlay->format==SDL_YUY2_OVERLAY?"YUY2":
   538 			overlay->format==SDL_UYVY_OVERLAY?"UYVY":
   539 			overlay->format==SDL_YVYU_OVERLAY?"YVYU":
   540 			"Unknown");
   541 	for(i=0; i<overlay->planes; i++)
   542 	{
   543 		printf("  plane %d: pitch=%d\n", i, overlay->pitches[i]);
   544 	}
   545 	
   546 	/* Convert to YUV, and draw to the overlay */
   547 #ifdef BENCHMARK_SDL
   548 	then = SDL_GetTicks();
   549 #endif
   550 	switch(overlay->format)
   551 	{
   552 		case SDL_YV12_OVERLAY:
   553 			ConvertRGBtoYV12(pic,overlay,monochrome,luminance);
   554 			break;
   555 		case SDL_UYVY_OVERLAY:
   556 			ConvertRGBtoUYVY(pic,overlay,monochrome,luminance);
   557 			break;
   558 		case SDL_YVYU_OVERLAY:
   559 			ConvertRGBtoYVYU(pic,overlay,monochrome,luminance);
   560 			break;
   561 		case SDL_YUY2_OVERLAY:
   562 			ConvertRGBtoYUY2(pic,overlay,monochrome,luminance);
   563 			break;
   564 		case SDL_IYUV_OVERLAY:
   565 			ConvertRGBtoIYUV(pic,overlay,monochrome,luminance);
   566 			break;
   567 		default:
   568 			printf("cannot convert RGB picture to obtained YUV format!\n");
   569 			exit(1);
   570 			break;
   571 	}
   572 #ifdef BENCHMARK_SDL
   573 	now = SDL_GetTicks();
   574 	printf("Conversion Time: %d milliseconds\n", now-then);
   575 #endif
   576 	
   577 	/* Do all the drawing work */
   578 #ifdef BENCHMARK_SDL
   579 	then = SDL_GetTicks();
   580 #endif
   581 	Draw();
   582 #ifdef BENCHMARK_SDL
   583 	now = SDL_GetTicks();
   584 	printf("Time: %d milliseconds\n", now-then);
   585 #endif
   586 	SDL_Delay(delay*1000);
   587 	return(0);
   588 }
   589