/[pcsx2_0.9.7]/trunk/3rdparty/SDL-1.3.0-5387/test/testoverlay2.c
ViewVC logotype

Contents of /trunk/3rdparty/SDL-1.3.0-5387/test/testoverlay2.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22