test/testoverlay2.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
   125 };
   125 };
   126 
   126 
   127 
   127 
   128 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   128 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   129 static void
   129 static void
   130 quit (int rc)
   130 quit(int rc)
   131 {
   131 {
   132     SDL_Quit ();
   132     SDL_Quit();
   133     exit (rc);
   133     exit(rc);
   134 }
   134 }
   135 
   135 
   136 /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
   136 /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
   137 
   137 
   138 /* NOTE: These RGB conversion functions are not intended for speed,
   138 /* NOTE: These RGB conversion functions are not intended for speed,
   139          only as examples.
   139          only as examples.
   140 */
   140 */
   141 
   141 
   142 void
   142 void
   143 RGBtoYUV (Uint8 * rgb, int *yuv, int monochrome, int luminance)
   143 RGBtoYUV(Uint8 * rgb, int *yuv, int monochrome, int luminance)
   144 {
   144 {
   145     if (monochrome) {
   145     if (monochrome) {
   146 #if 1                           /* these are the two formulas that I found on the FourCC site... */
   146 #if 1                           /* these are the two formulas that I found on the FourCC site... */
   147         yuv[0] = 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2];
   147         yuv[0] = 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2];
   148         yuv[1] = 128;
   148         yuv[1] = 128;
   170             yuv[0] = 255;
   170             yuv[0] = 255;
   171     }
   171     }
   172 }
   172 }
   173 
   173 
   174 void
   174 void
   175 ConvertRGBtoYV12 (SDL_Surface * s, SDL_Overlay * o, int monochrome,
   175 ConvertRGBtoYV12(SDL_Surface * s, SDL_Overlay * o, int monochrome,
   176                   int luminance)
   176                  int luminance)
   177 {
   177 {
   178     int x, y;
   178     int x, y;
   179     int yuv[3];
   179     int yuv[3];
   180     Uint8 *p, *op[3];
   180     Uint8 *p, *op[3];
   181 
   181 
   182     SDL_LockSurface (s);
   182     SDL_LockSurface(s);
   183     SDL_LockYUVOverlay (o);
   183     SDL_LockYUVOverlay(o);
   184 
   184 
   185     /* Convert */
   185     /* Convert */
   186     for (y = 0; y < s->h && y < o->h; y++) {
   186     for (y = 0; y < s->h && y < o->h; y++) {
   187         p = ((Uint8 *) s->pixels) + s->pitch * y;
   187         p = ((Uint8 *) s->pixels) + s->pitch * y;
   188         op[0] = o->pixels[0] + o->pitches[0] * y;
   188         op[0] = o->pixels[0] + o->pitches[0] * y;
   189         op[1] = o->pixels[1] + o->pitches[1] * (y / 2);
   189         op[1] = o->pixels[1] + o->pitches[1] * (y / 2);
   190         op[2] = o->pixels[2] + o->pitches[2] * (y / 2);
   190         op[2] = o->pixels[2] + o->pitches[2] * (y / 2);
   191         for (x = 0; x < s->w && x < o->w; x++) {
   191         for (x = 0; x < s->w && x < o->w; x++) {
   192             RGBtoYUV (p, yuv, monochrome, luminance);
   192             RGBtoYUV(p, yuv, monochrome, luminance);
   193             *(op[0]++) = yuv[0];
   193             *(op[0]++) = yuv[0];
   194             if (x % 2 == 0 && y % 2 == 0) {
   194             if (x % 2 == 0 && y % 2 == 0) {
   195                 *(op[1]++) = yuv[2];
   195                 *(op[1]++) = yuv[2];
   196                 *(op[2]++) = yuv[1];
   196                 *(op[2]++) = yuv[1];
   197             }
   197             }
   198             p += s->format->BytesPerPixel;
   198             p += s->format->BytesPerPixel;
   199         }
   199         }
   200     }
   200     }
   201 
   201 
   202     SDL_UnlockYUVOverlay (o);
   202     SDL_UnlockYUVOverlay(o);
   203     SDL_UnlockSurface (s);
   203     SDL_UnlockSurface(s);
   204 }
   204 }
   205 
   205 
   206 void
   206 void
   207 ConvertRGBtoIYUV (SDL_Surface * s, SDL_Overlay * o, int monochrome,
   207 ConvertRGBtoIYUV(SDL_Surface * s, SDL_Overlay * o, int monochrome,
   208                   int luminance)
   208                  int luminance)
   209 {
   209 {
   210     int x, y;
   210     int x, y;
   211     int yuv[3];
   211     int yuv[3];
   212     Uint8 *p, *op[3];
   212     Uint8 *p, *op[3];
   213 
   213 
   214     SDL_LockSurface (s);
   214     SDL_LockSurface(s);
   215     SDL_LockYUVOverlay (o);
   215     SDL_LockYUVOverlay(o);
   216 
   216 
   217     /* Convert */
   217     /* Convert */
   218     for (y = 0; y < s->h && y < o->h; y++) {
   218     for (y = 0; y < s->h && y < o->h; y++) {
   219         p = ((Uint8 *) s->pixels) + s->pitch * y;
   219         p = ((Uint8 *) s->pixels) + s->pitch * y;
   220         op[0] = o->pixels[0] + o->pitches[0] * y;
   220         op[0] = o->pixels[0] + o->pitches[0] * y;
   221         op[1] = o->pixels[1] + o->pitches[1] * (y / 2);
   221         op[1] = o->pixels[1] + o->pitches[1] * (y / 2);
   222         op[2] = o->pixels[2] + o->pitches[2] * (y / 2);
   222         op[2] = o->pixels[2] + o->pitches[2] * (y / 2);
   223         for (x = 0; x < s->w && x < o->w; x++) {
   223         for (x = 0; x < s->w && x < o->w; x++) {
   224             RGBtoYUV (p, yuv, monochrome, luminance);
   224             RGBtoYUV(p, yuv, monochrome, luminance);
   225             *(op[0]++) = yuv[0];
   225             *(op[0]++) = yuv[0];
   226             if (x % 2 == 0 && y % 2 == 0) {
   226             if (x % 2 == 0 && y % 2 == 0) {
   227                 *(op[1]++) = yuv[1];
   227                 *(op[1]++) = yuv[1];
   228                 *(op[2]++) = yuv[2];
   228                 *(op[2]++) = yuv[2];
   229             }
   229             }
   230             p += s->format->BytesPerPixel;
   230             p += s->format->BytesPerPixel;
   231         }
   231         }
   232     }
   232     }
   233 
   233 
   234     SDL_UnlockYUVOverlay (o);
   234     SDL_UnlockYUVOverlay(o);
   235     SDL_UnlockSurface (s);
   235     SDL_UnlockSurface(s);
   236 }
   236 }
   237 
   237 
   238 void
   238 void
   239 ConvertRGBtoUYVY (SDL_Surface * s, SDL_Overlay * o, int monochrome,
   239 ConvertRGBtoUYVY(SDL_Surface * s, SDL_Overlay * o, int monochrome,
   240                   int luminance)
   240                  int luminance)
   241 {
   241 {
   242     int x, y;
   242     int x, y;
   243     int yuv[3];
   243     int yuv[3];
   244     Uint8 *p, *op;
   244     Uint8 *p, *op;
   245 
   245 
   246     SDL_LockSurface (s);
   246     SDL_LockSurface(s);
   247     SDL_LockYUVOverlay (o);
   247     SDL_LockYUVOverlay(o);
   248 
   248 
   249     for (y = 0; y < s->h && y < o->h; y++) {
   249     for (y = 0; y < s->h && y < o->h; y++) {
   250         p = ((Uint8 *) s->pixels) + s->pitch * y;
   250         p = ((Uint8 *) s->pixels) + s->pitch * y;
   251         op = o->pixels[0] + o->pitches[0] * y;
   251         op = o->pixels[0] + o->pitches[0] * y;
   252         for (x = 0; x < s->w && x < o->w; x++) {
   252         for (x = 0; x < s->w && x < o->w; x++) {
   253             RGBtoYUV (p, yuv, monochrome, luminance);
   253             RGBtoYUV(p, yuv, monochrome, luminance);
   254             if (x % 2 == 0) {
   254             if (x % 2 == 0) {
   255                 *(op++) = yuv[1];
   255                 *(op++) = yuv[1];
   256                 *(op++) = yuv[0];
   256                 *(op++) = yuv[0];
   257                 *(op++) = yuv[2];
   257                 *(op++) = yuv[2];
   258             } else
   258             } else
   260 
   260 
   261             p += s->format->BytesPerPixel;
   261             p += s->format->BytesPerPixel;
   262         }
   262         }
   263     }
   263     }
   264 
   264 
   265     SDL_UnlockYUVOverlay (o);
   265     SDL_UnlockYUVOverlay(o);
   266     SDL_UnlockSurface (s);
   266     SDL_UnlockSurface(s);
   267 }
   267 }
   268 
   268 
   269 void
   269 void
   270 ConvertRGBtoYVYU (SDL_Surface * s, SDL_Overlay * o, int monochrome,
   270 ConvertRGBtoYVYU(SDL_Surface * s, SDL_Overlay * o, int monochrome,
   271                   int luminance)
   271                  int luminance)
   272 {
   272 {
   273     int x, y;
   273     int x, y;
   274     int yuv[3];
   274     int yuv[3];
   275     Uint8 *p, *op;
   275     Uint8 *p, *op;
   276 
   276 
   277     SDL_LockSurface (s);
   277     SDL_LockSurface(s);
   278     SDL_LockYUVOverlay (o);
   278     SDL_LockYUVOverlay(o);
   279 
   279 
   280     for (y = 0; y < s->h && y < o->h; y++) {
   280     for (y = 0; y < s->h && y < o->h; y++) {
   281         p = ((Uint8 *) s->pixels) + s->pitch * y;
   281         p = ((Uint8 *) s->pixels) + s->pitch * y;
   282         op = o->pixels[0] + o->pitches[0] * y;
   282         op = o->pixels[0] + o->pitches[0] * y;
   283         for (x = 0; x < s->w && x < o->w; x++) {
   283         for (x = 0; x < s->w && x < o->w; x++) {
   284             RGBtoYUV (p, yuv, monochrome, luminance);
   284             RGBtoYUV(p, yuv, monochrome, luminance);
   285             if (x % 2 == 0) {
   285             if (x % 2 == 0) {
   286                 *(op++) = yuv[0];
   286                 *(op++) = yuv[0];
   287                 *(op++) = yuv[2];
   287                 *(op++) = yuv[2];
   288                 op[1] = yuv[1];
   288                 op[1] = yuv[1];
   289             } else {
   289             } else {
   293 
   293 
   294             p += s->format->BytesPerPixel;
   294             p += s->format->BytesPerPixel;
   295         }
   295         }
   296     }
   296     }
   297 
   297 
   298     SDL_UnlockYUVOverlay (o);
   298     SDL_UnlockYUVOverlay(o);
   299     SDL_UnlockSurface (s);
   299     SDL_UnlockSurface(s);
   300 }
   300 }
   301 
   301 
   302 void
   302 void
   303 ConvertRGBtoYUY2 (SDL_Surface * s, SDL_Overlay * o, int monochrome,
   303 ConvertRGBtoYUY2(SDL_Surface * s, SDL_Overlay * o, int monochrome,
   304                   int luminance)
   304                  int luminance)
   305 {
   305 {
   306     int x, y;
   306     int x, y;
   307     int yuv[3];
   307     int yuv[3];
   308     Uint8 *p, *op;
   308     Uint8 *p, *op;
   309 
   309 
   310     SDL_LockSurface (s);
   310     SDL_LockSurface(s);
   311     SDL_LockYUVOverlay (o);
   311     SDL_LockYUVOverlay(o);
   312 
   312 
   313     for (y = 0; y < s->h && y < o->h; y++) {
   313     for (y = 0; y < s->h && y < o->h; y++) {
   314         p = ((Uint8 *) s->pixels) + s->pitch * y;
   314         p = ((Uint8 *) s->pixels) + s->pitch * y;
   315         op = o->pixels[0] + o->pitches[0] * y;
   315         op = o->pixels[0] + o->pitches[0] * y;
   316         for (x = 0; x < s->w && x < o->w; x++) {
   316         for (x = 0; x < s->w && x < o->w; x++) {
   317             RGBtoYUV (p, yuv, monochrome, luminance);
   317             RGBtoYUV(p, yuv, monochrome, luminance);
   318             if (x % 2 == 0) {
   318             if (x % 2 == 0) {
   319                 *(op++) = yuv[0];
   319                 *(op++) = yuv[0];
   320                 *(op++) = yuv[1];
   320                 *(op++) = yuv[1];
   321                 op[1] = yuv[2];
   321                 op[1] = yuv[2];
   322             } else {
   322             } else {
   326 
   326 
   327             p += s->format->BytesPerPixel;
   327             p += s->format->BytesPerPixel;
   328         }
   328         }
   329     }
   329     }
   330 
   330 
   331     SDL_UnlockYUVOverlay (o);
   331     SDL_UnlockYUVOverlay(o);
   332     SDL_UnlockSurface (s);
   332     SDL_UnlockSurface(s);
   333 }
   333 }
   334 
   334 
   335 static void
   335 static void
   336 PrintUsage (char *argv0)
   336 PrintUsage(char *argv0)
   337 {
   337 {
   338     fprintf (stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
   338     fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
   339     fprintf (stderr, "\n");
   339     fprintf(stderr, "\n");
   340     fprintf (stderr, "Where 'arg' is any of the following options:\n");
   340     fprintf(stderr, "Where 'arg' is any of the following options:\n");
   341     fprintf (stderr, "\n");
   341     fprintf(stderr, "\n");
   342     fprintf (stderr, "	-fps <frames per second>\n");
   342     fprintf(stderr, "	-fps <frames per second>\n");
   343     fprintf (stderr,
   343     fprintf(stderr,
   344              "	-format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
   344             "	-format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
   345     fprintf (stderr,
   345     fprintf(stderr,
   346              "	-scale <scale factor> (initial scale of the overlay)\n");
   346             "	-scale <scale factor> (initial scale of the overlay)\n");
   347     fprintf (stderr, "	-help (shows this help)\n");
   347     fprintf(stderr, "	-help (shows this help)\n");
   348     fprintf (stderr, "\n");
   348     fprintf(stderr, "\n");
   349     fprintf (stderr,
   349     fprintf(stderr,
   350              "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
   350             "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
   351     fprintf (stderr, "\n");
   351     fprintf(stderr, "\n");
   352 }
   352 }
   353 
   353 
   354 int
   354 int
   355 main (int argc, char **argv)
   355 main(int argc, char **argv)
   356 {
   356 {
   357     Uint8 *RawMooseData;
   357     Uint8 *RawMooseData;
   358     SDL_RWops *handle;
   358     SDL_RWops *handle;
   359     SDL_Surface *screen;
   359     SDL_Surface *screen;
   360     SDL_Surface *MooseFrame[MOOSEFRAMES_COUNT];
   360     SDL_Surface *MooseFrame[MOOSEFRAMES_COUNT];
   368     int fps = 12;
   368     int fps = 12;
   369     int fpsdelay;
   369     int fpsdelay;
   370     int overlay_format = SDL_YUY2_OVERLAY;
   370     int overlay_format = SDL_YUY2_OVERLAY;
   371     int scale = 5;
   371     int scale = 5;
   372 
   372 
   373     if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
   373     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
   374         fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
   374         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   375         return 3;
   375         return 3;
   376     }
   376     }
   377 
   377 
   378     while (argc > 1) {
   378     while (argc > 1) {
   379         if (strcmp (argv[1], "-fps") == 0) {
   379         if (strcmp(argv[1], "-fps") == 0) {
   380             if (argv[2]) {
   380             if (argv[2]) {
   381                 fps = atoi (argv[2]);
   381                 fps = atoi(argv[2]);
   382                 if (fps == 0) {
   382                 if (fps == 0) {
   383                     fprintf (stderr,
   383                     fprintf(stderr,
   384                              "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   384                             "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   385                     quit (10);
   385                     quit(10);
   386                 }
   386                 }
   387                 if ((fps < 0) || (fps > 1000)) {
   387                 if ((fps < 0) || (fps > 1000)) {
   388                     fprintf (stderr,
   388                     fprintf(stderr,
   389                              "The -fps option must be in range from 1 to 1000, default is 12.\n");
   389                             "The -fps option must be in range from 1 to 1000, default is 12.\n");
   390                     quit (10);
   390                     quit(10);
   391                 }
   391                 }
   392                 argv += 2;
   392                 argv += 2;
   393                 argc -= 2;
   393                 argc -= 2;
   394             } else {
   394             } else {
   395                 fprintf (stderr,
   395                 fprintf(stderr,
   396                          "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   396                         "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   397                 quit (10);
   397                 quit(10);
   398             }
   398             }
   399         } else if (strcmp (argv[1], "-format") == 0) {
   399         } else if (strcmp(argv[1], "-format") == 0) {
   400             if (argv[2]) {
   400             if (argv[2]) {
   401                 if (!strcmp (argv[2], "YV12"))
   401                 if (!strcmp(argv[2], "YV12"))
   402                     overlay_format = SDL_YV12_OVERLAY;
   402                     overlay_format = SDL_YV12_OVERLAY;
   403                 else if (!strcmp (argv[2], "IYUV"))
   403                 else if (!strcmp(argv[2], "IYUV"))
   404                     overlay_format = SDL_IYUV_OVERLAY;
   404                     overlay_format = SDL_IYUV_OVERLAY;
   405                 else if (!strcmp (argv[2], "YUY2"))
   405                 else if (!strcmp(argv[2], "YUY2"))
   406                     overlay_format = SDL_YUY2_OVERLAY;
   406                     overlay_format = SDL_YUY2_OVERLAY;
   407                 else if (!strcmp (argv[2], "UYVY"))
   407                 else if (!strcmp(argv[2], "UYVY"))
   408                     overlay_format = SDL_UYVY_OVERLAY;
   408                     overlay_format = SDL_UYVY_OVERLAY;
   409                 else if (!strcmp (argv[2], "YVYU"))
   409                 else if (!strcmp(argv[2], "YVYU"))
   410                     overlay_format = SDL_YVYU_OVERLAY;
   410                     overlay_format = SDL_YVYU_OVERLAY;
   411                 else {
   411                 else {
   412                     fprintf (stderr,
   412                     fprintf(stderr,
   413                              "The -format option %s is not recognized, see help for info.\n",
   413                             "The -format option %s is not recognized, see help for info.\n",
   414                              argv[2]);
   414                             argv[2]);
   415                     quit (10);
   415                     quit(10);
   416                 }
   416                 }
   417                 argv += 2;
   417                 argv += 2;
   418                 argc -= 2;
   418                 argc -= 2;
   419             } else {
   419             } else {
   420                 fprintf (stderr,
   420                 fprintf(stderr,
   421                          "The -format option requires an argument, default is YUY2.\n");
   421                         "The -format option requires an argument, default is YUY2.\n");
   422                 quit (10);
   422                 quit(10);
   423             }
   423             }
   424         } else if (strcmp (argv[1], "-scale") == 0) {
   424         } else if (strcmp(argv[1], "-scale") == 0) {
   425             if (argv[2]) {
   425             if (argv[2]) {
   426                 scale = atoi (argv[2]);
   426                 scale = atoi(argv[2]);
   427                 if (scale == 0) {
   427                 if (scale == 0) {
   428                     fprintf (stderr,
   428                     fprintf(stderr,
   429                              "The -scale option requires an argument [from 1 to 50], default is 5.\n");
   429                             "The -scale option requires an argument [from 1 to 50], default is 5.\n");
   430                     quit (10);
   430                     quit(10);
   431                 }
   431                 }
   432                 if ((scale < 0) || (scale > 50)) {
   432                 if ((scale < 0) || (scale > 50)) {
   433                     fprintf (stderr,
   433                     fprintf(stderr,
   434                              "The -scale option must be in range from 1 to 50, default is 5.\n");
   434                             "The -scale option must be in range from 1 to 50, default is 5.\n");
   435                     quit (10);
   435                     quit(10);
   436                 }
   436                 }
   437                 argv += 2;
   437                 argv += 2;
   438                 argc -= 2;
   438                 argc -= 2;
   439             } else {
   439             } else {
   440                 fprintf (stderr,
   440                 fprintf(stderr,
   441                          "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   441                         "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   442                 quit (10);
   442                 quit(10);
   443             }
   443             }
   444         } else if ((strcmp (argv[1], "-help") == 0)
   444         } else if ((strcmp(argv[1], "-help") == 0)
   445                    || (strcmp (argv[1], "-h") == 0)) {
   445                    || (strcmp(argv[1], "-h") == 0)) {
   446             PrintUsage (argv[0]);
   446             PrintUsage(argv[0]);
   447             quit (0);
   447             quit(0);
   448         } else {
   448         } else {
   449             fprintf (stderr, "Unrecognized option: %s.\n", argv[1]);
   449             fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
   450             quit (10);
   450             quit(10);
   451         }
   451         }
   452         break;
   452         break;
   453     }
   453     }
   454 
   454 
   455     RawMooseData = (Uint8 *) malloc (MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
   455     RawMooseData = (Uint8 *) malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
   456     if (RawMooseData == NULL) {
   456     if (RawMooseData == NULL) {
   457         fprintf (stderr, "Can't allocate memory for movie !\n");
   457         fprintf(stderr, "Can't allocate memory for movie !\n");
   458         free (RawMooseData);
   458         free(RawMooseData);
   459         quit (1);
   459         quit(1);
   460     }
   460     }
   461 
   461 
   462     /* load the trojan moose images */
   462     /* load the trojan moose images */
   463     handle = SDL_RWFromFile ("moose.dat", "rb");
   463     handle = SDL_RWFromFile("moose.dat", "rb");
   464     if (handle == NULL) {
   464     if (handle == NULL) {
   465         fprintf (stderr, "Can't find the file moose.dat !\n");
   465         fprintf(stderr, "Can't find the file moose.dat !\n");
   466         free (RawMooseData);
   466         free(RawMooseData);
   467         quit (2);
   467         quit(2);
   468     }
   468     }
   469 
   469 
   470     SDL_RWread (handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
   470     SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
   471 
   471 
   472     SDL_RWclose (handle);
   472     SDL_RWclose(handle);
   473 
   473 
   474     /* Set video mode */
   474     /* Set video mode */
   475     if ((screen =
   475     if ((screen =
   476          SDL_SetVideoMode (MOOSEPIC_W * scale, MOOSEPIC_H * scale, 0,
   476          SDL_SetVideoMode(MOOSEPIC_W * scale, MOOSEPIC_H * scale, 0,
   477                            SDL_RESIZABLE | SDL_SWSURFACE)) == NULL) {
   477                           SDL_RESIZABLE | SDL_SWSURFACE)) == NULL) {
   478         fprintf (stderr, "Couldn't set video mode: %s\n", SDL_GetError ());
   478         fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
   479         free (RawMooseData);
   479         free(RawMooseData);
   480         quit (4);
   480         quit(4);
   481     }
   481     }
   482 
   482 
   483     /* Set the window manager title bar */
   483     /* Set the window manager title bar */
   484     SDL_WM_SetCaption ("SDL test overlay: running moose", "testoverlay2");
   484     SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2");
   485 
   485 
   486     for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
   486     for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
   487         MooseFrame[i] =
   487         MooseFrame[i] =
   488             SDL_CreateRGBSurfaceFrom (RawMooseData + i * MOOSEFRAME_SIZE,
   488             SDL_CreateRGBSurfaceFrom(RawMooseData + i * MOOSEFRAME_SIZE,
   489                                       MOOSEPIC_W, MOOSEPIC_H, 8, MOOSEPIC_W,
   489                                      MOOSEPIC_W, MOOSEPIC_H, 8, MOOSEPIC_W,
   490                                       0, 0, 0, 0);
   490                                      0, 0, 0, 0);
   491         if (MooseFrame[i] == NULL) {
   491         if (MooseFrame[i] == NULL) {
   492             fprintf (stderr, "Couldn't create SDL_Surfaces:%s\n",
   492             fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n",
   493                      SDL_GetError ());
   493                     SDL_GetError());
   494             free (RawMooseData);
   494             free(RawMooseData);
   495             quit (5);
   495             quit(5);
   496         }
   496         }
   497         SDL_SetColors (MooseFrame[i], MooseColors, 0, 84);
   497         SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);
   498 
   498 
   499         {
   499         {
   500             SDL_Surface *newsurf;
   500             SDL_Surface *newsurf;
   501             SDL_PixelFormat format;
   501             SDL_PixelFormat format;
   502 
   502 
   523             format.Aloss = 8;
   523             format.Aloss = 8;
   524             format.colorkey = 0;
   524             format.colorkey = 0;
   525             format.alpha = 0;
   525             format.alpha = 0;
   526 
   526 
   527             newsurf =
   527             newsurf =
   528                 SDL_ConvertSurface (MooseFrame[i], &format, SDL_SWSURFACE);
   528                 SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE);
   529             if (!newsurf) {
   529             if (!newsurf) {
   530                 fprintf (stderr,
   530                 fprintf(stderr,
   531                          "Couldn't convert picture to 32bits RGB: %s\n",
   531                         "Couldn't convert picture to 32bits RGB: %s\n",
   532                          SDL_GetError ());
   532                         SDL_GetError());
   533                 quit (6);
   533                 quit(6);
   534             }
   534             }
   535             SDL_FreeSurface (MooseFrame[i]);
   535             SDL_FreeSurface(MooseFrame[i]);
   536             MooseFrame[i] = newsurf;
   536             MooseFrame[i] = newsurf;
   537         }
   537         }
   538     }
   538     }
   539 
   539 
   540     free (RawMooseData);
   540     free(RawMooseData);
   541 
   541 
   542     overlay =
   542     overlay =
   543         SDL_CreateYUVOverlay (MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen);
   543         SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen);
   544     if (!overlay) {
   544     if (!overlay) {
   545         fprintf (stderr, "Couldn't create overlay: %s\n", SDL_GetError ());
   545         fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
   546         quit (7);
   546         quit(7);
   547     }
   547     }
   548 
   548 
   549     printf ("Created %dx%dx%d %s %s overlay\n", overlay->w, overlay->h,
   549     printf("Created %dx%dx%d %s %s overlay\n", overlay->w, overlay->h,
   550             overlay->planes, overlay->hw_overlay ? "hardware" : "software",
   550            overlay->planes, overlay->hw_overlay ? "hardware" : "software",
   551             overlay->format == SDL_YV12_OVERLAY ? "YV12" : overlay->format ==
   551            overlay->format == SDL_YV12_OVERLAY ? "YV12" : overlay->format ==
   552             SDL_IYUV_OVERLAY ? "IYUV" : overlay->format ==
   552            SDL_IYUV_OVERLAY ? "IYUV" : overlay->format ==
   553             SDL_YUY2_OVERLAY ? "YUY2" : overlay->format ==
   553            SDL_YUY2_OVERLAY ? "YUY2" : overlay->format ==
   554             SDL_UYVY_OVERLAY ? "UYVY" : overlay->format ==
   554            SDL_UYVY_OVERLAY ? "UYVY" : overlay->format ==
   555             SDL_YVYU_OVERLAY ? "YVYU" : "Unknown");
   555            SDL_YVYU_OVERLAY ? "YVYU" : "Unknown");
   556 
   556 
   557     for (i = 0; i < overlay->planes; i++) {
   557     for (i = 0; i < overlay->planes; i++) {
   558         printf ("  plane %d: pitch=%d\n", i, overlay->pitches[i]);
   558         printf("  plane %d: pitch=%d\n", i, overlay->pitches[i]);
   559     }
   559     }
   560 
   560 
   561     overlayrect.x = 0;
   561     overlayrect.x = 0;
   562     overlayrect.y = 0;
   562     overlayrect.y = 0;
   563     overlayrect.w = MOOSEPIC_W * scale;
   563     overlayrect.w = MOOSEPIC_W * scale;
   566     /* set the start frame */
   566     /* set the start frame */
   567     i = 0;
   567     i = 0;
   568     fpsdelay = 1000 / fps;
   568     fpsdelay = 1000 / fps;
   569 
   569 
   570     /* Ignore key up events, they don't even get filtered */
   570     /* Ignore key up events, they don't even get filtered */
   571     SDL_EventState (SDL_KEYUP, SDL_IGNORE);
   571     SDL_EventState(SDL_KEYUP, SDL_IGNORE);
   572 
   572 
   573     lastftick = SDL_GetTicks ();
   573     lastftick = SDL_GetTicks();
   574 
   574 
   575     /* Loop, waiting for QUIT or RESIZE */
   575     /* Loop, waiting for QUIT or RESIZE */
   576     while (1) {
   576     while (1) {
   577         if (SDL_PollEvent (&event)) {
   577         if (SDL_PollEvent(&event)) {
   578             switch (event.type) {
   578             switch (event.type) {
   579             case SDL_VIDEORESIZE:
   579             case SDL_VIDEORESIZE:
   580                 screen =
   580                 screen =
   581                     SDL_SetVideoMode (event.resize.w, event.resize.h, 0,
   581                     SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
   582                                       SDL_RESIZABLE | SDL_SWSURFACE);
   582                                      SDL_RESIZABLE | SDL_SWSURFACE);
   583                 overlayrect.w = event.resize.w;
   583                 overlayrect.w = event.resize.w;
   584                 overlayrect.h = event.resize.h;
   584                 overlayrect.h = event.resize.h;
   585                 if (paused) {
   585                 if (paused) {
   586                     resized = 1;
   586                     resized = 1;
   587                 }
   587                 }
   597                 }
   597                 }
   598                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   598                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   599                     break;
   599                     break;
   600                 }
   600                 }
   601             case SDL_QUIT:
   601             case SDL_QUIT:
   602                 SDL_FreeYUVOverlay (overlay);
   602                 SDL_FreeYUVOverlay(overlay);
   603                 for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
   603                 for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
   604                     SDL_FreeSurface (MooseFrame[i]);
   604                     SDL_FreeSurface(MooseFrame[i]);
   605                 }
   605                 }
   606                 quit (0);
   606                 quit(0);
   607             }
   607             }
   608         }
   608         }
   609 
   609 
   610         if ((!paused) || (resized)) {
   610         if ((!paused) || (resized)) {
   611             if (((SDL_GetTicks () - lastftick) > fpsdelay) || (resized)) {
   611             if (((SDL_GetTicks() - lastftick) > fpsdelay) || (resized)) {
   612                 lastftick = SDL_GetTicks ();
   612                 lastftick = SDL_GetTicks();
   613 
   613 
   614                 switch (overlay_format) {
   614                 switch (overlay_format) {
   615                 case SDL_YUY2_OVERLAY:
   615                 case SDL_YUY2_OVERLAY:
   616                     ConvertRGBtoYUY2 (MooseFrame[i], overlay, 0, 100);
   616                     ConvertRGBtoYUY2(MooseFrame[i], overlay, 0, 100);
   617                     break;
   617                     break;
   618                 case SDL_YV12_OVERLAY:
   618                 case SDL_YV12_OVERLAY:
   619                     ConvertRGBtoYV12 (MooseFrame[i], overlay, 0, 100);
   619                     ConvertRGBtoYV12(MooseFrame[i], overlay, 0, 100);
   620                     break;
   620                     break;
   621                 case SDL_UYVY_OVERLAY:
   621                 case SDL_UYVY_OVERLAY:
   622                     ConvertRGBtoUYVY (MooseFrame[i], overlay, 0, 100);
   622                     ConvertRGBtoUYVY(MooseFrame[i], overlay, 0, 100);
   623                     break;
   623                     break;
   624                 case SDL_YVYU_OVERLAY:
   624                 case SDL_YVYU_OVERLAY:
   625                     ConvertRGBtoYVYU (MooseFrame[i], overlay, 0, 100);
   625                     ConvertRGBtoYVYU(MooseFrame[i], overlay, 0, 100);
   626                     break;
   626                     break;
   627                 case SDL_IYUV_OVERLAY:
   627                 case SDL_IYUV_OVERLAY:
   628                     ConvertRGBtoIYUV (MooseFrame[i], overlay, 0, 100);
   628                     ConvertRGBtoIYUV(MooseFrame[i], overlay, 0, 100);
   629                     break;
   629                     break;
   630                 }
   630                 }
   631 
   631 
   632                 SDL_DisplayYUVOverlay (overlay, &overlayrect);
   632                 SDL_DisplayYUVOverlay(overlay, &overlayrect);
   633                 if (!resized) {
   633                 if (!resized) {
   634                     i++;
   634                     i++;
   635                     if (i == 10) {
   635                     if (i == 10) {
   636                         i = 0;
   636                         i = 0;
   637                     }
   637                     }
   639                     resized = 0;
   639                     resized = 0;
   640                 }
   640                 }
   641             }
   641             }
   642         }
   642         }
   643         /* kind of timeslice to OS */
   643         /* kind of timeslice to OS */
   644         SDL_Delay (1);
   644         SDL_Delay(1);
   645     }
   645     }
   646 
   646 
   647     SDL_Quit ();
   647     SDL_Quit();
   648     return 0;
   648     return 0;
   649 }
   649 }