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