test/testoverlay.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 09 May 2006 07:26:58 +0000
changeset 1790 828a17e05192
parent 1439 4d3bb026cd16
child 1662 782fd950bd46
child 1895 c121d94672cb
permissions -rw-r--r--
Date: Mon, 8 May 2006 14:19:30 -0700
From: Bob Ippolito
Subject: SDL trunk (r2346) and Mac OS X

The current state of the trunk doesn't quite compile on Mac OS X,
I've attached a series of patches that gets it to compile and kills a
few warnings.

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