Date: Thu, 16 Jan 2003 13:48:31 +0200
authorSam Lantinga <slouken@libsdl.org>
Mon, 20 Jan 2003 01:36:28 +0000
changeset 569e8063c656626
parent 568 0cd6b268193b
child 570 04d6411da49d
Date: Thu, 16 Jan 2003 13:48:31 +0200
From: "Mike Gorchak"

added help and options -mono to force monochrome RGB2YUB conversion and
-lum <percent> - luminance of image during conversion. Also rewritten code
which moves overlay into window. Now it depends on window size.
Also fully rewritten -scale option, image now scaling from 50% from center
of screen until border of window is reached - it really tests scaler, old
-scale test doesn't test downscale.
test/testoverlay.c
     1.1 --- a/test/testoverlay.c	Mon Jan 20 01:35:44 2003 +0000
     1.2 +++ b/test/testoverlay.c	Mon Jan 20 01:36:28 2003 +0000
     1.3 @@ -9,41 +9,71 @@
     1.4  
     1.5  #define NOTICE(X)	printf("%s", X);
     1.6  
     1.7 +#define WINDOW_WIDTH  640
     1.8 +#define WINDOW_HEIGHT 480
     1.9 +
    1.10  #include "SDL.h"
    1.11  
    1.12  SDL_Surface *screen, *pic;
    1.13  SDL_Overlay *overlay;
    1.14  int scale;
    1.15 +int monochrome;
    1.16 +int luminance;
    1.17 +int w, h;
    1.18  
    1.19  /* NOTE: These RGB conversion functions are not intended for speed,
    1.20           only as examples.
    1.21  */
    1.22  
    1.23 -void RGBtoYUV(Uint8 *rgb, int *yuv)
    1.24 +void RGBtoYUV(Uint8 *rgb, int *yuv, int monochrome, int luminance)
    1.25  {
    1.26 -	int i;
    1.27 +    int i;
    1.28 +
    1.29 +    if (monochrome)
    1.30 +    {
    1.31  #if 1 /* these are the two formulas that I found on the FourCC site... */
    1.32 -	yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
    1.33 -	yuv[1] = (rgb[2]-yuv[0])*0.565 + 128;
    1.34 -	yuv[2] = (rgb[0]-yuv[0])*0.713 + 128;
    1.35 +        yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
    1.36 +        yuv[1] = 128;
    1.37 +        yuv[2] = 128;
    1.38  #else
    1.39 -	yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
    1.40 -	yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
    1.41 -	yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
    1.42 +        yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
    1.43 +        yuv[1] = 128;
    1.44 +        yuv[2] = 128;
    1.45  #endif
    1.46 -	/* clamp values...if you need to, we don't seem to have a need */
    1.47 -	/*
    1.48 -	for(i=0;i<3;i++)
    1.49 -	{
    1.50 -		if(yuv[i]<0)
    1.51 -			yuv[i]=0;
    1.52 -		if(yuv[i]>255)
    1.53 -			yuv[i]=255;
    1.54 -	}
    1.55 -	*/
    1.56 +    }
    1.57 +    else
    1.58 +    {
    1.59 +#if 1 /* these are the two formulas that I found on the FourCC site... */
    1.60 +        yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
    1.61 +        yuv[1] = (rgb[2]-yuv[0])*0.565 + 128;
    1.62 +        yuv[2] = (rgb[0]-yuv[0])*0.713 + 128;
    1.63 +#else
    1.64 +        yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
    1.65 +        yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
    1.66 +        yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
    1.67 +#endif
    1.68 +    }
    1.69 +
    1.70 +    if (luminance!=100)
    1.71 +    {
    1.72 +        yuv[0]=yuv[0]*luminance/100;
    1.73 +        if (yuv[0]>255)
    1.74 +            yuv[0]=255;
    1.75 +    }
    1.76 +
    1.77 +    /* clamp values...if you need to, we don't seem to have a need */
    1.78 +    /*
    1.79 +    for(i=0;i<3;i++)
    1.80 +    {
    1.81 +        if(yuv[i]<0)
    1.82 +            yuv[i]=0;
    1.83 +        if(yuv[i]>255)
    1.84 +            yuv[i]=255;
    1.85 +    }
    1.86 +    */
    1.87  }
    1.88  
    1.89 -ConvertRGBtoYV12(SDL_Surface *s, SDL_Overlay *o)
    1.90 +ConvertRGBtoYV12(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
    1.91  {
    1.92  	int x,y;
    1.93  	int yuv[3];
    1.94 @@ -68,7 +98,7 @@
    1.95  		op[2]=o->pixels[2]+o->pitches[2]*(y/2);
    1.96  		for(x=0; x<s->w && x<o->w; x++)
    1.97  		{
    1.98 -			RGBtoYUV(p,yuv);
    1.99 +			RGBtoYUV(p, yuv, monochrome, luminance);
   1.100  			*(op[0]++)=yuv[0];
   1.101  			if(x%2==0 && y%2==0)
   1.102  			{
   1.103 @@ -83,7 +113,7 @@
   1.104  	SDL_UnlockSurface(s);
   1.105  }
   1.106  
   1.107 -ConvertRGBtoIYUV(SDL_Surface *s, SDL_Overlay *o)
   1.108 +ConvertRGBtoIYUV(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
   1.109  {
   1.110  	int x,y;
   1.111  	int yuv[3];
   1.112 @@ -108,7 +138,7 @@
   1.113  		op[2]=o->pixels[2]+o->pitches[2]*(y/2);
   1.114  		for(x=0; x<s->w && x<o->w; x++)
   1.115  		{
   1.116 -			RGBtoYUV(p,yuv);
   1.117 +			RGBtoYUV(p,yuv, monochrome, luminance);
   1.118  			*(op[0]++)=yuv[0];
   1.119  			if(x%2==0 && y%2==0)
   1.120  			{
   1.121 @@ -123,7 +153,7 @@
   1.122  	SDL_UnlockSurface(s);
   1.123  }
   1.124  
   1.125 -ConvertRGBtoUYVY(SDL_Surface *s, SDL_Overlay *o)
   1.126 +ConvertRGBtoUYVY(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
   1.127  {
   1.128  	int x,y;
   1.129  	int yuv[3];
   1.130 @@ -138,7 +168,7 @@
   1.131  		op=o->pixels[0]+o->pitches[0]*y;
   1.132  		for(x=0; x<s->w && x<o->w; x++)
   1.133  		{
   1.134 -			RGBtoYUV(p,yuv);
   1.135 +			RGBtoYUV(p, yuv, monochrome, luminance);
   1.136  			if(x%2==0)
   1.137  			{
   1.138  				*(op++)=yuv[1];
   1.139 @@ -156,7 +186,7 @@
   1.140  	SDL_UnlockSurface(s);
   1.141  }
   1.142  
   1.143 -ConvertRGBtoYVYU(SDL_Surface *s, SDL_Overlay *o)
   1.144 +ConvertRGBtoYVYU(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
   1.145  {
   1.146  	int x,y;
   1.147  	int yuv[3];
   1.148 @@ -171,7 +201,7 @@
   1.149  		op=o->pixels[0]+o->pitches[0]*y;
   1.150  		for(x=0; x<s->w && x<o->w; x++)
   1.151  		{
   1.152 -			RGBtoYUV(p,yuv);
   1.153 +			RGBtoYUV(p,yuv, monochrome, luminance);
   1.154  			if(x%2==0)
   1.155  			{
   1.156  				*(op++)=yuv[0];
   1.157 @@ -192,7 +222,7 @@
   1.158  	SDL_UnlockSurface(s);
   1.159  }
   1.160  
   1.161 -ConvertRGBtoYUY2(SDL_Surface *s, SDL_Overlay *o)
   1.162 +ConvertRGBtoYUY2(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
   1.163  {
   1.164  	int x,y;
   1.165  	int yuv[3];
   1.166 @@ -207,7 +237,7 @@
   1.167  		op=o->pixels[0]+o->pitches[0]*y;
   1.168  		for(x=0; x<s->w && x<o->w; x++)
   1.169  		{
   1.170 -			RGBtoYUV(p,yuv);
   1.171 +			RGBtoYUV(p,yuv, monochrome, luminance);
   1.172  			if(x%2==0)
   1.173  			{
   1.174  				*(op++)=yuv[0];
   1.175 @@ -232,12 +262,13 @@
   1.176  {
   1.177  	SDL_Rect rect;
   1.178  	int i;
   1.179 +        int disp;
   1.180  
   1.181  	if(!scale)
   1.182  	{
   1.183  		rect.w=overlay->w;
   1.184  		rect.h=overlay->h;
   1.185 -		for(i=0; i<200; i++)
   1.186 +		for(i=0; i<h-rect.h && i<w-rect.w; i++)
   1.187  		{
   1.188  			rect.x=i;
   1.189  			rect.y=i;
   1.190 @@ -246,22 +277,45 @@
   1.191  	}
   1.192  	else
   1.193  	{
   1.194 -		rect.w=screen->w;
   1.195 -		rect.h=screen->h;
   1.196 -		for(i=0; i<200; i++)
   1.197 +		rect.w=overlay->w/2;
   1.198 +		rect.h=overlay->h/2;
   1.199 +		rect.x=(w-rect.w)/2;
   1.200 +		rect.y=(h-rect.h)/2;
   1.201 +                disp=rect.y-1;
   1.202 +		for(i=0; i<disp; i++)
   1.203  		{
   1.204 -			rect.x=i-199;
   1.205 -			rect.y=i-199;
   1.206 +                        rect.w+=2;
   1.207 +                        rect.h+=2;
   1.208 +                        rect.x--;
   1.209 +                        rect.y--;
   1.210  			SDL_DisplayYUVOverlay(overlay,&rect);
   1.211  		}
   1.212  	}
   1.213  	printf("Displayed %d times.\n",i);
   1.214  }
   1.215  
   1.216 +static void PrintUsage(char *argv0)
   1.217 +{
   1.218 +	fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
   1.219 +	fprintf(stderr, "Where 'arg' is one of:\n");
   1.220 +	fprintf(stderr, "	-delay <seconds>\n");
   1.221 +	fprintf(stderr, "	-width <pixels>\n");
   1.222 +	fprintf(stderr, "	-height <pixels>\n");
   1.223 +	fprintf(stderr, "	-bpp <bits>\n");
   1.224 +	fprintf(stderr, "	-format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
   1.225 +	fprintf(stderr, "	-hw\n");
   1.226 +	fprintf(stderr, "	-flip\n");
   1.227 +	fprintf(stderr, "	-scale (test scaling features, from 50%% upto window size)\n");
   1.228 +	fprintf(stderr, "	-mono (use monochromatic RGB2YUV conversion)\n");
   1.229 +	fprintf(stderr, "	-lum <perc> (use luminance correction during RGB2YUV conversion,\n");
   1.230 +	fprintf(stderr, "	             from 0%% to unlimited, normal is 100%%)\n");
   1.231 +	fprintf(stderr, "	-help (shows this help)\n");
   1.232 +	fprintf(stderr, "	-fullscreen (test overlay in fullscreen mode)\n");
   1.233 +}
   1.234 +
   1.235  int main(int argc, char **argv)
   1.236  {
   1.237  	int flip;
   1.238 -	int w, h;
   1.239  	int delay;
   1.240  	int desired_bpp;
   1.241  	Uint32 video_flags, overlay_format;
   1.242 @@ -274,9 +328,11 @@
   1.243  	/* Set default options and check command-line */
   1.244  	flip = 0;
   1.245  	scale=0;
   1.246 +        monochrome=0;
   1.247 +        luminance=100;
   1.248  	delay = 1;
   1.249 -	w = 640;
   1.250 -	h = 480;
   1.251 +	w = WINDOW_WIDTH;
   1.252 +	h = WINDOW_HEIGHT;
   1.253  	desired_bpp = 0;
   1.254  	video_flags = 0;
   1.255  	overlay_format = SDL_YV12_OVERLAY;
   1.256 @@ -324,6 +380,17 @@
   1.257  				exit(1);
   1.258  			}
   1.259  		} else
   1.260 +		if ( strcmp(argv[1], "-lum") == 0 ) {
   1.261 +			if ( argv[2] ) {
   1.262 +				luminance = atoi(argv[2]);
   1.263 +				argv += 2;
   1.264 +				argc -= 2;
   1.265 +			} else {
   1.266 +				fprintf(stderr,
   1.267 +				"The -lum option requires an argument\n");
   1.268 +				exit(1);
   1.269 +			}
   1.270 +		} else
   1.271  		if ( strcmp(argv[1], "-format") == 0 ) {
   1.272  			if ( argv[2] ) {
   1.273  				if(!strcmp(argv[2],"YV12"))
   1.274 @@ -364,6 +431,15 @@
   1.275  			argv += 1;
   1.276  			argc -= 1;
   1.277  		} else
   1.278 +		if ( strcmp(argv[1], "-mono") == 0 ) {
   1.279 +			monochrome = 1;
   1.280 +			argv += 1;
   1.281 +			argc -= 1;
   1.282 +		} else
   1.283 +		if (( strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) {
   1.284 +                        PrintUsage(argv[0]);
   1.285 +                        exit(1);
   1.286 +		} else
   1.287  		if ( strcmp(argv[1], "-fullscreen") == 0 ) {
   1.288  			video_flags |= SDL_FULLSCREEN;
   1.289  			argv += 1;
   1.290 @@ -473,19 +549,19 @@
   1.291  	switch(overlay->format)
   1.292  	{
   1.293  		case SDL_YV12_OVERLAY:
   1.294 -			ConvertRGBtoYV12(pic,overlay);
   1.295 +			ConvertRGBtoYV12(pic,overlay,monochrome,luminance);
   1.296  			break;
   1.297  		case SDL_UYVY_OVERLAY:
   1.298 -			ConvertRGBtoUYVY(pic,overlay);
   1.299 +			ConvertRGBtoUYVY(pic,overlay,monochrome,luminance);
   1.300  			break;
   1.301  		case SDL_YVYU_OVERLAY:
   1.302 -			ConvertRGBtoYVYU(pic,overlay);
   1.303 +			ConvertRGBtoYVYU(pic,overlay,monochrome,luminance);
   1.304  			break;
   1.305  		case SDL_YUY2_OVERLAY:
   1.306 -			ConvertRGBtoYUY2(pic,overlay);
   1.307 +			ConvertRGBtoYUY2(pic,overlay,monochrome,luminance);
   1.308  			break;
   1.309  		case SDL_IYUV_OVERLAY:
   1.310 -			ConvertRGBtoIYUV(pic,overlay);
   1.311 +			ConvertRGBtoIYUV(pic,overlay,monochrome,luminance);
   1.312  			break;
   1.313  		default:
   1.314  			printf("cannot convert RGB picture to obtained YUV format!\n");