Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
571 lines (517 loc) · 16.2 KB

testoverlay.c

File metadata and controls

571 lines (517 loc) · 16.2 KB
 
Apr 8, 2011
Apr 8, 2011
1
2
3
4
5
6
7
8
9
10
11
/*
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
12
13
14
15
16
17
18
19
20
21
22
/* Bring up a window and play with it */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BENCHMARK_SDL
#define NOTICE(X) printf("%s", X);
Jan 20, 2003
Jan 20, 2003
23
24
25
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
26
27
28
29
30
#include "SDL.h"
SDL_Surface *screen, *pic;
SDL_Overlay *overlay;
int scale;
Jan 20, 2003
Jan 20, 2003
31
32
33
int monochrome;
int luminance;
int w, h;
Sep 28, 2005
Sep 28, 2005
35
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
Jul 10, 2006
Jul 10, 2006
36
37
static void
quit(int rc)
Sep 28, 2005
Sep 28, 2005
38
{
Jul 10, 2006
Jul 10, 2006
39
40
SDL_Quit();
exit(rc);
Sep 28, 2005
Sep 28, 2005
41
42
}
43
44
45
46
/* NOTE: These RGB conversion functions are not intended for speed,
only as examples.
*/
Jul 10, 2006
Jul 10, 2006
47
48
void
RGBtoYUV(Uint8 * rgb, int *yuv, int monochrome, int luminance)
Jul 10, 2006
Jul 10, 2006
50
51
if (monochrome) {
#if 1 /* these are the two formulas that I found on the FourCC site... */
Sep 16, 2010
Sep 16, 2010
52
yuv[0] = (int)(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]);
Jan 20, 2003
Jan 20, 2003
53
54
yuv[1] = 128;
yuv[2] = 128;
55
#else
Sep 16, 2010
Sep 16, 2010
56
yuv[0] = (int)((0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16);
Jan 20, 2003
Jan 20, 2003
57
58
yuv[1] = 128;
yuv[2] = 128;
59
#endif
Jul 10, 2006
Jul 10, 2006
60
61
} else {
#if 1 /* these are the two formulas that I found on the FourCC site... */
Sep 16, 2010
Sep 16, 2010
62
63
64
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);
Jan 20, 2003
Jan 20, 2003
65
#else
Sep 16, 2010
Sep 16, 2010
66
67
68
yuv[0] = (int)((0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16);
yuv[1] = (int)(128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]));
yuv[2] = (int)(128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]));
Jan 20, 2003
Jan 20, 2003
69
70
71
#endif
}
Jul 10, 2006
Jul 10, 2006
72
73
74
75
if (luminance != 100) {
yuv[0] = yuv[0] * luminance / 100;
if (yuv[0] > 255)
yuv[0] = 255;
Jan 20, 2003
Jan 20, 2003
76
77
78
79
}
/* clamp values...if you need to, we don't seem to have a need */
/*
Jul 10, 2006
Jul 10, 2006
80
81
82
83
84
85
86
87
for(i=0;i<3;i++)
{
if(yuv[i]<0)
yuv[i]=0;
if(yuv[i]>255)
yuv[i]=255;
}
*/
Jul 10, 2006
Jul 10, 2006
90
91
92
void
ConvertRGBtoYV12(SDL_Surface * s, SDL_Overlay * o, int monochrome,
int luminance)
Jul 10, 2006
Jul 10, 2006
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
int x, y;
int yuv[3];
Uint8 *p, *op[3];
SDL_LockSurface(s);
SDL_LockYUVOverlay(o);
/* Black initialization */
/*
memset(o->pixels[0],0,o->pitches[0]*o->h);
memset(o->pixels[1],128,o->pitches[1]*((o->h+1)/2));
memset(o->pixels[2],128,o->pitches[2]*((o->h+1)/2));
*/
/* 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);
127
128
}
Jul 10, 2006
Jul 10, 2006
129
130
131
void
ConvertRGBtoIYUV(SDL_Surface * s, SDL_Overlay * o, int monochrome,
int luminance)
Jul 10, 2006
Jul 10, 2006
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
int x, y;
int yuv[3];
Uint8 *p, *op[3];
SDL_LockSurface(s);
SDL_LockYUVOverlay(o);
/* Black initialization */
/*
memset(o->pixels[0],0,o->pitches[0]*o->h);
memset(o->pixels[1],128,o->pitches[1]*((o->h+1)/2));
memset(o->pixels[2],128,o->pitches[2]*((o->h+1)/2));
*/
/* 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);
166
167
}
Jul 10, 2006
Jul 10, 2006
168
169
170
void
ConvertRGBtoUYVY(SDL_Surface * s, SDL_Overlay * o, int monochrome,
int luminance)
Jul 10, 2006
Jul 10, 2006
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
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);
197
198
}
Jul 10, 2006
Jul 10, 2006
199
200
201
void
ConvertRGBtoYVYU(SDL_Surface * s, SDL_Overlay * o, int monochrome,
int luminance)
Jul 10, 2006
Jul 10, 2006
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
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);
230
231
}
Jul 10, 2006
Jul 10, 2006
232
233
234
void
ConvertRGBtoYUY2(SDL_Surface * s, SDL_Overlay * o, int monochrome,
int luminance)
Jul 10, 2006
Jul 10, 2006
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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);
263
264
}
Jul 10, 2006
Jul 10, 2006
265
266
void
Draw()
Jul 10, 2006
Jul 10, 2006
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
SDL_Rect rect;
int i;
int disp;
if (!scale) {
rect.w = overlay->w;
rect.h = overlay->h;
for (i = 0; i < h - rect.h && i < w - rect.w; i++) {
rect.x = i;
rect.y = i;
SDL_DisplayYUVOverlay(overlay, &rect);
}
} else {
rect.w = overlay->w / 2;
rect.h = overlay->h / 2;
rect.x = (w - rect.w) / 2;
rect.y = (h - rect.h) / 2;
disp = rect.y - 1;
for (i = 0; i < disp; i++) {
rect.w += 2;
rect.h += 2;
rect.x--;
rect.y--;
SDL_DisplayYUVOverlay(overlay, &rect);
}
}
printf("Displayed %d times.\n", i);
295
296
}
Jul 10, 2006
Jul 10, 2006
297
298
static void
PrintUsage(char *argv0)
Jan 20, 2003
Jan 20, 2003
299
{
Jul 10, 2006
Jul 10, 2006
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
fprintf(stderr, "Where 'arg' is one of:\n");
fprintf(stderr, " -delay <seconds>\n");
fprintf(stderr, " -width <pixels>\n");
fprintf(stderr, " -height <pixels>\n");
fprintf(stderr, " -bpp <bits>\n");
fprintf(stderr,
" -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
fprintf(stderr, " -hw\n");
fprintf(stderr, " -flip\n");
fprintf(stderr,
" -scale (test scaling features, from 50%% upto window size)\n");
fprintf(stderr, " -mono (use monochromatic RGB2YUV conversion)\n");
fprintf(stderr,
" -lum <perc> (use luminance correction during RGB2YUV conversion,\n");
fprintf(stderr,
" from 0%% to unlimited, normal is 100%%)\n");
fprintf(stderr, " -help (shows this help)\n");
fprintf(stderr, " -fullscreen (test overlay in fullscreen mode)\n");
Jan 20, 2003
Jan 20, 2003
319
320
}
Jul 10, 2006
Jul 10, 2006
321
322
int
main(int argc, char **argv)
Jul 10, 2006
Jul 10, 2006
324
325
326
327
328
329
char *argv0 = argv[0];
int flip;
int delay;
int desired_bpp;
Uint32 video_flags, overlay_format;
char *bmpfile;
330
#ifdef BENCHMARK_SDL
Jul 10, 2006
Jul 10, 2006
331
Uint32 then, now;
332
#endif
Jul 10, 2006
Jul 10, 2006
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
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
471
472
473
474
475
476
477
478
479
480
481
482
int i;
/* Set default options and check command-line */
flip = 0;
scale = 0;
monochrome = 0;
luminance = 100;
delay = 1;
w = WINDOW_WIDTH;
h = WINDOW_HEIGHT;
desired_bpp = 0;
video_flags = 0;
overlay_format = SDL_YV12_OVERLAY;
while (argc > 1) {
if (strcmp(argv[1], "-delay") == 0) {
if (argv[2]) {
delay = atoi(argv[2]);
argv += 2;
argc -= 2;
} else {
fprintf(stderr, "The -delay option requires an argument\n");
return (1);
}
} else if (strcmp(argv[1], "-width") == 0) {
if (argv[2] && ((w = atoi(argv[2])) > 0)) {
argv += 2;
argc -= 2;
} else {
fprintf(stderr, "The -width option requires an argument\n");
return (1);
}
} else if (strcmp(argv[1], "-height") == 0) {
if (argv[2] && ((h = atoi(argv[2])) > 0)) {
argv += 2;
argc -= 2;
} else {
fprintf(stderr, "The -height option requires an argument\n");
return (1);
}
} else if (strcmp(argv[1], "-bpp") == 0) {
if (argv[2]) {
desired_bpp = atoi(argv[2]);
argv += 2;
argc -= 2;
} else {
fprintf(stderr, "The -bpp option requires an argument\n");
return (1);
}
} else if (strcmp(argv[1], "-lum") == 0) {
if (argv[2]) {
luminance = atoi(argv[2]);
argv += 2;
argc -= 2;
} else {
fprintf(stderr, "The -lum option requires an argument\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\n",
argv[2]);
return (1);
}
argv += 2;
argc -= 2;
} else {
fprintf(stderr, "The -format option requires an argument\n");
return (1);
}
} else if (strcmp(argv[1], "-hw") == 0) {
video_flags |= SDL_HWSURFACE;
argv += 1;
argc -= 1;
} else if (strcmp(argv[1], "-flip") == 0) {
video_flags |= SDL_DOUBLEBUF;
argv += 1;
argc -= 1;
} else if (strcmp(argv[1], "-scale") == 0) {
scale = 1;
argv += 1;
argc -= 1;
} else if (strcmp(argv[1], "-mono") == 0) {
monochrome = 1;
argv += 1;
argc -= 1;
} else if ((strcmp(argv[1], "-help") == 0)
|| (strcmp(argv[1], "-h") == 0)) {
PrintUsage(argv0);
return (1);
} else if (strcmp(argv[1], "-fullscreen") == 0) {
video_flags |= SDL_FULLSCREEN;
argv += 1;
argc -= 1;
} else
break;
}
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return (1);
}
/* Initialize the display */
screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
if (screen == NULL) {
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
w, h, desired_bpp, SDL_GetError());
quit(1);
}
printf("Set%s %dx%dx%d mode\n",
screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
screen->w, screen->h, screen->format->BitsPerPixel);
printf("(video surface located in %s memory)\n",
(screen->flags & SDL_HWSURFACE) ? "video" : "system");
if (screen->flags & SDL_DOUBLEBUF) {
printf("Double-buffering enabled\n");
flip = 1;
}
/* Set the window manager title bar */
SDL_WM_SetCaption("SDL test overlay", "testoverlay");
/* Load picture */
bmpfile = (argv[1] ? argv[1] : "sample.bmp");
pic = SDL_LoadBMP(bmpfile);
if (pic == NULL) {
fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
quit(1);
}
/* Convert the picture to 32bits, for easy conversion */
{
SDL_Surface *newsurf;
SDL_PixelFormat format;
format.palette = NULL;
format.BitsPerPixel = 32;
format.BytesPerPixel = 4;
483
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
Jul 10, 2006
Jul 10, 2006
484
485
486
format.Rshift = 0;
format.Gshift = 8;
format.Bshift = 16;
487
#else
Jul 10, 2006
Jul 10, 2006
488
489
490
format.Rshift = 24;
format.Gshift = 16;
format.Bshift = 8;
491
#endif
Jul 10, 2006
Jul 10, 2006
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
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;
newsurf = SDL_ConvertSurface(pic, &format, SDL_SWSURFACE);
if (!newsurf) {
fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n",
SDL_GetError());
quit(1);
}
SDL_FreeSurface(pic);
pic = newsurf;
}
/* Create the overlay */
overlay = SDL_CreateYUVOverlay(pic->w, pic->h, overlay_format, screen);
if (overlay == NULL) {
fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
quit(1);
}
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]);
}
/* Convert to YUV, and draw to the overlay */
530
#ifdef BENCHMARK_SDL
Jul 10, 2006
Jul 10, 2006
531
then = SDL_GetTicks();
532
#endif
Jul 10, 2006
Jul 10, 2006
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
switch (overlay->format) {
case SDL_YV12_OVERLAY:
ConvertRGBtoYV12(pic, overlay, monochrome, luminance);
break;
case SDL_UYVY_OVERLAY:
ConvertRGBtoUYVY(pic, overlay, monochrome, luminance);
break;
case SDL_YVYU_OVERLAY:
ConvertRGBtoYVYU(pic, overlay, monochrome, luminance);
break;
case SDL_YUY2_OVERLAY:
ConvertRGBtoYUY2(pic, overlay, monochrome, luminance);
break;
case SDL_IYUV_OVERLAY:
ConvertRGBtoIYUV(pic, overlay, monochrome, luminance);
break;
default:
printf("cannot convert RGB picture to obtained YUV format!\n");
quit(1);
break;
}
554
#ifdef BENCHMARK_SDL
Jul 10, 2006
Jul 10, 2006
555
556
now = SDL_GetTicks();
printf("Conversion Time: %d milliseconds\n", now - then);
557
#endif
Jul 10, 2006
Jul 10, 2006
558
559
/* Do all the drawing work */
560
#ifdef BENCHMARK_SDL
Jul 10, 2006
Jul 10, 2006
561
then = SDL_GetTicks();
562
#endif
Jul 10, 2006
Jul 10, 2006
563
Draw();
564
#ifdef BENCHMARK_SDL
Jul 10, 2006
Jul 10, 2006
565
566
now = SDL_GetTicks();
printf("Time: %d milliseconds\n", now - then);
567
#endif
Jul 10, 2006
Jul 10, 2006
568
569
570
SDL_Delay(delay * 1000);
SDL_Quit();
return (0);