| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
struct Vertex |
| 24 |
{ |
| 25 |
Point pt; |
| 26 |
Point uv; |
| 27 |
}; |
| 28 |
|
| 29 |
struct Polygon |
| 30 |
{ |
| 31 |
Vertex vertex[3]; |
| 32 |
}; |
| 33 |
|
| 34 |
struct AALine |
| 35 |
{ |
| 36 |
int32_t x[2]; |
| 37 |
int32_t coverage[2]; |
| 38 |
int32_t length[2]; |
| 39 |
}; |
| 40 |
|
| 41 |
struct AASpans |
| 42 |
{ |
| 43 |
AALine *lines; |
| 44 |
int32_t yStart; |
| 45 |
int32_t yEnd; |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
static float dudx, dvdx; |
| 50 |
static float dxdya, dxdyb, dudya, dvdya; |
| 51 |
static float xa, xb, ua, va; |
| 52 |
|
| 53 |
|
| 54 |
static inline int32_t _modf(float v) |
| 55 |
{ |
| 56 |
return 255 - ((int(v * 256.0f)) & 255); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
static bool _rasterMaskedPolygonImageSegment(SwSurface* surface, const SwImage& image, const SwBBox& region, int yStart, int yEnd, AASpans* aaSpans, uint8_t opacity, uint8_t dirFlag = 0) |
| 61 |
{ |
| 62 |
TVGERR("SW_ENGINE", "TODO: _rasterMaskedPolygonImageSegment()"); |
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
static void _rasterBlendingPolygonImageSegment(SwSurface* surface, const SwImage& image, const SwBBox& bbox, int yStart, int yEnd, AASpans* aaSpans, uint8_t opacity) |
| 68 |
{ |
| 69 |
float _dudx = dudx, _dvdx = dvdx; |
| 70 |
float _dxdya = dxdya, _dxdyb = dxdyb, _dudya = dudya, _dvdya = dvdya; |
| 71 |
float _xa = xa, _xb = xb, _ua = ua, _va = va; |
| 72 |
auto sbuf = image.buf32; |
| 73 |
auto dbuf = surface->buf32; |
| 74 |
int32_t sw = static_cast<int32_t>(image.w); |
| 75 |
int32_t sh = static_cast<int32_t>(image.h); |
| 76 |
int32_t x1, x2, x, y, ar, ab, iru, irv, px, ay; |
| 77 |
int32_t vv = 0, uu = 0; |
| 78 |
float dx, u, v; |
| 79 |
uint32_t* buf; |
| 80 |
|
| 81 |
if (yStart < bbox.min.y) yStart = bbox.min.y; |
| 82 |
if (yEnd > bbox.max.y) yEnd = bbox.max.y; |
| 83 |
|
| 84 |
y = yStart; |
| 85 |
|
| 86 |
while (y < yEnd) { |
| 87 |
x1 = std::max((SwCoord)_xa, bbox.min.x); |
| 88 |
x2 = std::min((SwCoord)_xb, bbox.max.x); |
| 89 |
|
| 90 |
|
| 91 |
if (aaSpans) { |
| 92 |
ay = y - aaSpans->yStart; |
| 93 |
if (aaSpans->lines[ay].x[0] > x1) aaSpans->lines[ay].x[0] = x1; |
| 94 |
if (aaSpans->lines[ay].x[1] < x2) aaSpans->lines[ay].x[1] = x2; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
if ((x2 - x1) >= 1 && (x1 < bbox.max.x) && (x2 > bbox.min.x)) { |
| 99 |
|
| 100 |
|
| 101 |
dx = 1 - (_xa - x1); |
| 102 |
u = _ua + dx * _dudx; |
| 103 |
v = _va + dx * _dvdx; |
| 104 |
|
| 105 |
buf = dbuf + ((y * surface->stride) + x1); |
| 106 |
|
| 107 |
x = x1; |
| 108 |
|
| 109 |
|
| 110 |
while (x++ < x2) { |
| 111 |
uu = (int) u; |
| 112 |
vv = (int) v; |
| 113 |
|
| 114 |
if ((uint32_t) uu >= image.w || (uint32_t) vv >= image.h) continue; |
| 115 |
|
| 116 |
ar = _modf(u); |
| 117 |
ab = _modf(v); |
| 118 |
iru = uu + 1; |
| 119 |
irv = vv + 1; |
| 120 |
|
| 121 |
px = *(sbuf + (vv * image.stride) + uu); |
| 122 |
|
| 123 |
|
| 124 |
if (iru < sw) { |
| 125 |
|
| 126 |
int px2 = *(sbuf + (vv * image.stride) + iru); |
| 127 |
px = INTERPOLATE(px, px2, ar); |
| 128 |
} |
| 129 |
|
| 130 |
if (irv < sh) { |
| 131 |
|
| 132 |
int px2 = *(sbuf + (irv * image.stride) + uu); |
| 133 |
|
| 134 |
if (iru < sw) { |
| 135 |
|
| 136 |
int px3 = *(sbuf + (irv * image.stride) + iru); |
| 137 |
px2 = INTERPOLATE(px2, px3, ar); |
| 138 |
} |
| 139 |
px = INTERPOLATE(px, px2, ab); |
| 140 |
} |
| 141 |
auto tmp = surface->blender(px, *buf, 255); |
| 142 |
*buf = INTERPOLATE(tmp, *buf, MULTIPLY(opacity, A(px))); |
| 143 |
++buf; |
| 144 |
|
| 145 |
|
| 146 |
u += _dudx; |
| 147 |
v += _dvdx; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
_xa += _dxdya; |
| 153 |
_xb += _dxdyb; |
| 154 |
_ua += _dudya; |
| 155 |
_va += _dvdya; |
| 156 |
|
| 157 |
++y; |
| 158 |
} |
| 159 |
xa = _xa; |
| 160 |
xb = _xb; |
| 161 |
ua = _ua; |
| 162 |
va = _va; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
static void _rasterPolygonImageSegment(SwSurface* surface, const SwImage& image, const SwBBox& bbox, int yStart, int yEnd, AASpans* aaSpans, uint8_t opacity, bool matting) |
| 167 |
{ |
| 168 |
float _dudx = dudx, _dvdx = dvdx; |
| 169 |
float _dxdya = dxdya, _dxdyb = dxdyb, _dudya = dudya, _dvdya = dvdya; |
| 170 |
float _xa = xa, _xb = xb, _ua = ua, _va = va; |
| 171 |
auto sbuf = image.buf32; |
| 172 |
auto dbuf = surface->buf32; |
| 173 |
int32_t sw = static_cast<int32_t>(image.w); |
| 174 |
int32_t sh = static_cast<int32_t>(image.h); |
| 175 |
int32_t x1, x2, x, y, ar, ab, iru, irv, px, ay; |
| 176 |
int32_t vv = 0, uu = 0; |
| 177 |
float dx, u, v; |
| 178 |
uint32_t* buf; |
| 179 |
|
| 180 |
|
| 181 |
auto csize = matting ? surface->compositor->image.channelSize: 0; |
| 182 |
auto alpha = matting ? surface->alpha(surface->compositor->method) : nullptr; |
| 183 |
uint8_t* cmp = nullptr; |
| 184 |
|
| 185 |
if (yStart < bbox.min.y) yStart = bbox.min.y; |
| 186 |
if (yEnd > bbox.max.y) yEnd = bbox.max.y; |
| 187 |
|
| 188 |
y = yStart; |
| 189 |
|
| 190 |
while (y < yEnd) { |
| 191 |
x1 = std::max((SwCoord)_xa, bbox.min.x); |
| 192 |
x2 = std::min((SwCoord)_xb, bbox.max.x); |
| 193 |
|
| 194 |
|
| 195 |
if (aaSpans) { |
| 196 |
ay = y - aaSpans->yStart; |
| 197 |
if (aaSpans->lines[ay].x[0] > x1) aaSpans->lines[ay].x[0] = x1; |
| 198 |
if (aaSpans->lines[ay].x[1] < x2) aaSpans->lines[ay].x[1] = x2; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
if ((x2 - x1) >= 1 && (x1 < bbox.max.x) && (x2 > bbox.min.x)) { |
| 203 |
|
| 204 |
|
| 205 |
dx = 1 - (_xa - x1); |
| 206 |
u = _ua + dx * _dudx; |
| 207 |
v = _va + dx * _dvdx; |
| 208 |
|
| 209 |
buf = dbuf + ((y * surface->stride) + x1); |
| 210 |
|
| 211 |
x = x1; |
| 212 |
|
| 213 |
if (matting) cmp = &surface->compositor->image.buf8[(y * surface->compositor->image.stride + x1) * csize]; |
| 214 |
|
| 215 |
if (opacity == 255) { |
| 216 |
|
| 217 |
while (x++ < x2) { |
| 218 |
uu = (int) u; |
| 219 |
vv = (int) v; |
| 220 |
|
| 221 |
if ((uint32_t) uu >= image.w || (uint32_t) vv >= image.h) continue; |
| 222 |
|
| 223 |
ar = _modf(u); |
| 224 |
ab = _modf(v); |
| 225 |
|
| 226 |
iru = uu + 1; |
| 227 |
irv = vv + 1; |
| 228 |
|
| 229 |
px = *(sbuf + (vv * image.stride) + uu); |
| 230 |
|
| 231 |
|
| 232 |
if (iru < sw) { |
| 233 |
|
| 234 |
int px2 = *(sbuf + (vv * image.stride) + iru); |
| 235 |
px = INTERPOLATE(px, px2, ar); |
| 236 |
} |
| 237 |
|
| 238 |
if (irv < sh) { |
| 239 |
|
| 240 |
int px2 = *(sbuf + (irv * image.stride) + uu); |
| 241 |
|
| 242 |
|
| 243 |
if (iru < sw) { |
| 244 |
|
| 245 |
int px3 = *(sbuf + (irv * image.stride) + iru); |
| 246 |
px2 = INTERPOLATE(px2, px3, ar); |
| 247 |
} |
| 248 |
px = INTERPOLATE(px, px2, ab); |
| 249 |
} |
| 250 |
uint32_t src; |
| 251 |
if (matting) { |
| 252 |
src = ALPHA_BLEND(px, alpha(cmp)); |
| 253 |
cmp += csize; |
| 254 |
} else { |
| 255 |
src = px; |
| 256 |
} |
| 257 |
*buf = src + ALPHA_BLEND(*buf, IA(src)); |
| 258 |
++buf; |
| 259 |
|
| 260 |
|
| 261 |
u += _dudx; |
| 262 |
v += _dvdx; |
| 263 |
} |
| 264 |
} else { |
| 265 |
|
| 266 |
while (x++ < x2) { |
| 267 |
uu = (int) u; |
| 268 |
vv = (int) v; |
| 269 |
|
| 270 |
if ((uint32_t) uu >= image.w || (uint32_t) vv >= image.h) continue; |
| 271 |
|
| 272 |
ar = _modf(u); |
| 273 |
ab = _modf(v); |
| 274 |
|
| 275 |
iru = uu + 1; |
| 276 |
irv = vv + 1; |
| 277 |
|
| 278 |
px = *(sbuf + (vv * image.stride) + uu); |
| 279 |
|
| 280 |
|
| 281 |
if (iru < sw) { |
| 282 |
|
| 283 |
int px2 = *(sbuf + (vv * image.stride) + iru); |
| 284 |
px = INTERPOLATE(px, px2, ar); |
| 285 |
} |
| 286 |
|
| 287 |
if (irv < sh) { |
| 288 |
|
| 289 |
int px2 = *(sbuf + (irv * image.stride) + uu); |
| 290 |
|
| 291 |
|
| 292 |
if (iru < sw) { |
| 293 |
|
| 294 |
int px3 = *(sbuf + (irv * image.stride) + iru); |
| 295 |
px2 = INTERPOLATE(px2, px3, ar); |
| 296 |
} |
| 297 |
px = INTERPOLATE(px, px2, ab); |
| 298 |
} |
| 299 |
uint32_t src; |
| 300 |
if (matting) { |
| 301 |
src = ALPHA_BLEND(px, MULTIPLY(opacity, alpha(cmp))); |
| 302 |
cmp += csize; |
| 303 |
} else { |
| 304 |
src = ALPHA_BLEND(px, opacity); |
| 305 |
} |
| 306 |
*buf = src + ALPHA_BLEND(*buf, IA(src)); |
| 307 |
++buf; |
| 308 |
|
| 309 |
|
| 310 |
u += _dudx; |
| 311 |
v += _dvdx; |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
_xa += _dxdya; |
| 318 |
_xb += _dxdyb; |
| 319 |
_ua += _dudya; |
| 320 |
_va += _dvdya; |
| 321 |
|
| 322 |
++y; |
| 323 |
} |
| 324 |
xa = _xa; |
| 325 |
xb = _xb; |
| 326 |
ua = _ua; |
| 327 |
va = _va; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
static void _rasterPolygonImage(SwSurface* surface, const SwImage& image, const SwBBox& bbox, Polygon& polygon, AASpans* aaSpans, uint8_t opacity) |
| 333 |
{ |
| 334 |
float x[3] = {polygon.vertex[0].pt.x, polygon.vertex[1].pt.x, polygon.vertex[2].pt.x}; |
| 335 |
float y[3] = {polygon.vertex[0].pt.y, polygon.vertex[1].pt.y, polygon.vertex[2].pt.y}; |
| 336 |
float u[3] = {polygon.vertex[0].uv.x, polygon.vertex[1].uv.x, polygon.vertex[2].uv.x}; |
| 337 |
float v[3] = {polygon.vertex[0].uv.y, polygon.vertex[1].uv.y, polygon.vertex[2].uv.y}; |
| 338 |
|
| 339 |
float off_y; |
| 340 |
float dxdy[3] = {0.0f, 0.0f, 0.0f}; |
| 341 |
|
| 342 |
auto upper = false; |
| 343 |
|
| 344 |
|
| 345 |
if (y[0] > y[1]) { |
| 346 |
std::swap(x[0], x[1]); |
| 347 |
std::swap(y[0], y[1]); |
| 348 |
std::swap(u[0], u[1]); |
| 349 |
std::swap(v[0], v[1]); |
| 350 |
} |
| 351 |
if (y[0] > y[2]) { |
| 352 |
std::swap(x[0], x[2]); |
| 353 |
std::swap(y[0], y[2]); |
| 354 |
std::swap(u[0], u[2]); |
| 355 |
std::swap(v[0], v[2]); |
| 356 |
} |
| 357 |
if (y[1] > y[2]) { |
| 358 |
std::swap(x[1], x[2]); |
| 359 |
std::swap(y[1], y[2]); |
| 360 |
std::swap(u[1], u[2]); |
| 361 |
std::swap(v[1], v[2]); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
int yi[3] = {(int)y[0], (int)y[1], (int)y[2]}; |
| 366 |
|
| 367 |
|
| 368 |
if ((yi[0] == yi[1] && yi[0] == yi[2]) || ((int) x[0] == (int) x[1] && (int) x[0] == (int) x[2])) return; |
| 369 |
|
| 370 |
|
| 371 |
auto denom = ((x[2] - x[0]) * (y[1] - y[0]) - (x[1] - x[0]) * (y[2] - y[0])); |
| 372 |
|
| 373 |
|
| 374 |
if (tvg::zero(denom)) return; |
| 375 |
|
| 376 |
denom = 1 / denom; |
| 377 |
dudx = ((u[2] - u[0]) * (y[1] - y[0]) - (u[1] - u[0]) * (y[2] - y[0])) * denom; |
| 378 |
dvdx = ((v[2] - v[0]) * (y[1] - y[0]) - (v[1] - v[0]) * (y[2] - y[0])) * denom; |
| 379 |
auto dudy = ((u[1] - u[0]) * (x[2] - x[0]) - (u[2] - u[0]) * (x[1] - x[0])) * denom; |
| 380 |
auto dvdy = ((v[1] - v[0]) * (x[2] - x[0]) - (v[2] - v[0]) * (x[1] - x[0])) * denom; |
| 381 |
|
| 382 |
|
| 383 |
if (y[1] > y[0]) dxdy[0] = (x[1] - x[0]) / (y[1] - y[0]); |
| 384 |
if (y[2] > y[0]) dxdy[1] = (x[2] - x[0]) / (y[2] - y[0]); |
| 385 |
if (y[2] > y[1]) dxdy[2] = (x[2] - x[1]) / (y[2] - y[1]); |
| 386 |
|
| 387 |
|
| 388 |
auto side = (dxdy[1] > dxdy[0]) ? true : false; |
| 389 |
|
| 390 |
if (tvg::equal(y[0], y[1])) side = x[0] > x[1]; |
| 391 |
if (tvg::equal(y[1], y[2])) side = x[2] > x[1]; |
| 392 |
|
| 393 |
auto compositing = _compositing(surface); |
| 394 |
auto blending = _blending(surface); |
| 395 |
|
| 396 |
|
| 397 |
if (!side) { |
| 398 |
|
| 399 |
dxdya = dxdy[1]; |
| 400 |
dudya = dxdya * dudx + dudy; |
| 401 |
dvdya = dxdya * dvdx + dvdy; |
| 402 |
|
| 403 |
|
| 404 |
auto dy = 1.0f - (y[0] - yi[0]); |
| 405 |
xa = x[0] + dy * dxdya; |
| 406 |
ua = u[0] + dy * dudya; |
| 407 |
va = v[0] + dy * dvdya; |
| 408 |
|
| 409 |
|
| 410 |
if (yi[0] < yi[1]) { |
| 411 |
off_y = y[0] < bbox.min.y ? (bbox.min.y - y[0]) : 0; |
| 412 |
xa += (off_y * dxdya); |
| 413 |
ua += (off_y * dudya); |
| 414 |
va += (off_y * dvdya); |
| 415 |
|
| 416 |
|
| 417 |
dxdyb = dxdy[0]; |
| 418 |
xb = x[0] + dy * dxdyb + (off_y * dxdyb); |
| 419 |
|
| 420 |
if (compositing) { |
| 421 |
if (_matting(surface)) _rasterPolygonImageSegment(surface, image, bbox, yi[0], yi[1], aaSpans, opacity, true); |
| 422 |
else _rasterMaskedPolygonImageSegment(surface, image, bbox, yi[0], yi[1], aaSpans, opacity, 1); |
| 423 |
} else if (blending) { |
| 424 |
_rasterBlendingPolygonImageSegment(surface, image, bbox, yi[0], yi[1], aaSpans, opacity); |
| 425 |
} else { |
| 426 |
_rasterPolygonImageSegment(surface, image, bbox, yi[0], yi[1], aaSpans, opacity, false); |
| 427 |
} |
| 428 |
upper = true; |
| 429 |
} |
| 430 |
|
| 431 |
if (yi[1] < yi[2]) { |
| 432 |
off_y = y[1] < bbox.min.y ? (bbox.min.y - y[1]) : 0; |
| 433 |
if (!upper) { |
| 434 |
xa += (off_y * dxdya); |
| 435 |
ua += (off_y * dudya); |
| 436 |
va += (off_y * dvdya); |
| 437 |
} |
| 438 |
|
| 439 |
dxdyb = dxdy[2]; |
| 440 |
xb = x[1] + (1 - (y[1] - yi[1])) * dxdyb + (off_y * dxdyb); |
| 441 |
if (compositing) { |
| 442 |
if (_matting(surface)) _rasterPolygonImageSegment(surface, image, bbox, yi[1], yi[2], aaSpans, opacity, true); |
| 443 |
else _rasterMaskedPolygonImageSegment(surface, image, bbox, yi[1], yi[2], aaSpans, opacity, 2); |
| 444 |
} else if (blending) { |
| 445 |
_rasterBlendingPolygonImageSegment(surface, image, bbox, yi[1], yi[2], aaSpans, opacity); |
| 446 |
} else { |
| 447 |
_rasterPolygonImageSegment(surface, image, bbox, yi[1], yi[2], aaSpans, opacity, false); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
} else { |
| 452 |
|
| 453 |
dxdyb = dxdy[1]; |
| 454 |
auto dy = 1.0f - (y[0] - yi[0]); |
| 455 |
xb = x[0] + dy * dxdyb; |
| 456 |
|
| 457 |
|
| 458 |
if (yi[0] < yi[1]) { |
| 459 |
off_y = y[0] < bbox.min.y ? (bbox.min.y - y[0]) : 0; |
| 460 |
xb += (off_y *dxdyb); |
| 461 |
|
| 462 |
|
| 463 |
dxdya = dxdy[0]; |
| 464 |
dudya = dxdya * dudx + dudy; |
| 465 |
dvdya = dxdya * dvdx + dvdy; |
| 466 |
|
| 467 |
xa = x[0] + dy * dxdya + (off_y * dxdya); |
| 468 |
ua = u[0] + dy * dudya + (off_y * dudya); |
| 469 |
va = v[0] + dy * dvdya + (off_y * dvdya); |
| 470 |
|
| 471 |
if (compositing) { |
| 472 |
if (_matting(surface)) _rasterPolygonImageSegment(surface, image, bbox, yi[0], yi[1], aaSpans, opacity, true); |
| 473 |
else _rasterMaskedPolygonImageSegment(surface, image, bbox, yi[0], yi[1], aaSpans, opacity, 3); |
| 474 |
} else if (blending) { |
| 475 |
_rasterBlendingPolygonImageSegment(surface, image, bbox, yi[0], yi[1], aaSpans, opacity); |
| 476 |
} else { |
| 477 |
_rasterPolygonImageSegment(surface, image, bbox, yi[0], yi[1], aaSpans, opacity, false); |
| 478 |
} |
| 479 |
upper = true; |
| 480 |
} |
| 481 |
|
| 482 |
if (yi[1] < yi[2]) { |
| 483 |
off_y = y[1] < bbox.min.y ? (bbox.min.y - y[1]) : 0; |
| 484 |
if (!upper) xb += (off_y *dxdyb); |
| 485 |
|
| 486 |
|
| 487 |
dxdya = dxdy[2]; |
| 488 |
dudya = dxdya * dudx + dudy; |
| 489 |
dvdya = dxdya * dvdx + dvdy; |
| 490 |
dy = 1 - (y[1] - yi[1]); |
| 491 |
xa = x[1] + dy * dxdya + (off_y * dxdya); |
| 492 |
ua = u[1] + dy * dudya + (off_y * dudya); |
| 493 |
va = v[1] + dy * dvdya + (off_y * dvdya); |
| 494 |
|
| 495 |
if (compositing) { |
| 496 |
if (_matting(surface)) _rasterPolygonImageSegment(surface, image, bbox, yi[1], yi[2], aaSpans, opacity, true); |
| 497 |
else _rasterMaskedPolygonImageSegment(surface, image, bbox, yi[1], yi[2], aaSpans, opacity, 4); |
| 498 |
} else if (blending) { |
| 499 |
_rasterBlendingPolygonImageSegment(surface, image, bbox, yi[1], yi[2], aaSpans, opacity); |
| 500 |
} else { |
| 501 |
_rasterPolygonImageSegment(surface, image, bbox, yi[1], yi[2], aaSpans, opacity, false); |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
static AASpans* _AASpans(int yStart, int yEnd) |
| 509 |
{ |
| 510 |
auto aaSpans = static_cast<AASpans*>(malloc(sizeof(AASpans))); |
| 511 |
aaSpans->yStart = yStart; |
| 512 |
aaSpans->yEnd = yEnd; |
| 513 |
|
| 514 |
|
| 515 |
auto height = yEnd - yStart; |
| 516 |
|
| 517 |
aaSpans->lines = static_cast<AALine*>(malloc(height * sizeof(AALine))); |
| 518 |
|
| 519 |
for (int32_t i = 0; i < height; i++) { |
| 520 |
aaSpans->lines[i].x[0] = INT32_MAX; |
| 521 |
aaSpans->lines[i].x[1] = 0; |
| 522 |
aaSpans->lines[i].length[0] = 0; |
| 523 |
aaSpans->lines[i].length[1] = 0; |
| 524 |
} |
| 525 |
return aaSpans; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
static void _calcIrregularCoverage(AALine* lines, int32_t eidx, int32_t y, int32_t diagonal, int32_t edgeDist, bool reverse) |
| 530 |
{ |
| 531 |
if (eidx == 1) reverse = !reverse; |
| 532 |
int32_t coverage = (255 / (diagonal + 2)); |
| 533 |
int32_t tmp; |
| 534 |
for (int32_t ry = 0; ry < (diagonal + 2); ry++) { |
| 535 |
tmp = y - ry - edgeDist; |
| 536 |
if (tmp < 0) return; |
| 537 |
lines[tmp].length[eidx] = 1; |
| 538 |
if (reverse) lines[tmp].coverage[eidx] = 255 - (coverage * ry); |
| 539 |
else lines[tmp].coverage[eidx] = (coverage * ry); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
static void _calcVertCoverage(AALine *lines, int32_t eidx, int32_t y, int32_t rewind, bool reverse) |
| 545 |
{ |
| 546 |
if (eidx == 1) reverse = !reverse; |
| 547 |
int32_t coverage = (255 / (rewind + 1)); |
| 548 |
int32_t tmp; |
| 549 |
for (int ry = 1; ry < (rewind + 1); ry++) { |
| 550 |
tmp = y - ry; |
| 551 |
if (tmp < 0) return; |
| 552 |
lines[tmp].length[eidx] = 1; |
| 553 |
if (reverse) lines[tmp].coverage[eidx] = (255 - (coverage * ry)); |
| 554 |
else lines[tmp].coverage[eidx] = (coverage * ry); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
static void _calcHorizCoverage(AALine *lines, int32_t eidx, int32_t y, int32_t x, int32_t x2) |
| 560 |
{ |
| 561 |
lines[y].length[eidx] = abs(x - x2); |
| 562 |
lines[y].coverage[eidx] = (255 / (lines[y].length[eidx] + 1)); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
static void _calcAAEdge(AASpans *aaSpans, int32_t eidx) |
| 572 |
{ |
| 573 |
|
| 574 |
#define DirOutHor 0x0011 |
| 575 |
#define DirOutVer 0x0001 |
| 576 |
#define DirInHor 0x0010 |
| 577 |
#define DirInVer 0x0000 |
| 578 |
#define DirNone 0x1000 |
| 579 |
|
| 580 |
#define PUSH_VERTEX() \ |
| 581 |
do { \ |
| 582 |
pEdge.x = lines[y].x[eidx]; \ |
| 583 |
pEdge.y = y; \ |
| 584 |
ptx[0] = tx[0]; \ |
| 585 |
ptx[1] = tx[1]; \ |
| 586 |
} while (0) |
| 587 |
|
| 588 |
struct Point |
| 589 |
{ |
| 590 |
int32_t x, y; |
| 591 |
}; |
| 592 |
|
| 593 |
int32_t y = 0; |
| 594 |
Point pEdge = {-1, -1}; |
| 595 |
Point edgeDiff = {0, 0}; |
| 596 |
|
| 597 |
|
| 598 |
int32_t tx[2] = {0, 0}; |
| 599 |
|
| 600 |
int32_t ptx[2] = {0, 0}; |
| 601 |
int32_t diagonal = 0; |
| 602 |
|
| 603 |
auto yStart = aaSpans->yStart; |
| 604 |
auto yEnd = aaSpans->yEnd; |
| 605 |
auto lines = aaSpans->lines; |
| 606 |
|
| 607 |
int32_t prevDir = DirNone; |
| 608 |
int32_t curDir = DirNone; |
| 609 |
|
| 610 |
yEnd -= yStart; |
| 611 |
|
| 612 |
|
| 613 |
if (y < yEnd) { |
| 614 |
pEdge.x = lines[y].x[eidx]; |
| 615 |
pEdge.y = y; |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
for (y++; y < yEnd; y++) { |
| 620 |
|
| 621 |
if (lines[y].x[0] == INT32_MAX) continue; |
| 622 |
|
| 623 |
|
| 624 |
if (eidx == 0) { |
| 625 |
tx[0] = pEdge.x; |
| 626 |
tx[1] = lines[y].x[0]; |
| 627 |
} else { |
| 628 |
tx[0] = lines[y].x[1]; |
| 629 |
tx[1] = pEdge.x; |
| 630 |
} |
| 631 |
edgeDiff.x = (tx[0] - tx[1]); |
| 632 |
edgeDiff.y = (y - pEdge.y); |
| 633 |
|
| 634 |
|
| 635 |
if (edgeDiff.x > 0) { |
| 636 |
if (edgeDiff.y == 1) curDir = DirOutHor; |
| 637 |
else curDir = DirOutVer; |
| 638 |
} else if (edgeDiff.x < 0) { |
| 639 |
if (edgeDiff.y == 1) curDir = DirInHor; |
| 640 |
else curDir = DirInVer; |
| 641 |
} else curDir = DirNone; |
| 642 |
|
| 643 |
|
| 644 |
if ((curDir == prevDir) && (y < yEnd)) { |
| 645 |
if ((abs(edgeDiff.x) == 1) && (edgeDiff.y == 1)) { |
| 646 |
++diagonal; |
| 647 |
PUSH_VERTEX(); |
| 648 |
continue; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
switch (curDir) { |
| 653 |
case DirOutHor: { |
| 654 |
_calcHorizCoverage(lines, eidx, y, tx[0], tx[1]); |
| 655 |
if (diagonal > 0) { |
| 656 |
_calcIrregularCoverage(lines, eidx, y, diagonal, 0, true); |
| 657 |
diagonal = 0; |
| 658 |
} |
| 659 |
|
| 660 |
if (prevDir == DirOutVer) _calcHorizCoverage(lines, eidx, pEdge.y, ptx[0], ptx[1]); |
| 661 |
|
| 662 |
|
| 663 |
if (y == 1) _calcHorizCoverage(lines, eidx, pEdge.y, tx[0], tx[1]); |
| 664 |
PUSH_VERTEX(); |
| 665 |
} |
| 666 |
break; |
| 667 |
case DirOutVer: { |
| 668 |
_calcVertCoverage(lines, eidx, y, edgeDiff.y, true); |
| 669 |
if (diagonal > 0) { |
| 670 |
_calcIrregularCoverage(lines, eidx, y, diagonal, edgeDiff.y, false); |
| 671 |
diagonal = 0; |
| 672 |
} |
| 673 |
|
| 674 |
if (prevDir == DirOutHor) _calcHorizCoverage(lines, eidx, pEdge.y, ptx[0], ptx[1]); |
| 675 |
PUSH_VERTEX(); |
| 676 |
} |
| 677 |
break; |
| 678 |
case DirInHor: { |
| 679 |
_calcHorizCoverage(lines, eidx, (y - 1), tx[0], tx[1]); |
| 680 |
if (diagonal > 0) { |
| 681 |
_calcIrregularCoverage(lines, eidx, y, diagonal, 0, false); |
| 682 |
diagonal = 0; |
| 683 |
} |
| 684 |
|
| 685 |
if (prevDir == DirOutHor) _calcHorizCoverage(lines, eidx, pEdge.y, ptx[0], ptx[1]); |
| 686 |
PUSH_VERTEX(); |
| 687 |
} |
| 688 |
break; |
| 689 |
case DirInVer: { |
| 690 |
_calcVertCoverage(lines, eidx, y, edgeDiff.y, false); |
| 691 |
if (prevDir == DirOutHor) edgeDiff.y -= 1; |
| 692 |
if (diagonal > 0) { |
| 693 |
_calcIrregularCoverage(lines, eidx, y, diagonal, edgeDiff.y, true); |
| 694 |
diagonal = 0; |
| 695 |
} |
| 696 |
|
| 697 |
if (prevDir == DirOutHor) _calcHorizCoverage(lines, eidx, pEdge.y, ptx[0], ptx[1]); |
| 698 |
PUSH_VERTEX(); |
| 699 |
} |
| 700 |
break; |
| 701 |
} |
| 702 |
if (curDir != DirNone) prevDir = curDir; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
if ((edgeDiff.y == 1) && (edgeDiff.x != 0)) { |
| 707 |
if (y >= yEnd) y = (yEnd - 1); |
| 708 |
_calcHorizCoverage(lines, eidx, y - 1, ptx[0], ptx[1]); |
| 709 |
_calcHorizCoverage(lines, eidx, y, tx[0], tx[1]); |
| 710 |
} else { |
| 711 |
++y; |
| 712 |
if (y > yEnd) y = yEnd; |
| 713 |
_calcVertCoverage(lines, eidx, y, (edgeDiff.y + 1), (prevDir & 0x00000001)); |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
static void _apply(SwSurface* surface, AASpans* aaSpans) |
| 719 |
{ |
| 720 |
auto end = surface->buf32 + surface->h * surface->stride; |
| 721 |
auto y = aaSpans->yStart; |
| 722 |
uint32_t pixel; |
| 723 |
uint32_t* dst; |
| 724 |
int32_t pos; |
| 725 |
|
| 726 |
|
| 727 |
_calcAAEdge(aaSpans, 0); |
| 728 |
|
| 729 |
_calcAAEdge(aaSpans, 1); |
| 730 |
|
| 731 |
while (y < aaSpans->yEnd) { |
| 732 |
auto line = &aaSpans->lines[y - aaSpans->yStart]; |
| 733 |
auto width = line->x[1] - line->x[0]; |
| 734 |
if (width > 0) { |
| 735 |
auto offset = y * surface->stride; |
| 736 |
|
| 737 |
|
| 738 |
dst = surface->buf32 + (offset + line->x[0]); |
| 739 |
if (line->x[0] > 1) pixel = *(dst - 1); |
| 740 |
else pixel = *dst; |
| 741 |
pos = 1; |
| 742 |
|
| 743 |
|
| 744 |
if (dst + line->length[0] >= end) { |
| 745 |
pos += static_cast<int32_t>(dst + line->length[0] - end); |
| 746 |
} |
| 747 |
|
| 748 |
while (pos <= line->length[0]) { |
| 749 |
*dst = INTERPOLATE(*dst, pixel, line->coverage[0] * pos); |
| 750 |
++dst; |
| 751 |
++pos; |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
dst = surface->buf32 + offset + line->x[1] - 1; |
| 756 |
|
| 757 |
if (line->x[1] < (int32_t)(surface->w - 1)) pixel = *(dst + 1); |
| 758 |
else pixel = *dst; |
| 759 |
pos = line->length[1]; |
| 760 |
|
| 761 |
|
| 762 |
if (dst - pos < surface->buf32) pos = static_cast<int32_t>(dst - surface->buf32); |
| 763 |
|
| 764 |
while (pos > 0) { |
| 765 |
*dst = INTERPOLATE(*dst, pixel, 255 - (line->coverage[1] * pos)); |
| 766 |
--dst; |
| 767 |
--pos; |
| 768 |
} |
| 769 |
} |
| 770 |
y++; |
| 771 |
} |
| 772 |
|
| 773 |
free(aaSpans->lines); |
| 774 |
free(aaSpans); |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
bool rasterTexmapPolygon(SwSurface* surface, const SwImage& image, const Matrix& transform, const SwBBox& bbox, uint8_t opacity) |
| 789 |
{ |
| 790 |
if (surface->channelSize == sizeof(uint8_t)) { |
| 791 |
TVGERR("SW_ENGINE", "Not supported grayscale Textmap polygon!"); |
| 792 |
return false; |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
Vertex vertices[4]; |
| 797 |
vertices[0] = {{0.0f, 0.0f}, {0.0f, 0.0f}}; |
| 798 |
vertices[1] = {{float(image.w), 0.0f}, {float(image.w), 0.0f}}; |
| 799 |
vertices[2] = {{float(image.w), float(image.h)}, {float(image.w), float(image.h)}}; |
| 800 |
vertices[3] = {{0.0f, float(image.h)}, {0.0f, float(image.h)}}; |
| 801 |
|
| 802 |
float ys = FLT_MAX, ye = -1.0f; |
| 803 |
for (int i = 0; i < 4; i++) { |
| 804 |
vertices[i].pt *= transform; |
| 805 |
if (vertices[i].pt.y < ys) ys = vertices[i].pt.y; |
| 806 |
if (vertices[i].pt.y > ye) ye = vertices[i].pt.y; |
| 807 |
} |
| 808 |
|
| 809 |
auto yStart = std::max(static_cast<SwCoord>(ys), bbox.min.y); |
| 810 |
auto yEnd = std::min(static_cast<SwCoord>(ye), bbox.max.y); |
| 811 |
auto aaSpans = rightAngle(transform) ? nullptr : _AASpans(yStart, yEnd); |
| 812 |
|
| 813 |
Polygon polygon; |
| 814 |
|
| 815 |
|
| 816 |
polygon.vertex[0] = vertices[0]; |
| 817 |
polygon.vertex[1] = vertices[1]; |
| 818 |
polygon.vertex[2] = vertices[3]; |
| 819 |
|
| 820 |
_rasterPolygonImage(surface, image, bbox, polygon, aaSpans, opacity); |
| 821 |
|
| 822 |
|
| 823 |
polygon.vertex[0] = vertices[1]; |
| 824 |
polygon.vertex[1] = vertices[2]; |
| 825 |
polygon.vertex[2] = vertices[3]; |
| 826 |
|
| 827 |
_rasterPolygonImage(surface, image, bbox, polygon, aaSpans, opacity); |
| 828 |
|
| 829 |
#if 0 |
| 830 |
if (_compositing(surface) && _masking(surface) && !_direct(surface->compositor->method)) { |
| 831 |
_compositeMaskImage(surface, &surface->compositor->image, surface->compositor->bbox); |
| 832 |
} |
| 833 |
#endif |
| 834 |
if (aaSpans) _apply(surface, aaSpans); |
| 835 |
return true; |
| 836 |
} |