History

avfilter/vsrc_gradients: add circular type

Paul B Mahol committed 4 years ago

Commit 8e1cedb1 parent cc6c5ff2 files · +65 −29Browse files

Showing the changes to libavfilter (1 of 2 files). Show all

Unified · Split

libavfilter/vsrc_gradients.c+64−28
@@ -79,10 +79,11 @@ static const AVOption gradients_options[] = { · 78 unmodified lines
7979 {"duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
8080 {"d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64=-1}, -1, INT64_MAX, FLAGS },
8181 {"speed", "set gradients rotation speed", OFFSET(speed), AV_OPT_TYPE_FLOAT,{.dbl=0.01}, 0.00001, 1, FLAGS },
82− {"type", "set gradient type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "type" },
83− {"t", "set gradient type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "type" },
82+ {"type", "set gradient type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "type" },
83+ {"t", "set gradient type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "type" },
8484 {"linear", "set gradient type", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "type" },
8585 {"radial", "set gradient type", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "type" },
86+ {"circular", "set gradient type", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "type" },
8687 {NULL},
8788 };
8889
@@ -115,10 +116,10 @@ static uint64_t lerp_color16(uint8_t c0[4], uint8_t c1[4], float x) · 26 unmodified lines
115116 (llrintf((c0[3] * y + c1[3] * x) * 256)) << 48;
116117 }
117118
118−static uint32_t lerp_colors(uint8_t arr[3][4], int nb_colors, float step)
119+static uint32_t lerp_colors(uint8_t arr[3][4], int nb_colors, int nb_wrap_colors, float step)
119120 {
120121 float scl;
121− int i;
122+ int i, j;
122123
123124 if (nb_colors == 1 || step <= 0.0) {
124125 return arr[0][0] | (arr[0][1] << 8) | (arr[0][2] << 16) | (arr[0][3] << 24);
@@ -127,16 +128,21 @@ static uint32_t lerp_colors(uint8_t arr[3][4], int nb_colors, float step) · 2 unmodified lines
127128 return arr[i][0] | (arr[i][1] << 8) | (arr[i][2] << 16) | (arr[i][3] << 24);
128129 }
129130
130− scl = step * (nb_colors - 1);
131+ scl = step * (nb_wrap_colors - 1);
131132 i = floorf(scl);
133+ j = i + 1;
134+ if (i >= nb_colors - 1) {
135+ i = nb_colors - 1;
136+ j = 0;
137+ }
132138
133− return lerp_color(arr[i], arr[i + 1], scl - i);
139+ return lerp_color(arr[i], arr[j], scl - i);
134140 }
135141
136−static uint64_t lerp_colors16(uint8_t arr[3][4], int nb_colors, float step)
142+static uint64_t lerp_colors16(uint8_t arr[3][4], int nb_colors, int nb_wrap_colors, float step)
137143 {
138144 float scl;
139− int i;
145+ int i, j;
140146
141147 if (nb_colors == 1 || step <= 0.0) {
142148 return ((uint64_t)arr[0][0] << 8) | ((uint64_t)arr[0][1] << 24) | ((uint64_t)arr[0][2] << 40) | ((uint64_t)arr[0][3] << 56);
@@ -145,17 +151,23 @@ static uint64_t lerp_colors16(uint8_t arr[3][4], int nb_colors, float step) · 2 unmodified lines
145151 return ((uint64_t)arr[i][0] << 8) | ((uint64_t)arr[i][1] << 24) | ((uint64_t)arr[i][2] << 40) | ((uint64_t)arr[i][3] << 56);
146152 }
147153
148− scl = step * (nb_colors - 1);
154+ scl = step * (nb_wrap_colors - 1);
149155 i = floorf(scl);
156+ j = i + 1;
157+ if (i >= nb_colors - 1) {
158+ i = nb_colors - 1;
159+ j = 0;
160+ }
150161
151− return lerp_color16(arr[i], arr[i + 1], scl - i);
162+ return lerp_color16(arr[i], arr[j], scl - i);
152163 }
153164
154−static void lerp_colors32(float arr[3][4], int nb_colors, float step,
165+static void lerp_colors32(float arr[3][4], int nb_colors,
166+ int nb_wrap_colors, float step,
155167 float *r, float *g, float *b, float *a)
156168 {
157169 float scl, x;
158− int i;
170+ int i, j;
159171
160172 if (nb_colors == 1 || step <= 0.0) {
161173 *r = arr[0][0];
@@ -172,31 +184,55 @@ static void lerp_colors32(float arr[3][4], int nb_colors, float step, · 10 unmodified lines
172184 return;
173185 }
174186
175− scl = step * (nb_colors - 1);
187+ scl = step * (nb_wrap_colors - 1);
176188 i = floorf(scl);
177189 x = scl - i;
190+ j = i + 1;
191+ if (i >= nb_colors - 1) {
192+ i = nb_colors - 1;
193+ j = 0;
194+ }
178195
179− *r = lerpf(arr[i][0], arr[i + 1][0], x);
180− *g = lerpf(arr[i][1], arr[i + 1][1], x);
181− *b = lerpf(arr[i][2], arr[i + 1][2], x);
182− *a = lerpf(arr[i][3], arr[i + 1][3], x);
196+ *r = lerpf(arr[i][0], arr[j][0], x);
197+ *g = lerpf(arr[i][1], arr[j][1], x);
198+ *b = lerpf(arr[i][2], arr[j][2], x);
199+ *a = lerpf(arr[i][3], arr[j][3], x);
183200 }
184201
185202 static float project(float origin_x, float origin_y,
186203 float dest_x, float dest_y,
187− int point_x, int point_y, int type)
204+ float point_x, float point_y, int type)
188205 {
189− // Rise and run of line.
206+ float op_x = point_x - origin_x;
207+ float op_y = point_y - origin_y;
190208 float od_x = dest_x - origin_x;
191209 float od_y = dest_y - origin_y;
210+ float op_x_od;
211+ float od_s_q;
192212
193− // Distance-squared of line.
194− float od_s_q = type ? sqrtf(od_x * od_x + od_y * od_y) : od_x * od_x + od_y * od_y;
213+ switch (type) {
214+ case 0:
215+ od_s_q = od_x * od_x + od_y * od_y;
216+ break;
217+ case 1:
218+ od_s_q = sqrtf(od_x * od_x + od_y * od_y);
219+ break;
220+ case 2:
221+ od_s_q = M_PI * 2.f;
222+ break;
223+ }
195224
196− // Rise and run of projection.
197− float op_x = point_x - origin_x;
198− float op_y = point_y - origin_y;
199− float op_x_od = type ? sqrtf(op_x * op_x + op_y * op_y) : op_x * od_x + op_y * od_y;
225+ switch (type) {
226+ case 0:
227+ op_x_od = op_x * od_x + op_y * od_y;
228+ break;
229+ case 1:
230+ op_x_od = sqrtf(op_x * op_x + op_y * op_y);
231+ break;
232+ case 2:
233+ op_x_od = atan2f(op_x, op_y) + M_PI;
234+ break;
235+ }
200236
201237 // Normalize and clamp range.
202238 return av_clipf(op_x_od / od_s_q, 0.f, 1.f);
@@ -216,7 +252,7 @@ static int draw_gradients_slice(AVFilterContext *ctx, void *arg, int job, int nb · 13 unmodified lines
216252 for (int y = start; y < end; y++) {
217253 for (int x = 0; x < width; x++) {
218254 float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y, s->type);
219− dst[x] = lerp_colors(s->color_rgba, s->nb_colors, factor);
255+ dst[x] = lerp_colors(s->color_rgba, s->nb_colors, s->nb_colors + (s->type == 2), factor);
220256 }
221257
222258 dst += linesize;
@@ -239,7 +275,7 @@ static int draw_gradients_slice16(AVFilterContext *ctx, void *arg, int job, int · 16 unmodified lines
239275 for (int y = start; y < end; y++) {
240276 for (int x = 0; x < width; x++) {
241277 float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y, s->type);
242− dst[x] = lerp_colors16(s->color_rgba, s->nb_colors, factor);
278+ dst[x] = lerp_colors16(s->color_rgba, s->nb_colors, s->nb_colors + s->type == 2, factor);
243279 }
244280
245281 dst += linesize;
@@ -268,7 +304,7 @@ static int draw_gradients_slice32_planar(AVFilterContext *ctx, void *arg, int jo · 22 unmodified lines
268304 for (int y = start; y < end; y++) {
269305 for (int x = 0; x < width; x++) {
270306 float factor = project(s->fx0, s->fy0, s->fx1, s->fy1, x, y, s->type);
271− lerp_colors32(s->color_rgbaf, s->nb_colors, factor,
307+ lerp_colors32(s->color_rgbaf, s->nb_colors, s->nb_colors + s->type == 2 ,factor,
272308 &dst_r[x], &dst_g[x], &dst_b[x], &dst_a[x]);
273309 }
274310

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t