00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdlib.h>
00025 #include <stdint.h>
00026 #include <stdio.h>
00027
00028 #define SCALEBITS 8
00029 #define ONE_HALF (1 << (SCALEBITS - 1))
00030 #define FIX(x) ((int) ((x) * (1L << SCALEBITS) + 0.5))
00031
00032 static void rgb24_to_yuv420p(uint8_t *lum, uint8_t *cb, uint8_t *cr,
00033 uint8_t *src, int width, int height)
00034 {
00035 int wrap, wrap3, x, y;
00036 int r, g, b, r1, g1, b1;
00037 uint8_t *p;
00038
00039 wrap = width;
00040 wrap3 = width * 3;
00041 p = src;
00042 for (y = 0; y < height; y += 2) {
00043 for (x = 0; x < width; x += 2) {
00044 r = p[0];
00045 g = p[1];
00046 b = p[2];
00047 r1 = r;
00048 g1 = g;
00049 b1 = b;
00050 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
00051 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
00052 r = p[3];
00053 g = p[4];
00054 b = p[5];
00055 r1 += r;
00056 g1 += g;
00057 b1 += b;
00058 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
00059 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
00060 p += wrap3;
00061 lum += wrap;
00062
00063 r = p[0];
00064 g = p[1];
00065 b = p[2];
00066 r1 += r;
00067 g1 += g;
00068 b1 += b;
00069 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
00070 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
00071 r = p[3];
00072 g = p[4];
00073 b = p[5];
00074 r1 += r;
00075 g1 += g;
00076 b1 += b;
00077 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
00078 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
00079
00080 cb[0] = 128 + ((- FIX(0.16874) * r1 -
00081 FIX(0.33126) * g1 +
00082 FIX(0.50000) * b1 +
00083 4 * ONE_HALF - 1)
00084 >> (SCALEBITS + 2));
00085 cr[0] = 128 + ((FIX(0.50000) * r1 -
00086 FIX(0.41869) * g1 -
00087 FIX(0.08131) * b1 +
00088 4 * ONE_HALF - 1)
00089 >> (SCALEBITS + 2));
00090
00091 cb++;
00092 cr++;
00093 p += -wrap3 + 2 * 3;
00094 lum += -wrap + 2;
00095 }
00096 p += wrap3;
00097 lum += wrap;
00098 }
00099 }
00100
00101
00102 #define DEFAULT_WIDTH 352
00103 #define DEFAULT_HEIGHT 288
00104 #define DEFAULT_NB_PICT 50
00105
00106 static void pgmyuv_save(const char *filename, int w, int h,
00107 unsigned char *rgb_tab)
00108 {
00109 FILE *f;
00110 int i, h2, w2;
00111 unsigned char *cb, *cr;
00112 unsigned char *lum_tab, *cb_tab, *cr_tab;
00113
00114 lum_tab = malloc(w * h);
00115 cb_tab = malloc((w * h) / 4);
00116 cr_tab = malloc((w * h) / 4);
00117
00118 rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
00119
00120 f = fopen(filename, "wb");
00121 fprintf(f, "P5\n%d %d\n%d\n", w, (h * 3) / 2, 255);
00122 fwrite(lum_tab, 1, w * h, f);
00123 h2 = h / 2;
00124 w2 = w / 2;
00125 cb = cb_tab;
00126 cr = cr_tab;
00127 for (i = 0; i < h2; i++) {
00128 fwrite(cb, 1, w2, f);
00129 fwrite(cr, 1, w2, f);
00130 cb += w2;
00131 cr += w2;
00132 }
00133 fclose(f);
00134
00135 free(lum_tab);
00136 free(cb_tab);
00137 free(cr_tab);
00138 }
00139
00140 unsigned char *rgb_tab;
00141 int width, height, wrap;
00142
00143 static void put_pixel(int x, int y, int r, int g, int b)
00144 {
00145 unsigned char *p;
00146
00147 if (x < 0 || x >= width ||
00148 y < 0 || y >= height)
00149 return;
00150
00151 p = rgb_tab + y * wrap + x * 3;
00152 p[0] = r;
00153 p[1] = g;
00154 p[2] = b;
00155 }
00156
00157 static unsigned int myrnd(unsigned int *seed_ptr, int n)
00158 {
00159 unsigned int seed, val;
00160
00161 seed = *seed_ptr;
00162 seed = (seed * 314159) + 1;
00163 if (n == 256) {
00164 val = seed >> 24;
00165 } else {
00166 val = seed % n;
00167 }
00168 *seed_ptr = seed;
00169 return val;
00170 }
00171
00172 #define NOISE_X 10
00173 #define NOISE_Y 30
00174 #define NOISE_W 26
00175
00176 #define FRAC_BITS 8
00177 #define FRAC_ONE (1 << FRAC_BITS)
00178
00179
00180 static int int_cos(int a)
00181 {
00182 int v, neg;
00183 a = a & (FRAC_ONE - 1);
00184 if (a >= (FRAC_ONE / 2))
00185 a = FRAC_ONE - a;
00186 neg = 0;
00187 if (a > (FRAC_ONE / 4)) {
00188 neg = -1;
00189 a = (FRAC_ONE / 2) - a;
00190 }
00191 v = FRAC_ONE - ((a * a) >> 4);
00192 v = (v ^ neg) - neg;
00193 return v;
00194 }
00195
00196 #define NB_OBJS 10
00197
00198 typedef struct VObj {
00199 int x, y, w, h;
00200 int r, g, b;
00201 } VObj;
00202
00203 VObj objs[NB_OBJS];
00204
00205 unsigned int seed = 1;
00206
00207 static void gen_image(int num, int w, int h)
00208 {
00209 int r, g, b, x, y, i, dx, dy, x1, y1;
00210 unsigned int seed1;
00211
00212 if (num == 0) {
00213 for (i = 0; i < NB_OBJS; i++) {
00214 objs[i].x = myrnd(&seed, w);
00215 objs[i].y = myrnd(&seed, h);
00216 objs[i].w = myrnd(&seed, w / 4) + 10;
00217 objs[i].h = myrnd(&seed, h / 4) + 10;
00218 objs[i].r = myrnd(&seed, 256);
00219 objs[i].g = myrnd(&seed, 256);
00220 objs[i].b = myrnd(&seed, 256);
00221 }
00222 }
00223
00224
00225
00226 dx = int_cos(num * FRAC_ONE / 50) * 35;
00227 dy = int_cos(num * FRAC_ONE / 50 + FRAC_ONE / 10) * 30;
00228 for (y = 0; y < h; y++) {
00229 for (x = 0; x < w; x++) {
00230 x1 = (x << FRAC_BITS) + dx;
00231 y1 = (y << FRAC_BITS) + dy;
00232 r = ((y1 * 7) >> FRAC_BITS) & 0xff;
00233 g = (((x1 + y1) * 9) >> FRAC_BITS) & 0xff;
00234 b = ((x1 * 5) >> FRAC_BITS) & 0xff;
00235 put_pixel(x, y, r, g, b);
00236 }
00237 }
00238
00239
00240 seed1 = num;
00241 for (y = 0; y < NOISE_W; y++) {
00242 for (x = 0; x < NOISE_W; x++) {
00243 r = myrnd(&seed1, 256);
00244 g = myrnd(&seed1, 256);
00245 b = myrnd(&seed1, 256);
00246 put_pixel(x + NOISE_X, y + NOISE_Y, r, g, b);
00247 }
00248 }
00249
00250
00251 for (i = 0; i < NB_OBJS; i++) {
00252 VObj *p = &objs[i];
00253 seed1 = i;
00254 for (y = 0; y < p->h; y++) {
00255 for (x = 0; x < p->w; x++) {
00256 r = p->r;
00257 g = p->g;
00258 b = p->b;
00259
00260 r += myrnd(&seed1, 50);
00261 g += myrnd(&seed1, 50);
00262 b += myrnd(&seed1, 50);
00263 put_pixel(x + p->x, y + p->y, r, g, b);
00264 }
00265 }
00266 p->x += myrnd(&seed, 21) - 10;
00267 p->y += myrnd(&seed, 21) - 10;
00268 }
00269 }
00270
00271 int main(int argc, char **argv)
00272 {
00273 int w, h, i;
00274 char buf[1024];
00275
00276 if (argc != 2) {
00277 printf("usage: %s file\n"
00278 "generate a test video stream\n", argv[0]);
00279 exit(1);
00280 }
00281
00282 w = DEFAULT_WIDTH;
00283 h = DEFAULT_HEIGHT;
00284
00285 rgb_tab = malloc(w * h * 3);
00286 wrap = w * 3;
00287 width = w;
00288 height = h;
00289
00290 for (i = 0; i < DEFAULT_NB_PICT; i++) {
00291 snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i);
00292 gen_image(i, w, h);
00293 pgmyuv_save(buf, w, h, rgb_tab);
00294 }
00295
00296 free(rgb_tab);
00297 return 0;
00298 }