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