00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <errno.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025
00026 #define SCALEBITS 8
00027 #define ONE_HALF (1 << (SCALEBITS - 1))
00028 #define FIX(x) ((int) ((x) * (1L << SCALEBITS) + 0.5))
00029
00030 #define err_if(expr) do { \
00031 if (expr) { \
00032 fprintf(stderr, "%s\n", strerror(errno)); \
00033 exit(1); \
00034 } \
00035 } while (0)
00036
00037 static void rgb24_to_yuv420p(unsigned char *lum, unsigned char *cb,
00038 unsigned char *cr, unsigned char *src,
00039 int width, int height)
00040 {
00041 int wrap, wrap3, x, y;
00042 int r, g, b, r1, g1, b1;
00043 unsigned char *p;
00044
00045 wrap = width;
00046 wrap3 = width * 3;
00047 p = src;
00048 for (y = 0; y < height; y += 2) {
00049 for (x = 0; x < width; x += 2) {
00050 r = p[0];
00051 g = p[1];
00052 b = p[2];
00053 r1 = r;
00054 g1 = g;
00055 b1 = b;
00056 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
00057 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
00058 r = p[3];
00059 g = p[4];
00060 b = p[5];
00061 r1 += r;
00062 g1 += g;
00063 b1 += b;
00064 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
00065 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
00066 p += wrap3;
00067 lum += wrap;
00068
00069 r = p[0];
00070 g = p[1];
00071 b = p[2];
00072 r1 += r;
00073 g1 += g;
00074 b1 += b;
00075 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
00076 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
00077 r = p[3];
00078 g = p[4];
00079 b = p[5];
00080 r1 += r;
00081 g1 += g;
00082 b1 += b;
00083 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
00084 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
00085
00086 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
00087 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
00088 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
00089 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
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 err_if(fwrite(lum_tab, 1, w * h, f) != w * h);
00123 h2 = h / 2;
00124 w2 = w / 2;
00125 cb = cb_tab;
00126 cr = cr_tab;
00127 for (i = 0; i < h2; i++) {
00128 err_if(fwrite(cb, 1, w2, f) != w2);
00129 err_if(fwrite(cr, 1, w2, f) != w2);
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 static unsigned char *rgb_tab;
00141 static 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 }