Skip to content

Latest commit

 

History

History
587 lines (527 loc) · 16.3 KB

testoverlay2.c

File metadata and controls

587 lines (527 loc) · 16.3 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/********************************************************************************
* *
* Test of the overlay used for moved pictures, test more closed to real life. *
* Running trojan moose :) Coded by Mike Gorchak. *
* *
********************************************************************************/
#include "SDL.h"
#define MOOSEPIC_W 64
#define MOOSEPIC_H 88
#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
#define MOOSEFRAMES_COUNT 10
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}
};
/* 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.
*/
void RGBtoYUV(Uint8 *rgb, int *yuv, int monochrome, int luminance)
{
int i;
if (monochrome)
{
#if 1 /* these are the two formulas that I found on the FourCC site... */
yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
yuv[1] = 128;
yuv[2] = 128;
#else
yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
yuv[1] = 128;
yuv[2] = 128;
#endif
}
else
{
#if 1 /* these are the two formulas that I found on the FourCC site... */
yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
yuv[1] = (rgb[2]-yuv[0])*0.565 + 128;
yuv[2] = (rgb[0]-yuv[0])*0.713 + 128;
#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
}
if (luminance!=100)
{
yuv[0]=yuv[0]*luminance/100;
if (yuv[0]>255)
yuv[0]=255;
}
}
ConvertRGBtoYV12(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
{
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[2];
*(op[2]++)=yuv[1];
}
p+=s->format->BytesPerPixel;
}
}
SDL_UnlockYUVOverlay(o);
SDL_UnlockSurface(s);
}
ConvertRGBtoIYUV(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
{
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);
}
ConvertRGBtoUYVY(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
{
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);
}
ConvertRGBtoYVYU(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
{
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);
}
ConvertRGBtoYUY2(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
{
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);
}
static void PrintUsage(char *argv0)
{
fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
May 6, 2004
May 6, 2004
257
258
259
fprintf(stderr, "\n");
fprintf(stderr, "Where 'arg' is any of the following options:\n");
fprintf(stderr, "\n");
260
261
262
263
264
fprintf(stderr, " -fps <frames per second>\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");
fprintf(stderr, "\n");
May 6, 2004
May 6, 2004
265
fprintf(stderr, "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
266
267
268
269
270
271
272
273
274
275
276
277
278
279
fprintf(stderr, "\n");
}
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;
Dec 10, 2003
Dec 10, 2003
280
int resized=0;
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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 */
Aug 8, 2003
Aug 8, 2003
386
handle=SDL_RWFromFile("moose.dat", "rb");
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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);
Aug 8, 2003
Aug 8, 2003
406
/* Set video mode */
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
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);
May 6, 2004
May 6, 2004
471
472
473
474
475
if (!overlay)
{
fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
return 7;
}
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
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;
Dec 10, 2003
Dec 10, 2003
516
517
518
519
if (paused)
{
resized=1;
}
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
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;
}
}
Dec 10, 2003
Dec 10, 2003
541
if ((!paused)||(resized))
542
{
Dec 10, 2003
Dec 10, 2003
543
if (((SDL_GetTicks()-lastftick)>fpsdelay)||(resized))
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
{
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);
Dec 10, 2003
Dec 10, 2003
567
568
569
570
571
572
573
574
575
if (!resized)
{
i++;
if (i==10)
{
i=0;
}
}
else
576
{
Dec 10, 2003
Dec 10, 2003
577
resized=0;
578
579
580
581
582
583
584
585
586
}
}
}
/* kind of timeslice to OS */
SDL_Delay(1);
}
return 0;
}