comparison test/testoverlay.c @ 569:e8063c656626

Date: Thu, 16 Jan 2003 13:48:31 +0200 From: "Mike Gorchak" added help and options -mono to force monochrome RGB2YUB conversion and -lum <percent> - luminance of image during conversion. Also rewritten code which moves overlay into window. Now it depends on window size. Also fully rewritten -scale option, image now scaling from 50% from center of screen until border of window is reached - it really tests scaler, old -scale test doesn't test downscale.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 20 Jan 2003 01:36:28 +0000
parents 255c7ee077cb
children 2e726be3dc08
comparison
equal deleted inserted replaced
568:0cd6b268193b 569:e8063c656626
7 7
8 #define BENCHMARK_SDL 8 #define BENCHMARK_SDL
9 9
10 #define NOTICE(X) printf("%s", X); 10 #define NOTICE(X) printf("%s", X);
11 11
12 #define WINDOW_WIDTH 640
13 #define WINDOW_HEIGHT 480
14
12 #include "SDL.h" 15 #include "SDL.h"
13 16
14 SDL_Surface *screen, *pic; 17 SDL_Surface *screen, *pic;
15 SDL_Overlay *overlay; 18 SDL_Overlay *overlay;
16 int scale; 19 int scale;
20 int monochrome;
21 int luminance;
22 int w, h;
17 23
18 /* NOTE: These RGB conversion functions are not intended for speed, 24 /* NOTE: These RGB conversion functions are not intended for speed,
19 only as examples. 25 only as examples.
20 */ 26 */
21 27
22 void RGBtoYUV(Uint8 *rgb, int *yuv) 28 void RGBtoYUV(Uint8 *rgb, int *yuv, int monochrome, int luminance)
23 { 29 {
24 int i; 30 int i;
31
32 if (monochrome)
33 {
25 #if 1 /* these are the two formulas that I found on the FourCC site... */ 34 #if 1 /* these are the two formulas that I found on the FourCC site... */
26 yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2]; 35 yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
27 yuv[1] = (rgb[2]-yuv[0])*0.565 + 128; 36 yuv[1] = 128;
28 yuv[2] = (rgb[0]-yuv[0])*0.713 + 128; 37 yuv[2] = 128;
29 #else 38 #else
30 yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16; 39 yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
31 yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]); 40 yuv[1] = 128;
32 yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]); 41 yuv[2] = 128;
33 #endif 42 #endif
34 /* clamp values...if you need to, we don't seem to have a need */ 43 }
35 /* 44 else
36 for(i=0;i<3;i++) 45 {
37 { 46 #if 1 /* these are the two formulas that I found on the FourCC site... */
38 if(yuv[i]<0) 47 yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
39 yuv[i]=0; 48 yuv[1] = (rgb[2]-yuv[0])*0.565 + 128;
40 if(yuv[i]>255) 49 yuv[2] = (rgb[0]-yuv[0])*0.713 + 128;
41 yuv[i]=255; 50 #else
42 } 51 yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
43 */ 52 yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
44 } 53 yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
45 54 #endif
46 ConvertRGBtoYV12(SDL_Surface *s, SDL_Overlay *o) 55 }
56
57 if (luminance!=100)
58 {
59 yuv[0]=yuv[0]*luminance/100;
60 if (yuv[0]>255)
61 yuv[0]=255;
62 }
63
64 /* clamp values...if you need to, we don't seem to have a need */
65 /*
66 for(i=0;i<3;i++)
67 {
68 if(yuv[i]<0)
69 yuv[i]=0;
70 if(yuv[i]>255)
71 yuv[i]=255;
72 }
73 */
74 }
75
76 ConvertRGBtoYV12(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
47 { 77 {
48 int x,y; 78 int x,y;
49 int yuv[3]; 79 int yuv[3];
50 Uint8 *p,*op[3]; 80 Uint8 *p,*op[3];
51 81
66 op[0]=o->pixels[0]+o->pitches[0]*y; 96 op[0]=o->pixels[0]+o->pitches[0]*y;
67 op[1]=o->pixels[1]+o->pitches[1]*(y/2); 97 op[1]=o->pixels[1]+o->pitches[1]*(y/2);
68 op[2]=o->pixels[2]+o->pitches[2]*(y/2); 98 op[2]=o->pixels[2]+o->pitches[2]*(y/2);
69 for(x=0; x<s->w && x<o->w; x++) 99 for(x=0; x<s->w && x<o->w; x++)
70 { 100 {
71 RGBtoYUV(p,yuv); 101 RGBtoYUV(p, yuv, monochrome, luminance);
72 *(op[0]++)=yuv[0]; 102 *(op[0]++)=yuv[0];
73 if(x%2==0 && y%2==0) 103 if(x%2==0 && y%2==0)
74 { 104 {
75 *(op[1]++)=yuv[2]; 105 *(op[1]++)=yuv[2];
76 *(op[2]++)=yuv[1]; 106 *(op[2]++)=yuv[1];
81 111
82 SDL_UnlockYUVOverlay(o); 112 SDL_UnlockYUVOverlay(o);
83 SDL_UnlockSurface(s); 113 SDL_UnlockSurface(s);
84 } 114 }
85 115
86 ConvertRGBtoIYUV(SDL_Surface *s, SDL_Overlay *o) 116 ConvertRGBtoIYUV(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
87 { 117 {
88 int x,y; 118 int x,y;
89 int yuv[3]; 119 int yuv[3];
90 Uint8 *p,*op[3]; 120 Uint8 *p,*op[3];
91 121
106 op[0]=o->pixels[0]+o->pitches[0]*y; 136 op[0]=o->pixels[0]+o->pitches[0]*y;
107 op[1]=o->pixels[1]+o->pitches[1]*(y/2); 137 op[1]=o->pixels[1]+o->pitches[1]*(y/2);
108 op[2]=o->pixels[2]+o->pitches[2]*(y/2); 138 op[2]=o->pixels[2]+o->pitches[2]*(y/2);
109 for(x=0; x<s->w && x<o->w; x++) 139 for(x=0; x<s->w && x<o->w; x++)
110 { 140 {
111 RGBtoYUV(p,yuv); 141 RGBtoYUV(p,yuv, monochrome, luminance);
112 *(op[0]++)=yuv[0]; 142 *(op[0]++)=yuv[0];
113 if(x%2==0 && y%2==0) 143 if(x%2==0 && y%2==0)
114 { 144 {
115 *(op[1]++)=yuv[1]; 145 *(op[1]++)=yuv[1];
116 *(op[2]++)=yuv[2]; 146 *(op[2]++)=yuv[2];
121 151
122 SDL_UnlockYUVOverlay(o); 152 SDL_UnlockYUVOverlay(o);
123 SDL_UnlockSurface(s); 153 SDL_UnlockSurface(s);
124 } 154 }
125 155
126 ConvertRGBtoUYVY(SDL_Surface *s, SDL_Overlay *o) 156 ConvertRGBtoUYVY(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
127 { 157 {
128 int x,y; 158 int x,y;
129 int yuv[3]; 159 int yuv[3];
130 Uint8 *p,*op; 160 Uint8 *p,*op;
131 161
136 { 166 {
137 p=s->pixels+s->pitch*y; 167 p=s->pixels+s->pitch*y;
138 op=o->pixels[0]+o->pitches[0]*y; 168 op=o->pixels[0]+o->pitches[0]*y;
139 for(x=0; x<s->w && x<o->w; x++) 169 for(x=0; x<s->w && x<o->w; x++)
140 { 170 {
141 RGBtoYUV(p,yuv); 171 RGBtoYUV(p, yuv, monochrome, luminance);
142 if(x%2==0) 172 if(x%2==0)
143 { 173 {
144 *(op++)=yuv[1]; 174 *(op++)=yuv[1];
145 *(op++)=yuv[0]; 175 *(op++)=yuv[0];
146 *(op++)=yuv[2]; 176 *(op++)=yuv[2];
154 184
155 SDL_UnlockYUVOverlay(o); 185 SDL_UnlockYUVOverlay(o);
156 SDL_UnlockSurface(s); 186 SDL_UnlockSurface(s);
157 } 187 }
158 188
159 ConvertRGBtoYVYU(SDL_Surface *s, SDL_Overlay *o) 189 ConvertRGBtoYVYU(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
160 { 190 {
161 int x,y; 191 int x,y;
162 int yuv[3]; 192 int yuv[3];
163 Uint8 *p,*op; 193 Uint8 *p,*op;
164 194
169 { 199 {
170 p=s->pixels+s->pitch*y; 200 p=s->pixels+s->pitch*y;
171 op=o->pixels[0]+o->pitches[0]*y; 201 op=o->pixels[0]+o->pitches[0]*y;
172 for(x=0; x<s->w && x<o->w; x++) 202 for(x=0; x<s->w && x<o->w; x++)
173 { 203 {
174 RGBtoYUV(p,yuv); 204 RGBtoYUV(p,yuv, monochrome, luminance);
175 if(x%2==0) 205 if(x%2==0)
176 { 206 {
177 *(op++)=yuv[0]; 207 *(op++)=yuv[0];
178 *(op++)=yuv[2]; 208 *(op++)=yuv[2];
179 op[1]=yuv[1]; 209 op[1]=yuv[1];
190 220
191 SDL_UnlockYUVOverlay(o); 221 SDL_UnlockYUVOverlay(o);
192 SDL_UnlockSurface(s); 222 SDL_UnlockSurface(s);
193 } 223 }
194 224
195 ConvertRGBtoYUY2(SDL_Surface *s, SDL_Overlay *o) 225 ConvertRGBtoYUY2(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance)
196 { 226 {
197 int x,y; 227 int x,y;
198 int yuv[3]; 228 int yuv[3];
199 Uint8 *p,*op; 229 Uint8 *p,*op;
200 230
205 { 235 {
206 p=s->pixels+s->pitch*y; 236 p=s->pixels+s->pitch*y;
207 op=o->pixels[0]+o->pitches[0]*y; 237 op=o->pixels[0]+o->pitches[0]*y;
208 for(x=0; x<s->w && x<o->w; x++) 238 for(x=0; x<s->w && x<o->w; x++)
209 { 239 {
210 RGBtoYUV(p,yuv); 240 RGBtoYUV(p,yuv, monochrome, luminance);
211 if(x%2==0) 241 if(x%2==0)
212 { 242 {
213 *(op++)=yuv[0]; 243 *(op++)=yuv[0];
214 *(op++)=yuv[1]; 244 *(op++)=yuv[1];
215 op[1]=yuv[2]; 245 op[1]=yuv[2];
230 260
231 void Draw() 261 void Draw()
232 { 262 {
233 SDL_Rect rect; 263 SDL_Rect rect;
234 int i; 264 int i;
265 int disp;
235 266
236 if(!scale) 267 if(!scale)
237 { 268 {
238 rect.w=overlay->w; 269 rect.w=overlay->w;
239 rect.h=overlay->h; 270 rect.h=overlay->h;
240 for(i=0; i<200; i++) 271 for(i=0; i<h-rect.h && i<w-rect.w; i++)
241 { 272 {
242 rect.x=i; 273 rect.x=i;
243 rect.y=i; 274 rect.y=i;
244 SDL_DisplayYUVOverlay(overlay,&rect); 275 SDL_DisplayYUVOverlay(overlay,&rect);
245 } 276 }
246 } 277 }
247 else 278 else
248 { 279 {
249 rect.w=screen->w; 280 rect.w=overlay->w/2;
250 rect.h=screen->h; 281 rect.h=overlay->h/2;
251 for(i=0; i<200; i++) 282 rect.x=(w-rect.w)/2;
252 { 283 rect.y=(h-rect.h)/2;
253 rect.x=i-199; 284 disp=rect.y-1;
254 rect.y=i-199; 285 for(i=0; i<disp; i++)
286 {
287 rect.w+=2;
288 rect.h+=2;
289 rect.x--;
290 rect.y--;
255 SDL_DisplayYUVOverlay(overlay,&rect); 291 SDL_DisplayYUVOverlay(overlay,&rect);
256 } 292 }
257 } 293 }
258 printf("Displayed %d times.\n",i); 294 printf("Displayed %d times.\n",i);
259 } 295 }
260 296
297 static void PrintUsage(char *argv0)
298 {
299 fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
300 fprintf(stderr, "Where 'arg' is one of:\n");
301 fprintf(stderr, " -delay <seconds>\n");
302 fprintf(stderr, " -width <pixels>\n");
303 fprintf(stderr, " -height <pixels>\n");
304 fprintf(stderr, " -bpp <bits>\n");
305 fprintf(stderr, " -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
306 fprintf(stderr, " -hw\n");
307 fprintf(stderr, " -flip\n");
308 fprintf(stderr, " -scale (test scaling features, from 50%% upto window size)\n");
309 fprintf(stderr, " -mono (use monochromatic RGB2YUV conversion)\n");
310 fprintf(stderr, " -lum <perc> (use luminance correction during RGB2YUV conversion,\n");
311 fprintf(stderr, " from 0%% to unlimited, normal is 100%%)\n");
312 fprintf(stderr, " -help (shows this help)\n");
313 fprintf(stderr, " -fullscreen (test overlay in fullscreen mode)\n");
314 }
315
261 int main(int argc, char **argv) 316 int main(int argc, char **argv)
262 { 317 {
263 int flip; 318 int flip;
264 int w, h;
265 int delay; 319 int delay;
266 int desired_bpp; 320 int desired_bpp;
267 Uint32 video_flags, overlay_format; 321 Uint32 video_flags, overlay_format;
268 char *bmpfile; 322 char *bmpfile;
269 #ifdef BENCHMARK_SDL 323 #ifdef BENCHMARK_SDL
272 int i; 326 int i;
273 327
274 /* Set default options and check command-line */ 328 /* Set default options and check command-line */
275 flip = 0; 329 flip = 0;
276 scale=0; 330 scale=0;
331 monochrome=0;
332 luminance=100;
277 delay = 1; 333 delay = 1;
278 w = 640; 334 w = WINDOW_WIDTH;
279 h = 480; 335 h = WINDOW_HEIGHT;
280 desired_bpp = 0; 336 desired_bpp = 0;
281 video_flags = 0; 337 video_flags = 0;
282 overlay_format = SDL_YV12_OVERLAY; 338 overlay_format = SDL_YV12_OVERLAY;
283 339
284 while ( argc > 1 ) { 340 while ( argc > 1 ) {
319 argv += 2; 375 argv += 2;
320 argc -= 2; 376 argc -= 2;
321 } else { 377 } else {
322 fprintf(stderr, 378 fprintf(stderr,
323 "The -bpp option requires an argument\n"); 379 "The -bpp option requires an argument\n");
380 exit(1);
381 }
382 } else
383 if ( strcmp(argv[1], "-lum") == 0 ) {
384 if ( argv[2] ) {
385 luminance = atoi(argv[2]);
386 argv += 2;
387 argc -= 2;
388 } else {
389 fprintf(stderr,
390 "The -lum option requires an argument\n");
324 exit(1); 391 exit(1);
325 } 392 }
326 } else 393 } else
327 if ( strcmp(argv[1], "-format") == 0 ) { 394 if ( strcmp(argv[1], "-format") == 0 ) {
328 if ( argv[2] ) { 395 if ( argv[2] ) {
362 if ( strcmp(argv[1], "-scale") == 0 ) { 429 if ( strcmp(argv[1], "-scale") == 0 ) {
363 scale = 1; 430 scale = 1;
364 argv += 1; 431 argv += 1;
365 argc -= 1; 432 argc -= 1;
366 } else 433 } else
434 if ( strcmp(argv[1], "-mono") == 0 ) {
435 monochrome = 1;
436 argv += 1;
437 argc -= 1;
438 } else
439 if (( strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) {
440 PrintUsage(argv[0]);
441 exit(1);
442 } else
367 if ( strcmp(argv[1], "-fullscreen") == 0 ) { 443 if ( strcmp(argv[1], "-fullscreen") == 0 ) {
368 video_flags |= SDL_FULLSCREEN; 444 video_flags |= SDL_FULLSCREEN;
369 argv += 1; 445 argv += 1;
370 argc -= 1; 446 argc -= 1;
371 } else 447 } else
471 then = SDL_GetTicks(); 547 then = SDL_GetTicks();
472 #endif 548 #endif
473 switch(overlay->format) 549 switch(overlay->format)
474 { 550 {
475 case SDL_YV12_OVERLAY: 551 case SDL_YV12_OVERLAY:
476 ConvertRGBtoYV12(pic,overlay); 552 ConvertRGBtoYV12(pic,overlay,monochrome,luminance);
477 break; 553 break;
478 case SDL_UYVY_OVERLAY: 554 case SDL_UYVY_OVERLAY:
479 ConvertRGBtoUYVY(pic,overlay); 555 ConvertRGBtoUYVY(pic,overlay,monochrome,luminance);
480 break; 556 break;
481 case SDL_YVYU_OVERLAY: 557 case SDL_YVYU_OVERLAY:
482 ConvertRGBtoYVYU(pic,overlay); 558 ConvertRGBtoYVYU(pic,overlay,monochrome,luminance);
483 break; 559 break;
484 case SDL_YUY2_OVERLAY: 560 case SDL_YUY2_OVERLAY:
485 ConvertRGBtoYUY2(pic,overlay); 561 ConvertRGBtoYUY2(pic,overlay,monochrome,luminance);
486 break; 562 break;
487 case SDL_IYUV_OVERLAY: 563 case SDL_IYUV_OVERLAY:
488 ConvertRGBtoIYUV(pic,overlay); 564 ConvertRGBtoIYUV(pic,overlay,monochrome,luminance);
489 break; 565 break;
490 default: 566 default:
491 printf("cannot convert RGB picture to obtained YUV format!\n"); 567 printf("cannot convert RGB picture to obtained YUV format!\n");
492 exit(1); 568 exit(1);
493 break; 569 break;