| 1 | /* |
| 2 | * Copyright (c) 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 | |
| 26 | /************************************************************************/ |
| 27 | /* Gaussian Blur Implementation */ |
| 28 | /************************************************************************/ |
| 29 | |
| 30 | struct SwGaussianBlur |
| 31 | { |
| 32 | static constexpr int MAX_LEVEL = 3; |
| 33 | int level; |
| 34 | int kernel[MAX_LEVEL]; |
| 35 | int extends; |
| 36 | }; |
| 37 | |
| 38 | |
| 39 | static inline int _gaussianEdgeWrap(int end, int idx) |
| 40 | { |
| 41 | auto r = idx % (end + 1); |
| 42 | return (r < 0) ? (end + 1) + r : r; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | static inline int _gaussianEdgeExtend(int end, int idx) |
| 47 | { |
| 48 | if (idx < 0) return 0; |
| 49 | else if (idx > end) return end; |
| 50 | return idx; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | template<int border> |
| 55 | static inline int _gaussianRemap(int end, int idx) |
| 56 | { |
| 57 | if (border == 1) return _gaussianEdgeWrap(end, idx); |
| 58 | return _gaussianEdgeExtend(end, idx); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | //TODO: SIMD OPTIMIZATION? |
| 63 | template<int border = 0> |
| 64 | static void _gaussianFilter(uint8_t* dst, uint8_t* src, int32_t stride, int32_t w, int32_t h, const SwBBox& bbox, int32_t dimension, bool flipped) |
| 65 | { |
| 66 | if (flipped) { |
| 67 | src += (bbox.min.x * stride + bbox.min.y) << 2; |
| 68 | dst += (bbox.min.x * stride + bbox.min.y) << 2; |
| 69 | } else { |
| 70 | src += (bbox.min.y * stride + bbox.min.x) << 2; |
| 71 | dst += (bbox.min.y * stride + bbox.min.x) << 2; |
| 72 | } |
| 73 | |
| 74 | auto iarr = 1.0f / (dimension + dimension + 1); |
| 75 | auto end = w - 1; |
| 76 | |
| 77 | #pragma omp parallel for |
| 78 | for (int y = 0; y < h; ++y) { |
| 79 | auto p = y * stride; |
| 80 | auto i = p * 4; //current index |
| 81 | auto l = -(dimension + 1); //left index |
| 82 | auto r = dimension; //right index |
| 83 | int acc[4] = {0, 0, 0, 0}; //sliding accumulator |
| 84 | |
| 85 | //initial accumulation |
| 86 | for (int x = l; x < r; ++x) { |
| 87 | auto id = (_gaussianRemap<border>(end, x) + p) * 4; |
| 88 | acc[0] += src[id++]; |
| 89 | acc[1] += src[id++]; |
| 90 | acc[2] += src[id++]; |
| 91 | acc[3] += src[id]; |
| 92 | } |
| 93 | //perform filtering |
| 94 | for (int x = 0; x < w; ++x, ++r, ++l) { |
| 95 | auto rid = (_gaussianRemap<border>(end, r) + p) * 4; |
| 96 | auto lid = (_gaussianRemap<border>(end, l) + p) * 4; |
| 97 | acc[0] += src[rid++] - src[lid++]; |
| 98 | acc[1] += src[rid++] - src[lid++]; |
| 99 | acc[2] += src[rid++] - src[lid++]; |
| 100 | acc[3] += src[rid] - src[lid]; |
| 101 | //ignored rounding for the performance. It should be originally: acc[idx] * iarr + 0.5f |
| 102 | dst[i++] = static_cast<uint8_t>(acc[0] * iarr); |
| 103 | dst[i++] = static_cast<uint8_t>(acc[1] * iarr); |
| 104 | dst[i++] = static_cast<uint8_t>(acc[2] * iarr); |
| 105 | dst[i++] = static_cast<uint8_t>(acc[3] * iarr); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | static int _gaussianInit(SwGaussianBlur* data, float sigma, int quality) |
| 112 | { |
| 113 | const auto MAX_LEVEL = SwGaussianBlur::MAX_LEVEL; |
| 114 | |
| 115 | if (tvg::zero(sigma)) return 0; |
| 116 | |
| 117 | data->level = int(SwGaussianBlur::MAX_LEVEL * ((quality - 1) * 0.01f)) + 1; |
| 118 | |
| 119 | //compute box kernel sizes |
| 120 | auto wl = (int) sqrt((12 * sigma / MAX_LEVEL) + 1); |
| 121 | if (wl % 2 == 0) --wl; |
| 122 | auto wu = wl + 2; |
| 123 | auto mi = (12 * sigma - MAX_LEVEL * wl * wl - 4 * MAX_LEVEL * wl - 3 * MAX_LEVEL) / (-4 * wl - 4); |
| 124 | auto m = int(mi + 0.5f); |
| 125 | auto extends = 0; |
| 126 | |
| 127 | for (int i = 0; i < data->level; i++) { |
| 128 | data->kernel[i] = ((i < m ? wl : wu) - 1) / 2; |
| 129 | extends += data->kernel[i]; |
| 130 | } |
| 131 | |
| 132 | return extends; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | bool effectGaussianBlurRegion(RenderEffectGaussianBlur* params) |
| 137 | { |
| 138 | //bbox region expansion for feathering |
| 139 | auto& region = params->extend; |
| 140 | auto extra = static_cast<SwGaussianBlur*>(params->rd)->extends; |
| 141 | |
| 142 | if (params->direction != 2) { |
| 143 | region.x = -extra; |
| 144 | region.w = extra * 2; |
| 145 | } |
| 146 | if (params->direction != 1) { |
| 147 | region.y = -extra; |
| 148 | region.h = extra * 2; |
| 149 | } |
| 150 | |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | void effectGaussianBlurUpdate(RenderEffectGaussianBlur* params, const Matrix& transform) |
| 156 | { |
| 157 | if (!params->rd) params->rd = (SwGaussianBlur*)malloc(sizeof(SwGaussianBlur)); |
| 158 | auto rd = static_cast<SwGaussianBlur*>(params->rd); |
| 159 | |
| 160 | //compute box kernel sizes |
| 161 | auto scale = sqrt(transform.e11 * transform.e11 + transform.e12 * transform.e12); |
| 162 | rd->extends = _gaussianInit(rd, std::pow(params->sigma * scale, 2), params->quality); |
| 163 | |
| 164 | //invalid |
| 165 | if (rd->extends == 0) { |
| 166 | params->valid = false; |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | params->valid = true; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | bool effectGaussianBlur(SwCompositor* cmp, SwSurface* surface, const RenderEffectGaussianBlur* params) |
| 175 | { |
| 176 | auto& buffer = surface->compositor->image; |
| 177 | auto data = static_cast<SwGaussianBlur*>(params->rd); |
| 178 | auto& bbox = cmp->bbox; |
| 179 | auto w = (bbox.max.x - bbox.min.x); |
| 180 | auto h = (bbox.max.y - bbox.min.y); |
| 181 | auto stride = cmp->image.stride; |
| 182 | auto front = cmp->image.buf32; |
| 183 | auto back = buffer.buf32; |
| 184 | auto swapped = false; |
| 185 | |
| 186 | TVGLOG("SW_ENGINE", "GaussianFilter region(%ld, %ld, %ld, %ld) params(%f %d %d), level(%d)", bbox.min.x, bbox.min.y, bbox.max.x, bbox.max.y, params->sigma, params->direction, params->border, data->level); |
| 187 | |
| 188 | /* It is best to take advantage of the Gaussian blur’s separable property |
| 189 | by dividing the process into two passes. horizontal and vertical. |
| 190 | We can expect fewer calculations. */ |
| 191 | |
| 192 | //horizontal |
| 193 | if (params->direction != 2) { |
| 194 | for (int i = 0; i < data->level; ++i) { |
| 195 | _gaussianFilter(reinterpret_cast<uint8_t*>(back), reinterpret_cast<uint8_t*>(front), stride, w, h, bbox, data->kernel[i], false); |
| 196 | std::swap(front, back); |
| 197 | swapped = !swapped; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | //vertical. x/y flipping and horionztal access is pretty compatible with the memory architecture. |
| 202 | if (params->direction != 1) { |
| 203 | rasterXYFlip(front, back, stride, w, h, bbox, false); |
| 204 | std::swap(front, back); |
| 205 | |
| 206 | for (int i = 0; i < data->level; ++i) { |
| 207 | _gaussianFilter(reinterpret_cast<uint8_t*>(back), reinterpret_cast<uint8_t*>(front), stride, h, w, bbox, data->kernel[i], true); |
| 208 | std::swap(front, back); |
| 209 | swapped = !swapped; |
| 210 | } |
| 211 | |
| 212 | rasterXYFlip(front, back, stride, h, w, bbox, true); |
| 213 | std::swap(front, back); |
| 214 | } |
| 215 | |
| 216 | if (swapped) std::swap(cmp->image.buf8, buffer.buf8); |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | /************************************************************************/ |
| 222 | /* Drop Shadow Implementation */ |
| 223 | /************************************************************************/ |
| 224 | |
| 225 | struct SwDropShadow : SwGaussianBlur |
| 226 | { |
| 227 | SwPoint offset; |
| 228 | }; |
| 229 | |
| 230 | |
| 231 | //TODO: SIMD OPTIMIZATION? |
| 232 | static void _dropShadowFilter(uint32_t* dst, uint32_t* src, int stride, int w, int h, const SwBBox& bbox, int32_t dimension, uint32_t color, bool flipped) |
| 233 | { |
| 234 | if (flipped) { |
| 235 | src += (bbox.min.x * stride + bbox.min.y); |
| 236 | dst += (bbox.min.x * stride + bbox.min.y); |
| 237 | } else { |
| 238 | src += (bbox.min.y * stride + bbox.min.x); |
| 239 | dst += (bbox.min.y * stride + bbox.min.x); |
| 240 | } |
| 241 | auto iarr = 1.0f / (dimension + dimension + 1); |
| 242 | auto end = w - 1; |
| 243 | |
| 244 | #pragma omp parallel for |
| 245 | for (int y = 0; y < h; ++y) { |
| 246 | auto p = y * stride; |
| 247 | auto i = p; //current index |
| 248 | auto l = -(dimension + 1); //left index |
| 249 | auto r = dimension; //right index |
| 250 | int acc = 0; //sliding accumulator |
| 251 | |
| 252 | //initial accumulation |
| 253 | for (int x = l; x < r; ++x) { |
| 254 | auto id = _gaussianEdgeExtend(end, x) + p; |
| 255 | acc += A(src[id]); |
| 256 | } |
| 257 | //perform filtering |
| 258 | for (int x = 0; x < w; ++x, ++r, ++l) { |
| 259 | auto rid = _gaussianEdgeExtend(end, r) + p; |
| 260 | auto lid = _gaussianEdgeExtend(end, l) + p; |
| 261 | acc += A(src[rid]) - A(src[lid]); |
| 262 | //ignored rounding for the performance. It should be originally: acc * iarr |
| 263 | dst[i++] = ALPHA_BLEND(color, static_cast<uint8_t>(acc * iarr)); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | |
| 269 | static void _shift(uint32_t** dst, uint32_t** src, int dstride, int sstride, int wmax, int hmax, const SwBBox& bbox, SwPoint offset, SwSize& size) |
| 270 | { |
| 271 | size.w = bbox.max.x - bbox.min.x; |
| 272 | size.h = bbox.max.y - bbox.min.y; |
| 273 | |
| 274 | //shift |
| 275 | if (offset.x < 0) { |
| 276 | *src -= offset.x; |
| 277 | size.w += offset.x; |
| 278 | } else { |
| 279 | *dst += offset.x; |
| 280 | size.w -= offset.x; |
| 281 | } |
| 282 | |
| 283 | if (offset.y < 0) { |
| 284 | *src -= (offset.y * sstride); |
| 285 | size.h += offset.y; |
| 286 | } else { |
| 287 | *dst += (offset.y * dstride); |
| 288 | size.h -= offset.y; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | |
| 293 | static void _dropShadowNoFilter(uint32_t* dst, uint32_t* src, int dstride, int sstride, int dw, int dh, const SwBBox& bbox, const SwPoint& offset, uint32_t color, uint8_t opacity, bool direct) |
| 294 | { |
| 295 | src += (bbox.min.y * sstride + bbox.min.x); |
| 296 | dst += (bbox.min.y * dstride + bbox.min.x); |
| 297 | |
| 298 | SwSize size; |
| 299 | _shift(&dst, &src, dstride, sstride, dw, dh, bbox, offset, size); |
| 300 | |
| 301 | for (auto y = 0; y < size.h; ++y) { |
| 302 | auto s2 = src; |
| 303 | auto d2 = dst; |
| 304 | for (int x = 0; x < size.w; ++x, ++d2, ++s2) { |
| 305 | auto a = MULTIPLY(opacity, A(*s2)); |
| 306 | if (!direct || a == 255) *d2 = ALPHA_BLEND(color, a); |
| 307 | else *d2 = INTERPOLATE(color, *d2, a); |
| 308 | } |
| 309 | src += sstride; |
| 310 | dst += dstride; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
| 315 | static void _dropShadowNoFilter(SwImage* dimg, SwImage* simg, const SwBBox& bbox, const SwPoint& offset, uint32_t color) |
| 316 | { |
| 317 | int dstride = dimg->stride; |
| 318 | int sstride = simg->stride; |
| 319 | |
| 320 | //shadow image |
| 321 | _dropShadowNoFilter(dimg->buf32, simg->buf32, dstride, sstride, dimg->w, dimg->h, bbox, offset, color, 255, false); |
| 322 | |
| 323 | //original image |
| 324 | auto src = simg->buf32 + (bbox.min.y * sstride + bbox.min.x); |
| 325 | auto dst = dimg->buf32 + (bbox.min.y * dstride + bbox.min.x); |
| 326 | |
| 327 | for (auto y = 0; y < (bbox.max.y - bbox.min.y); ++y) { |
| 328 | auto s = src; |
| 329 | auto d = dst; |
| 330 | for (int x = 0; x < (bbox.max.x - bbox.min.x); ++x, ++d, ++s) { |
| 331 | *d = *s + ALPHA_BLEND(*d, IA(*s)); |
| 332 | } |
| 333 | src += sstride; |
| 334 | dst += dstride; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | |
| 339 | static void _dropShadowShift(uint32_t* dst, uint32_t* src, int dstride, int sstride, int dw, int dh, const SwBBox& bbox, const SwPoint& offset, uint8_t opacity, bool direct) |
| 340 | { |
| 341 | src += (bbox.min.y * sstride + bbox.min.x); |
| 342 | dst += (bbox.min.y * dstride + bbox.min.x); |
| 343 | |
| 344 | SwSize size; |
| 345 | _shift(&dst, &src, dstride, sstride, dw, dh, bbox, offset, size); |
| 346 | |
| 347 | for (auto y = 0; y < size.h; ++y) { |
| 348 | if (direct) rasterTranslucentPixel32(dst, src, size.w, opacity); |
| 349 | else rasterPixel32(dst, src, size.w, opacity); |
| 350 | src += sstride; |
| 351 | dst += dstride; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | |
| 356 | bool effectDropShadowRegion(RenderEffectDropShadow* params) |
| 357 | { |
| 358 | //bbox region expansion for feathering |
| 359 | auto& region = params->extend; |
| 360 | auto& offset = static_cast<SwDropShadow*>(params->rd)->offset; |
| 361 | auto extra = static_cast<SwDropShadow*>(params->rd)->extends; |
| 362 | |
| 363 | region.x = -extra; |
| 364 | region.w = extra * 2; |
| 365 | region.y = -extra; |
| 366 | region.h = extra * 2; |
| 367 | |
| 368 | region.x = std::min(region.x + (int32_t)offset.x, region.x); |
| 369 | region.y = std::min(region.y + (int32_t)offset.y, region.y); |
| 370 | region.w += abs(offset.x); |
| 371 | region.h += abs(offset.y); |
| 372 | |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | void effectDropShadowUpdate(RenderEffectDropShadow* params, const Matrix& transform) |
| 378 | { |
| 379 | if (!params->rd) params->rd = (SwDropShadow*)malloc(sizeof(SwDropShadow)); |
| 380 | auto rd = static_cast<SwDropShadow*>(params->rd); |
| 381 | |
| 382 | //compute box kernel sizes |
| 383 | auto scale = sqrt(transform.e11 * transform.e11 + transform.e12 * transform.e12); |
| 384 | rd->extends = _gaussianInit(rd, std::pow(params->sigma * scale, 2), params->quality); |
| 385 | |
| 386 | //invalid |
| 387 | if (params->color[3] == 0) { |
| 388 | params->valid = false; |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | //offset |
| 393 | auto radian = tvg::deg2rad(90.0f - params->angle) - tvg::radian(transform); |
| 394 | rd->offset = {(int32_t)((params->distance * scale) * cosf(radian)), (int32_t)(-1.0f * (params->distance * scale) * sinf(radian))}; |
| 395 | |
| 396 | params->valid = true; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | //A quite same integration with effectGaussianBlur(). See it for detailed comments. |
| 401 | //surface[0]: the original image, to overlay it into the filtered image. |
| 402 | //surface[1]: temporary buffer for generating the filtered image. |
| 403 | bool effectDropShadow(SwCompositor* cmp, SwSurface* surface[2], const RenderEffectDropShadow* params, bool direct) |
| 404 | { |
| 405 | //FIXME: if the body is partially visible due to clipping, the shadow also becomes partially visible. |
| 406 | |
| 407 | auto data = static_cast<SwDropShadow*>(params->rd); |
| 408 | auto& bbox = cmp->bbox; |
| 409 | auto w = (bbox.max.x - bbox.min.x); |
| 410 | auto h = (bbox.max.y - bbox.min.y); |
| 411 | |
| 412 | //outside the screen |
| 413 | if (abs(data->offset.x) >= w || abs(data->offset.y) >= h) return true; |
| 414 | |
| 415 | SwImage* buffer[] = {&surface[0]->compositor->image, &surface[1]->compositor->image}; |
| 416 | auto color = cmp->recoverSfc->join(params->color[0], params->color[1], params->color[2], 255); |
| 417 | auto stride = cmp->image.stride; |
| 418 | auto front = cmp->image.buf32; |
| 419 | auto back = buffer[1]->buf32; |
| 420 | |
| 421 | auto opacity = direct ? MULTIPLY(params->color[3], cmp->opacity) : params->color[3]; |
| 422 | |
| 423 | TVGLOG("SW_ENGINE", "DropShadow region(%ld, %ld, %ld, %ld) params(%f %f %f), level(%d)", bbox.min.x, bbox.min.y, bbox.max.x, bbox.max.y, params->angle, params->distance, params->sigma, data->level); |
| 424 | |
| 425 | //no filter required |
| 426 | if (data->extends == 0) { |
| 427 | if (direct) { |
| 428 | _dropShadowNoFilter(cmp->recoverSfc->buf32, cmp->image.buf32, cmp->recoverSfc->stride, cmp->image.stride, cmp->recoverSfc->w, cmp->recoverSfc->h, bbox, data->offset, color, opacity, direct); |
| 429 | } else { |
| 430 | _dropShadowNoFilter(buffer[1], &cmp->image, bbox, data->offset, color); |
| 431 | std::swap(cmp->image.buf32, buffer[1]->buf32); |
| 432 | } |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | //saving the original image in order to overlay it into the filtered image. |
| 437 | _dropShadowFilter(back, front, stride, w, h, bbox, data->kernel[0], color, false); |
| 438 | std::swap(front, buffer[0]->buf32); |
| 439 | std::swap(front, back); |
| 440 | |
| 441 | //horizontal |
| 442 | for (int i = 1; i < data->level; ++i) { |
| 443 | _dropShadowFilter(back, front, stride, w, h, bbox, data->kernel[i], color, false); |
| 444 | std::swap(front, back); |
| 445 | } |
| 446 | |
| 447 | //vertical |
| 448 | rasterXYFlip(front, back, stride, w, h, bbox, false); |
| 449 | std::swap(front, back); |
| 450 | |
| 451 | for (int i = 0; i < data->level; ++i) { |
| 452 | _dropShadowFilter(back, front, stride, h, w, bbox, data->kernel[i], color, true); |
| 453 | std::swap(front, back); |
| 454 | } |
| 455 | |
| 456 | rasterXYFlip(front, back, stride, h, w, bbox, true); |
| 457 | std::swap(cmp->image.buf32, back); |
| 458 | |
| 459 | //draw to the main surface directly |
| 460 | if (direct) { |
| 461 | _dropShadowShift(cmp->recoverSfc->buf32, cmp->image.buf32, cmp->recoverSfc->stride, cmp->image.stride, cmp->recoverSfc->w, cmp->recoverSfc->h, bbox, data->offset, opacity, direct); |
| 462 | std::swap(cmp->image.buf32, buffer[0]->buf32); |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | //draw to the intermediate surface |
| 467 | rasterClear(surface[1], bbox.min.x, bbox.min.y, w, h); |
| 468 | _dropShadowShift(buffer[1]->buf32, cmp->image.buf32, buffer[1]->stride, cmp->image.stride, buffer[1]->w, buffer[1]->h, bbox, data->offset, opacity, direct); |
| 469 | std::swap(cmp->image.buf32, buffer[1]->buf32); |
| 470 | |
| 471 | //compositing shadow and body |
| 472 | auto s = buffer[0]->buf32 + (bbox.min.y * buffer[0]->stride + bbox.min.x); |
| 473 | auto d = cmp->image.buf32 + (bbox.min.y * cmp->image.stride + bbox.min.x); |
| 474 | |
| 475 | for (auto y = 0; y < h; ++y) { |
| 476 | rasterTranslucentPixel32(d, s, w, 255); |
| 477 | s += buffer[0]->stride; |
| 478 | d += cmp->image.stride; |
| 479 | } |
| 480 | |
| 481 | return true; |
| 482 | } |
| 483 | |
| 484 | |
| 485 | /************************************************************************/ |
| 486 | /* Fill Implementation */ |
| 487 | /************************************************************************/ |
| 488 | |
| 489 | void effectFillUpdate(RenderEffectFill* params) |
| 490 | { |
| 491 | params->valid = true; |
| 492 | } |
| 493 | |
| 494 | |
| 495 | bool effectFill(SwCompositor* cmp, const RenderEffectFill* params, bool direct) |
| 496 | { |
| 497 | auto opacity = direct ? MULTIPLY(params->color[3], cmp->opacity) : params->color[3]; |
| 498 | |
| 499 | auto& bbox = cmp->bbox; |
| 500 | auto w = size_t(bbox.max.x - bbox.min.x); |
| 501 | auto h = size_t(bbox.max.y - bbox.min.y); |
| 502 | auto color = cmp->recoverSfc->join(params->color[0], params->color[1], params->color[2], 255); |
| 503 | |
| 504 | TVGLOG("SW_ENGINE", "Fill region(%ld, %ld, %ld, %ld), param(%d %d %d %d)", bbox.min.x, bbox.min.y, bbox.max.x, bbox.max.y, params->color[0], params->color[1], params->color[2], params->color[3]); |
| 505 | |
| 506 | if (direct) { |
| 507 | auto dbuffer = cmp->recoverSfc->buf32 + (bbox.min.y * cmp->recoverSfc->stride + bbox.min.x); |
| 508 | auto sbuffer = cmp->image.buf32 + (bbox.min.y * cmp->image.stride + bbox.min.x); |
| 509 | for (size_t y = 0; y < h; ++y) { |
| 510 | auto dst = dbuffer; |
| 511 | auto src = sbuffer; |
| 512 | for (size_t x = 0; x < w; ++x, ++dst, ++src) { |
| 513 | auto a = MULTIPLY(opacity, A(*src)); |
| 514 | auto tmp = ALPHA_BLEND(color, a); |
| 515 | *dst = tmp + ALPHA_BLEND(*dst, 255 - a); |
| 516 | } |
| 517 | dbuffer += cmp->image.stride; |
| 518 | sbuffer += cmp->recoverSfc->stride; |
| 519 | } |
| 520 | cmp->valid = true; //no need the subsequent composition |
| 521 | } else { |
| 522 | auto dbuffer = cmp->image.buf32 + (bbox.min.y * cmp->image.stride + bbox.min.x); |
| 523 | for (size_t y = 0; y < h; ++y) { |
| 524 | auto dst = dbuffer; |
| 525 | for (size_t x = 0; x < w; ++x, ++dst) { |
| 526 | *dst = ALPHA_BLEND(color, MULTIPLY(opacity, A(*dst))); |
| 527 | } |
| 528 | dbuffer += cmp->image.stride; |
| 529 | } |
| 530 | } |
| 531 | return true; |
| 532 | } |
| 533 | |
| 534 | |
| 535 | /************************************************************************/ |
| 536 | /* Tint Implementation */ |
| 537 | /************************************************************************/ |
| 538 | |
| 539 | void effectTintUpdate(RenderEffectTint* params) |
| 540 | { |
| 541 | params->valid = true; |
| 542 | } |
| 543 | |
| 544 | |
| 545 | bool effectTint(SwCompositor* cmp, const RenderEffectTint* params, bool direct) |
| 546 | { |
| 547 | auto& bbox = cmp->bbox; |
| 548 | auto w = size_t(bbox.max.x - bbox.min.x); |
| 549 | auto h = size_t(bbox.max.y - bbox.min.y); |
| 550 | auto black = cmp->recoverSfc->join(params->black[0], params->black[1], params->black[2], 255); |
| 551 | auto white = cmp->recoverSfc->join(params->white[0], params->white[1], params->white[2], 255); |
| 552 | auto opacity = cmp->opacity; |
| 553 | auto luma = cmp->recoverSfc->alphas[2]; //luma function |
| 554 | |
| 555 | TVGLOG("SW_ENGINE", "Tint region(%ld, %ld, %ld, %ld), param(%d %d %d, %d %d %d, %d)", bbox.min.x, bbox.min.y, bbox.max.x, bbox.max.y, params->black[0], params->black[1], params->black[2], params->white[0], params->white[1], params->white[2], params->intensity); |
| 556 | |
| 557 | if (direct) { |
| 558 | auto dbuffer = cmp->recoverSfc->buf32 + (bbox.min.y * cmp->recoverSfc->stride + bbox.min.x); |
| 559 | auto sbuffer = cmp->image.buf32 + (bbox.min.y * cmp->image.stride + bbox.min.x); |
| 560 | for (size_t y = 0; y < h; ++y) { |
| 561 | auto dst = dbuffer; |
| 562 | auto src = sbuffer; |
| 563 | for (size_t x = 0; x < w; ++x, ++dst, ++src) { |
| 564 | auto tmp = rasterUnpremultiply(*src); |
| 565 | auto val = INTERPOLATE(INTERPOLATE(black, white, luma((uint8_t*)&tmp)), tmp, params->intensity); |
| 566 | *dst = INTERPOLATE(val, *dst, MULTIPLY(opacity, A(tmp))); |
| 567 | } |
| 568 | dbuffer += cmp->image.stride; |
| 569 | sbuffer += cmp->recoverSfc->stride; |
| 570 | } |
| 571 | cmp->valid = true; //no need the subsequent composition |
| 572 | } else { |
| 573 | auto dbuffer = cmp->image.buf32 + (bbox.min.y * cmp->image.stride + bbox.min.x); |
| 574 | for (size_t y = 0; y < h; ++y) { |
| 575 | auto dst = dbuffer; |
| 576 | for (size_t x = 0; x < w; ++x, ++dst) { |
| 577 | auto tmp = rasterUnpremultiply(*dst); |
| 578 | auto val = INTERPOLATE(INTERPOLATE(black, white, luma((uint8_t*)&tmp)), tmp, params->intensity); |
| 579 | *dst = ALPHA_BLEND(val, A(tmp)); |
| 580 | } |
| 581 | dbuffer += cmp->image.stride; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | |
| 589 | /************************************************************************/ |
| 590 | /* Tritone Implementation */ |
| 591 | /************************************************************************/ |
| 592 | |
| 593 | static uint32_t _trintone(uint32_t s, uint32_t m, uint32_t h, int l) |
| 594 | { |
| 595 | if (l < 128) { |
| 596 | auto a = std::min(l * 2, 255); |
| 597 | return ALPHA_BLEND(s, 255 - a) + ALPHA_BLEND(m, a); |
| 598 | } else { |
| 599 | auto a = 2 * std::max(0, l - 128); |
| 600 | return ALPHA_BLEND(m, 255 - a) + ALPHA_BLEND(h, a); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | |
| 605 | void effectTritoneUpdate(RenderEffectTritone* params) |
| 606 | { |
| 607 | params->valid = true; |
| 608 | } |
| 609 | |
| 610 | |
| 611 | bool effectTritone(SwCompositor* cmp, const RenderEffectTritone* params, bool direct) |
| 612 | { |
| 613 | auto& bbox = cmp->bbox; |
| 614 | auto w = size_t(bbox.max.x - bbox.min.x); |
| 615 | auto h = size_t(bbox.max.y - bbox.min.y); |
| 616 | auto shadow = cmp->recoverSfc->join(params->shadow[0], params->shadow[1], params->shadow[2], 255); |
| 617 | auto midtone = cmp->recoverSfc->join(params->midtone[0], params->midtone[1], params->midtone[2], 255); |
| 618 | auto highlight = cmp->recoverSfc->join(params->highlight[0], params->highlight[1], params->highlight[2], 255); |
| 619 | auto opacity = cmp->opacity; |
| 620 | auto luma = cmp->recoverSfc->alphas[2]; //luma function |
| 621 | |
| 622 | TVGLOG("SW_ENGINE", "Tritone region(%ld, %ld, %ld, %ld), param(%d %d %d, %d %d %d, %d %d %d)", bbox.min.x, bbox.min.y, bbox.max.x, bbox.max.y, params->shadow[0], params->shadow[1], params->shadow[2], params->midtone[0], params->midtone[1], params->midtone[2], params->highlight[0], params->highlight[1], params->highlight[2]); |
| 623 | |
| 624 | if (direct) { |
| 625 | auto dbuffer = cmp->recoverSfc->buf32 + (bbox.min.y * cmp->recoverSfc->stride + bbox.min.x); |
| 626 | auto sbuffer = cmp->image.buf32 + (bbox.min.y * cmp->image.stride + bbox.min.x); |
| 627 | for (size_t y = 0; y < h; ++y) { |
| 628 | auto dst = dbuffer; |
| 629 | auto src = sbuffer; |
| 630 | for (size_t x = 0; x < w; ++x, ++dst, ++src) { |
| 631 | auto tmp = rasterUnpremultiply(*src); |
| 632 | *dst = INTERPOLATE(_trintone(shadow, midtone, highlight, luma((uint8_t*)&tmp)), *dst, MULTIPLY(opacity, A(tmp))); |
| 633 | } |
| 634 | dbuffer += cmp->image.stride; |
| 635 | sbuffer += cmp->recoverSfc->stride; |
| 636 | } |
| 637 | cmp->valid = true; //no need the subsequent composition |
| 638 | } else { |
| 639 | auto dbuffer = cmp->image.buf32 + (bbox.min.y * cmp->image.stride + bbox.min.x); |
| 640 | for (size_t y = 0; y < h; ++y) { |
| 641 | auto dst = dbuffer; |
| 642 | for (size_t x = 0; x < w; ++x, ++dst) { |
| 643 | auto tmp = rasterUnpremultiply(*dst); |
| 644 | *dst = ALPHA_BLEND(_trintone(shadow, midtone, highlight, luma((uint8_t*)&tmp)), A(tmp)); |
| 645 | } |
| 646 | dbuffer += cmp->image.stride; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | return true; |
| 651 | } |