2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #define ONE_HALF (1 << (SCALEBITS - 1))
26 #define FIX(x) ((int) ((x) * (1L << SCALEBITS) + 0.5))
28 #define err_if(expr) do { \
30 fprintf(stderr, "%s\n", strerror(errno)); \
35 static void rgb24_to_yuv420p(unsigned char *lum, unsigned char *cb,
36 unsigned char *cr, unsigned char *src,
37 int width, int height)
39 int wrap, wrap3, x, y;
40 int r, g, b, r1, g1, b1;
46 for (y = 0; y < height; y += 2) {
47 for (x = 0; x < width; x += 2) {
54 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
55 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
62 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
63 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
73 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
74 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
81 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
82 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
84 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
85 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
86 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
87 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
100 #define DEFAULT_WIDTH 352
101 #define DEFAULT_HEIGHT 288
102 #define DEFAULT_NB_PICT 50
104 static void pgmyuv_save(const char *filename, int w, int h,
105 unsigned char *rgb_tab)
109 unsigned char *cb, *cr;
110 unsigned char *lum_tab, *cb_tab, *cr_tab;
112 lum_tab = malloc(w * h);
113 cb_tab = malloc(w * h / 4);
114 cr_tab = malloc(w * h / 4);
116 rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
119 f = fopen(filename, "wb");
120 fprintf(f, "P5\n%d %d\n%d\n", w, h * 3 / 2, 255);
125 err_if(fwrite(lum_tab, 1, w * h, f) != w * h);
132 for (i = 0; i < h2; i++) {
133 err_if(fwrite(cb, 1, w2, f) != w2);
134 err_if(fwrite(cr, 1, w2, f) != w2);
140 for (i = 0; i < h2; i++) {
141 err_if(fwrite(cb, 1, w2, f) != w2);
144 for (i = 0; i < h2; i++) {
145 err_if(fwrite(cr, 1, w2, f) != w2);
155 static unsigned char *rgb_tab;
156 static int width, height, wrap;
158 static void put_pixel(int x, int y, int r, int g, int b)
162 if (x < 0 || x >= width ||
163 y < 0 || y >= height)
166 p = rgb_tab + y * wrap + x * 3;