test/testoverlay2.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5357 ea8fec220775
child 6256 1d905b13b102
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 /********************************************************************************
    13  *                                                                              *
    14  * Test of the overlay used for moved pictures, test more closed to real life.  *
    15  * Running trojan moose :) Coded by Mike Gorchak.                               *
    16  *                                                                              *
    17  ********************************************************************************/
    18 
    19 #include <stdlib.h>
    20 #include <stdio.h>
    21 #include <string.h>
    22 
    23 #include "SDL.h"
    24 
    25 #define MOOSEPIC_W 64
    26 #define MOOSEPIC_H 88
    27 
    28 #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
    29 #define MOOSEFRAMES_COUNT 10
    30 
    31 SDL_Color MooseColors[84] = {
    32     {49, 49, 49}
    33     , {66, 24, 0}
    34     , {66, 33, 0}
    35     , {66, 66, 66}
    36     ,
    37     {66, 115, 49}
    38     , {74, 33, 0}
    39     , {74, 41, 16}
    40     , {82, 33, 8}
    41     ,
    42     {82, 41, 8}
    43     , {82, 49, 16}
    44     , {82, 82, 82}
    45     , {90, 41, 8}
    46     ,
    47     {90, 41, 16}
    48     , {90, 57, 24}
    49     , {99, 49, 16}
    50     , {99, 66, 24}
    51     ,
    52     {99, 66, 33}
    53     , {99, 74, 33}
    54     , {107, 57, 24}
    55     , {107, 82, 41}
    56     ,
    57     {115, 57, 33}
    58     , {115, 66, 33}
    59     , {115, 66, 41}
    60     , {115, 74, 0}
    61     ,
    62     {115, 90, 49}
    63     , {115, 115, 115}
    64     , {123, 82, 0}
    65     , {123, 99, 57}
    66     ,
    67     {132, 66, 41}
    68     , {132, 74, 41}
    69     , {132, 90, 8}
    70     , {132, 99, 33}
    71     ,
    72     {132, 99, 66}
    73     , {132, 107, 66}
    74     , {140, 74, 49}
    75     , {140, 99, 16}
    76     ,
    77     {140, 107, 74}
    78     , {140, 115, 74}
    79     , {148, 107, 24}
    80     , {148, 115, 82}
    81     ,
    82     {148, 123, 74}
    83     , {148, 123, 90}
    84     , {156, 115, 33}
    85     , {156, 115, 90}
    86     ,
    87     {156, 123, 82}
    88     , {156, 132, 82}
    89     , {156, 132, 99}
    90     , {156, 156, 156}
    91     ,
    92     {165, 123, 49}
    93     , {165, 123, 90}
    94     , {165, 132, 82}
    95     , {165, 132, 90}
    96     ,
    97     {165, 132, 99}
    98     , {165, 140, 90}
    99     , {173, 132, 57}
   100     , {173, 132, 99}
   101     ,
   102     {173, 140, 107}
   103     , {173, 140, 115}
   104     , {173, 148, 99}
   105     , {173, 173, 173}
   106     ,
   107     {181, 140, 74}
   108     , {181, 148, 115}
   109     , {181, 148, 123}
   110     , {181, 156, 107}
   111     ,
   112     {189, 148, 123}
   113     , {189, 156, 82}
   114     , {189, 156, 123}
   115     , {189, 156, 132}
   116     ,
   117     {189, 189, 189}
   118     , {198, 156, 123}
   119     , {198, 165, 132}
   120     , {206, 165, 99}
   121     ,
   122     {206, 165, 132}
   123     , {206, 173, 140}
   124     , {206, 206, 206}
   125     , {214, 173, 115}
   126     ,
   127     {214, 173, 140}
   128     , {222, 181, 148}
   129     , {222, 189, 132}
   130     , {222, 189, 156}
   131     ,
   132     {222, 222, 222}
   133     , {231, 198, 165}
   134     , {231, 231, 231}
   135     , {239, 206, 173}
   136 };
   137 
   138 
   139 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   140 static void
   141 quit(int rc)
   142 {
   143     SDL_Quit();
   144     exit(rc);
   145 }
   146 
   147 /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
   148 
   149 /* NOTE: These RGB conversion functions are not intended for speed,
   150          only as examples.
   151 */
   152 
   153 void
   154 RGBtoYUV(Uint8 * rgb, int *yuv, int monochrome, int luminance)
   155 {
   156     if (monochrome) {
   157 #if 1                           /* these are the two formulas that I found on the FourCC site... */
   158         yuv[0] = (int)(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]);
   159         yuv[1] = 128;
   160         yuv[2] = 128;
   161 #else
   162         yuv[0] = (int)(0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
   163         yuv[1] = 128;
   164         yuv[2] = 128;
   165 #endif
   166     } else {
   167 #if 1                           /* these are the two formulas that I found on the FourCC site... */
   168         yuv[0] = (int)(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]);
   169         yuv[1] = (int)((rgb[2] - yuv[0]) * 0.565 + 128);
   170         yuv[2] = (int)((rgb[0] - yuv[0]) * 0.713 + 128);
   171 #else
   172         yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
   173         yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
   174         yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
   175 #endif
   176     }
   177 
   178     if (luminance != 100) {
   179         yuv[0] = yuv[0] * luminance / 100;
   180         if (yuv[0] > 255)
   181             yuv[0] = 255;
   182     }
   183 }
   184 
   185 void
   186 ConvertRGBtoYV12(Uint8 *rgb, Uint8 *out, int w, int h,
   187                  int monochrome, int luminance)
   188 {
   189     int x, y;
   190     int yuv[3];
   191     Uint8 *op[3];
   192 
   193     op[0] = out;
   194     op[1] = op[0] + w*h;
   195     op[2] = op[1] + w*h/4;
   196     for (y = 0; y < h; ++y) {
   197         for (x = 0; x < w; ++x) {
   198             RGBtoYUV(rgb, yuv, monochrome, luminance);
   199             *(op[0]++) = yuv[0];
   200             if (x % 2 == 0 && y % 2 == 0) {
   201                 *(op[1]++) = yuv[2];
   202                 *(op[2]++) = yuv[1];
   203             }
   204             rgb += 3;
   205         }
   206     }
   207 }
   208 
   209 void
   210 ConvertRGBtoIYUV(SDL_Surface * s, SDL_Overlay * o, int monochrome,
   211                  int luminance)
   212 {
   213     int x, y;
   214     int yuv[3];
   215     Uint8 *p, *op[3];
   216 
   217     SDL_LockSurface(s);
   218     SDL_LockYUVOverlay(o);
   219 
   220     /* Convert */
   221     for (y = 0; y < s->h && y < o->h; y++) {
   222         p = ((Uint8 *) s->pixels) + s->pitch * y;
   223         op[0] = o->pixels[0] + o->pitches[0] * y;
   224         op[1] = o->pixels[1] + o->pitches[1] * (y / 2);
   225         op[2] = o->pixels[2] + o->pitches[2] * (y / 2);
   226         for (x = 0; x < s->w && x < o->w; x++) {
   227             RGBtoYUV(p, yuv, monochrome, luminance);
   228             *(op[0]++) = yuv[0];
   229             if (x % 2 == 0 && y % 2 == 0) {
   230                 *(op[1]++) = yuv[1];
   231                 *(op[2]++) = yuv[2];
   232             }
   233             p += s->format->BytesPerPixel;
   234         }
   235     }
   236 
   237     SDL_UnlockYUVOverlay(o);
   238     SDL_UnlockSurface(s);
   239 }
   240 
   241 void
   242 ConvertRGBtoUYVY(SDL_Surface * s, SDL_Overlay * o, int monochrome,
   243                  int luminance)
   244 {
   245     int x, y;
   246     int yuv[3];
   247     Uint8 *p, *op;
   248 
   249     SDL_LockSurface(s);
   250     SDL_LockYUVOverlay(o);
   251 
   252     for (y = 0; y < s->h && y < o->h; y++) {
   253         p = ((Uint8 *) s->pixels) + s->pitch * y;
   254         op = o->pixels[0] + o->pitches[0] * y;
   255         for (x = 0; x < s->w && x < o->w; x++) {
   256             RGBtoYUV(p, yuv, monochrome, luminance);
   257             if (x % 2 == 0) {
   258                 *(op++) = yuv[1];
   259                 *(op++) = yuv[0];
   260                 *(op++) = yuv[2];
   261             } else
   262                 *(op++) = yuv[0];
   263 
   264             p += s->format->BytesPerPixel;
   265         }
   266     }
   267 
   268     SDL_UnlockYUVOverlay(o);
   269     SDL_UnlockSurface(s);
   270 }
   271 
   272 void
   273 ConvertRGBtoYVYU(SDL_Surface * s, SDL_Overlay * o, int monochrome,
   274                  int luminance)
   275 {
   276     int x, y;
   277     int yuv[3];
   278     Uint8 *p, *op;
   279 
   280     SDL_LockSurface(s);
   281     SDL_LockYUVOverlay(o);
   282 
   283     for (y = 0; y < s->h && y < o->h; y++) {
   284         p = ((Uint8 *) s->pixels) + s->pitch * y;
   285         op = o->pixels[0] + o->pitches[0] * y;
   286         for (x = 0; x < s->w && x < o->w; x++) {
   287             RGBtoYUV(p, yuv, monochrome, luminance);
   288             if (x % 2 == 0) {
   289                 *(op++) = yuv[0];
   290                 *(op++) = yuv[2];
   291                 op[1] = yuv[1];
   292             } else {
   293                 *op = yuv[0];
   294                 op += 2;
   295             }
   296 
   297             p += s->format->BytesPerPixel;
   298         }
   299     }
   300 
   301     SDL_UnlockYUVOverlay(o);
   302     SDL_UnlockSurface(s);
   303 }
   304 
   305 void
   306 ConvertRGBtoYUY2(SDL_Surface * s, SDL_Overlay * o, int monochrome,
   307                  int luminance)
   308 {
   309     int x, y;
   310     int yuv[3];
   311     Uint8 *p, *op;
   312 
   313     SDL_LockSurface(s);
   314     SDL_LockYUVOverlay(o);
   315 
   316     for (y = 0; y < s->h && y < o->h; y++) {
   317         p = ((Uint8 *) s->pixels) + s->pitch * y;
   318         op = o->pixels[0] + o->pitches[0] * y;
   319         for (x = 0; x < s->w && x < o->w; x++) {
   320             RGBtoYUV(p, yuv, monochrome, luminance);
   321             if (x % 2 == 0) {
   322                 *(op++) = yuv[0];
   323                 *(op++) = yuv[1];
   324                 op[1] = yuv[2];
   325             } else {
   326                 *op = yuv[0];
   327                 op += 2;
   328             }
   329 
   330             p += s->format->BytesPerPixel;
   331         }
   332     }
   333 
   334     SDL_UnlockYUVOverlay(o);
   335     SDL_UnlockSurface(s);
   336 }
   337 
   338 static void
   339 PrintUsage(char *argv0)
   340 {
   341     fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
   342     fprintf(stderr, "\n");
   343     fprintf(stderr, "Where 'arg' is any of the following options:\n");
   344     fprintf(stderr, "\n");
   345     fprintf(stderr, "    -fps <frames per second>\n");
   346     fprintf(stderr, "    -nodelay\n");
   347     fprintf(stderr, "    -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
   348     fprintf(stderr, "    -scale <scale factor> (initial scale of the overlay)\n");
   349     fprintf(stderr, "    -help (shows this help)\n");
   350     fprintf(stderr, "\n");
   351     fprintf(stderr,
   352             "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
   353     fprintf(stderr, "\n");
   354 }
   355 
   356 int
   357 main(int argc, char **argv)
   358 {
   359     Uint8 *RawMooseData;
   360     SDL_RWops *handle;
   361     int window_w;
   362     int window_h;
   363     SDL_Window *window;
   364     SDL_Renderer *renderer;
   365     Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2];
   366     SDL_Texture *MooseTexture;
   367     SDL_Rect displayrect;
   368     SDL_Event event;
   369     int paused = 0;
   370     int i, j;
   371     int fps = 12;
   372     int fpsdelay;
   373     int nodelay = 0;
   374     Uint32 pixel_format = SDL_PIXELFORMAT_YV12;
   375     int scale = 5;
   376     SDL_bool done = SDL_FALSE;
   377 
   378     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
   379         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   380         return 3;
   381     }
   382 
   383     while (argc > 1) {
   384         if (strcmp(argv[1], "-fps") == 0) {
   385             if (argv[2]) {
   386                 fps = atoi(argv[2]);
   387                 if (fps == 0) {
   388                     fprintf(stderr,
   389                             "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   390                     quit(10);
   391                 }
   392                 if ((fps < 0) || (fps > 1000)) {
   393                     fprintf(stderr,
   394                             "The -fps option must be in range from 1 to 1000, default is 12.\n");
   395                     quit(10);
   396                 }
   397                 argv += 2;
   398                 argc -= 2;
   399             } else {
   400                 fprintf(stderr,
   401                         "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   402                 quit(10);
   403             }
   404         } else if (strcmp(argv[1], "-nodelay") == 0) {
   405             nodelay = 1;
   406             argv += 1;
   407             argc -= 1;
   408         } else if (strcmp(argv[1], "-format") == 0) {
   409             if (argv[2]) {
   410                 if (!strcmp(argv[2], "YV12"))
   411                     pixel_format = SDL_PIXELFORMAT_YV12;
   412                 else if (!strcmp(argv[2], "IYUV"))
   413                     pixel_format = SDL_PIXELFORMAT_IYUV;
   414                 else if (!strcmp(argv[2], "YUY2"))
   415                     pixel_format = SDL_PIXELFORMAT_YUY2;
   416                 else if (!strcmp(argv[2], "UYVY"))
   417                     pixel_format = SDL_PIXELFORMAT_UYVY;
   418                 else if (!strcmp(argv[2], "YVYU"))
   419                     pixel_format = SDL_PIXELFORMAT_YVYU;
   420                 else {
   421                     fprintf(stderr,
   422                             "The -format option %s is not recognized, see help for info.\n",
   423                             argv[2]);
   424                     quit(10);
   425                 }
   426                 argv += 2;
   427                 argc -= 2;
   428             } else {
   429                 fprintf(stderr,
   430                         "The -format option requires an argument, default is YUY2.\n");
   431                 quit(10);
   432             }
   433         } else if (strcmp(argv[1], "-scale") == 0) {
   434             if (argv[2]) {
   435                 scale = atoi(argv[2]);
   436                 if (scale == 0) {
   437                     fprintf(stderr,
   438                             "The -scale option requires an argument [from 1 to 50], default is 5.\n");
   439                     quit(10);
   440                 }
   441                 if ((scale < 0) || (scale > 50)) {
   442                     fprintf(stderr,
   443                             "The -scale option must be in range from 1 to 50, default is 5.\n");
   444                     quit(10);
   445                 }
   446                 argv += 2;
   447                 argc -= 2;
   448             } else {
   449                 fprintf(stderr,
   450                         "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   451                 quit(10);
   452             }
   453         } else if ((strcmp(argv[1], "-help") == 0)
   454                    || (strcmp(argv[1], "-h") == 0)) {
   455             PrintUsage(argv[0]);
   456             quit(0);
   457         } else {
   458             fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
   459             quit(10);
   460         }
   461         break;
   462     }
   463 
   464     RawMooseData = (Uint8 *) malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
   465     if (RawMooseData == NULL) {
   466         fprintf(stderr, "Can't allocate memory for movie !\n");
   467         free(RawMooseData);
   468         quit(1);
   469     }
   470 
   471     /* load the trojan moose images */
   472     handle = SDL_RWFromFile("moose.dat", "rb");
   473     if (handle == NULL) {
   474         fprintf(stderr, "Can't find the file moose.dat !\n");
   475         free(RawMooseData);
   476         quit(2);
   477     }
   478 
   479     SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
   480 
   481     SDL_RWclose(handle);
   482 
   483     /* Create the window and renderer */
   484     window_w = MOOSEPIC_W * scale;
   485     window_h = MOOSEPIC_H * scale;
   486     window = SDL_CreateWindow("Happy Moose",
   487                               SDL_WINDOWPOS_UNDEFINED,
   488                               SDL_WINDOWPOS_UNDEFINED,
   489                               window_w, window_h,
   490                               SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
   491     if (!window) {
   492         fprintf(stderr, "Couldn't set create window: %s\n", SDL_GetError());
   493         free(RawMooseData);
   494         quit(4);
   495     }
   496 
   497     renderer = SDL_CreateRenderer(window, -1, 0);
   498     if (!renderer) {
   499         fprintf(stderr, "Couldn't set create renderer: %s\n", SDL_GetError());
   500         free(RawMooseData);
   501         quit(4);
   502     }
   503 
   504     MooseTexture = SDL_CreateTexture(renderer, pixel_format, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
   505     if (!MooseTexture) {
   506         fprintf(stderr, "Couldn't set create texture: %s\n", SDL_GetError());
   507         free(RawMooseData);
   508         quit(5);
   509     }
   510 
   511     for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
   512         Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
   513         Uint8 *rgb;
   514         Uint8 *frame;
   515 
   516         rgb = MooseFrameRGB;
   517         frame = RawMooseData + i * MOOSEFRAME_SIZE;
   518         for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
   519             rgb[0] = MooseColors[frame[j]].r;
   520             rgb[1] = MooseColors[frame[j]].g;
   521             rgb[2] = MooseColors[frame[j]].b;
   522             rgb += 3;
   523         }
   524         ConvertRGBtoYV12(MooseFrameRGB, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H, 0, 100);
   525     }
   526 
   527     free(RawMooseData);
   528 
   529     /* set the start frame */
   530     i = 0;
   531     if (nodelay) {
   532         fpsdelay = 0;
   533     } else {
   534         fpsdelay = 1000 / fps;
   535     }
   536 
   537     displayrect.x = 0;
   538     displayrect.y = 0;
   539     displayrect.w = window_w;
   540     displayrect.h = window_h;
   541 
   542     /* Ignore key up events, they don't even get filtered */
   543     SDL_EventState(SDL_KEYUP, SDL_IGNORE);
   544 
   545     /* Loop, waiting for QUIT or RESIZE */
   546     while (!done) {
   547         while (SDL_PollEvent(&event)) {
   548             switch (event.type) {
   549             case SDL_WINDOWEVENT:
   550                 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
   551                     SDL_RenderSetViewport(renderer, NULL);
   552                     displayrect.w = window_w = event.window.data1;
   553                     displayrect.h = window_h = event.window.data2;
   554                 }
   555                 break;
   556             case SDL_MOUSEBUTTONDOWN:
   557                 displayrect.x = event.button.x - window_w / 2;
   558                 displayrect.y = event.button.y - window_h / 2;
   559                 break;
   560             case SDL_MOUSEMOTION:
   561                 if (event.motion.state) {
   562                     displayrect.x = event.motion.x - window_w / 2;
   563                     displayrect.y = event.motion.y - window_h / 2;
   564                 }
   565                 break;
   566             case SDL_KEYDOWN:
   567                 if (event.key.keysym.sym == SDLK_SPACE) {
   568                     paused = !paused;
   569                     break;
   570                 }
   571                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   572                     break;
   573                 }
   574             case SDL_QUIT:
   575                 done = SDL_TRUE;
   576                 break;
   577             }
   578         }
   579         SDL_Delay(fpsdelay);
   580 
   581         if (!paused) {
   582             i = (i + 1) % MOOSEFRAMES_COUNT;
   583 
   584             SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W*SDL_BYTESPERPIXEL(pixel_format));
   585         }
   586         SDL_RenderClear(renderer);
   587         SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
   588         SDL_RenderPresent(renderer);
   589     }
   590     SDL_DestroyRenderer(renderer);
   591     quit(0);
   592     return 0;
   593 }
   594 
   595 /* vi: set ts=4 sw=4 expandtab: */