Debian Codegraph @

thirdparty/thorvg/src/renderer/sw_engine/tvgSwFill.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "tvgMath.h"
24 #include "tvgSwCommon.h"
25 #include "tvgFill.h"
26
27 /************************************************************************/
28 /* Internal Class Implementation */
29 /************************************************************************/
30
31 #define RADIAL_A_THRESHOLD 0.0005f
32 #define FIXPT_BITS 8
33 #define FIXPT_SIZE (1<<FIXPT_BITS)
34
35 /*
36 * quadratic equation with the following coefficients (rx and ry defined in the _calculateCoefficients()):
37 * A = a // fill->radial.a
38 * B = 2 * (dr * fr + rx * dx + ry * dy)
39 * C = fr^2 - rx^2 - ry^2
40 * Derivatives are computed with respect to dx.
41 * This procedure aims to optimize and eliminate the need to calculate all values from the beginning
42 * for consecutive x values with a constant y. The Taylor series expansions are computed as long as
43 * its terms are non-zero.
44 */
45 static void _calculateCoefficients(const SwFill* fill, uint32_t x, uint32_t y, float& b, float& deltaB, float& det, float& deltaDet, float& deltaDeltaDet)
46 {
47 auto radial = &fill->radial;
48
49 auto rx = (x + 0.5f) * radial->a11 + (y + 0.5f) * radial->a12 + radial->a13 - radial->fx;
50 auto ry = (x + 0.5f) * radial->a21 + (y + 0.5f) * radial->a22 + radial->a23 - radial->fy;
51
52 b = (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy) * radial->invA;
53 deltaB = (radial->a11 * radial->dx + radial->a21 * radial->dy) * radial->invA;
54
55 auto rr = rx * rx + ry * ry;
56 auto deltaRr = 2.0f * (rx * radial->a11 + ry * radial->a21) * radial->invA;
57 auto deltaDeltaRr = 2.0f * (radial->a11 * radial->a11 + radial->a21 * radial->a21) * radial->invA;
58
59 det = b * b + (rr - radial->fr * radial->fr) * radial->invA;
60 deltaDet = 2.0f * b * deltaB + deltaB * deltaB + deltaRr + deltaDeltaRr * 0.5f;
61 deltaDeltaDet = 2.0f * deltaB * deltaB + deltaDeltaRr;
62 }
63
64
65 static uint32_t _estimateAAMargin(const Fill* fdata)
66 {
67 constexpr float marginScalingFactor = 800.0f;
68 if (fdata->type() == Type::RadialGradient) {
69 auto radius = P(static_cast<const RadialGradient*>(fdata))->r;
70 return tvg::zero(radius) ? 0 : static_cast<uint32_t>(marginScalingFactor / radius);
71 }
72 auto grad = P(static_cast<const LinearGradient*>(fdata));
73 Point p1 {grad->x1, grad->y1};
74 Point p2 {grad->x2, grad->y2};
75 auto len = length(&p1, &p2);
76 return tvg::zero(len) ? 0 : static_cast<uint32_t>(marginScalingFactor / len);
77 }
78
79
80 static void _adjustAAMargin(uint32_t& iMargin, uint32_t index)
81 {
82 constexpr float threshold = 0.1f;
83 constexpr uint32_t iMarginMax = 40;
84
85 auto iThreshold = static_cast<uint32_t>(index * threshold);
86 if (iMargin > iThreshold) iMargin = iThreshold;
87 if (iMargin > iMarginMax) iMargin = iMarginMax;
88 }
89
90
91 static inline uint32_t _alphaUnblend(uint32_t c)
92 {
93 auto a = (c >> 24);
94 if (a == 255 || a == 0) return c;
95 auto invA = 255.0f / static_cast<float>(a);
96 auto c0 = static_cast<uint8_t>(static_cast<float>((c >> 16) & 0xFF) * invA);
97 auto c1 = static_cast<uint8_t>(static_cast<float>((c >> 8) & 0xFF) * invA);
98 auto c2 = static_cast<uint8_t>(static_cast<float>(c & 0xFF) * invA);
99
100 return (a << 24) | (c0 << 16) | (c1 << 8) | c2;
101 }
102
103
104 static void _applyAA(SwFill* fill, uint32_t begin, uint32_t end)
105 {
106 if (begin == 0 || end == 0) return;
107
108 auto i = SW_COLOR_TABLE - end;
109 auto rgbaEnd = _alphaUnblend(fill->ctable[i]);
110 auto rgbaBegin = _alphaUnblend(fill->ctable[begin]);
111
112 auto dt = 1.0f / (begin + end + 1.0f);
113 float t = dt;
114 while (i != begin) {
115 auto dist = 255 - static_cast<int32_t>(255 * t);
116 auto color = INTERPOLATE(rgbaEnd, rgbaBegin, dist);
117 fill->ctable[i++] = ALPHA_BLEND((color | 0xff000000), (color >> 24));
118
119 if (i == SW_COLOR_TABLE) i = 0;
120 t += dt;
121 }
122 }
123
124
125 static bool _updateColorTable(SwFill* fill, const Fill* fdata, const SwSurface* surface, uint8_t opacity)
126 {
127 if (fill->solid) return true;
128
129 const Fill::ColorStop* colors;
130 auto cnt = fdata->colorStops(&colors);
131 if (cnt == 0 || !colors) return false;
132
133 auto pColors = colors;
134
135 auto a = MULTIPLY(pColors->a, opacity);
136 if (a < 255) fill->translucent = true;
137
138 auto r = pColors->r;
139 auto g = pColors->g;
140 auto b = pColors->b;
141 auto rgba = surface->join(r, g, b, a);
142 auto inc = 1.0f / static_cast<float>(SW_COLOR_TABLE);
143 auto pos = 1.5f * inc;
144 uint32_t i = 0;
145
146 //If repeat is true, anti-aliasing must be applied between the last and the first colors.
147 auto repeat = fill->spread == FillSpread::Repeat;
148 uint32_t iAABegin = repeat ? _estimateAAMargin(fdata) : 0;
149 uint32_t iAAEnd = 0;
150
151 fill->ctable[i++] = ALPHA_BLEND(rgba | 0xff000000, a);
152
153 while (pos <= pColors->offset) {
154 fill->ctable[i] = fill->ctable[i - 1];
155 ++i;
156 pos += inc;
157 }
158
159 for (uint32_t j = 0; j < cnt - 1; ++j) {
160 if (repeat && j == cnt - 2 && iAAEnd == 0) {
161 iAAEnd = iAABegin;
162 _adjustAAMargin(iAAEnd, SW_COLOR_TABLE - i);
163 }
164
165 auto curr = colors + j;
166 auto next = curr + 1;
167 auto delta = 1.0f / (next->offset - curr->offset);
168 auto a2 = MULTIPLY(next->a, opacity);
169 if (!fill->translucent && a2 < 255) fill->translucent = true;
170
171 auto rgba2 = surface->join(next->r, next->g, next->b, a2);
172
173 while (pos < next->offset && i < SW_COLOR_TABLE) {
174 auto t = (pos - curr->offset) * delta;
175 auto dist = static_cast<int32_t>(255 * t);
176 auto dist2 = 255 - dist;
177
178 auto color = INTERPOLATE(rgba, rgba2, dist2);
179 fill->ctable[i] = ALPHA_BLEND((color | 0xff000000), (color >> 24));
180
181 ++i;
182 pos += inc;
183 }
184 rgba = rgba2;
185 a = a2;
186
187 if (repeat && j == 0) _adjustAAMargin(iAABegin, i - 1);
188 }
189 rgba = ALPHA_BLEND((rgba | 0xff000000), a);
190
191 for (; i < SW_COLOR_TABLE; ++i)
192 fill->ctable[i] = rgba;
193
194 //For repeat fill spread apply anti-aliasing between the last and first colors,
195 //othewise make sure the last color stop is represented at the end of the table.
196 if (repeat) _applyAA(fill, iAABegin, iAAEnd);
197 else fill->ctable[SW_COLOR_TABLE - 1] = rgba;
198
199 return true;
200 }
201
202
203 bool _prepareLinear(SwFill* fill, const LinearGradient* linear, const Matrix& transform)
204 {
205 float x1, x2, y1, y2;
206 if (linear->linear(&x1, &y1, &x2, &y2) != Result::Success) return false;
207
208 fill->linear.dx = x2 - x1;
209 fill->linear.dy = y2 - y1;
210 auto len = fill->linear.dx * fill->linear.dx + fill->linear.dy * fill->linear.dy;
211
212 if (len < FLOAT_EPSILON) {
213 if (tvg::zero(fill->linear.dx) && tvg::zero(fill->linear.dy)) {
214 fill->solid = true;
215 }
216 return true;
217 }
218
219 fill->linear.dx /= len;
220 fill->linear.dy /= len;
221 fill->linear.offset = -fill->linear.dx * x1 - fill->linear.dy * y1;
222
223 auto gradTransform = linear->transform();
224 bool isTransformation = !identity((const Matrix*)(&gradTransform));
225
226 if (isTransformation) {
227 gradTransform = transform * gradTransform;
228 } else {
229 gradTransform = transform;
230 isTransformation = true;
231 }
232
233 if (isTransformation) {
234 Matrix invTransform;
235 if (!inverse(&gradTransform, &invTransform)) return false;
236
237 fill->linear.offset += fill->linear.dx * invTransform.e13 + fill->linear.dy * invTransform.e23;
238
239 auto dx = fill->linear.dx;
240 fill->linear.dx = dx * invTransform.e11 + fill->linear.dy * invTransform.e21;
241 fill->linear.dy = dx * invTransform.e12 + fill->linear.dy * invTransform.e22;
242 }
243
244 return true;
245 }
246
247
248 bool _prepareRadial(SwFill* fill, const RadialGradient* radial, const Matrix& transform)
249 {
250 auto cx = P(radial)->cx;
251 auto cy = P(radial)->cy;
252 auto r = P(radial)->r;
253 auto fx = P(radial)->fx;
254 auto fy = P(radial)->fy;
255 auto fr = P(radial)->fr;
256
257 if (tvg::zero(r)) {
258 fill->solid = true;
259 return true;
260 }
261
262 fill->radial.dr = r - fr;
263 fill->radial.dx = cx - fx;
264 fill->radial.dy = cy - fy;
265 fill->radial.fr = fr;
266 fill->radial.fx = fx;
267 fill->radial.fy = fy;
268 fill->radial.a = fill->radial.dr * fill->radial.dr - fill->radial.dx * fill->radial.dx - fill->radial.dy * fill->radial.dy;
269
270 //This condition fulfills the SVG 1.1 std:
271 //the focal point, if outside the end circle, is moved to be on the end circle
272 //See: the SVG 2 std requirements: https://www.w3.org/TR/SVG2/pservers.html#RadialGradientNotes
273 constexpr float precision = 0.01f;
274 if (fill->radial.a <= precision) {
275 auto dist = sqrtf(fill->radial.dx * fill->radial.dx + fill->radial.dy * fill->radial.dy);
276 //retract focal point slightly from edge to avoid numerical errors:
277 fill->radial.fx = cx + r * (1.0f - precision) * (fx - cx) / dist;
278 fill->radial.fy = cy + r * (1.0f - precision) * (fy - cy) / dist;
279 fill->radial.dx = cx - fill->radial.fx;
280 fill->radial.dy = cy - fill->radial.fy;
281 // Prevent loss of precision on Apple Silicon when dr=dy and dx=0 due to FMA
282 // https://github.com/thorvg/thorvg/issues/2014
283 auto dr2 = fill->radial.dr * fill->radial.dr;
284 auto dx2 = fill->radial.dx * fill->radial.dx;
285 auto dy2 = fill->radial.dy * fill->radial.dy;
286
287 fill->radial.a = dr2 - dx2 - dy2;
288 }
289
290 if (fill->radial.a > 0) fill->radial.invA = 1.0f / fill->radial.a;
291
292 auto gradTransform = radial->transform();
293 bool isTransformation = !identity((const Matrix*)(&gradTransform));
294
295 if (isTransformation) gradTransform = transform * gradTransform;
296 else {
297 gradTransform = transform;
298 isTransformation = true;
299 }
300
301 if (isTransformation) {
302 Matrix invTransform;
303 if (!inverse(&gradTransform, &invTransform)) return false;
304 fill->radial.a11 = invTransform.e11;
305 fill->radial.a12 = invTransform.e12;
306 fill->radial.a13 = invTransform.e13;
307 fill->radial.a21 = invTransform.e21;
308 fill->radial.a22 = invTransform.e22;
309 fill->radial.a23 = invTransform.e23;
310 } else {
311 fill->radial.a11 = fill->radial.a22 = 1.0f;
312 fill->radial.a12 = fill->radial.a13 = 0.0f;
313 fill->radial.a21 = fill->radial.a23 = 0.0f;
314 }
315 return true;
316 }
317
318
319 static inline uint32_t _clamp(const SwFill* fill, int32_t pos)
320 {
321 switch (fill->spread) {
322 case FillSpread::Pad: {
323 if (pos >= SW_COLOR_TABLE) pos = SW_COLOR_TABLE - 1;
324 else if (pos < 0) pos = 0;
325 break;
326 }
327 case FillSpread::Repeat: {
328 pos = pos % SW_COLOR_TABLE;
329 if (pos < 0) pos = SW_COLOR_TABLE + pos;
330 break;
331 }
332 case FillSpread::Reflect: {
333 auto limit = SW_COLOR_TABLE * 2;
334 pos = pos % limit;
335 if (pos < 0) pos = limit + pos;
336 if (pos >= SW_COLOR_TABLE) pos = (limit - pos - 1);
337 break;
338 }
339 }
340 return pos;
341 }
342
343
344 static inline uint32_t _fixedPixel(const SwFill* fill, int32_t pos)
345 {
346 int32_t i = (pos + (FIXPT_SIZE / 2)) >> FIXPT_BITS;
347 return fill->ctable[_clamp(fill, i)];
348 }
349
350
351 static inline uint32_t _pixel(const SwFill* fill, float pos)
352 {
353 auto i = static_cast<int32_t>(pos * (SW_COLOR_TABLE - 1) + 0.5f);
354 return fill->ctable[_clamp(fill, i)];
355 }
356
357
358 /************************************************************************/
359 /* External Class Implementation */
360 /************************************************************************/
361
362
363 void fillRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwAlpha alpha, uint8_t csize, uint8_t opacity)
364 {
365 //edge case
366 if (fill->radial.a < RADIAL_A_THRESHOLD) {
367 auto radial = &fill->radial;
368 auto rx = (x + 0.5f) * radial->a11 + (y + 0.5f) * radial->a12 + radial->a13 - radial->fx;
369 auto ry = (x + 0.5f) * radial->a21 + (y + 0.5f) * radial->a22 + radial->a23 - radial->fy;
370
371 if (opacity == 255) {
372 for (uint32_t i = 0 ; i < len ; ++i, ++dst, cmp += csize) {
373 auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy);
374 *dst = opBlendNormal(_pixel(fill, x0), *dst, alpha(cmp));
375 rx += radial->a11;
376 ry += radial->a21;
377 }
378 } else {
379 for (uint32_t i = 0 ; i < len ; ++i, ++dst, cmp += csize) {
380 auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy);
381 *dst = opBlendNormal(_pixel(fill, x0), *dst, MULTIPLY(opacity, alpha(cmp)));
382 rx += radial->a11;
383 ry += radial->a21;
384 }
385 }
386 } else {
387 float b, deltaB, det, deltaDet, deltaDeltaDet;
388 _calculateCoefficients(fill, x, y, b, deltaB, det, deltaDet, deltaDeltaDet);
389
390 if (opacity == 255) {
391 for (uint32_t i = 0 ; i < len ; ++i, ++dst, cmp += csize) {
392 *dst = opBlendNormal(_pixel(fill, sqrtf(det) - b), *dst, alpha(cmp));
393 det += deltaDet;
394 deltaDet += deltaDeltaDet;
395 b += deltaB;
396 }
397 } else {
398 for (uint32_t i = 0 ; i < len ; ++i, ++dst, cmp += csize) {
399 *dst = opBlendNormal(_pixel(fill, sqrtf(det) - b), *dst, MULTIPLY(opacity, alpha(cmp)));
400 det += deltaDet;
401 deltaDet += deltaDeltaDet;
402 b += deltaB;
403 }
404 }
405 }
406 }
407
408
409 void fillRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, uint8_t a)
410 {
411 if (fill->radial.a < RADIAL_A_THRESHOLD) {
412 auto radial = &fill->radial;
413 auto rx = (x + 0.5f) * radial->a11 + (y + 0.5f) * radial->a12 + radial->a13 - radial->fx;
414 auto ry = (x + 0.5f) * radial->a21 + (y + 0.5f) * radial->a22 + radial->a23 - radial->fy;
415 for (uint32_t i = 0; i < len; ++i, ++dst) {
416 auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy);
417 *dst = op(_pixel(fill, x0), *dst, a);
418 rx += radial->a11;
419 ry += radial->a21;
420 }
421 } else {
422 float b, deltaB, det, deltaDet, deltaDeltaDet;
423 _calculateCoefficients(fill, x, y, b, deltaB, det, deltaDet, deltaDeltaDet);
424
425 for (uint32_t i = 0; i < len; ++i, ++dst) {
426 *dst = op(_pixel(fill, sqrtf(det) - b), *dst, a);
427 det += deltaDet;
428 deltaDet += deltaDeltaDet;
429 b += deltaB;
430 }
431 }
432 }
433
434
435 void fillRadial(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, SwMask maskOp, uint8_t a)
436 {
437 if (fill->radial.a < RADIAL_A_THRESHOLD) {
438 auto radial = &fill->radial;
439 auto rx = (x + 0.5f) * radial->a11 + (y + 0.5f) * radial->a12 + radial->a13 - radial->fx;
440 auto ry = (x + 0.5f) * radial->a21 + (y + 0.5f) * radial->a22 + radial->a23 - radial->fy;
441 for (uint32_t i = 0 ; i < len ; ++i, ++dst) {
442 auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy);
443 auto src = MULTIPLY(a, A(_pixel(fill, x0)));
444 *dst = maskOp(src, *dst, ~src);
445 rx += radial->a11;
446 ry += radial->a21;
447 }
448 } else {
449 float b, deltaB, det, deltaDet, deltaDeltaDet;
450 _calculateCoefficients(fill, x, y, b, deltaB, det, deltaDet, deltaDeltaDet);
451
452 for (uint32_t i = 0 ; i < len ; ++i, ++dst) {
453 auto src = MULTIPLY(a, A(_pixel(fill, sqrtf(det) - b)));
454 *dst = maskOp(src, *dst, ~src);
455 det += deltaDet;
456 deltaDet += deltaDeltaDet;
457 b += deltaB;
458 }
459 }
460 }
461
462
463 void fillRadial(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwMask maskOp, uint8_t a)
464 {
465 if (fill->radial.a < RADIAL_A_THRESHOLD) {
466 auto radial = &fill->radial;
467 auto rx = (x + 0.5f) * radial->a11 + (y + 0.5f) * radial->a12 + radial->a13 - radial->fx;
468 auto ry = (x + 0.5f) * radial->a21 + (y + 0.5f) * radial->a22 + radial->a23 - radial->fy;
469 for (uint32_t i = 0 ; i < len ; ++i, ++dst, ++cmp) {
470 auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy);
471 auto src = MULTIPLY(A(A(_pixel(fill, x0))), a);
472 auto tmp = maskOp(src, *cmp, 0);
473 *dst = tmp + MULTIPLY(*dst, ~tmp);
474 rx += radial->a11;
475 ry += radial->a21;
476 }
477 } else {
478 float b, deltaB, det, deltaDet, deltaDeltaDet;
479 _calculateCoefficients(fill, x, y, b, deltaB, det, deltaDet, deltaDeltaDet);
480
481 for (uint32_t i = 0 ; i < len ; ++i, ++dst, ++cmp) {
482 auto src = MULTIPLY(A(_pixel(fill, sqrtf(det))), a);
483 auto tmp = maskOp(src, *cmp, 0);
484 *dst = tmp + MULTIPLY(*dst, ~tmp);
485 det += deltaDet;
486 deltaDet += deltaDeltaDet;
487 b += deltaB;
488 }
489 }
490 }
491
492
493 void fillRadial(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, SwBlender op2, uint8_t a)
494 {
495 if (fill->radial.a < RADIAL_A_THRESHOLD) {
496 auto radial = &fill->radial;
497 auto rx = (x + 0.5f) * radial->a11 + (y + 0.5f) * radial->a12 + radial->a13 - radial->fx;
498 auto ry = (x + 0.5f) * radial->a21 + (y + 0.5f) * radial->a22 + radial->a23 - radial->fy;
499
500 if (a == 255) {
501 for (uint32_t i = 0; i < len; ++i, ++dst) {
502 auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy);
503 auto tmp = op(_pixel(fill, x0), *dst, 255);
504 *dst = op2(tmp, *dst, 255);
505 rx += radial->a11;
506 ry += radial->a21;
507 }
508 } else {
509 for (uint32_t i = 0; i < len; ++i, ++dst) {
510 auto x0 = 0.5f * (rx * rx + ry * ry - radial->fr * radial->fr) / (radial->dr * radial->fr + rx * radial->dx + ry * radial->dy);
511 auto tmp = op(_pixel(fill, x0), *dst, 255);
512 auto tmp2 = op2(tmp, *dst, 255);
513 *dst = INTERPOLATE(tmp2, *dst, a);
514 rx += radial->a11;
515 ry += radial->a21;
516 }
517 }
518 } else {
519 float b, deltaB, det, deltaDet, deltaDeltaDet;
520 _calculateCoefficients(fill, x, y, b, deltaB, det, deltaDet, deltaDeltaDet);
521 if (a == 255) {
522 for (uint32_t i = 0 ; i < len ; ++i, ++dst) {
523 auto tmp = op(_pixel(fill, sqrtf(det) - b), *dst, 255);
524 *dst = op2(tmp, *dst, 255);
525 det += deltaDet;
526 deltaDet += deltaDeltaDet;
527 b += deltaB;
528 }
529 } else {
530 for (uint32_t i = 0 ; i < len ; ++i, ++dst) {
531 auto tmp = op(_pixel(fill, sqrtf(det) - b), *dst, 255);
532 auto tmp2 = op2(tmp, *dst, 255);
533 *dst = INTERPOLATE(tmp2, *dst, a);
534 det += deltaDet;
535 deltaDet += deltaDeltaDet;
536 b += deltaB;
537 }
538 }
539 }
540 }
541
542
543 void fillLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwAlpha alpha, uint8_t csize, uint8_t opacity)
544 {
545 //Rotation
546 float rx = x + 0.5f;
547 float ry = y + 0.5f;
548 float t = (fill->linear.dx * rx + fill->linear.dy * ry + fill->linear.offset) * (SW_COLOR_TABLE - 1);
549 float inc = (fill->linear.dx) * (SW_COLOR_TABLE - 1);
550
551 if (opacity == 255) {
552 if (tvg::zero(inc)) {
553 auto color = _fixedPixel(fill, static_cast<int32_t>(t * FIXPT_SIZE));
554 for (uint32_t i = 0; i < len; ++i, ++dst, cmp += csize) {
555 *dst = opBlendNormal(color, *dst, alpha(cmp));
556 }
557 return;
558 }
559
560 auto vMax = static_cast<float>(INT32_MAX >> (FIXPT_BITS + 1));
561 auto vMin = -vMax;
562 auto v = t + (inc * len);
563
564 //we can use fixed point math
565 if (v < vMax && v > vMin) {
566 auto t2 = static_cast<int32_t>(t * FIXPT_SIZE);
567 auto inc2 = static_cast<int32_t>(inc * FIXPT_SIZE);
568 for (uint32_t j = 0; j < len; ++j, ++dst, cmp += csize) {
569 *dst = opBlendNormal(_fixedPixel(fill, t2), *dst, alpha(cmp));
570 t2 += inc2;
571 }
572 //we have to fallback to float math
573 } else {
574 uint32_t counter = 0;
575 while (counter++ < len) {
576 *dst = opBlendNormal(_pixel(fill, t / SW_COLOR_TABLE), *dst, alpha(cmp));
577 ++dst;
578 t += inc;
579 cmp += csize;
580 }
581 }
582 } else {
583 if (tvg::zero(inc)) {
584 auto color = _fixedPixel(fill, static_cast<int32_t>(t * FIXPT_SIZE));
585 for (uint32_t i = 0; i < len; ++i, ++dst, cmp += csize) {
586 *dst = opBlendNormal(color, *dst, MULTIPLY(alpha(cmp), opacity));
587 }
588 return;
589 }
590
591 auto vMax = static_cast<float>(INT32_MAX >> (FIXPT_BITS + 1));
592 auto vMin = -vMax;
593 auto v = t + (inc * len);
594
595 //we can use fixed point math
596 if (v < vMax && v > vMin) {
597 auto t2 = static_cast<int32_t>(t * FIXPT_SIZE);
598 auto inc2 = static_cast<int32_t>(inc * FIXPT_SIZE);
599 for (uint32_t j = 0; j < len; ++j, ++dst, cmp += csize) {
600 *dst = opBlendNormal(_fixedPixel(fill, t2), *dst, MULTIPLY(alpha(cmp), opacity));
601 t2 += inc2;
602 }
603 //we have to fallback to float math
604 } else {
605 uint32_t counter = 0;
606 while (counter++ < len) {
607 *dst = opBlendNormal(_pixel(fill, t / SW_COLOR_TABLE), *dst, MULTIPLY(opacity, alpha(cmp)));
608 ++dst;
609 t += inc;
610 cmp += csize;
611 }
612 }
613 }
614 }
615
616
617 void fillLinear(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, SwMask maskOp, uint8_t a)
618 {
619 //Rotation
620 float rx = x + 0.5f;
621 float ry = y + 0.5f;
622 float t = (fill->linear.dx * rx + fill->linear.dy * ry + fill->linear.offset) * (SW_COLOR_TABLE - 1);
623 float inc = (fill->linear.dx) * (SW_COLOR_TABLE - 1);
624
625 if (tvg::zero(inc)) {
626 auto src = MULTIPLY(a, A(_fixedPixel(fill, static_cast<int32_t>(t * FIXPT_SIZE))));
627 for (uint32_t i = 0; i < len; ++i, ++dst) {
628 *dst = maskOp(src, *dst, ~src);
629 }
630 return;
631 }
632
633 auto vMax = static_cast<float>(INT32_MAX >> (FIXPT_BITS + 1));
634 auto vMin = -vMax;
635 auto v = t + (inc * len);
636
637 //we can use fixed point math
638 if (v < vMax && v > vMin) {
639 auto t2 = static_cast<int32_t>(t * FIXPT_SIZE);
640 auto inc2 = static_cast<int32_t>(inc * FIXPT_SIZE);
641 for (uint32_t j = 0; j < len; ++j, ++dst) {
642 auto src = MULTIPLY(A(_fixedPixel(fill, t2)), a);
643 *dst = maskOp(src, *dst, ~src);
644 t2 += inc2;
645 }
646 //we have to fallback to float math
647 } else {
648 uint32_t counter = 0;
649 while (counter++ < len) {
650 auto src = MULTIPLY(A(_pixel(fill, t / SW_COLOR_TABLE)), a);
651 *dst = maskOp(src, *dst, ~src);
652 ++dst;
653 t += inc;
654 }
655 }
656 }
657
658
659 void fillLinear(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwMask maskOp, uint8_t a)
660 {
661 //Rotation
662 float rx = x + 0.5f;
663 float ry = y + 0.5f;
664 float t = (fill->linear.dx * rx + fill->linear.dy * ry + fill->linear.offset) * (SW_COLOR_TABLE - 1);
665 float inc = (fill->linear.dx) * (SW_COLOR_TABLE - 1);
666
667 if (tvg::zero(inc)) {
668 auto src = A(_fixedPixel(fill, static_cast<int32_t>(t * FIXPT_SIZE)));
669 src = MULTIPLY(src, a);
670 for (uint32_t i = 0; i < len; ++i, ++dst, ++cmp) {
671 auto tmp = maskOp(src, *cmp, 0);
672 *dst = tmp + MULTIPLY(*dst, ~tmp);
673 }
674 return;
675 }
676
677 auto vMax = static_cast<float>(INT32_MAX >> (FIXPT_BITS + 1));
678 auto vMin = -vMax;
679 auto v = t + (inc * len);
680
681 //we can use fixed point math
682 if (v < vMax && v > vMin) {
683 auto t2 = static_cast<int32_t>(t * FIXPT_SIZE);
684 auto inc2 = static_cast<int32_t>(inc * FIXPT_SIZE);
685 for (uint32_t j = 0; j < len; ++j, ++dst, ++cmp) {
686 auto src = MULTIPLY(a, A(_fixedPixel(fill, t2)));
687 auto tmp = maskOp(src, *cmp, 0);
688 *dst = tmp + MULTIPLY(*dst, ~tmp);
689 t2 += inc2;
690 }
691 //we have to fallback to float math
692 } else {
693 uint32_t counter = 0;
694 while (counter++ < len) {
695 auto src = MULTIPLY(A(_pixel(fill, t / SW_COLOR_TABLE)), a);
696 auto tmp = maskOp(src, *cmp, 0);
697 *dst = tmp + MULTIPLY(*dst, ~tmp);
698 ++dst;
699 ++cmp;
700 t += inc;
701 }
702 }
703 }
704
705
706 void fillLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, uint8_t a)
707 {
708 //Rotation
709 float rx = x + 0.5f;
710 float ry = y + 0.5f;
711 float t = (fill->linear.dx * rx + fill->linear.dy * ry + fill->linear.offset) * (SW_COLOR_TABLE - 1);
712 float inc = (fill->linear.dx) * (SW_COLOR_TABLE - 1);
713
714 if (tvg::zero(inc)) {
715 auto color = _fixedPixel(fill, static_cast<int32_t>(t * FIXPT_SIZE));
716 for (uint32_t i = 0; i < len; ++i, ++dst) {
717 *dst = op(color, *dst, a);
718 }
719 return;
720 }
721
722 auto vMax = static_cast<float>(INT32_MAX >> (FIXPT_BITS + 1));
723 auto vMin = -vMax;
724 auto v = t + (inc * len);
725
726 //we can use fixed point math
727 if (v < vMax && v > vMin) {
728 auto t2 = static_cast<int32_t>(t * FIXPT_SIZE);
729 auto inc2 = static_cast<int32_t>(inc * FIXPT_SIZE);
730 for (uint32_t j = 0; j < len; ++j, ++dst) {
731 *dst = op(_fixedPixel(fill, t2), *dst, a);
732 t2 += inc2;
733 }
734 //we have to fallback to float math
735 } else {
736 uint32_t counter = 0;
737 while (counter++ < len) {
738 *dst = op(_pixel(fill, t / SW_COLOR_TABLE), *dst, a);
739 ++dst;
740 t += inc;
741 }
742 }
743 }
744
745
746 void fillLinear(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, SwBlender op2, uint8_t a)
747 {
748 //Rotation
749 float rx = x + 0.5f;
750 float ry = y + 0.5f;
751 float t = (fill->linear.dx * rx + fill->linear.dy * ry + fill->linear.offset) * (SW_COLOR_TABLE - 1);
752 float inc = (fill->linear.dx) * (SW_COLOR_TABLE - 1);
753
754 if (tvg::zero(inc)) {
755 auto color = _fixedPixel(fill, static_cast<int32_t>(t * FIXPT_SIZE));
756 if (a == 255) {
757 for (uint32_t i = 0; i < len; ++i, ++dst) {
758 auto tmp = op(color, *dst, a);
759 *dst = op2(tmp, *dst, 255);
760 }
761 } else {
762 for (uint32_t i = 0; i < len; ++i, ++dst) {
763 auto tmp = op(color, *dst, a);
764 auto tmp2 = op2(tmp, *dst, 255);
765 *dst = INTERPOLATE(tmp2, *dst, a);
766 }
767 }
768 return;
769 }
770
771 auto vMax = static_cast<float>(INT32_MAX >> (FIXPT_BITS + 1));
772 auto vMin = -vMax;
773 auto v = t + (inc * len);
774
775 if (a == 255) {
776 //we can use fixed point math
777 if (v < vMax && v > vMin) {
778 auto t2 = static_cast<int32_t>(t * FIXPT_SIZE);
779 auto inc2 = static_cast<int32_t>(inc * FIXPT_SIZE);
780 for (uint32_t j = 0; j < len; ++j, ++dst) {
781 auto tmp = op(_fixedPixel(fill, t2), *dst, 255);
782 *dst = op2(tmp, *dst, 255);
783 t2 += inc2;
784 }
785 //we have to fallback to float math
786 } else {
787 uint32_t counter = 0;
788 while (counter++ < len) {
789 auto tmp = op(_pixel(fill, t / SW_COLOR_TABLE), *dst, 255);
790 *dst = op2(tmp, *dst, 255);
791 ++dst;
792 t += inc;
793 }
794 }
795 } else {
796 //we can use fixed point math
797 if (v < vMax && v > vMin) {
798 auto t2 = static_cast<int32_t>(t * FIXPT_SIZE);
799 auto inc2 = static_cast<int32_t>(inc * FIXPT_SIZE);
800 for (uint32_t j = 0; j < len; ++j, ++dst) {
801 auto tmp = op(_fixedPixel(fill, t2), *dst, 255);
802 auto tmp2 = op2(tmp, *dst, 255);
803 *dst = INTERPOLATE(tmp2, *dst, a);
804 t2 += inc2;
805 }
806 //we have to fallback to float math
807 } else {
808 uint32_t counter = 0;
809 while (counter++ < len) {
810 auto tmp = op(_pixel(fill, t / SW_COLOR_TABLE), *dst, 255);
811 auto tmp2 = op2(tmp, *dst, 255);
812 *dst = INTERPOLATE(tmp2, *dst, a);
813 ++dst;
814 t += inc;
815 }
816 }
817 }
818 }
819
820
821 bool fillGenColorTable(SwFill* fill, const Fill* fdata, const Matrix& transform, SwSurface* surface, uint8_t opacity, bool ctable)
822 {
823 if (!fill) return false;
824
825 fill->spread = fdata->spread();
826
827 if (fdata->type() == Type::LinearGradient) {
828 if (!_prepareLinear(fill, static_cast<const LinearGradient*>(fdata), transform)) return false;
829 } else if (fdata->type() == Type::RadialGradient) {
830 if (!_prepareRadial(fill, static_cast<const RadialGradient*>(fdata), transform)) return false;
831 }
832
833 if (ctable) return _updateColorTable(fill, fdata, surface, opacity);
834 return true;
835 }
836
837
838 const Fill::ColorStop* fillFetchSolid(const SwFill* fill, const Fill* fdata)
839 {
840 if (!fill->solid) return nullptr;
841
842 const Fill::ColorStop* colors;
843 auto cnt = fdata->colorStops(&colors);
844 if (cnt == 0 || !colors) return nullptr;
845
846 return colors + cnt - 1;
847 }
848
849
850 void fillReset(SwFill* fill)
851 {
852 fill->translucent = false;
853 fill->solid = false;
854 }
855
856
857 void fillFree(SwFill* fill)
858 {
859 if (fill) free(fill);
860 }