test/testoverlay.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 19 Jan 2006 21:28:52 +0000
changeset 1257 448a9a64537b
parent 1151 be9c9c8f6d53
child 1439 4d3bb026cd16
permissions -rw-r--r--
[PATCH] SDL_GetVideoMode() does not find best mode, part 2

Following commit 1.51, I come accross a problem when SDL must choose between
several video modes that could suit the one asked.

If I ask 320x240 with this list:
768x480 768x240 640x400 640x200 384x480 384x240 320x400 320x200

The smallest selectables modes are 384x240 and 320x400. And SDL choose the later
in this list, but 384x240 is more suitable. So I added a check to compare
the pixel count (surface) of modes, and select the one which has the smallest
pixel count.

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