comparison test/testoverlay2.c @ 672:5955fa16d4b8

Date: Mon, 28 Jul 2003 18:30:50 +0300 From: "Mike Gorchak" Subject: New test for overlays While writting QNX Photon Overlay code I've found that the existent overlay test application is not enough, it performs too artificial tests - it always moves/resizes the overlay's viewport, but in the real life that happens very rare. So I decided to write new test (commonly base on testoverlay.c code) and called it testoverlay2 :) moose.dat - raw 8 bit paletted data of the small movie. (It was a moose.gif, also included in the attachment as reference, but not needed for this test, it is just for information). I cannot find, who created this .gif, so no credits this time :) But over inet I saw this gif zillion times, so I think we do not infringing somebody rights :)
author Sam Lantinga <slouken@libsdl.org>
date Fri, 08 Aug 2003 21:31:18 +0000
parents
children 63419da96d13
comparison
equal deleted inserted replaced
671:338a62f9a528 672:5955fa16d4b8
1 /********************************************************************************
2 * *
3 * Test of the overlay used for moved pictures, test more closed to real life. *
4 * Running trojan moose :) Coded by Mike Gorchak. *
5 * *
6 ********************************************************************************/
7
8 #include "SDL.h"
9
10 #define MOOSEPIC_W 64
11 #define MOOSEPIC_H 88
12
13 #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
14 #define MOOSEFRAMES_COUNT 10
15
16 SDL_Color MooseColors[84]={
17 { 49, 49, 49}, { 66, 24, 0}, { 66, 33, 0}, { 66, 66, 66},
18 { 66, 115, 49}, { 74, 33, 0}, { 74, 41, 16}, { 82, 33, 8},
19 { 82, 41, 8}, { 82, 49, 16}, { 82, 82, 82}, { 90, 41, 8},
20 { 90, 41, 16}, { 90, 57, 24}, { 99, 49, 16}, { 99, 66, 24},
21 { 99, 66, 33}, { 99, 74, 33}, {107, 57, 24}, {107, 82, 41},
22 {115, 57, 33}, {115, 66, 33}, {115, 66, 41}, {115, 74, 0},
23 {115, 90, 49}, {115, 115, 115}, {123, 82, 0}, {123, 99, 57},
24 {132, 66, 41}, {132, 74, 41}, {132, 90, 8}, {132, 99, 33},
25 {132, 99, 66}, {132, 107, 66}, {140, 74, 49}, {140, 99, 16},
26 {140, 107, 74}, {140, 115, 74}, {148, 107, 24}, {148, 115, 82},
27 {148, 123, 74}, {148, 123, 90}, {156, 115, 33}, {156, 115, 90},
28 {156, 123, 82}, {156, 132, 82}, {156, 132, 99}, {156, 156, 156},
29 {165, 123, 49}, {165, 123, 90}, {165, 132, 82}, {165, 132, 90},
30 {165, 132, 99}, {165, 140, 90}, {173, 132, 57}, {173, 132, 99},
31 {173, 140, 107}, {173, 140, 115}, {173, 148, 99}, {173, 173, 173},
32 {181, 140, 74}, {181, 148, 115}, {181, 148, 123}, {181, 156, 107},
33 {189, 148, 123}, {189, 156, 82}, {189, 156, 123}, {189, 156, 132},
34 {189, 189, 189}, {198, 156, 123}, {198, 165, 132}, {206, 165, 99},
35 {206, 165, 132}, {206, 173, 140}, {206, 206, 206}, {214, 173, 115},
36 {214, 173, 140}, {222, 181, 148}, {222, 189, 132}, {222, 189, 156},
37 {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173}
38 };
39
40 /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
41
42 /* NOTE: These RGB conversion functions are not intended for speed,
43 only as examples.
44 */
45
46 void RGBtoYUV(Uint8 *rgb, int *yuv, int monochrome, int luminance)
47 {
48 int i;
49
50 if (monochrome)
51 {
52 #if 1 /* these are the two formulas that I found on the FourCC site... */
53 yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
54 yuv[1] = 128;
55 yuv[2] = 128;
56 #else
57 yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
58 yuv[1] = 128;
59 yuv[2] = 128;
60 #endif
61 }
62 else
63 {
64 #if 1 /* these are the two formulas that I found on the FourCC site... */
65 yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
66 yuv[1] = (rgb[2]-yuv[0])*0.565 + 128;
67 yuv[2] = (rgb[0]-yuv[0])*0.713 + 128;
68 #else
69 yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
70 yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
71 yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
72 #endif
73 }
74
75 if (luminance!=100)
76 {
77 yuv[0]=yuv[0]*luminance/100;
78 if (yuv[0]>255)
79 yuv[0]=255;
80 }
81 }
82
83 ConvertRGBtoYV12(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
84 {
85 int x,y;
86 int yuv[3];
87 Uint8 *p,*op[3];
88
89 SDL_LockSurface(s);
90 SDL_LockYUVOverlay(o);
91
92 /* Convert */
93 for(y=0; y<s->h && y<o->h; y++)
94 {
95 p=((Uint8 *) s->pixels)+s->pitch*y;
96 op[0]=o->pixels[0]+o->pitches[0]*y;
97 op[1]=o->pixels[1]+o->pitches[1]*(y/2);
98 op[2]=o->pixels[2]+o->pitches[2]*(y/2);
99 for(x=0; x<s->w && x<o->w; x++)
100 {
101 RGBtoYUV(p, yuv, monochrome, luminance);
102 *(op[0]++)=yuv[0];
103 if(x%2==0 && y%2==0)
104 {
105 *(op[1]++)=yuv[2];
106 *(op[2]++)=yuv[1];
107 }
108 p+=s->format->BytesPerPixel;
109 }
110 }
111
112 SDL_UnlockYUVOverlay(o);
113 SDL_UnlockSurface(s);
114 }
115
116 ConvertRGBtoIYUV(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
117 {
118 int x,y;
119 int yuv[3];
120 Uint8 *p,*op[3];
121
122 SDL_LockSurface(s);
123 SDL_LockYUVOverlay(o);
124
125 /* Convert */
126 for(y=0; y<s->h && y<o->h; y++)
127 {
128 p=((Uint8 *) s->pixels)+s->pitch*y;
129 op[0]=o->pixels[0]+o->pitches[0]*y;
130 op[1]=o->pixels[1]+o->pitches[1]*(y/2);
131 op[2]=o->pixels[2]+o->pitches[2]*(y/2);
132 for(x=0; x<s->w && x<o->w; x++)
133 {
134 RGBtoYUV(p,yuv, monochrome, luminance);
135 *(op[0]++)=yuv[0];
136 if(x%2==0 && y%2==0)
137 {
138 *(op[1]++)=yuv[1];
139 *(op[2]++)=yuv[2];
140 }
141 p+=s->format->BytesPerPixel;
142 }
143 }
144
145 SDL_UnlockYUVOverlay(o);
146 SDL_UnlockSurface(s);
147 }
148
149 ConvertRGBtoUYVY(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
150 {
151 int x,y;
152 int yuv[3];
153 Uint8 *p,*op;
154
155 SDL_LockSurface(s);
156 SDL_LockYUVOverlay(o);
157
158 for(y=0; y<s->h && y<o->h; y++)
159 {
160 p=((Uint8 *) s->pixels)+s->pitch*y;
161 op=o->pixels[0]+o->pitches[0]*y;
162 for(x=0; x<s->w && x<o->w; x++)
163 {
164 RGBtoYUV(p, yuv, monochrome, luminance);
165 if(x%2==0)
166 {
167 *(op++)=yuv[1];
168 *(op++)=yuv[0];
169 *(op++)=yuv[2];
170 }
171 else
172 *(op++)=yuv[0];
173
174 p+=s->format->BytesPerPixel;
175 }
176 }
177
178 SDL_UnlockYUVOverlay(o);
179 SDL_UnlockSurface(s);
180 }
181
182 ConvertRGBtoYVYU(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
183 {
184 int x,y;
185 int yuv[3];
186 Uint8 *p,*op;
187
188 SDL_LockSurface(s);
189 SDL_LockYUVOverlay(o);
190
191 for(y=0; y<s->h && y<o->h; y++)
192 {
193 p=((Uint8 *) s->pixels)+s->pitch*y;
194 op=o->pixels[0]+o->pitches[0]*y;
195 for(x=0; x<s->w && x<o->w; x++)
196 {
197 RGBtoYUV(p,yuv, monochrome, luminance);
198 if(x%2==0)
199 {
200 *(op++)=yuv[0];
201 *(op++)=yuv[2];
202 op[1]=yuv[1];
203 }
204 else
205 {
206 *op=yuv[0];
207 op+=2;
208 }
209
210 p+=s->format->BytesPerPixel;
211 }
212 }
213
214 SDL_UnlockYUVOverlay(o);
215 SDL_UnlockSurface(s);
216 }
217
218 ConvertRGBtoYUY2(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
219 {
220 int x,y;
221 int yuv[3];
222 Uint8 *p,*op;
223
224 SDL_LockSurface(s);
225 SDL_LockYUVOverlay(o);
226
227 for(y=0; y<s->h && y<o->h; y++)
228 {
229 p=((Uint8 *) s->pixels)+s->pitch*y;
230 op=o->pixels[0]+o->pitches[0]*y;
231 for(x=0; x<s->w && x<o->w; x++)
232 {
233 RGBtoYUV(p,yuv, monochrome, luminance);
234 if(x%2==0)
235 {
236 *(op++)=yuv[0];
237 *(op++)=yuv[1];
238 op[1]=yuv[2];
239 }
240 else
241 {
242 *op=yuv[0];
243 op+=2;
244 }
245
246 p+=s->format->BytesPerPixel;
247 }
248 }
249
250 SDL_UnlockYUVOverlay(o);
251 SDL_UnlockSurface(s);
252 }
253
254 void Draw()
255 {
256 }
257
258 static void PrintUsage(char *argv0)
259 {
260 fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
261 fprintf(stderr, "Where 'arg' is one of:\n");
262 fprintf(stderr, " -fps <frames per second>\n");
263 fprintf(stderr, " -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
264 fprintf(stderr, " -scale <scale factor> (initial scale of the overlay)\n");
265 fprintf(stderr, " -help (shows this help)\n");
266 fprintf(stderr, "\n");
267 fprintf(stderr, " Press ESC to exit, or SPACE to freeze the movie while application running.\n");
268 fprintf(stderr, "\n");
269 }
270
271 int main(int argc, char **argv)
272 {
273 Uint8* RawMooseData;
274 SDL_RWops* handle;
275 SDL_Surface* screen;
276 SDL_Surface* MooseFrame[MOOSEFRAMES_COUNT];
277 SDL_Overlay* overlay;
278 SDL_Rect overlayrect;
279 SDL_Event event;
280 Uint32 lastftick;
281 int paused=0;
282 int i;
283 int fps=12;
284 int fpsdelay;
285 int overlay_format=SDL_YUY2_OVERLAY;
286 int scale=5;
287
288 while ( argc > 1 )
289 {
290 if (strcmp(argv[1], "-fps")== 0)
291 {
292 if (argv[2])
293 {
294 fps = atoi(argv[2]);
295 if (fps==0)
296 {
297 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
298 return -1;
299 }
300 if ((fps<0) || (fps>1000))
301 {
302 fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n");
303 return -1;
304 }
305 argv += 2;
306 argc -= 2;
307 }
308 else
309 {
310 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
311 return -1;
312 }
313 } else
314 if (strcmp(argv[1], "-format") == 0)
315 {
316 if (argv[2])
317 {
318 if (!strcmp(argv[2],"YV12"))
319 overlay_format = SDL_YV12_OVERLAY;
320 else if(!strcmp(argv[2],"IYUV"))
321 overlay_format = SDL_IYUV_OVERLAY;
322 else if(!strcmp(argv[2],"YUY2"))
323 overlay_format = SDL_YUY2_OVERLAY;
324 else if(!strcmp(argv[2],"UYVY"))
325 overlay_format = SDL_UYVY_OVERLAY;
326 else if(!strcmp(argv[2],"YVYU"))
327 overlay_format = SDL_YVYU_OVERLAY;
328 else
329 {
330 fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]);
331 return -1;
332 }
333 argv += 2;
334 argc -= 2;
335 }
336 else
337 {
338 fprintf(stderr, "The -format option requires an argument, default is YUY2.\n");
339 return -1;
340 }
341 } else
342 if (strcmp(argv[1], "-scale") == 0)
343 {
344 if (argv[2])
345 {
346 scale = atoi(argv[2]);
347 if (scale==0)
348 {
349 fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n");
350 return -1;
351 }
352 if ((scale<0) || (scale>50))
353 {
354 fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n");
355 return -1;
356 }
357 argv += 2;
358 argc -= 2;
359 }
360 else
361 {
362 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
363 return -1;
364 }
365 } else
366 if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0))
367 {
368 PrintUsage(argv[0]);
369 return 0;
370 } else
371 {
372 fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
373 return -1;
374 }
375 break;
376 }
377
378 RawMooseData=(Uint8*)malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
379 if (RawMooseData==NULL)
380 {
381 fprintf(stderr, "Can't allocate memory for movie !\n");
382 free(RawMooseData);
383 return 1;
384 }
385
386 /* load the trojan moose images */
387 handle=SDL_RWFromFile("moose.dat", "r");
388 if (handle==NULL)
389 {
390 fprintf(stderr, "Can't find the file moose.dat !\n");
391 free(RawMooseData);
392 return 2;
393 }
394
395 SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
396
397 SDL_RWclose(handle);
398
399 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
400 {
401 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
402 free(RawMooseData);
403 return 3;
404 }
405 atexit(SDL_Quit);
406
407 /* Set 640x480 video mode */
408 if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL )
409 {
410 fprintf(stderr, "Couldn't set video mode: %s\n", 0, SDL_GetError());
411 free(RawMooseData);
412 return 4;
413 }
414
415 /* Set the window manager title bar */
416 SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2");
417
418 for (i=0; i<MOOSEFRAMES_COUNT; i++)
419 {
420 MooseFrame[i]=SDL_CreateRGBSurfaceFrom(RawMooseData+i*MOOSEFRAME_SIZE, MOOSEPIC_W,
421 MOOSEPIC_H, 8, MOOSEPIC_W, 0, 0, 0, 0);
422 if (MooseFrame[i]==NULL)
423 {
424 fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", 0, SDL_GetError());
425 free(RawMooseData);
426 return 5;
427 }
428 SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);
429
430 {
431 SDL_Surface *newsurf;
432 SDL_PixelFormat format;
433
434 format.palette=NULL;
435 format.BitsPerPixel=32;
436 format.BytesPerPixel=4;
437 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
438 format.Rshift=0;
439 format.Gshift=8;
440 format.Bshift=16;
441 #else
442 format.Rshift=24;
443 format.Gshift=16;
444 format.Bshift=8;
445 #endif
446 format.Ashift=0;
447 format.Rmask=0xff<<format.Rshift;
448 format.Gmask=0xff<<format.Gshift;
449 format.Bmask=0xff<<format.Bshift;
450 format.Amask=0;
451 format.Rloss=0;
452 format.Gloss=0;
453 format.Bloss=0;
454 format.Aloss=8;
455 format.colorkey=0;
456 format.alpha=0;
457
458 newsurf=SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE);
459 if(!newsurf)
460 {
461 fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError());
462 return 6;
463 }
464 SDL_FreeSurface(MooseFrame[i]);
465 MooseFrame[i]=newsurf;
466 }
467 }
468
469 free(RawMooseData);
470
471 overlay=SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen);
472
473 printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
474 overlay->hw_overlay?"hardware":"software",
475 overlay->format==SDL_YV12_OVERLAY?"YV12":
476 overlay->format==SDL_IYUV_OVERLAY?"IYUV":
477 overlay->format==SDL_YUY2_OVERLAY?"YUY2":
478 overlay->format==SDL_UYVY_OVERLAY?"UYVY":
479 overlay->format==SDL_YVYU_OVERLAY?"YVYU":
480 "Unknown");
481
482 for(i=0; i<overlay->planes; i++)
483 {
484 printf(" plane %d: pitch=%d\n", i, overlay->pitches[i]);
485 }
486
487 overlayrect.x=0;
488 overlayrect.y=0;
489 overlayrect.w=MOOSEPIC_W*scale;
490 overlayrect.h=MOOSEPIC_H*scale;
491
492 /* set the start frame */
493 i=0;
494 fpsdelay=1000/fps;
495
496 /* Ignore key up events, they don't even get filtered */
497 SDL_EventState(SDL_KEYUP, SDL_IGNORE);
498
499 lastftick=SDL_GetTicks();
500
501 /* Loop, waiting for QUIT or RESIZE */
502 while (1)
503 {
504 if (SDL_PollEvent(&event))
505 {
506 switch (event.type)
507 {
508 case SDL_VIDEORESIZE:
509 screen=SDL_SetVideoMode(event.resize.w, event.resize.h, 0, SDL_RESIZABLE | SDL_SWSURFACE);
510 overlayrect.w=event.resize.w;
511 overlayrect.h=event.resize.h;
512 break;
513 case SDL_KEYDOWN:
514 if (event.key.keysym.sym == SDLK_SPACE)
515 {
516 paused=!paused;
517 break;
518 }
519 if (event.key.keysym.sym != SDLK_ESCAPE)
520 {
521 break;
522 }
523 case SDL_QUIT:
524 SDL_FreeYUVOverlay(overlay);
525 for (i=0; i<MOOSEFRAMES_COUNT; i++)
526 {
527 SDL_FreeSurface(MooseFrame[i]);
528 }
529 return 0;
530 }
531 }
532
533 if (!paused)
534 {
535 if ((SDL_GetTicks()-lastftick)>fpsdelay)
536 {
537 lastftick=SDL_GetTicks();
538
539 switch (overlay_format)
540 {
541 case SDL_YUY2_OVERLAY:
542 ConvertRGBtoYUY2(MooseFrame[i], overlay, 0, 100);
543 break;
544 case SDL_YV12_OVERLAY:
545 ConvertRGBtoYV12(MooseFrame[i], overlay, 0, 100);
546 break;
547 case SDL_UYVY_OVERLAY:
548 ConvertRGBtoUYVY(MooseFrame[i], overlay, 0, 100);
549 break;
550 case SDL_YVYU_OVERLAY:
551 ConvertRGBtoYVYU(MooseFrame[i], overlay, 0, 100);
552 break;
553 case SDL_IYUV_OVERLAY:
554 ConvertRGBtoIYUV(MooseFrame[i], overlay, 0, 100);
555 break;
556 }
557
558 SDL_DisplayYUVOverlay(overlay, &overlayrect);
559 i++;
560 if (i==10)
561 {
562 i=0;
563 }
564 }
565 }
566 /* kind of timeslice to OS */
567 SDL_Delay(1);
568 }
569
570 return 0;
571 }
572