tests: Refactor rotozoom/videogen common code into a separate file.
[libav.git] / tests / utils.c
CommitLineData
f0ccd53a
DB
1/*
2 * This file is part of Libav.
3 *
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.
8 *
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.
13 *
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
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21
22#define SCALEBITS 8
23#define ONE_HALF (1 << (SCALEBITS - 1))
24#define FIX(x) ((int) ((x) * (1L << SCALEBITS) + 0.5))
25
26static void rgb24_to_yuv420p(unsigned char *lum, unsigned char *cb,
27 unsigned char *cr, unsigned char *src,
28 int width, int height)
29{
30 int wrap, wrap3, x, y;
31 int r, g, b, r1, g1, b1;
32 unsigned char *p;
33
34 wrap = width;
35 wrap3 = width * 3;
36 p = src;
37 for (y = 0; y < height; y += 2) {
38 for (x = 0; x < width; x += 2) {
39 r = p[0];
40 g = p[1];
41 b = p[2];
42 r1 = r;
43 g1 = g;
44 b1 = b;
45 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
46 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
47 r = p[3];
48 g = p[4];
49 b = p[5];
50 r1 += r;
51 g1 += g;
52 b1 += b;
53 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
54 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
55 p += wrap3;
56 lum += wrap;
57
58 r = p[0];
59 g = p[1];
60 b = p[2];
61 r1 += r;
62 g1 += g;
63 b1 += b;
64 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
65 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
66 r = p[3];
67 g = p[4];
68 b = p[5];
69 r1 += r;
70 g1 += g;
71 b1 += b;
72 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
73 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
74
75 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
76 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
77 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
78 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
79
80 cb++;
81 cr++;
82 p += -wrap3 + 2 * 3;
83 lum += -wrap + 2;
84 }
85 p += wrap3;
86 lum += wrap;
87 }
88}
89
90/* cif format */
91#define DEFAULT_WIDTH 352
92#define DEFAULT_HEIGHT 288
93#define DEFAULT_NB_PICT 50
94
95static void pgmyuv_save(const char *filename, int w, int h,
96 unsigned char *rgb_tab)
97{
98 FILE *f;
99 int i, h2, w2;
100 unsigned char *cb, *cr;
101 unsigned char *lum_tab, *cb_tab, *cr_tab;
102
103 lum_tab = malloc(w * h);
104 cb_tab = malloc(w * h / 4);
105 cr_tab = malloc(w * h / 4);
106
107 rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
108
109 f = fopen(filename, "wb");
110 fprintf(f, "P5\n%d %d\n%d\n", w, h * 3 / 2, 255);
111 fwrite(lum_tab, 1, w * h, f);
112 h2 = h / 2;
113 w2 = w / 2;
114 cb = cb_tab;
115 cr = cr_tab;
116 for (i = 0; i < h2; i++) {
117 fwrite(cb, 1, w2, f);
118 fwrite(cr, 1, w2, f);
119 cb += w2;
120 cr += w2;
121 }
122 fclose(f);
123
124 free(lum_tab);
125 free(cb_tab);
126 free(cr_tab);
127}
128
129static unsigned char *rgb_tab;
130static int width, height, wrap;
131
132static void put_pixel(int x, int y, int r, int g, int b)
133{
134 unsigned char *p;
135
136 if (x < 0 || x >= width ||
137 y < 0 || y >= height)
138 return;
139
140 p = rgb_tab + y * wrap + x * 3;
141 p[0] = r;
142 p[1] = g;
143 p[2] = b;
144}