1 /********************************************************************************
3 * Test of the overlay used for moved pictures, test more closed to real life. *
4 * Running trojan moose :) Coded by Mike Gorchak. *
6 ********************************************************************************/
17 #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
18 #define MOOSEFRAMES_COUNT 10
20 SDL_Color MooseColors
[84]={
21 { 49, 49, 49}, { 66, 24, 0}, { 66, 33, 0}, { 66, 66, 66},
22 { 66, 115, 49}, { 74, 33, 0}, { 74, 41, 16}, { 82, 33, 8},
23 { 82, 41, 8}, { 82, 49, 16}, { 82, 82, 82}, { 90, 41, 8},
24 { 90, 41, 16}, { 90, 57, 24}, { 99, 49, 16}, { 99, 66, 24},
25 { 99, 66, 33}, { 99, 74, 33}, {107, 57, 24}, {107, 82, 41},
26 {115, 57, 33}, {115, 66, 33}, {115, 66, 41}, {115, 74, 0},
27 {115, 90, 49}, {115, 115, 115}, {123, 82, 0}, {123, 99, 57},
28 {132, 66, 41}, {132, 74, 41}, {132, 90, 8}, {132, 99, 33},
29 {132, 99, 66}, {132, 107, 66}, {140, 74, 49}, {140, 99, 16},
30 {140, 107, 74}, {140, 115, 74}, {148, 107, 24}, {148, 115, 82},
31 {148, 123, 74}, {148, 123, 90}, {156, 115, 33}, {156, 115, 90},
32 {156, 123, 82}, {156, 132, 82}, {156, 132, 99}, {156, 156, 156},
33 {165, 123, 49}, {165, 123, 90}, {165, 132, 82}, {165, 132, 90},
34 {165, 132, 99}, {165, 140, 90}, {173, 132, 57}, {173, 132, 99},
35 {173, 140, 107}, {173, 140, 115}, {173, 148, 99}, {173, 173, 173},
36 {181, 140, 74}, {181, 148, 115}, {181, 148, 123}, {181, 156, 107},
37 {189, 148, 123}, {189, 156, 82}, {189, 156, 123}, {189, 156, 132},
38 {189, 189, 189}, {198, 156, 123}, {198, 165, 132}, {206, 165, 99},
39 {206, 165, 132}, {206, 173, 140}, {206, 206, 206}, {214, 173, 115},
40 {214, 173, 140}, {222, 181, 148}, {222, 189, 132}, {222, 189, 156},
41 {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173}
45 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
46 static void quit(int rc
)
52 /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
54 /* NOTE: These RGB conversion functions are not intended for speed,
58 void RGBtoYUV(Uint8
*rgb
, int *yuv
, int monochrome
, int luminance
)
62 #if 1 /* these are the two formulas that I found on the FourCC site... */
63 yuv
[0] = 0.299*rgb
[0] + 0.587*rgb
[1] + 0.114*rgb
[2];
67 yuv
[0] = (0.257 * rgb
[0]) + (0.504 * rgb
[1]) + (0.098 * rgb
[2]) + 16;
74 #if 1 /* these are the two formulas that I found on the FourCC site... */
75 yuv
[0] = 0.299*rgb
[0] + 0.587*rgb
[1] + 0.114*rgb
[2];
76 yuv
[1] = (rgb
[2]-yuv
[0])*0.565 + 128;
77 yuv
[2] = (rgb
[0]-yuv
[0])*0.713 + 128;
79 yuv
[0] = (0.257 * rgb
[0]) + (0.504 * rgb
[1]) + (0.098 * rgb
[2]) + 16;
80 yuv
[1] = 128 - (0.148 * rgb
[0]) - (0.291 * rgb
[1]) + (0.439 * rgb
[2]);
81 yuv
[2] = 128 + (0.439 * rgb
[0]) - (0.368 * rgb
[1]) - (0.071 * rgb
[2]);
87 yuv
[0]=yuv
[0]*luminance
/100;
93 void ConvertRGBtoYV12(SDL_Surface
*s
, SDL_Overlay
*o
, int monochrome
, int luminance
)
100 SDL_LockYUVOverlay(o
);
103 for(y
=0; y
<s
->h
&& y
<o
->h
; y
++)
105 p
=((Uint8
*) s
->pixels
)+s
->pitch
*y
;
106 op
[0]=o
->pixels
[0]+o
->pitches
[0]*y
;
107 op
[1]=o
->pixels
[1]+o
->pitches
[1]*(y
/2);
108 op
[2]=o
->pixels
[2]+o
->pitches
[2]*(y
/2);
109 for(x
=0; x
<s
->w
&& x
<o
->w
; x
++)
111 RGBtoYUV(p
, yuv
, monochrome
, luminance
);
118 p
+=s
->format
->BytesPerPixel
;
122 SDL_UnlockYUVOverlay(o
);
123 SDL_UnlockSurface(s
);
126 void ConvertRGBtoIYUV(SDL_Surface
*s
, SDL_Overlay
*o
, int monochrome
, int luminance
)
133 SDL_LockYUVOverlay(o
);
136 for(y
=0; y
<s
->h
&& y
<o
->h
; y
++)
138 p
=((Uint8
*) s
->pixels
)+s
->pitch
*y
;
139 op
[0]=o
->pixels
[0]+o
->pitches
[0]*y
;
140 op
[1]=o
->pixels
[1]+o
->pitches
[1]*(y
/2);
141 op
[2]=o
->pixels
[2]+o
->pitches
[2]*(y
/2);
142 for(x
=0; x
<s
->w
&& x
<o
->w
; x
++)
144 RGBtoYUV(p
,yuv
, monochrome
, luminance
);
151 p
+=s
->format
->BytesPerPixel
;
155 SDL_UnlockYUVOverlay(o
);
156 SDL_UnlockSurface(s
);
159 void ConvertRGBtoUYVY(SDL_Surface
*s
, SDL_Overlay
*o
, int monochrome
, int luminance
)
166 SDL_LockYUVOverlay(o
);
168 for(y
=0; y
<s
->h
&& y
<o
->h
; y
++)
170 p
=((Uint8
*) s
->pixels
)+s
->pitch
*y
;
171 op
=o
->pixels
[0]+o
->pitches
[0]*y
;
172 for(x
=0; x
<s
->w
&& x
<o
->w
; x
++)
174 RGBtoYUV(p
, yuv
, monochrome
, luminance
);
184 p
+=s
->format
->BytesPerPixel
;
188 SDL_UnlockYUVOverlay(o
);
189 SDL_UnlockSurface(s
);
192 void ConvertRGBtoYVYU(SDL_Surface
*s
, SDL_Overlay
*o
, int monochrome
, int luminance
)
199 SDL_LockYUVOverlay(o
);
201 for(y
=0; y
<s
->h
&& y
<o
->h
; y
++)
203 p
=((Uint8
*) s
->pixels
)+s
->pitch
*y
;
204 op
=o
->pixels
[0]+o
->pitches
[0]*y
;
205 for(x
=0; x
<s
->w
&& x
<o
->w
; x
++)
207 RGBtoYUV(p
,yuv
, monochrome
, luminance
);
220 p
+=s
->format
->BytesPerPixel
;
224 SDL_UnlockYUVOverlay(o
);
225 SDL_UnlockSurface(s
);
228 void ConvertRGBtoYUY2(SDL_Surface
*s
, SDL_Overlay
*o
, int monochrome
, int luminance
)
235 SDL_LockYUVOverlay(o
);
237 for(y
=0; y
<s
->h
&& y
<o
->h
; y
++)
239 p
=((Uint8
*) s
->pixels
)+s
->pitch
*y
;
240 op
=o
->pixels
[0]+o
->pitches
[0]*y
;
241 for(x
=0; x
<s
->w
&& x
<o
->w
; x
++)
243 RGBtoYUV(p
,yuv
, monochrome
, luminance
);
256 p
+=s
->format
->BytesPerPixel
;
260 SDL_UnlockYUVOverlay(o
);
261 SDL_UnlockSurface(s
);
264 static void PrintUsage(char *argv0
)
266 fprintf(stderr
, "Usage: %s [arg] [arg] [arg] ...\n", argv0
);
267 fprintf(stderr
, "\n");
268 fprintf(stderr
, "Where 'arg' is any of the following options:\n");
269 fprintf(stderr
, "\n");
270 fprintf(stderr
, " -fps <frames per second>\n");
271 fprintf(stderr
, " -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
272 fprintf(stderr
, " -scale <scale factor> (initial scale of the overlay)\n");
273 fprintf(stderr
, " -help (shows this help)\n");
274 fprintf(stderr
, "\n");
275 fprintf(stderr
, "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
276 fprintf(stderr
, "\n");
279 int main(int argc
, char **argv
)
284 SDL_Surface
* MooseFrame
[MOOSEFRAMES_COUNT
];
285 SDL_Overlay
* overlay
;
286 SDL_Rect overlayrect
;
294 int overlay_format
=SDL_YUY2_OVERLAY
;
297 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
) < 0)
299 fprintf(stderr
, "Couldn't initialize SDL: %s\n", SDL_GetError());
305 if (strcmp(argv
[1], "-fps")== 0)
312 fprintf(stderr
, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
315 if ((fps
<0) || (fps
>1000))
317 fprintf(stderr
, "The -fps option must be in range from 1 to 1000, default is 12.\n");
325 fprintf(stderr
, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
329 if (strcmp(argv
[1], "-format") == 0)
333 if (!strcmp(argv
[2],"YV12"))
334 overlay_format
= SDL_YV12_OVERLAY
;
335 else if(!strcmp(argv
[2],"IYUV"))
336 overlay_format
= SDL_IYUV_OVERLAY
;
337 else if(!strcmp(argv
[2],"YUY2"))
338 overlay_format
= SDL_YUY2_OVERLAY
;
339 else if(!strcmp(argv
[2],"UYVY"))
340 overlay_format
= SDL_UYVY_OVERLAY
;
341 else if(!strcmp(argv
[2],"YVYU"))
342 overlay_format
= SDL_YVYU_OVERLAY
;
345 fprintf(stderr
, "The -format option %s is not recognized, see help for info.\n", argv
[2]);
353 fprintf(stderr
, "The -format option requires an argument, default is YUY2.\n");
357 if (strcmp(argv
[1], "-scale") == 0)
361 scale
= atoi(argv
[2]);
364 fprintf(stderr
, "The -scale option requires an argument [from 1 to 50], default is 5.\n");
367 if ((scale
<0) || (scale
>50))
369 fprintf(stderr
, "The -scale option must be in range from 1 to 50, default is 5.\n");
377 fprintf(stderr
, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
381 if ((strcmp(argv
[1], "-help") == 0 ) || (strcmp(argv
[1], "-h") == 0))
387 fprintf(stderr
, "Unrecognized option: %s.\n", argv
[1]);
393 RawMooseData
=(Uint8
*)malloc(MOOSEFRAME_SIZE
* MOOSEFRAMES_COUNT
);
394 if (RawMooseData
==NULL
)
396 fprintf(stderr
, "Can't allocate memory for movie !\n");
401 /* load the trojan moose images */
402 handle
=SDL_RWFromFile("moose.dat", "rb");
405 fprintf(stderr
, "Can't find the file moose.dat !\n");
410 SDL_RWread(handle
, RawMooseData
, MOOSEFRAME_SIZE
, MOOSEFRAMES_COUNT
);
415 if ( (screen
=SDL_SetVideoMode(MOOSEPIC_W
*scale
, MOOSEPIC_H
*scale
, 0, SDL_RESIZABLE
| SDL_SWSURFACE
)) == NULL
)
417 fprintf(stderr
, "Couldn't set video mode: %s\n", SDL_GetError());
422 /* Set the window manager title bar */
423 SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2");
425 for (i
=0; i
<MOOSEFRAMES_COUNT
; i
++)
427 MooseFrame
[i
]=SDL_CreateRGBSurfaceFrom(RawMooseData
+i
*MOOSEFRAME_SIZE
, MOOSEPIC_W
,
428 MOOSEPIC_H
, 8, MOOSEPIC_W
, 0, 0, 0, 0);
429 if (MooseFrame
[i
]==NULL
)
431 fprintf(stderr
, "Couldn't create SDL_Surfaces:%s\n", SDL_GetError());
435 SDL_SetColors(MooseFrame
[i
], MooseColors
, 0, 84);
438 SDL_Surface
*newsurf
;
439 SDL_PixelFormat format
;
442 format
.BitsPerPixel
=32;
443 format
.BytesPerPixel
=4;
444 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
454 format
.Rmask
=0xff<<format
.Rshift
;
455 format
.Gmask
=0xff<<format
.Gshift
;
456 format
.Bmask
=0xff<<format
.Bshift
;
465 newsurf
=SDL_ConvertSurface(MooseFrame
[i
], &format
, SDL_SWSURFACE
);
468 fprintf(stderr
, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError());
471 SDL_FreeSurface(MooseFrame
[i
]);
472 MooseFrame
[i
]=newsurf
;
478 overlay
=SDL_CreateYUVOverlay(MOOSEPIC_W
, MOOSEPIC_H
, overlay_format
, screen
);
481 fprintf(stderr
, "Couldn't create overlay: %s\n", SDL_GetError());
485 printf("Created %dx%dx%d %s %s overlay\n",overlay
->w
,overlay
->h
,overlay
->planes
,
486 overlay
->hw_overlay
?"hardware":"software",
487 overlay
->format
==SDL_YV12_OVERLAY
?"YV12":
488 overlay
->format
==SDL_IYUV_OVERLAY
?"IYUV":
489 overlay
->format
==SDL_YUY2_OVERLAY
?"YUY2":
490 overlay
->format
==SDL_UYVY_OVERLAY
?"UYVY":
491 overlay
->format
==SDL_YVYU_OVERLAY
?"YVYU":
494 for(i
=0; i
<overlay
->planes
; i
++)
496 printf(" plane %d: pitch=%d\n", i
, overlay
->pitches
[i
]);
501 overlayrect
.w
=MOOSEPIC_W
*scale
;
502 overlayrect
.h
=MOOSEPIC_H
*scale
;
504 /* set the start frame */
508 /* Ignore key up events, they don't even get filtered */
509 SDL_EventState(SDL_KEYUP
, SDL_IGNORE
);
511 lastftick
=SDL_GetTicks();
513 /* Loop, waiting for QUIT or RESIZE */
516 if (SDL_PollEvent(&event
))
520 case SDL_VIDEORESIZE
:
521 screen
=SDL_SetVideoMode(event
.resize
.w
, event
.resize
.h
, 0, SDL_RESIZABLE
| SDL_SWSURFACE
);
522 overlayrect
.w
=event
.resize
.w
;
523 overlayrect
.h
=event
.resize
.h
;
529 case SDL_MOUSEBUTTONDOWN
:
530 overlayrect
.x
= event
.button
.x
- overlayrect
.w
/2;
531 overlayrect
.y
= event
.button
.y
- overlayrect
.h
/2;
534 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
539 if (event
.key
.keysym
.sym
!= SDLK_ESCAPE
)
544 SDL_FreeYUVOverlay(overlay
);
545 for (i
=0; i
<MOOSEFRAMES_COUNT
; i
++)
547 SDL_FreeSurface(MooseFrame
[i
]);
553 if ((!paused
)||(resized
))
555 if (((SDL_GetTicks()-lastftick
)>fpsdelay
)||(resized
))
557 lastftick
=SDL_GetTicks();
559 switch (overlay_format
)
561 case SDL_YUY2_OVERLAY
:
562 ConvertRGBtoYUY2(MooseFrame
[i
], overlay
, 0, 100);
564 case SDL_YV12_OVERLAY
:
565 ConvertRGBtoYV12(MooseFrame
[i
], overlay
, 0, 100);
567 case SDL_UYVY_OVERLAY
:
568 ConvertRGBtoUYVY(MooseFrame
[i
], overlay
, 0, 100);
570 case SDL_YVYU_OVERLAY
:
571 ConvertRGBtoYVYU(MooseFrame
[i
], overlay
, 0, 100);
573 case SDL_IYUV_OVERLAY
:
574 ConvertRGBtoIYUV(MooseFrame
[i
], overlay
, 0, 100);
578 SDL_DisplayYUVOverlay(overlay
, &overlayrect
);
593 /* kind of timeslice to OS */