25
25
#include <stdint.h>
29
#define ONE_HALF (1 << (SCALEBITS - 1))
30
#define FIX(x) ((int) ((x) * (1L << SCALEBITS) + 0.5))
32
static void rgb24_to_yuv420p(uint8_t *lum, uint8_t *cb, uint8_t *cr,
33
uint8_t *src, int width, int height)
35
int wrap, wrap3, x, y;
36
int r, g, b, r1, g1, b1;
42
for (y = 0; y < height; y += 2) {
43
for (x = 0; x < width; x += 2) {
50
lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
51
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
58
lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
59
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
69
lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
70
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
77
lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
78
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
80
cb[0] = 128 + ((- FIX(0.16874) * r1 -
85
cr[0] = 128 + ((FIX(0.50000) * r1 -
102
#define DEFAULT_WIDTH 352
103
#define DEFAULT_HEIGHT 288
104
#define DEFAULT_NB_PICT 50 /* 2 seconds */
106
static void pgmyuv_save(const char *filename, int w, int h,
107
unsigned char *rgb_tab)
111
unsigned char *cb, *cr;
112
unsigned char *lum_tab, *cb_tab, *cr_tab;
114
lum_tab = malloc(w * h);
115
cb_tab = malloc((w * h) / 4);
116
cr_tab = malloc((w * h) / 4);
118
rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
120
f = fopen(filename, "wb");
121
fprintf(f, "P5\n%d %d\n%d\n", w, (h * 3) / 2, 255);
122
fwrite(lum_tab, 1, w * h, f);
127
for (i = 0; i < h2; i++) {
128
fwrite(cb, 1, w2, f);
129
fwrite(cr, 1, w2, f);
140
unsigned char *rgb_tab;
141
int width, height, wrap;
143
static void put_pixel(int x, int y, int r, int g, int b)
147
if (x < 0 || x >= width ||
148
y < 0 || y >= height)
151
p = rgb_tab + y * wrap + x * 3;
157
30
static unsigned int myrnd(unsigned int *seed_ptr, int n)
290
167
for (i = 0; i < DEFAULT_NB_PICT; i++) {
291
snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i);
292
168
gen_image(i, w, h);
293
pgmyuv_save(buf, w, h, rgb_tab);
170
snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i);
171
pgmyuv_save(buf, w, h, rgb_tab);
173
pgmyuv_save(NULL, w, h, rgb_tab);