int main(int argc, char **argv) { Uint8* RawMooseData; SDL_RWops* handle; SDL_Surface* screen; SDL_Surface* MooseFrame[MOOSEFRAMES_COUNT]; SDL_Overlay* overlay; SDL_Rect overlayrect; SDL_Event event; Uint32 lastftick; int paused=0; int resized=0; int i; int fps=12; int fpsdelay; int overlay_format=SDL_YUY2_OVERLAY; int scale=5; while ( argc > 1 ) { if (strcmp(argv[1], "-fps")== 0) { if (argv[2]) { fps = atoi(argv[2]); if (fps==0) { fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); return -1; } if ((fps<0) || (fps>1000)) { fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n"); return -1; } argv += 2; argc -= 2; } else { fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); return -1; } } else if (strcmp(argv[1], "-format") == 0) { if (argv[2]) { if (!strcmp(argv[2],"YV12")) overlay_format = SDL_YV12_OVERLAY; else if(!strcmp(argv[2],"IYUV")) overlay_format = SDL_IYUV_OVERLAY; else if(!strcmp(argv[2],"YUY2")) overlay_format = SDL_YUY2_OVERLAY; else if(!strcmp(argv[2],"UYVY")) overlay_format = SDL_UYVY_OVERLAY; else if(!strcmp(argv[2],"YVYU")) overlay_format = SDL_YVYU_OVERLAY; else { fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]); return -1; } argv += 2; argc -= 2; } else { fprintf(stderr, "The -format option requires an argument, default is YUY2.\n"); return -1; } } else if (strcmp(argv[1], "-scale") == 0) { if (argv[2]) { scale = atoi(argv[2]); if (scale==0) { fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n"); return -1; } if ((scale<0) || (scale>50)) { fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n"); return -1; } argv += 2; argc -= 2; } else { fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); return -1; } } else if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) { PrintUsage(argv[0]); return 0; } else { fprintf(stderr, "Unrecognized option: %s.\n", argv[1]); return -1; } break; } RawMooseData=(Uint8*)malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT); if (RawMooseData==NULL) { fprintf(stderr, "Can't allocate memory for movie !\n"); free(RawMooseData); return 1; } /* load the trojan moose images */ handle=SDL_RWFromFile("moose.dat", "rb"); if (handle==NULL) { fprintf(stderr, "Can't find the file moose.dat !\n"); free(RawMooseData); return 2; } SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT); SDL_RWclose(handle); if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); free(RawMooseData); return 3; } atexit(SDL_Quit); /* Set video mode */ if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL ) { fprintf(stderr, "Couldn't set video mode: %s\n", 0, SDL_GetError()); free(RawMooseData); return 4; } /* Set the window manager title bar */ SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2"); for (i=0; i<MOOSEFRAMES_COUNT; i++) { MooseFrame[i]=SDL_CreateRGBSurfaceFrom(RawMooseData+i*MOOSEFRAME_SIZE, MOOSEPIC_W, MOOSEPIC_H, 8, MOOSEPIC_W, 0, 0, 0, 0); if (MooseFrame[i]==NULL) { fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", 0, SDL_GetError()); free(RawMooseData); return 5; } SDL_SetColors(MooseFrame[i], MooseColors, 0, 84); { SDL_Surface *newsurf; SDL_PixelFormat format; format.palette=NULL; format.BitsPerPixel=32; format.BytesPerPixel=4; #if SDL_BYTEORDER == SDL_LIL_ENDIAN format.Rshift=0; format.Gshift=8; format.Bshift=16; #else format.Rshift=24; format.Gshift=16; format.Bshift=8; #endif format.Ashift=0; format.Rmask=0xff<<format.Rshift; format.Gmask=0xff<<format.Gshift; format.Bmask=0xff<<format.Bshift; format.Amask=0; format.Rloss=0; format.Gloss=0; format.Bloss=0; format.Aloss=8; format.colorkey=0; format.alpha=0; newsurf=SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE); if(!newsurf) { fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError()); return 6; } SDL_FreeSurface(MooseFrame[i]); MooseFrame[i]=newsurf; } } free(RawMooseData); overlay=SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen); if (!overlay) { fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError()); return 7; } printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes, overlay->hw_overlay?"hardware":"software", overlay->format==SDL_YV12_OVERLAY?"YV12": overlay->format==SDL_IYUV_OVERLAY?"IYUV": overlay->format==SDL_YUY2_OVERLAY?"YUY2": overlay->format==SDL_UYVY_OVERLAY?"UYVY": overlay->format==SDL_YVYU_OVERLAY?"YVYU": "Unknown"); for(i=0; i<overlay->planes; i++) { printf(" plane %d: pitch=%d\n", i, overlay->pitches[i]); } overlayrect.x=0; overlayrect.y=0; overlayrect.w=MOOSEPIC_W*scale; overlayrect.h=MOOSEPIC_H*scale; /* set the start frame */ i=0; fpsdelay=1000/fps; /* Ignore key up events, they don't even get filtered */ SDL_EventState(SDL_KEYUP, SDL_IGNORE); lastftick=SDL_GetTicks(); /* Loop, waiting for QUIT or RESIZE */ while (1) { if (SDL_PollEvent(&event)) { switch (event.type) { case SDL_VIDEORESIZE: screen=SDL_SetVideoMode(event.resize.w, event.resize.h, 0, SDL_RESIZABLE | SDL_SWSURFACE); overlayrect.w=event.resize.w; overlayrect.h=event.resize.h; if (paused) { resized=1; } break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_SPACE) { paused=!paused; break; } if (event.key.keysym.sym != SDLK_ESCAPE) { break; } case SDL_QUIT: SDL_FreeYUVOverlay(overlay); for (i=0; i<MOOSEFRAMES_COUNT; i++) { SDL_FreeSurface(MooseFrame[i]); } return 0; } } if ((!paused)||(resized)) { if (((SDL_GetTicks()-lastftick)>fpsdelay)||(resized)) { lastftick=SDL_GetTicks(); switch (overlay_format) { case SDL_YUY2_OVERLAY: ConvertRGBtoYUY2(MooseFrame[i], overlay, 0, 100); break; case SDL_YV12_OVERLAY: ConvertRGBtoYV12(MooseFrame[i], overlay, 0, 100); break; case SDL_UYVY_OVERLAY: ConvertRGBtoUYVY(MooseFrame[i], overlay, 0, 100); break; case SDL_YVYU_OVERLAY: ConvertRGBtoYVYU(MooseFrame[i], overlay, 0, 100); break; case SDL_IYUV_OVERLAY: ConvertRGBtoIYUV(MooseFrame[i], overlay, 0, 100); break; } SDL_DisplayYUVOverlay(overlay, &overlayrect); if (!resized) { i++; if (i==10) { i=0; } } else { resized=0; } } } /* kind of timeslice to OS */ SDL_Delay(1); } return 0; }
int main(int argc, char **argv) { char *argv0 = argv[0]; int flip; int delay; int desired_bpp; Uint32 video_flags, overlay_format; char *bmpfile; #ifdef BENCHMARK_SDL Uint32 then, now; #endif int i; /* Set default options and check command-line */ flip = 0; scale = 0; monochrome = 0; luminance = 100; delay = 1; w = WINDOW_WIDTH; h = WINDOW_HEIGHT; desired_bpp = 0; video_flags = 0; overlay_format = SDL_YV12_OVERLAY; while (argc > 1) { if (strcmp(argv[1], "-delay") == 0) { if (argv[2]) { delay = atoi(argv[2]); argv += 2; argc -= 2; } else { fprintf(stderr, "The -delay option requires an argument\n"); return (1); } } else if (strcmp(argv[1], "-width") == 0) { if (argv[2] && ((w = atoi(argv[2])) > 0)) { argv += 2; argc -= 2; } else { fprintf(stderr, "The -width option requires an argument\n"); return (1); } } else if (strcmp(argv[1], "-height") == 0) { if (argv[2] && ((h = atoi(argv[2])) > 0)) { argv += 2; argc -= 2; } else { fprintf(stderr, "The -height option requires an argument\n"); return (1); } } else if (strcmp(argv[1], "-bpp") == 0) { if (argv[2]) { desired_bpp = atoi(argv[2]); argv += 2; argc -= 2; } else { fprintf(stderr, "The -bpp option requires an argument\n"); return (1); } } else if (strcmp(argv[1], "-lum") == 0) { if (argv[2]) { luminance = atoi(argv[2]); argv += 2; argc -= 2; } else { fprintf(stderr, "The -lum option requires an argument\n"); return (1); } } else if (strcmp(argv[1], "-format") == 0) { if (argv[2]) { if (!strcmp(argv[2], "YV12")) overlay_format = SDL_YV12_OVERLAY; else if (!strcmp(argv[2], "IYUV")) overlay_format = SDL_IYUV_OVERLAY; else if (!strcmp(argv[2], "YUY2")) overlay_format = SDL_YUY2_OVERLAY; else if (!strcmp(argv[2], "UYVY")) overlay_format = SDL_UYVY_OVERLAY; else if (!strcmp(argv[2], "YVYU")) overlay_format = SDL_YVYU_OVERLAY; else { fprintf(stderr, "The -format option %s is not recognized\n", argv[2]); return (1); } argv += 2; argc -= 2; } else { fprintf(stderr, "The -format option requires an argument\n"); return (1); } } else if (strcmp(argv[1], "-hw") == 0) { video_flags |= SDL_HWSURFACE; argv += 1; argc -= 1; } else if (strcmp(argv[1], "-flip") == 0) { video_flags |= SDL_DOUBLEBUF; argv += 1; argc -= 1; } else if (strcmp(argv[1], "-scale") == 0) { scale = 1; argv += 1; argc -= 1; } else if (strcmp(argv[1], "-mono") == 0) { monochrome = 1; argv += 1; argc -= 1; } else if ((strcmp(argv[1], "-help") == 0) || (strcmp(argv[1], "-h") == 0)) { PrintUsage(argv0); return (1); } else if (strcmp(argv[1], "-fullscreen") == 0) { video_flags |= SDL_FULLSCREEN; argv += 1; argc -= 1; } else break; } if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); return (1); } /* Initialize the display */ screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags); if (screen == NULL) { fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", w, h, desired_bpp, SDL_GetError()); quit(1); } printf("Set%s %dx%dx%d mode\n", screen->flags & SDL_FULLSCREEN ? " fullscreen" : "", screen->w, screen->h, screen->format->BitsPerPixel); printf("(video surface located in %s memory)\n", (screen->flags & SDL_HWSURFACE) ? "video" : "system"); if (screen->flags & SDL_DOUBLEBUF) { printf("Double-buffering enabled\n"); flip = 1; } /* Set the window manager title bar */ SDL_WM_SetCaption("SDL test overlay", "testoverlay"); /* Load picture */ bmpfile = (argv[1] ? argv[1] : "sample.bmp"); pic = SDL_LoadBMP(bmpfile); if (pic == NULL) { fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError()); quit(1); } /* Convert the picture to 32bits, for easy conversion */ { SDL_Surface *newsurf; SDL_PixelFormat format; format.palette = NULL; format.BitsPerPixel = 32; format.BytesPerPixel = 4; #if SDL_BYTEORDER == SDL_LIL_ENDIAN format.Rshift = 0; format.Gshift = 8; format.Bshift = 16; #else format.Rshift = 24; format.Gshift = 16; format.Bshift = 8; #endif format.Ashift = 0; format.Rmask = 0xff << format.Rshift; format.Gmask = 0xff << format.Gshift; format.Bmask = 0xff << format.Bshift; format.Amask = 0; format.Rloss = 0; format.Gloss = 0; format.Bloss = 0; format.Aloss = 8; newsurf = SDL_ConvertSurface(pic, &format, SDL_SWSURFACE); if (!newsurf) { fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError()); quit(1); } SDL_FreeSurface(pic); pic = newsurf; } /* Create the overlay */ overlay = SDL_CreateYUVOverlay(pic->w, pic->h, overlay_format, screen); if (overlay == NULL) { fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError()); quit(1); } printf("Created %dx%dx%d %s %s overlay\n", overlay->w, overlay->h, overlay->planes, overlay->hw_overlay ? "hardware" : "software", overlay->format == SDL_YV12_OVERLAY ? "YV12" : overlay->format == SDL_IYUV_OVERLAY ? "IYUV" : overlay->format == SDL_YUY2_OVERLAY ? "YUY2" : overlay->format == SDL_UYVY_OVERLAY ? "UYVY" : overlay->format == SDL_YVYU_OVERLAY ? "YVYU" : "Unknown"); for (i = 0; i < overlay->planes; i++) { printf(" plane %d: pitch=%d\n", i, overlay->pitches[i]); } /* Convert to YUV, and draw to the overlay */ #ifdef BENCHMARK_SDL then = SDL_GetTicks(); #endif switch (overlay->format) { case SDL_YV12_OVERLAY: ConvertRGBtoYV12(pic, overlay, monochrome, luminance); break; case SDL_UYVY_OVERLAY: ConvertRGBtoUYVY(pic, overlay, monochrome, luminance); break; case SDL_YVYU_OVERLAY: ConvertRGBtoYVYU(pic, overlay, monochrome, luminance); break; case SDL_YUY2_OVERLAY: ConvertRGBtoYUY2(pic, overlay, monochrome, luminance); break; case SDL_IYUV_OVERLAY: ConvertRGBtoIYUV(pic, overlay, monochrome, luminance); break; default: printf("cannot convert RGB picture to obtained YUV format!\n"); quit(1); break; } #ifdef BENCHMARK_SDL now = SDL_GetTicks(); printf("Conversion Time: %d milliseconds\n", now - then); #endif /* Do all the drawing work */ #ifdef BENCHMARK_SDL then = SDL_GetTicks(); #endif Draw(); #ifdef BENCHMARK_SDL now = SDL_GetTicks(); printf("Time: %d milliseconds\n", now - then); #endif SDL_Delay(delay * 1000); SDL_Quit(); return (0); }
inline void Convert32bit(SDL_Surface *display) { SDL_Surface *newsurf; SDL_PixelFormat format; format.palette=NULL; format.BitsPerPixel=32; format.BytesPerPixel=4; #if SDL_BYTEORDER == SDL_LIL_ENDIAN format.Rshift=0; format.Gshift=8; format.Bshift=16; #else format.Rshift=24; format.Gshift=16; format.Bshift=8; #endif format.Ashift=0; format.Rmask=0xff<<format.Rshift; format.Gmask=0xff<<format.Gshift; format.Bmask=0xff<<format.Bshift; format.Amask=0; format.Rloss=0; format.Gloss=0; format.Bloss=0; format.Aloss=8; format.colorkey=0; format.alpha=0; newsurf=SDL_ConvertSurface(display, &format, SDL_SWSURFACE); if(!newsurf) { fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError()); exit(1); } switch(overlay->format) { case SDL_YV12_OVERLAY: ConvertRGBtoYV12(newsurf,overlay); break; case SDL_UYVY_OVERLAY: ConvertRGBtoUYVY(newsurf,overlay); break; case SDL_YVYU_OVERLAY: ConvertRGBtoYVYU(newsurf,overlay); break; case SDL_YUY2_OVERLAY: ConvertRGBtoYUY2(newsurf,overlay); break; case SDL_IYUV_OVERLAY: ConvertRGBtoIYUV(newsurf,overlay); break; default: printf("cannot convert RGB picture to obtained YUV format!\n"); exit(1); break; } }