test/testoverlay.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 03 Jun 2011 16:03:10 -0400
changeset 5547 4ccecd0901e2
parent 5535 96594ac5fd1a
permissions -rw-r--r--
Assert code's stdio interface was reading from the wrong variable.

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