test/testoverlay.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1439 4d3bb026cd16
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

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