1 /*
2 * Copyright (C) 2010 Georg Martius <georg.martius@web.de>
3 * Copyright (C) 2010 Daniel G. Taylor <dan@programmer-art.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * transform input video
25 */
26
29
31
32 #define INTERPOLATE_METHOD(name) \
33 static uint8_t name(float x, float y, const uint8_t *src, \
34 int width, int height, int stride, uint8_t def)
35
36 #define PIXEL(img, x, y, w, h, stride, def) \
37 ((x) < 0 || (y) < 0) ? (def) : \
38 (((x) >= (w) || (y) >= (h)) ? (def) : \
39 img[(x) + (y) * (stride)])
40
41 /**
42 * Nearest neighbor interpolation
43 */
45 {
47 }
48
49 /**
50 * Bilinear interpolation
51 */
53 {
54 int x_c, x_f, y_c, y_f;
55 int v1, v2, v3, v4;
56
58 return def;
59 } else {
61 x_c = x_f + 1;
62
64 y_c = y_f + 1;
65
70
71 return (v1*(x - x_f)*(y - y_f) + v2*((x - x_f)*(y_c - y)) +
72 v3*(x_c - x)*(y - y_f) + v4*((x_c - x)*(y_c - y)));
73 }
74 }
75
76 /**
77 * Biquadratic interpolation
78 */
80 {
81 int x_c, x_f, y_c, y_f;
82 uint8_t v1, v2, v3, v4;
83 float f1, f2, f3, f4;
84
86 return def;
87 else {
89 x_c = x_f + 1;
91 y_c = y_f + 1;
92
97
98 f1 = 1 - sqrt((x_c - x) * (y_c - y));
99 f2 = 1 - sqrt((x_c - x) * (y - y_f));
100 f3 = 1 - sqrt((x - x_f) * (y_c - y));
101 f4 = 1 - sqrt((x - x_f) * (y - y_f));
102 return (v1 * f1 + v2 * f2 + v3 * f3 + v4 * f4) / (f1 + f2 + f3 + f4);
103 }
104 }
105
107 float x_shift,
108 float y_shift,
109 float angle,
110 float scale_x,
111 float scale_y,
112 float *matrix
113 ) {
114 matrix[0] = scale_x * cos(angle);
115 matrix[1] = -sin(angle);
116 matrix[2] = x_shift;
117 matrix[3] = -matrix[1];
118 matrix[4] = scale_y * cos(angle);
119 matrix[5] = y_shift;
120 matrix[6] = 0;
121 matrix[7] = 0;
122 matrix[8] = 1;
123 }
124
126 int src_stride, int dst_stride,
130 {
131 int x, y;
132 float x_s, y_s;
133 uint8_t def = 0;
135
138 func = interpolate_nearest;
139 break;
141 func = interpolate_bilinear;
142 break;
144 func = interpolate_biquadratic;
145 break;
146 default:
148 }
149
150 for (y = 0; y <
height; y++) {
151 for(x = 0; x <
width; x++) {
152 x_s = x * matrix[0] + y * matrix[1] + matrix[2];
153 y_s = x * matrix[3] + y * matrix[4] + matrix[5];
154
155 switch(fill) {
157 def =
src[y * src_stride + x];
158 break;
162 def =
src[(
int)y_s * src_stride + (
int)x_s];
163 break;
167
170 def =
src[(
int)y_s * src_stride + (
int)x_s];
171 }
172
174 }
175 }
176 return 0;
177 }