Mercurial > sdl-ios-xcode
comparison test/testoverlay2.c @ 1895:c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 10 Jul 2006 21:04:37 +0000 |
parents | f1211a4b7380 |
children | c785543d1843 |
comparison
equal
deleted
inserted
replaced
1894:c69cee13dd76 | 1895:c121d94672cb |
---|---|
15 #define MOOSEPIC_H 88 | 15 #define MOOSEPIC_H 88 |
16 | 16 |
17 #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H) | 17 #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H) |
18 #define MOOSEFRAMES_COUNT 10 | 18 #define MOOSEFRAMES_COUNT 10 |
19 | 19 |
20 SDL_Color MooseColors[84]={ | 20 SDL_Color MooseColors[84] = { |
21 { 49, 49, 49}, { 66, 24, 0}, { 66, 33, 0}, { 66, 66, 66}, | 21 {49, 49, 49} |
22 { 66, 115, 49}, { 74, 33, 0}, { 74, 41, 16}, { 82, 33, 8}, | 22 , {66, 24, 0} |
23 { 82, 41, 8}, { 82, 49, 16}, { 82, 82, 82}, { 90, 41, 8}, | 23 , {66, 33, 0} |
24 { 90, 41, 16}, { 90, 57, 24}, { 99, 49, 16}, { 99, 66, 24}, | 24 , {66, 66, 66} |
25 { 99, 66, 33}, { 99, 74, 33}, {107, 57, 24}, {107, 82, 41}, | 25 , |
26 {115, 57, 33}, {115, 66, 33}, {115, 66, 41}, {115, 74, 0}, | 26 {66, 115, 49} |
27 {115, 90, 49}, {115, 115, 115}, {123, 82, 0}, {123, 99, 57}, | 27 , {74, 33, 0} |
28 {132, 66, 41}, {132, 74, 41}, {132, 90, 8}, {132, 99, 33}, | 28 , {74, 41, 16} |
29 {132, 99, 66}, {132, 107, 66}, {140, 74, 49}, {140, 99, 16}, | 29 , {82, 33, 8} |
30 {140, 107, 74}, {140, 115, 74}, {148, 107, 24}, {148, 115, 82}, | 30 , |
31 {148, 123, 74}, {148, 123, 90}, {156, 115, 33}, {156, 115, 90}, | 31 {82, 41, 8} |
32 {156, 123, 82}, {156, 132, 82}, {156, 132, 99}, {156, 156, 156}, | 32 , {82, 49, 16} |
33 {165, 123, 49}, {165, 123, 90}, {165, 132, 82}, {165, 132, 90}, | 33 , {82, 82, 82} |
34 {165, 132, 99}, {165, 140, 90}, {173, 132, 57}, {173, 132, 99}, | 34 , {90, 41, 8} |
35 {173, 140, 107}, {173, 140, 115}, {173, 148, 99}, {173, 173, 173}, | 35 , |
36 {181, 140, 74}, {181, 148, 115}, {181, 148, 123}, {181, 156, 107}, | 36 {90, 41, 16} |
37 {189, 148, 123}, {189, 156, 82}, {189, 156, 123}, {189, 156, 132}, | 37 , {90, 57, 24} |
38 {189, 189, 189}, {198, 156, 123}, {198, 165, 132}, {206, 165, 99}, | 38 , {99, 49, 16} |
39 {206, 165, 132}, {206, 173, 140}, {206, 206, 206}, {214, 173, 115}, | 39 , {99, 66, 24} |
40 {214, 173, 140}, {222, 181, 148}, {222, 189, 132}, {222, 189, 156}, | 40 , |
41 {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173} | 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} | |
42 }; | 125 }; |
43 | 126 |
44 | 127 |
45 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ | 128 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
46 static void quit(int rc) | 129 static void |
47 { | 130 quit(int rc) |
48 SDL_Quit(); | 131 { |
49 exit(rc); | 132 SDL_Quit(); |
133 exit(rc); | |
50 } | 134 } |
51 | 135 |
52 /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */ | 136 /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */ |
53 | 137 |
54 /* NOTE: These RGB conversion functions are not intended for speed, | 138 /* NOTE: These RGB conversion functions are not intended for speed, |
55 only as examples. | 139 only as examples. |
56 */ | 140 */ |
57 | 141 |
58 void RGBtoYUV(Uint8 *rgb, int *yuv, int monochrome, int luminance) | 142 void |
59 { | 143 RGBtoYUV(Uint8 * rgb, int *yuv, int monochrome, int luminance) |
60 if (monochrome) | 144 { |
61 { | 145 if (monochrome) { |
62 #if 1 /* these are the two formulas that I found on the FourCC site... */ | 146 #if 1 /* these are the two formulas that I found on the FourCC site... */ |
63 yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2]; | 147 yuv[0] = 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]; |
64 yuv[1] = 128; | 148 yuv[1] = 128; |
65 yuv[2] = 128; | 149 yuv[2] = 128; |
66 #else | 150 #else |
67 yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16; | 151 yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16; |
68 yuv[1] = 128; | 152 yuv[1] = 128; |
69 yuv[2] = 128; | 153 yuv[2] = 128; |
70 #endif | 154 #endif |
71 } | 155 } else { |
72 else | 156 #if 1 /* these are the two formulas that I found on the FourCC site... */ |
73 { | 157 yuv[0] = 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]; |
74 #if 1 /* these are the two formulas that I found on the FourCC site... */ | 158 yuv[1] = (rgb[2] - yuv[0]) * 0.565 + 128; |
75 yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2]; | 159 yuv[2] = (rgb[0] - yuv[0]) * 0.713 + 128; |
76 yuv[1] = (rgb[2]-yuv[0])*0.565 + 128; | |
77 yuv[2] = (rgb[0]-yuv[0])*0.713 + 128; | |
78 #else | 160 #else |
79 yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16; | 161 yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16; |
80 yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]); | 162 yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]); |
81 yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]); | 163 yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]); |
82 #endif | 164 #endif |
83 } | 165 } |
84 | 166 |
85 if (luminance!=100) | 167 if (luminance != 100) { |
86 { | 168 yuv[0] = yuv[0] * luminance / 100; |
87 yuv[0]=yuv[0]*luminance/100; | 169 if (yuv[0] > 255) |
88 if (yuv[0]>255) | 170 yuv[0] = 255; |
89 yuv[0]=255; | 171 } |
90 } | 172 } |
91 } | 173 |
92 | 174 void |
93 void ConvertRGBtoYV12(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance) | 175 ConvertRGBtoYV12(SDL_Surface * s, SDL_Overlay * o, int monochrome, |
94 { | 176 int luminance) |
95 int x,y; | 177 { |
96 int yuv[3]; | 178 int x, y; |
97 Uint8 *p,*op[3]; | 179 int yuv[3]; |
98 | 180 Uint8 *p, *op[3]; |
99 SDL_LockSurface(s); | 181 |
100 SDL_LockYUVOverlay(o); | 182 SDL_LockSurface(s); |
101 | 183 SDL_LockYUVOverlay(o); |
102 /* Convert */ | 184 |
103 for(y=0; y<s->h && y<o->h; y++) | 185 /* Convert */ |
104 { | 186 for (y = 0; y < s->h && y < o->h; y++) { |
105 p=((Uint8 *) s->pixels)+s->pitch*y; | 187 p = ((Uint8 *) s->pixels) + s->pitch * y; |
106 op[0]=o->pixels[0]+o->pitches[0]*y; | 188 op[0] = o->pixels[0] + o->pitches[0] * y; |
107 op[1]=o->pixels[1]+o->pitches[1]*(y/2); | 189 op[1] = o->pixels[1] + o->pitches[1] * (y / 2); |
108 op[2]=o->pixels[2]+o->pitches[2]*(y/2); | 190 op[2] = o->pixels[2] + o->pitches[2] * (y / 2); |
109 for(x=0; x<s->w && x<o->w; x++) | 191 for (x = 0; x < s->w && x < o->w; x++) { |
110 { | 192 RGBtoYUV(p, yuv, monochrome, luminance); |
111 RGBtoYUV(p, yuv, monochrome, luminance); | 193 *(op[0]++) = yuv[0]; |
112 *(op[0]++)=yuv[0]; | 194 if (x % 2 == 0 && y % 2 == 0) { |
113 if(x%2==0 && y%2==0) | 195 *(op[1]++) = yuv[2]; |
114 { | 196 *(op[2]++) = yuv[1]; |
115 *(op[1]++)=yuv[2]; | 197 } |
116 *(op[2]++)=yuv[1]; | 198 p += s->format->BytesPerPixel; |
117 } | 199 } |
118 p+=s->format->BytesPerPixel; | 200 } |
119 } | 201 |
120 } | 202 SDL_UnlockYUVOverlay(o); |
121 | 203 SDL_UnlockSurface(s); |
122 SDL_UnlockYUVOverlay(o); | 204 } |
123 SDL_UnlockSurface(s); | 205 |
124 } | 206 void |
125 | 207 ConvertRGBtoIYUV(SDL_Surface * s, SDL_Overlay * o, int monochrome, |
126 void ConvertRGBtoIYUV(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance) | 208 int luminance) |
127 { | 209 { |
128 int x,y; | 210 int x, y; |
129 int yuv[3]; | 211 int yuv[3]; |
130 Uint8 *p,*op[3]; | 212 Uint8 *p, *op[3]; |
131 | 213 |
132 SDL_LockSurface(s); | 214 SDL_LockSurface(s); |
133 SDL_LockYUVOverlay(o); | 215 SDL_LockYUVOverlay(o); |
134 | 216 |
135 /* Convert */ | 217 /* Convert */ |
136 for(y=0; y<s->h && y<o->h; y++) | 218 for (y = 0; y < s->h && y < o->h; y++) { |
137 { | 219 p = ((Uint8 *) s->pixels) + s->pitch * y; |
138 p=((Uint8 *) s->pixels)+s->pitch*y; | 220 op[0] = o->pixels[0] + o->pitches[0] * y; |
139 op[0]=o->pixels[0]+o->pitches[0]*y; | 221 op[1] = o->pixels[1] + o->pitches[1] * (y / 2); |
140 op[1]=o->pixels[1]+o->pitches[1]*(y/2); | 222 op[2] = o->pixels[2] + o->pitches[2] * (y / 2); |
141 op[2]=o->pixels[2]+o->pitches[2]*(y/2); | 223 for (x = 0; x < s->w && x < o->w; x++) { |
142 for(x=0; x<s->w && x<o->w; x++) | 224 RGBtoYUV(p, yuv, monochrome, luminance); |
143 { | 225 *(op[0]++) = yuv[0]; |
144 RGBtoYUV(p,yuv, monochrome, luminance); | 226 if (x % 2 == 0 && y % 2 == 0) { |
145 *(op[0]++)=yuv[0]; | 227 *(op[1]++) = yuv[1]; |
146 if(x%2==0 && y%2==0) | 228 *(op[2]++) = yuv[2]; |
147 { | 229 } |
148 *(op[1]++)=yuv[1]; | 230 p += s->format->BytesPerPixel; |
149 *(op[2]++)=yuv[2]; | 231 } |
150 } | 232 } |
151 p+=s->format->BytesPerPixel; | 233 |
152 } | 234 SDL_UnlockYUVOverlay(o); |
153 } | 235 SDL_UnlockSurface(s); |
154 | 236 } |
155 SDL_UnlockYUVOverlay(o); | 237 |
156 SDL_UnlockSurface(s); | 238 void |
157 } | 239 ConvertRGBtoUYVY(SDL_Surface * s, SDL_Overlay * o, int monochrome, |
158 | 240 int luminance) |
159 void ConvertRGBtoUYVY(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance) | 241 { |
160 { | 242 int x, y; |
161 int x,y; | 243 int yuv[3]; |
162 int yuv[3]; | 244 Uint8 *p, *op; |
163 Uint8 *p,*op; | 245 |
164 | 246 SDL_LockSurface(s); |
165 SDL_LockSurface(s); | 247 SDL_LockYUVOverlay(o); |
166 SDL_LockYUVOverlay(o); | 248 |
167 | 249 for (y = 0; y < s->h && y < o->h; y++) { |
168 for(y=0; y<s->h && y<o->h; y++) | 250 p = ((Uint8 *) s->pixels) + s->pitch * y; |
169 { | 251 op = o->pixels[0] + o->pitches[0] * y; |
170 p=((Uint8 *) s->pixels)+s->pitch*y; | 252 for (x = 0; x < s->w && x < o->w; x++) { |
171 op=o->pixels[0]+o->pitches[0]*y; | 253 RGBtoYUV(p, yuv, monochrome, luminance); |
172 for(x=0; x<s->w && x<o->w; x++) | 254 if (x % 2 == 0) { |
173 { | 255 *(op++) = yuv[1]; |
174 RGBtoYUV(p, yuv, monochrome, luminance); | 256 *(op++) = yuv[0]; |
175 if(x%2==0) | 257 *(op++) = yuv[2]; |
176 { | 258 } else |
177 *(op++)=yuv[1]; | 259 *(op++) = yuv[0]; |
178 *(op++)=yuv[0]; | 260 |
179 *(op++)=yuv[2]; | 261 p += s->format->BytesPerPixel; |
180 } | 262 } |
181 else | 263 } |
182 *(op++)=yuv[0]; | 264 |
183 | 265 SDL_UnlockYUVOverlay(o); |
184 p+=s->format->BytesPerPixel; | 266 SDL_UnlockSurface(s); |
185 } | 267 } |
186 } | 268 |
187 | 269 void |
188 SDL_UnlockYUVOverlay(o); | 270 ConvertRGBtoYVYU(SDL_Surface * s, SDL_Overlay * o, int monochrome, |
189 SDL_UnlockSurface(s); | 271 int luminance) |
190 } | 272 { |
191 | 273 int x, y; |
192 void ConvertRGBtoYVYU(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance) | 274 int yuv[3]; |
193 { | 275 Uint8 *p, *op; |
194 int x,y; | 276 |
195 int yuv[3]; | 277 SDL_LockSurface(s); |
196 Uint8 *p,*op; | 278 SDL_LockYUVOverlay(o); |
197 | 279 |
198 SDL_LockSurface(s); | 280 for (y = 0; y < s->h && y < o->h; y++) { |
199 SDL_LockYUVOverlay(o); | 281 p = ((Uint8 *) s->pixels) + s->pitch * y; |
200 | 282 op = o->pixels[0] + o->pitches[0] * y; |
201 for(y=0; y<s->h && y<o->h; y++) | 283 for (x = 0; x < s->w && x < o->w; x++) { |
202 { | 284 RGBtoYUV(p, yuv, monochrome, luminance); |
203 p=((Uint8 *) s->pixels)+s->pitch*y; | 285 if (x % 2 == 0) { |
204 op=o->pixels[0]+o->pitches[0]*y; | 286 *(op++) = yuv[0]; |
205 for(x=0; x<s->w && x<o->w; x++) | 287 *(op++) = yuv[2]; |
206 { | 288 op[1] = yuv[1]; |
207 RGBtoYUV(p,yuv, monochrome, luminance); | 289 } else { |
208 if(x%2==0) | 290 *op = yuv[0]; |
209 { | 291 op += 2; |
210 *(op++)=yuv[0]; | 292 } |
211 *(op++)=yuv[2]; | 293 |
212 op[1]=yuv[1]; | 294 p += s->format->BytesPerPixel; |
213 } | 295 } |
214 else | 296 } |
215 { | 297 |
216 *op=yuv[0]; | 298 SDL_UnlockYUVOverlay(o); |
217 op+=2; | 299 SDL_UnlockSurface(s); |
218 } | 300 } |
219 | 301 |
220 p+=s->format->BytesPerPixel; | 302 void |
221 } | 303 ConvertRGBtoYUY2(SDL_Surface * s, SDL_Overlay * o, int monochrome, |
222 } | 304 int luminance) |
223 | 305 { |
224 SDL_UnlockYUVOverlay(o); | 306 int x, y; |
225 SDL_UnlockSurface(s); | 307 int yuv[3]; |
226 } | 308 Uint8 *p, *op; |
227 | 309 |
228 void ConvertRGBtoYUY2(SDL_Surface *s, SDL_Overlay *o, int monochrome, int luminance) | 310 SDL_LockSurface(s); |
229 { | 311 SDL_LockYUVOverlay(o); |
230 int x,y; | 312 |
231 int yuv[3]; | 313 for (y = 0; y < s->h && y < o->h; y++) { |
232 Uint8 *p,*op; | 314 p = ((Uint8 *) s->pixels) + s->pitch * y; |
233 | 315 op = o->pixels[0] + o->pitches[0] * y; |
234 SDL_LockSurface(s); | 316 for (x = 0; x < s->w && x < o->w; x++) { |
235 SDL_LockYUVOverlay(o); | 317 RGBtoYUV(p, yuv, monochrome, luminance); |
236 | 318 if (x % 2 == 0) { |
237 for(y=0; y<s->h && y<o->h; y++) | 319 *(op++) = yuv[0]; |
238 { | 320 *(op++) = yuv[1]; |
239 p=((Uint8 *) s->pixels)+s->pitch*y; | 321 op[1] = yuv[2]; |
240 op=o->pixels[0]+o->pitches[0]*y; | 322 } else { |
241 for(x=0; x<s->w && x<o->w; x++) | 323 *op = yuv[0]; |
242 { | 324 op += 2; |
243 RGBtoYUV(p,yuv, monochrome, luminance); | 325 } |
244 if(x%2==0) | 326 |
245 { | 327 p += s->format->BytesPerPixel; |
246 *(op++)=yuv[0]; | 328 } |
247 *(op++)=yuv[1]; | 329 } |
248 op[1]=yuv[2]; | 330 |
249 } | 331 SDL_UnlockYUVOverlay(o); |
250 else | 332 SDL_UnlockSurface(s); |
251 { | 333 } |
252 *op=yuv[0]; | 334 |
253 op+=2; | 335 static void |
254 } | 336 PrintUsage(char *argv0) |
255 | |
256 p+=s->format->BytesPerPixel; | |
257 } | |
258 } | |
259 | |
260 SDL_UnlockYUVOverlay(o); | |
261 SDL_UnlockSurface(s); | |
262 } | |
263 | |
264 static void PrintUsage(char *argv0) | |
265 { | 337 { |
266 fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0); | 338 fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0); |
267 fprintf(stderr, "\n"); | 339 fprintf(stderr, "\n"); |
268 fprintf(stderr, "Where 'arg' is any of the following options:\n"); | 340 fprintf(stderr, "Where 'arg' is any of the following options:\n"); |
269 fprintf(stderr, "\n"); | 341 fprintf(stderr, "\n"); |
270 fprintf(stderr, " -fps <frames per second>\n"); | 342 fprintf(stderr, " -fps <frames per second>\n"); |
271 fprintf(stderr, " -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n"); | 343 fprintf(stderr, |
272 fprintf(stderr, " -scale <scale factor> (initial scale of the overlay)\n"); | 344 " -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n"); |
345 fprintf(stderr, | |
346 " -scale <scale factor> (initial scale of the overlay)\n"); | |
273 fprintf(stderr, " -help (shows this help)\n"); | 347 fprintf(stderr, " -help (shows this help)\n"); |
274 fprintf(stderr, "\n"); | 348 fprintf(stderr, "\n"); |
275 fprintf(stderr, "Press ESC to exit, or SPACE to freeze the movie while application running.\n"); | 349 fprintf(stderr, |
350 "Press ESC to exit, or SPACE to freeze the movie while application running.\n"); | |
276 fprintf(stderr, "\n"); | 351 fprintf(stderr, "\n"); |
277 } | 352 } |
278 | 353 |
279 int main(int argc, char **argv) | 354 int |
280 { | 355 main(int argc, char **argv) |
281 Uint8* RawMooseData; | 356 { |
282 SDL_RWops* handle; | 357 Uint8 *RawMooseData; |
283 SDL_Surface* screen; | 358 SDL_RWops *handle; |
284 SDL_Surface* MooseFrame[MOOSEFRAMES_COUNT]; | 359 SDL_Surface *screen; |
285 SDL_Overlay* overlay; | 360 SDL_Surface *MooseFrame[MOOSEFRAMES_COUNT]; |
361 SDL_Overlay *overlay; | |
286 SDL_Rect overlayrect; | 362 SDL_Rect overlayrect; |
287 SDL_Event event; | 363 SDL_Event event; |
288 Uint32 lastftick; | 364 Uint32 lastftick; |
289 int paused=0; | 365 int paused = 0; |
290 int resized=0; | 366 int resized = 0; |
291 int i; | 367 int i; |
292 int fps=12; | 368 int fps = 12; |
293 int fpsdelay; | 369 int fpsdelay; |
294 int overlay_format=SDL_YUY2_OVERLAY; | 370 int overlay_format = SDL_YUY2_OVERLAY; |
295 int scale=5; | 371 int scale = 5; |
296 | 372 |
297 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) | 373 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) { |
298 { | |
299 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); | 374 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
300 return 3; | 375 return 3; |
301 } | 376 } |
302 | 377 |
303 while ( argc > 1 ) | 378 while (argc > 1) { |
304 { | 379 if (strcmp(argv[1], "-fps") == 0) { |
305 if (strcmp(argv[1], "-fps")== 0) | 380 if (argv[2]) { |
306 { | |
307 if (argv[2]) | |
308 { | |
309 fps = atoi(argv[2]); | 381 fps = atoi(argv[2]); |
310 if (fps==0) | 382 if (fps == 0) { |
311 { | 383 fprintf(stderr, |
312 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); | 384 "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); |
313 quit(10); | 385 quit(10); |
314 } | 386 } |
315 if ((fps<0) || (fps>1000)) | 387 if ((fps < 0) || (fps > 1000)) { |
316 { | 388 fprintf(stderr, |
317 fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n"); | 389 "The -fps option must be in range from 1 to 1000, default is 12.\n"); |
318 quit(10); | 390 quit(10); |
319 } | 391 } |
320 argv += 2; | 392 argv += 2; |
321 argc -= 2; | 393 argc -= 2; |
322 } | 394 } else { |
323 else | 395 fprintf(stderr, |
324 { | 396 "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); |
325 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); | |
326 quit(10); | 397 quit(10); |
327 } | 398 } |
328 } else | 399 } else if (strcmp(argv[1], "-format") == 0) { |
329 if (strcmp(argv[1], "-format") == 0) | 400 if (argv[2]) { |
330 { | 401 if (!strcmp(argv[2], "YV12")) |
331 if (argv[2]) | |
332 { | |
333 if (!strcmp(argv[2],"YV12")) | |
334 overlay_format = SDL_YV12_OVERLAY; | 402 overlay_format = SDL_YV12_OVERLAY; |
335 else if(!strcmp(argv[2],"IYUV")) | 403 else if (!strcmp(argv[2], "IYUV")) |
336 overlay_format = SDL_IYUV_OVERLAY; | 404 overlay_format = SDL_IYUV_OVERLAY; |
337 else if(!strcmp(argv[2],"YUY2")) | 405 else if (!strcmp(argv[2], "YUY2")) |
338 overlay_format = SDL_YUY2_OVERLAY; | 406 overlay_format = SDL_YUY2_OVERLAY; |
339 else if(!strcmp(argv[2],"UYVY")) | 407 else if (!strcmp(argv[2], "UYVY")) |
340 overlay_format = SDL_UYVY_OVERLAY; | 408 overlay_format = SDL_UYVY_OVERLAY; |
341 else if(!strcmp(argv[2],"YVYU")) | 409 else if (!strcmp(argv[2], "YVYU")) |
342 overlay_format = SDL_YVYU_OVERLAY; | 410 overlay_format = SDL_YVYU_OVERLAY; |
343 else | 411 else { |
344 { | 412 fprintf(stderr, |
345 fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]); | 413 "The -format option %s is not recognized, see help for info.\n", |
414 argv[2]); | |
346 quit(10); | 415 quit(10); |
347 } | 416 } |
348 argv += 2; | 417 argv += 2; |
349 argc -= 2; | 418 argc -= 2; |
350 } | 419 } else { |
351 else | 420 fprintf(stderr, |
352 { | 421 "The -format option requires an argument, default is YUY2.\n"); |
353 fprintf(stderr, "The -format option requires an argument, default is YUY2.\n"); | |
354 quit(10); | 422 quit(10); |
355 } | 423 } |
356 } else | 424 } else if (strcmp(argv[1], "-scale") == 0) { |
357 if (strcmp(argv[1], "-scale") == 0) | 425 if (argv[2]) { |
358 { | |
359 if (argv[2]) | |
360 { | |
361 scale = atoi(argv[2]); | 426 scale = atoi(argv[2]); |
362 if (scale==0) | 427 if (scale == 0) { |
363 { | 428 fprintf(stderr, |
364 fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n"); | 429 "The -scale option requires an argument [from 1 to 50], default is 5.\n"); |
365 quit(10); | 430 quit(10); |
366 } | 431 } |
367 if ((scale<0) || (scale>50)) | 432 if ((scale < 0) || (scale > 50)) { |
368 { | 433 fprintf(stderr, |
369 fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n"); | 434 "The -scale option must be in range from 1 to 50, default is 5.\n"); |
370 quit(10); | 435 quit(10); |
371 } | 436 } |
372 argv += 2; | 437 argv += 2; |
373 argc -= 2; | 438 argc -= 2; |
374 } | 439 } else { |
375 else | 440 fprintf(stderr, |
376 { | 441 "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); |
377 fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); | |
378 quit(10); | 442 quit(10); |
379 } | 443 } |
380 } else | 444 } else if ((strcmp(argv[1], "-help") == 0) |
381 if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) | 445 || (strcmp(argv[1], "-h") == 0)) { |
382 { | |
383 PrintUsage(argv[0]); | 446 PrintUsage(argv[0]); |
384 quit(0); | 447 quit(0); |
385 } else | 448 } else { |
386 { | |
387 fprintf(stderr, "Unrecognized option: %s.\n", argv[1]); | 449 fprintf(stderr, "Unrecognized option: %s.\n", argv[1]); |
388 quit(10); | 450 quit(10); |
389 } | 451 } |
390 break; | 452 break; |
391 } | 453 } |
392 | 454 |
393 RawMooseData=(Uint8*)malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT); | 455 RawMooseData = (Uint8 *) malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT); |
394 if (RawMooseData==NULL) | 456 if (RawMooseData == NULL) { |
395 { | |
396 fprintf(stderr, "Can't allocate memory for movie !\n"); | 457 fprintf(stderr, "Can't allocate memory for movie !\n"); |
397 free(RawMooseData); | 458 free(RawMooseData); |
398 quit(1); | 459 quit(1); |
399 } | 460 } |
400 | 461 |
401 /* load the trojan moose images */ | 462 /* load the trojan moose images */ |
402 handle=SDL_RWFromFile("moose.dat", "rb"); | 463 handle = SDL_RWFromFile("moose.dat", "rb"); |
403 if (handle==NULL) | 464 if (handle == NULL) { |
404 { | |
405 fprintf(stderr, "Can't find the file moose.dat !\n"); | 465 fprintf(stderr, "Can't find the file moose.dat !\n"); |
406 free(RawMooseData); | 466 free(RawMooseData); |
407 quit(2); | 467 quit(2); |
408 } | 468 } |
409 | 469 |
410 SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT); | 470 SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT); |
411 | 471 |
412 SDL_RWclose(handle); | 472 SDL_RWclose(handle); |
413 | 473 |
414 /* Set video mode */ | 474 /* Set video mode */ |
415 if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL ) | 475 if ((screen = |
416 { | 476 SDL_SetVideoMode(MOOSEPIC_W * scale, MOOSEPIC_H * scale, 0, |
477 SDL_RESIZABLE | SDL_SWSURFACE)) == NULL) { | |
417 fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError()); | 478 fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError()); |
418 free(RawMooseData); | 479 free(RawMooseData); |
419 quit(4); | 480 quit(4); |
420 } | 481 } |
421 | 482 |
422 /* Set the window manager title bar */ | 483 /* Set the window manager title bar */ |
423 SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2"); | 484 SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2"); |
424 | 485 |
425 for (i=0; i<MOOSEFRAMES_COUNT; i++) | 486 for (i = 0; i < MOOSEFRAMES_COUNT; i++) { |
426 { | 487 MooseFrame[i] = |
427 MooseFrame[i]=SDL_CreateRGBSurfaceFrom(RawMooseData+i*MOOSEFRAME_SIZE, MOOSEPIC_W, | 488 SDL_CreateRGBSurfaceFrom(RawMooseData + i * MOOSEFRAME_SIZE, |
428 MOOSEPIC_H, 8, MOOSEPIC_W, 0, 0, 0, 0); | 489 MOOSEPIC_W, MOOSEPIC_H, 8, MOOSEPIC_W, |
429 if (MooseFrame[i]==NULL) | 490 0, 0, 0, 0); |
430 { | 491 if (MooseFrame[i] == NULL) { |
431 fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", SDL_GetError()); | 492 fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", |
493 SDL_GetError()); | |
432 free(RawMooseData); | 494 free(RawMooseData); |
433 quit(5); | 495 quit(5); |
434 } | 496 } |
435 SDL_SetColors(MooseFrame[i], MooseColors, 0, 84); | 497 SDL_SetColors(MooseFrame[i], MooseColors, 0, 84); |
436 | 498 |
437 { | 499 { |
438 SDL_Surface *newsurf; | 500 SDL_Surface *newsurf; |
439 SDL_PixelFormat format; | 501 SDL_PixelFormat format; |
440 | 502 |
441 format.palette=NULL; | 503 format.palette = NULL; |
442 format.BitsPerPixel=32; | 504 format.BitsPerPixel = 32; |
443 format.BytesPerPixel=4; | 505 format.BytesPerPixel = 4; |
444 #if SDL_BYTEORDER == SDL_LIL_ENDIAN | 506 #if SDL_BYTEORDER == SDL_LIL_ENDIAN |
445 format.Rshift=0; | 507 format.Rshift = 0; |
446 format.Gshift=8; | 508 format.Gshift = 8; |
447 format.Bshift=16; | 509 format.Bshift = 16; |
448 #else | 510 #else |
449 format.Rshift=24; | 511 format.Rshift = 24; |
450 format.Gshift=16; | 512 format.Gshift = 16; |
451 format.Bshift=8; | 513 format.Bshift = 8; |
452 #endif | 514 #endif |
453 format.Ashift=0; | 515 format.Ashift = 0; |
454 format.Rmask=0xff<<format.Rshift; | 516 format.Rmask = 0xff << format.Rshift; |
455 format.Gmask=0xff<<format.Gshift; | 517 format.Gmask = 0xff << format.Gshift; |
456 format.Bmask=0xff<<format.Bshift; | 518 format.Bmask = 0xff << format.Bshift; |
457 format.Amask=0; | 519 format.Amask = 0; |
458 format.Rloss=0; | 520 format.Rloss = 0; |
459 format.Gloss=0; | 521 format.Gloss = 0; |
460 format.Bloss=0; | 522 format.Bloss = 0; |
461 format.Aloss=8; | 523 format.Aloss = 8; |
462 format.colorkey=0; | 524 format.colorkey = 0; |
463 format.alpha=0; | 525 format.alpha = 0; |
464 | 526 |
465 newsurf=SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE); | 527 newsurf = |
466 if(!newsurf) | 528 SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE); |
467 { | 529 if (!newsurf) { |
468 fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError()); | 530 fprintf(stderr, |
469 quit(6); | 531 "Couldn't convert picture to 32bits RGB: %s\n", |
470 } | 532 SDL_GetError()); |
471 SDL_FreeSurface(MooseFrame[i]); | 533 quit(6); |
472 MooseFrame[i]=newsurf; | 534 } |
473 } | 535 SDL_FreeSurface(MooseFrame[i]); |
536 MooseFrame[i] = newsurf; | |
537 } | |
474 } | 538 } |
475 | 539 |
476 free(RawMooseData); | 540 free(RawMooseData); |
477 | 541 |
478 overlay=SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen); | 542 overlay = |
479 if (!overlay) | 543 SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen); |
480 { | 544 if (!overlay) { |
481 fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError()); | 545 fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError()); |
482 quit(7); | 546 quit(7); |
483 } | 547 } |
484 | 548 |
485 printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes, | 549 printf("Created %dx%dx%d %s %s overlay\n", overlay->w, overlay->h, |
486 overlay->hw_overlay?"hardware":"software", | 550 overlay->planes, overlay->hw_overlay ? "hardware" : "software", |
487 overlay->format==SDL_YV12_OVERLAY?"YV12": | 551 overlay->format == SDL_YV12_OVERLAY ? "YV12" : overlay->format == |
488 overlay->format==SDL_IYUV_OVERLAY?"IYUV": | 552 SDL_IYUV_OVERLAY ? "IYUV" : overlay->format == |
489 overlay->format==SDL_YUY2_OVERLAY?"YUY2": | 553 SDL_YUY2_OVERLAY ? "YUY2" : overlay->format == |
490 overlay->format==SDL_UYVY_OVERLAY?"UYVY": | 554 SDL_UYVY_OVERLAY ? "UYVY" : overlay->format == |
491 overlay->format==SDL_YVYU_OVERLAY?"YVYU": | 555 SDL_YVYU_OVERLAY ? "YVYU" : "Unknown"); |
492 "Unknown"); | 556 |
493 | 557 for (i = 0; i < overlay->planes; i++) { |
494 for(i=0; i<overlay->planes; i++) | |
495 { | |
496 printf(" plane %d: pitch=%d\n", i, overlay->pitches[i]); | 558 printf(" plane %d: pitch=%d\n", i, overlay->pitches[i]); |
497 } | 559 } |
498 | 560 |
499 overlayrect.x=0; | 561 overlayrect.x = 0; |
500 overlayrect.y=0; | 562 overlayrect.y = 0; |
501 overlayrect.w=MOOSEPIC_W*scale; | 563 overlayrect.w = MOOSEPIC_W * scale; |
502 overlayrect.h=MOOSEPIC_H*scale; | 564 overlayrect.h = MOOSEPIC_H * scale; |
503 | 565 |
504 /* set the start frame */ | 566 /* set the start frame */ |
505 i=0; | 567 i = 0; |
506 fpsdelay=1000/fps; | 568 fpsdelay = 1000 / fps; |
507 | 569 |
508 /* Ignore key up events, they don't even get filtered */ | 570 /* Ignore key up events, they don't even get filtered */ |
509 SDL_EventState(SDL_KEYUP, SDL_IGNORE); | 571 SDL_EventState(SDL_KEYUP, SDL_IGNORE); |
510 | 572 |
511 lastftick=SDL_GetTicks(); | 573 lastftick = SDL_GetTicks(); |
512 | 574 |
513 /* Loop, waiting for QUIT or RESIZE */ | 575 /* Loop, waiting for QUIT or RESIZE */ |
514 while (1) | 576 while (1) { |
515 { | 577 if (SDL_PollEvent(&event)) { |
516 if (SDL_PollEvent(&event)) | 578 switch (event.type) { |
517 { | 579 case SDL_VIDEORESIZE: |
518 switch (event.type) | 580 screen = |
519 { | 581 SDL_SetVideoMode(event.resize.w, event.resize.h, 0, |
520 case SDL_VIDEORESIZE: | 582 SDL_RESIZABLE | SDL_SWSURFACE); |
521 screen=SDL_SetVideoMode(event.resize.w, event.resize.h, 0, SDL_RESIZABLE | SDL_SWSURFACE); | 583 overlayrect.w = event.resize.w; |
522 overlayrect.w=event.resize.w; | 584 overlayrect.h = event.resize.h; |
523 overlayrect.h=event.resize.h; | 585 if (paused) { |
524 if (paused) | 586 resized = 1; |
525 { | 587 } |
526 resized=1; | 588 break; |
527 } | 589 case SDL_MOUSEBUTTONDOWN: |
528 break; | 590 overlayrect.x = event.button.x - overlayrect.w / 2; |
529 case SDL_MOUSEBUTTONDOWN: | 591 overlayrect.y = event.button.y - overlayrect.h / 2; |
530 overlayrect.x = event.button.x - overlayrect.w/2; | 592 break; |
531 overlayrect.y = event.button.y - overlayrect.h/2; | 593 case SDL_KEYDOWN: |
532 break; | 594 if (event.key.keysym.sym == SDLK_SPACE) { |
533 case SDL_KEYDOWN: | 595 paused = !paused; |
534 if (event.key.keysym.sym == SDLK_SPACE) | 596 break; |
535 { | 597 } |
536 paused=!paused; | 598 if (event.key.keysym.sym != SDLK_ESCAPE) { |
537 break; | 599 break; |
538 } | 600 } |
539 if (event.key.keysym.sym != SDLK_ESCAPE) | 601 case SDL_QUIT: |
540 { | 602 SDL_FreeYUVOverlay(overlay); |
541 break; | 603 for (i = 0; i < MOOSEFRAMES_COUNT; i++) { |
542 } | 604 SDL_FreeSurface(MooseFrame[i]); |
543 case SDL_QUIT: | 605 } |
544 SDL_FreeYUVOverlay(overlay); | 606 quit(0); |
545 for (i=0; i<MOOSEFRAMES_COUNT; i++) | 607 } |
546 { | 608 } |
547 SDL_FreeSurface(MooseFrame[i]); | 609 |
548 } | 610 if ((!paused) || (resized)) { |
549 quit(0); | 611 if (((SDL_GetTicks() - lastftick) > fpsdelay) || (resized)) { |
550 } | 612 lastftick = SDL_GetTicks(); |
551 } | 613 |
552 | 614 switch (overlay_format) { |
553 if ((!paused)||(resized)) | 615 case SDL_YUY2_OVERLAY: |
554 { | 616 ConvertRGBtoYUY2(MooseFrame[i], overlay, 0, 100); |
555 if (((SDL_GetTicks()-lastftick)>fpsdelay)||(resized)) | 617 break; |
556 { | 618 case SDL_YV12_OVERLAY: |
557 lastftick=SDL_GetTicks(); | 619 ConvertRGBtoYV12(MooseFrame[i], overlay, 0, 100); |
558 | 620 break; |
559 switch (overlay_format) | 621 case SDL_UYVY_OVERLAY: |
560 { | 622 ConvertRGBtoUYVY(MooseFrame[i], overlay, 0, 100); |
561 case SDL_YUY2_OVERLAY: | 623 break; |
562 ConvertRGBtoYUY2(MooseFrame[i], overlay, 0, 100); | 624 case SDL_YVYU_OVERLAY: |
563 break; | 625 ConvertRGBtoYVYU(MooseFrame[i], overlay, 0, 100); |
564 case SDL_YV12_OVERLAY: | 626 break; |
565 ConvertRGBtoYV12(MooseFrame[i], overlay, 0, 100); | 627 case SDL_IYUV_OVERLAY: |
566 break; | 628 ConvertRGBtoIYUV(MooseFrame[i], overlay, 0, 100); |
567 case SDL_UYVY_OVERLAY: | 629 break; |
568 ConvertRGBtoUYVY(MooseFrame[i], overlay, 0, 100); | |
569 break; | |
570 case SDL_YVYU_OVERLAY: | |
571 ConvertRGBtoYVYU(MooseFrame[i], overlay, 0, 100); | |
572 break; | |
573 case SDL_IYUV_OVERLAY: | |
574 ConvertRGBtoIYUV(MooseFrame[i], overlay, 0, 100); | |
575 break; | |
576 } | 630 } |
577 | 631 |
578 SDL_DisplayYUVOverlay(overlay, &overlayrect); | 632 SDL_DisplayYUVOverlay(overlay, &overlayrect); |
579 if (!resized) | 633 if (!resized) { |
580 { | |
581 i++; | 634 i++; |
582 if (i==10) | 635 if (i == 10) { |
583 { | 636 i = 0; |
584 i=0; | |
585 } | 637 } |
586 } | 638 } else { |
587 else | 639 resized = 0; |
588 { | |
589 resized=0; | |
590 } | 640 } |
591 } | 641 } |
592 } | 642 } |
593 /* kind of timeslice to OS */ | 643 /* kind of timeslice to OS */ |
594 SDL_Delay(1); | 644 SDL_Delay(1); |
595 } | 645 } |
596 | 646 |
597 SDL_Quit(); | 647 SDL_Quit(); |
598 return 0; | 648 return 0; |
599 } | 649 } |
600 |