2 * copyright (c) Sebastien Bechet <s.bechet@av7.net>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #define ONE_HALF (1 << (SCALEBITS - 1))
28 #define FIX(x) ((int) ((x) * (1 << SCALEBITS) + 0.5))
30 #define err_if(expr) do { \
32 fprintf(stderr, "%s\n", strerror(errno)); \
37 static void rgb24_to_yuv420p(unsigned char *lum, unsigned char *cb,
38 unsigned char *cr, const unsigned char *src,
39 int width, int height)
41 int wrap, wrap3, x, y;
42 int r, g, b, r1, g1, b1;
43 const unsigned char *p;
48 for (y = 0; y < height; y += 2) {
49 for (x = 0; x < width; x += 2) {
56 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
57 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
64 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
65 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
75 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
76 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
83 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
84 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
86 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
87 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
88 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
89 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
102 #define DEFAULT_WIDTH 352
103 #define DEFAULT_HEIGHT 288
104 #define DEFAULT_NB_PICT 50
106 static void pgmyuv_save(const char *filename, int w, int h,
107 const 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);
121 f = fopen(filename, "wb");
122 fprintf(f, "P5\n%d %d\n%d\n", w, h * 3 / 2, 255);
127 err_if(fwrite(lum_tab, 1, w * h, f) != w * h);
134 for (i = 0; i < h2; i++) {
135 err_if(fwrite(cb, 1, w2, f) != w2);
136 err_if(fwrite(cr, 1, w2, f) != w2);
142 for (i = 0; i < h2; i++) {
143 err_if(fwrite(cb, 1, w2, f) != w2);
146 for (i = 0; i < h2; i++) {
147 err_if(fwrite(cr, 1, w2, f) != w2);
157 static unsigned char *rgb_tab;
158 static int width, height, wrap;
160 static void put_pixel(int x, int y, int r, int g, int b)
164 if (x < 0 || x >= width ||
165 y < 0 || y >= height)
168 p = rgb_tab + y * wrap + x * 3;