| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qsvgfilter_p.h" |
| 5 | |
| 6 | #include "qsvgnode_p.h" |
| 7 | #include "qsvgtinydocument_p.h" |
| 8 | #include "qpainter.h" |
| 9 | |
| 10 | #include <QLoggingCategory> |
| 11 | #include <QtGui/qimageiohandler.h> |
| 12 | #include <QVector4D> |
| 13 | |
| 14 | QT_BEGIN_NAMESPACE |
| 15 | |
| 16 | Q_DECLARE_LOGGING_CATEGORY(lcSvgDraw); |
| 17 | |
| 18 | QSvgFeFilterPrimitive::QSvgFeFilterPrimitive(QSvgNode *parent, QString input, QString result, |
| 19 | const QSvgRectF &rect) |
| 20 | : QSvgStructureNode(parent) |
| 21 | , m_input(input) |
| 22 | , m_result(result) |
| 23 | , m_rect(rect) |
| 24 | { |
| 25 | |
| 26 | } |
| 27 | |
| 28 | bool QSvgFeFilterPrimitive::(QPainter *, QSvgExtraStates &) const |
| 29 | { |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | QRectF QSvgFeFilterPrimitive::localSubRegion(const QRectF &itemBounds, const QRectF &filterBounds, |
| 34 | QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes) const |
| 35 | { |
| 36 | // 15.7.3 Filter primitive subregion |
| 37 | // https://www.w3.org/TR/SVG11/filters.html#FilterPrimitiveSubRegion |
| 38 | |
| 39 | QRectF clipRect = m_rect.resolveRelativeLengths(localRect: itemBounds, units: primitiveUnits); |
| 40 | |
| 41 | // the default subregion is 0%,0%,100%,100%, where as a special-case the percentages are |
| 42 | // relative to the dimensions of the filter region, thus making the the default filter primitive |
| 43 | // subregion equal to the filter region. |
| 44 | if (m_rect.unitX() == QtSvg::UnitTypes::unknown) |
| 45 | clipRect.setX(filterBounds.x()); |
| 46 | if (m_rect.unitY() == QtSvg::UnitTypes::unknown) |
| 47 | clipRect.setY(filterBounds.y()); |
| 48 | if (m_rect.unitW() == QtSvg::UnitTypes::unknown) |
| 49 | clipRect.setWidth(filterBounds.width()); |
| 50 | if (m_rect.unitH() == QtSvg::UnitTypes::unknown) |
| 51 | clipRect.setHeight(filterBounds.height()); |
| 52 | |
| 53 | clipRect = clipRect.intersected(r: filterBounds); |
| 54 | |
| 55 | return clipRect; |
| 56 | } |
| 57 | |
| 58 | QRectF QSvgFeFilterPrimitive::globalSubRegion(QPainter *p, |
| 59 | const QRectF &itemBounds, const QRectF &filterBounds, |
| 60 | QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits) const |
| 61 | { |
| 62 | return p->transform().mapRect(localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits)); |
| 63 | } |
| 64 | |
| 65 | void QSvgFeFilterPrimitive::clipToTransformedBounds(QImage *buffer, QPainter *p, const QRectF &localRect) const |
| 66 | { |
| 67 | QPainter painter(buffer); |
| 68 | painter.setRenderHints(hints: p->renderHints()); |
| 69 | painter.translate(offset: -buffer->offset()); |
| 70 | QPainterPath clipPath; |
| 71 | clipPath.setFillRule(Qt::OddEvenFill); |
| 72 | clipPath.addRect(rect: QRect(buffer->offset(), buffer->size()).adjusted(xp1: -10, yp1: -10, xp2: 20, yp2: 20)); |
| 73 | clipPath.addPolygon(polygon: p->transform().map(a: QPolygonF(localRect))); |
| 74 | painter.setCompositionMode(QPainter::CompositionMode_SourceIn); |
| 75 | painter.fillPath(path: clipPath, brush: Qt::transparent); |
| 76 | } |
| 77 | |
| 78 | bool QSvgFeFilterPrimitive::requiresSourceAlpha() const |
| 79 | { |
| 80 | return m_input == QLatin1StringView("SourceAlpha" ); |
| 81 | } |
| 82 | |
| 83 | const QSvgFeFilterPrimitive *QSvgFeFilterPrimitive::castToFilterPrimitive(const QSvgNode *node) |
| 84 | { |
| 85 | if (node->type() == QSvgNode::FeMerge || |
| 86 | node->type() == QSvgNode::FeColormatrix || |
| 87 | node->type() == QSvgNode::FeGaussianblur || |
| 88 | node->type() == QSvgNode::FeOffset || |
| 89 | node->type() == QSvgNode::FeComposite || |
| 90 | node->type() == QSvgNode::FeFlood ) { |
| 91 | return reinterpret_cast<const QSvgFeFilterPrimitive*>(node); |
| 92 | } else { |
| 93 | return nullptr; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | QSvgFeColorMatrix::QSvgFeColorMatrix(QSvgNode *parent, QString input, QString result, const QSvgRectF &rect, |
| 98 | ColorShiftType type, Matrix matrix) |
| 99 | : QSvgFeFilterPrimitive(parent, input, result, rect) |
| 100 | , m_type(type) |
| 101 | , m_matrix(matrix) |
| 102 | { |
| 103 | (void)m_type; |
| 104 | //Magic numbers see SVG 1.1(Second edition) |
| 105 | if (type == ColorShiftType::Saturate) { |
| 106 | qreal s = qBound(min: 0., val: matrix.data()[0], max: 1.); |
| 107 | |
| 108 | m_matrix.fill(value: 0); |
| 109 | |
| 110 | m_matrix.data()[0+0*5] = 0.213f + 0.787f * s; |
| 111 | m_matrix.data()[1+0*5] = 0.715f - 0.717f * s; |
| 112 | m_matrix.data()[2+0*5] = 0.072f - 0.072f * s; |
| 113 | |
| 114 | m_matrix.data()[0+1*5] = 0.213f - 0.213f * s; |
| 115 | m_matrix.data()[1+1*5] = 0.715f + 0.285f * s; |
| 116 | m_matrix.data()[2+1*5] = 0.072f - 0.072f * s; |
| 117 | |
| 118 | m_matrix.data()[0+2*5] = 0.213f - 0.213f * s; |
| 119 | m_matrix.data()[1+2*5] = 0.715f - 0.715f * s; |
| 120 | m_matrix.data()[2+2*5] = 0.072f + 0.928f * s; |
| 121 | |
| 122 | m_matrix.data()[3+3*5] = 1; |
| 123 | |
| 124 | } else if (type == ColorShiftType::HueRotate){ |
| 125 | qreal angle = matrix.data()[0]/180.*M_PI; |
| 126 | qreal s = sin(x: angle); |
| 127 | qreal c = cos(x: angle); |
| 128 | |
| 129 | m_matrix.fill(value: 0); |
| 130 | |
| 131 | QMatrix3x3 m1; |
| 132 | m1.data()[0+0*3] = 0.213f; |
| 133 | m1.data()[1+0*3] = 0.715f; |
| 134 | m1.data()[2+0*3] = 0.072f; |
| 135 | |
| 136 | m1.data()[0+1*3] = 0.213f; |
| 137 | m1.data()[1+1*3] = 0.715f; |
| 138 | m1.data()[2+1*3] = 0.072f; |
| 139 | |
| 140 | m1.data()[0+2*3] = 0.213f; |
| 141 | m1.data()[1+2*3] = 0.715f; |
| 142 | m1.data()[2+2*3] = 0.072f; |
| 143 | |
| 144 | QMatrix3x3 m2; |
| 145 | m2.data()[0+0*3] = 0.787 * c; |
| 146 | m2.data()[1+0*3] = -0.715 * c; |
| 147 | m2.data()[2+0*3] = -0.072 * c; |
| 148 | |
| 149 | m2.data()[0+1*3] = -0.213 * c; |
| 150 | m2.data()[1+1*3] = 0.285 * c; |
| 151 | m2.data()[2+1*3] = -0.072 * c; |
| 152 | |
| 153 | m2.data()[0+2*3] = -0.213 * c; |
| 154 | m2.data()[1+2*3] = -0.715 * c; |
| 155 | m2.data()[2+2*3] = 0.928 * c; |
| 156 | |
| 157 | QMatrix3x3 m3; |
| 158 | m3.data()[0+0*3] = -0.213 * s; |
| 159 | m3.data()[1+0*3] = -0.715 * s; |
| 160 | m3.data()[2+0*3] = 0.928 * s; |
| 161 | |
| 162 | m3.data()[0+1*3] = 0.143 * s; |
| 163 | m3.data()[1+1*3] = 0.140 * s; |
| 164 | m3.data()[2+1*3] = -0.283 * s; |
| 165 | |
| 166 | m3.data()[0+2*3] = -0.787 * s; |
| 167 | m3.data()[1+2*3] = 0.715 * s; |
| 168 | m3.data()[2+2*3] = 0.072 * s; |
| 169 | |
| 170 | QMatrix3x3 m = m1 + m2 + m3; |
| 171 | |
| 172 | m_matrix.data()[0+0*5] = m.data()[0+0*3]; |
| 173 | m_matrix.data()[1+0*5] = m.data()[1+0*3]; |
| 174 | m_matrix.data()[2+0*5] = m.data()[2+0*3]; |
| 175 | |
| 176 | m_matrix.data()[0+1*5] = m.data()[0+1*3]; |
| 177 | m_matrix.data()[1+1*5] = m.data()[1+1*3]; |
| 178 | m_matrix.data()[2+1*5] = m.data()[2+1*3]; |
| 179 | |
| 180 | m_matrix.data()[0+2*5] = m.data()[0+2*3]; |
| 181 | m_matrix.data()[1+2*5] = m.data()[1+2*3]; |
| 182 | m_matrix.data()[2+2*5] = m.data()[2+2*3]; |
| 183 | |
| 184 | m_matrix.data()[3+3*5] = 1; |
| 185 | } else if (type == ColorShiftType::LuminanceToAlpha){ |
| 186 | m_matrix.fill(value: 0); |
| 187 | |
| 188 | m_matrix.data()[0+3*5] = 0.2125; |
| 189 | m_matrix.data()[1+3*5] = 0.7154; |
| 190 | m_matrix.data()[2+3*5] = 0.0721; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | QSvgNode::Type QSvgFeColorMatrix::type() const |
| 195 | { |
| 196 | return QSvgNode::FeColormatrix; |
| 197 | } |
| 198 | |
| 199 | QImage QSvgFeColorMatrix::apply(const QMap<QString, QImage> &sources, QPainter *p, |
| 200 | const QRectF &itemBounds, const QRectF &filterBounds, |
| 201 | QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits) const |
| 202 | { |
| 203 | if (!sources.contains(key: m_input)) |
| 204 | return QImage(); |
| 205 | QImage source = sources[m_input]; |
| 206 | |
| 207 | QRect clipRectGlob = globalSubRegion(p, itemBounds, filterBounds, primitiveUnits, filterUnits).toRect(); |
| 208 | if (clipRectGlob.isEmpty()) |
| 209 | return QImage(); |
| 210 | |
| 211 | QImage result; |
| 212 | if (!QImageIOHandler::allocateImage(size: clipRectGlob.size(), format: QImage::Format_ARGB32_Premultiplied, image: &result)) { |
| 213 | qCWarning(lcSvgDraw) << "The requested filter buffer is too big, ignoring" ; |
| 214 | return QImage(); |
| 215 | } |
| 216 | result.setOffset(clipRectGlob.topLeft()); |
| 217 | result.fill(color: Qt::transparent); |
| 218 | |
| 219 | Q_ASSERT(source.depth() == 32); |
| 220 | |
| 221 | for (int i = 0; i < result.height(); i++) { |
| 222 | int sourceI = i - source.offset().y() + result.offset().y(); |
| 223 | |
| 224 | if (sourceI < 0 || sourceI >= source.height()) |
| 225 | continue; |
| 226 | |
| 227 | QRgb *sourceLine = reinterpret_cast<QRgb *>(source.scanLine(sourceI)); |
| 228 | QRgb *resultLine = reinterpret_cast<QRgb *>(result.scanLine(i)); |
| 229 | |
| 230 | for (int j = 0; j < result.width(); j++) { |
| 231 | int sourceJ = j - source.offset().x() + result.offset().x(); |
| 232 | |
| 233 | if (sourceJ < 0 || sourceJ >= source.width()) |
| 234 | continue; |
| 235 | |
| 236 | QRgb sourceColor = qUnpremultiply(p: sourceLine[sourceJ]); |
| 237 | qreal a = qAlpha(rgb: sourceColor); |
| 238 | qreal r = qRed(rgb: sourceColor); |
| 239 | qreal g = qGreen(rgb: sourceColor); |
| 240 | qreal b = qBlue(rgb: sourceColor); |
| 241 | |
| 242 | qreal r2 = m_matrix.data()[0+0*5] * r + |
| 243 | m_matrix.data()[1+0*5] * g + |
| 244 | m_matrix.data()[2+0*5] * b + |
| 245 | m_matrix.data()[3+0*5] * a + |
| 246 | m_matrix.data()[4+0*5] * 255.; |
| 247 | qreal g2 = m_matrix.data()[0+1*5] * r + |
| 248 | m_matrix.data()[1+1*5] * g + |
| 249 | m_matrix.data()[2+1*5] * b + |
| 250 | m_matrix.data()[3+1*5] * a + |
| 251 | m_matrix.data()[4+1*5] * 255.; |
| 252 | qreal b2 = m_matrix.data()[0+2*5] * r + |
| 253 | m_matrix.data()[1+2*5] * g + |
| 254 | m_matrix.data()[2+2*5] * b + |
| 255 | m_matrix.data()[3+2*5] * a + |
| 256 | m_matrix.data()[4+2*5] * 255.; |
| 257 | qreal a2 = m_matrix.data()[0+3*5] * r + |
| 258 | m_matrix.data()[1+3*5] * g + |
| 259 | m_matrix.data()[2+3*5] * b + |
| 260 | m_matrix.data()[3+3*5] * a + |
| 261 | m_matrix.data()[4+3*5] * 255.; |
| 262 | |
| 263 | QRgb rgba = qRgba(r: qBound(min: 0, val: int(r2), max: 255), |
| 264 | g: qBound(min: 0, val: int(g2), max: 255), |
| 265 | b: qBound(min: 0, val: int(b2), max: 255), |
| 266 | a: qBound(min: 0, val: int(a2), max: 255)); |
| 267 | resultLine[j] = qPremultiply(x: rgba); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | clipToTransformedBounds(buffer: &result, p, localRect: localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits)); |
| 272 | return result; |
| 273 | } |
| 274 | |
| 275 | QSvgFeGaussianBlur::QSvgFeGaussianBlur(QSvgNode *parent, QString input, QString result, const QSvgRectF &rect, |
| 276 | qreal stdDeviationX, qreal stdDeviationY, EdgeMode edgemode) |
| 277 | : QSvgFeFilterPrimitive(parent, input, result, rect) |
| 278 | , m_stdDeviationX(stdDeviationX) |
| 279 | , m_stdDeviationY(stdDeviationY) |
| 280 | , m_edgemode(edgemode) |
| 281 | { |
| 282 | (void)m_edgemode; |
| 283 | } |
| 284 | |
| 285 | QSvgNode::Type QSvgFeGaussianBlur::type() const |
| 286 | { |
| 287 | return QSvgNode::FeGaussianblur; |
| 288 | } |
| 289 | |
| 290 | QImage QSvgFeGaussianBlur::apply(const QMap<QString, QImage> &sources, QPainter *p, |
| 291 | const QRectF &itemBounds, const QRectF &filterBounds, |
| 292 | QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits) const |
| 293 | { |
| 294 | if (!sources.contains(key: m_input)) |
| 295 | return QImage(); |
| 296 | QImage source = sources[m_input]; |
| 297 | Q_ASSERT(source.depth() == 32); |
| 298 | |
| 299 | if (m_stdDeviationX == 0 && m_stdDeviationY == 0) |
| 300 | return source; |
| 301 | |
| 302 | const qreal scaleX = qHypot(x: p->transform().m11(), y: p->transform().m21()); |
| 303 | const qreal scaleY = qHypot(x: p->transform().m12(), y: p->transform().m22()); |
| 304 | |
| 305 | qreal sigma_x = scaleX * m_stdDeviationX; |
| 306 | qreal sigma_y = scaleY * m_stdDeviationY; |
| 307 | if (primitiveUnits == QtSvg::UnitTypes::objectBoundingBox) { |
| 308 | sigma_x *= itemBounds.width(); |
| 309 | sigma_y *= itemBounds.height(); |
| 310 | } |
| 311 | |
| 312 | constexpr double sd = 3. * M_SQRT1_2 / M_2_SQRTPI; // 3 * sqrt(2 * pi) / 4 |
| 313 | const int dx = floor(x: sigma_x * sd + 0.5); |
| 314 | const int dy = floor(x: sigma_y * sd + 0.5); |
| 315 | |
| 316 | const QTransform scaleXr = QTransform::fromScale(dx: scaleX, dy: scaleY); |
| 317 | const QTransform restXr = scaleXr.inverted() * p->transform(); |
| 318 | |
| 319 | QRect clipRectGlob = scaleXr.mapRect(localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits)).toRect(); |
| 320 | if (clipRectGlob.isEmpty()) |
| 321 | return QImage(); |
| 322 | |
| 323 | QImage tempSource; |
| 324 | if (!QImageIOHandler::allocateImage(size: clipRectGlob.size(), format: QImage::Format_ARGB32_Premultiplied, image: &tempSource)) { |
| 325 | qCWarning(lcSvgDraw) << "The requested filter buffer is too big, ignoring" ; |
| 326 | return QImage(); |
| 327 | } |
| 328 | tempSource.setOffset(clipRectGlob.topLeft()); |
| 329 | tempSource.fill(color: Qt::transparent); |
| 330 | QPainter copyPainter(&tempSource); |
| 331 | copyPainter.translate(offset: -tempSource.offset()); |
| 332 | copyPainter.setTransform(transform: restXr.inverted(), combine: true); |
| 333 | copyPainter.drawImage(p: source.offset(), image: source); |
| 334 | copyPainter.end(); |
| 335 | |
| 336 | QVarLengthArray<uint64_t, 32 * 32> buffer(tempSource.width() * tempSource.height()); |
| 337 | |
| 338 | const int sourceHeight = tempSource.height(); |
| 339 | const int sourceWidth = tempSource.width(); |
| 340 | QRgb *rawImage = reinterpret_cast<QRgb *>(tempSource.bits()); |
| 341 | |
| 342 | // https://www.w3.org/TR/SVG11/filters.html#feGaussianBlurElement: |
| 343 | // Three successive box-blurs build a piece-wise quadratic convolution kernel, |
| 344 | // which approximates the Gaussian kernel |
| 345 | for (int m = 0; m < 3; m++) { |
| 346 | for (int col = 0; col < 4 * 8; col += 8 ){ |
| 347 | // Generating the partial sum of color values from the top left corner |
| 348 | // These sums can be combined to yield the partial sum of any rectangular subregion |
| 349 | for (int i = 0; i < sourceWidth; i++) { |
| 350 | for (int j = 0; j < sourceHeight; j++) { |
| 351 | buffer[i + j * sourceWidth] = (rawImage[i + j * sourceWidth] >> col) & 0xff; |
| 352 | if (i > 0) |
| 353 | buffer[i + j * sourceWidth] += buffer[(i - 1) + j * sourceWidth]; |
| 354 | if (j > 0) |
| 355 | buffer[i + j * sourceWidth] += buffer[i + (j - 1) * sourceWidth]; |
| 356 | if (i > 0 && j > 0) |
| 357 | buffer[i + j * sourceWidth] -= buffer[(i - 1) + (j - 1) * sourceWidth]; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // https://www.w3.org/TR/SVG11/filters.html#feGaussianBlurElement: |
| 362 | // if d is odd, use three box-blurs of size 'd', centered on the output pixel. |
| 363 | // if d is even, two box-blurs of size 'd' (the first one centered on the pixel boundary |
| 364 | // between the output pixel and the one to the left, the second one centered on the pixel |
| 365 | // boundary between the output pixel and the one to the right) and one box blur of size |
| 366 | // 'd+1' centered on the output pixel. |
| 367 | auto adjustD = [](int d, int iteration) { |
| 368 | d = qMax(a: 1, b: d); // Treat d == 0 just like d == 1 |
| 369 | std::pair<int, int> result; |
| 370 | if (d % 2 == 1) |
| 371 | result = {d / 2 + 1, d / 2}; |
| 372 | else if (iteration == 0) |
| 373 | result = {d / 2 + 1, d / 2 - 1}; |
| 374 | else if (iteration == 1) |
| 375 | result = {d / 2, d / 2}; |
| 376 | else |
| 377 | result = {d / 2 + 1, d / 2}; |
| 378 | Q_ASSERT(result.first + result.second > 0); |
| 379 | return result; |
| 380 | }; |
| 381 | |
| 382 | const auto [dxleft, dxright] = adjustD(dx, m); |
| 383 | const auto [dytop, dybottom] = adjustD(dy, m); |
| 384 | for (int i = 0; i < sourceWidth; i++) { |
| 385 | for (int j = 0; j < sourceHeight; j++) { |
| 386 | const int i1 = qMax(a: 0, b: i - dxleft); |
| 387 | const int i2 = qMin(a: sourceWidth - 1, b: i + dxright); |
| 388 | const int j1 = qMax(a: 0, b: j - dytop); |
| 389 | const int j2 = qMin(a: sourceHeight - 1, b: j + dybottom); |
| 390 | |
| 391 | uint64_t colorValue64 = buffer[i2 + j2 * sourceWidth]; |
| 392 | colorValue64 -= buffer[i1 + j2 * sourceWidth]; |
| 393 | colorValue64 -= buffer[i2 + j1 * sourceWidth]; |
| 394 | colorValue64 += buffer[i1 + j1 * sourceWidth]; |
| 395 | colorValue64 /= uint64_t(dxleft + dxright) * uint64_t(dytop + dybottom); |
| 396 | |
| 397 | const unsigned int colorValue = colorValue64; |
| 398 | rawImage[i + j * sourceWidth] &= ~(0xff << col); |
| 399 | rawImage[i + j * sourceWidth] |= colorValue << col; |
| 400 | |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | QRectF trueClipRectGlob = globalSubRegion(p, itemBounds, filterBounds, primitiveUnits, filterUnits); |
| 407 | |
| 408 | QImage result; |
| 409 | if (!QImageIOHandler::allocateImage(size: trueClipRectGlob.toRect().size(), format: QImage::Format_ARGB32_Premultiplied, image: &result)) { |
| 410 | qCWarning(lcSvgDraw) << "The requested filter buffer is too big, ignoring" ; |
| 411 | return QImage(); |
| 412 | } |
| 413 | result.setOffset(trueClipRectGlob.toRect().topLeft()); |
| 414 | result.fill(color: Qt::transparent); |
| 415 | QPainter transformPainter(&result); |
| 416 | transformPainter.setRenderHint(hint: QPainter::Antialiasing, on: true); |
| 417 | |
| 418 | transformPainter.translate(offset: -result.offset()); |
| 419 | transformPainter.setTransform(transform: restXr, combine: true); |
| 420 | transformPainter.drawImage(p: clipRectGlob.topLeft(), image: tempSource); |
| 421 | transformPainter.end(); |
| 422 | |
| 423 | clipToTransformedBounds(buffer: &result, p, localRect: localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits)); |
| 424 | return result; |
| 425 | } |
| 426 | |
| 427 | QSvgFeOffset::QSvgFeOffset(QSvgNode *parent, QString input, QString result, const QSvgRectF &rect, |
| 428 | qreal dx, qreal dy) |
| 429 | : QSvgFeFilterPrimitive(parent, input, result, rect) |
| 430 | , m_dx(dx) |
| 431 | , m_dy(dy) |
| 432 | { |
| 433 | |
| 434 | } |
| 435 | |
| 436 | QSvgNode::Type QSvgFeOffset::type() const |
| 437 | { |
| 438 | return QSvgNode::FeOffset; |
| 439 | } |
| 440 | |
| 441 | QImage QSvgFeOffset::apply(const QMap<QString, QImage> &sources, QPainter *p, |
| 442 | const QRectF &itemBounds, const QRectF &filterBounds, |
| 443 | QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits) const |
| 444 | { |
| 445 | if (!sources.contains(key: m_input)) |
| 446 | return QImage(); |
| 447 | |
| 448 | const QImage &source = sources[m_input]; |
| 449 | |
| 450 | QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits); |
| 451 | QRect clipRectGlob = p->transform().mapRect(clipRect).toRect(); |
| 452 | |
| 453 | QPoint offset(m_dx, m_dy); |
| 454 | if (primitiveUnits == QtSvg::UnitTypes::objectBoundingBox) { |
| 455 | offset = QPoint(m_dx * itemBounds.width(), |
| 456 | m_dy * itemBounds.height()); |
| 457 | } |
| 458 | offset = p->transform().map(p: offset) - p->transform().map(p: QPoint(0, 0)); |
| 459 | |
| 460 | if (clipRectGlob.isEmpty()) |
| 461 | return QImage(); |
| 462 | |
| 463 | QImage result; |
| 464 | if (!QImageIOHandler::allocateImage(size: clipRectGlob.size(), format: QImage::Format_ARGB32_Premultiplied, image: &result)) { |
| 465 | qCWarning(lcSvgDraw) << "The requested filter buffer is too big, ignoring" ; |
| 466 | return QImage(); |
| 467 | } |
| 468 | result.setOffset(clipRectGlob.topLeft()); |
| 469 | result.fill(color: Qt::transparent); |
| 470 | |
| 471 | QPainter copyPainter(&result); |
| 472 | copyPainter.drawImage(p: source.offset() |
| 473 | - result.offset() + offset, image: source); |
| 474 | copyPainter.end(); |
| 475 | |
| 476 | clipToTransformedBounds(buffer: &result, p, localRect: clipRect); |
| 477 | return result; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | QSvgFeMerge::QSvgFeMerge(QSvgNode *parent, QString input, QString result, const QSvgRectF &rect) |
| 482 | : QSvgFeFilterPrimitive(parent, input, result, rect) |
| 483 | { |
| 484 | |
| 485 | } |
| 486 | |
| 487 | QSvgNode::Type QSvgFeMerge::type() const |
| 488 | { |
| 489 | return QSvgNode::FeMerge; |
| 490 | } |
| 491 | |
| 492 | QImage QSvgFeMerge::apply(const QMap<QString, QImage> &sources, QPainter *p, |
| 493 | const QRectF &itemBounds, const QRectF &filterBounds, |
| 494 | QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits) const |
| 495 | { |
| 496 | QList<QImage> mergeNodeResults; |
| 497 | for (int i = 0; i < renderers().size(); i++) { |
| 498 | QSvgNode *child = renderers().at(i); |
| 499 | if (child->type() == QSvgNode::FeMergenode) { |
| 500 | QSvgFeMergeNode *filter = static_cast<QSvgFeMergeNode*>(child); |
| 501 | mergeNodeResults.append(t: filter->apply(sources, p, itemBounds, filterBounds, primitiveUnits, filterUnits)); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits); |
| 506 | QRect clipRectGlob = p->transform().mapRect(clipRect).toRect(); |
| 507 | if (clipRectGlob.isEmpty()) |
| 508 | return QImage(); |
| 509 | |
| 510 | QImage result; |
| 511 | if (!QImageIOHandler::allocateImage(size: clipRectGlob.size(), format: QImage::Format_ARGB32_Premultiplied, image: &result)) { |
| 512 | qCWarning(lcSvgDraw) << "The requested filter buffer is too big, ignoring" ; |
| 513 | return QImage(); |
| 514 | } |
| 515 | result.setOffset(clipRectGlob.topLeft()); |
| 516 | result.fill(color: Qt::transparent); |
| 517 | |
| 518 | QPainter proxyPainter(&result); |
| 519 | for (const QImage &i : mergeNodeResults) { |
| 520 | proxyPainter.drawImage(r: QRect(i.offset() - result.offset(), i.size()), image: i); |
| 521 | } |
| 522 | proxyPainter.end(); |
| 523 | |
| 524 | clipToTransformedBounds(buffer: &result, p, localRect: clipRect); |
| 525 | return result; |
| 526 | } |
| 527 | |
| 528 | bool QSvgFeMerge::requiresSourceAlpha() const |
| 529 | { |
| 530 | for (int i = 0; i < renderers().size(); i++) { |
| 531 | QSvgNode *child = renderers().at(i); |
| 532 | if (child->type() == QSvgNode::FeMergenode) { |
| 533 | QSvgFeMergeNode *filter = static_cast<QSvgFeMergeNode *>(child); |
| 534 | if (filter->requiresSourceAlpha()) |
| 535 | return true; |
| 536 | } |
| 537 | } |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | QSvgFeMergeNode::QSvgFeMergeNode(QSvgNode *parent, QString input, QString result, const QSvgRectF &rect) |
| 542 | : QSvgFeFilterPrimitive(parent, input, result, rect) |
| 543 | { |
| 544 | |
| 545 | } |
| 546 | |
| 547 | QSvgNode::Type QSvgFeMergeNode::type() const |
| 548 | { |
| 549 | return QSvgNode::FeMergenode; |
| 550 | } |
| 551 | |
| 552 | QImage QSvgFeMergeNode::apply(const QMap<QString, QImage> &sources, QPainter *, |
| 553 | const QRectF &, const QRectF &, QtSvg::UnitTypes, QtSvg::UnitTypes) const |
| 554 | { |
| 555 | return sources.value(key: m_input); |
| 556 | } |
| 557 | |
| 558 | QSvgFeComposite::QSvgFeComposite(QSvgNode *parent, QString input, QString result, const QSvgRectF &rect, |
| 559 | QString input2, Operator op, QVector4D k) |
| 560 | : QSvgFeFilterPrimitive(parent, input, result, rect) |
| 561 | , m_input2(input2) |
| 562 | , m_operator(op) |
| 563 | , m_k(k) |
| 564 | { |
| 565 | |
| 566 | } |
| 567 | |
| 568 | QSvgNode::Type QSvgFeComposite::type() const |
| 569 | { |
| 570 | return QSvgNode::FeComposite; |
| 571 | } |
| 572 | |
| 573 | QImage QSvgFeComposite::apply(const QMap<QString, QImage> &sources, QPainter *p, |
| 574 | const QRectF &itemBounds, const QRectF &filterBounds, |
| 575 | QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits) const |
| 576 | { |
| 577 | if (!sources.contains(key: m_input)) |
| 578 | return QImage(); |
| 579 | if (!sources.contains(key: m_input2)) |
| 580 | return QImage(); |
| 581 | QImage source1 = sources[m_input]; |
| 582 | QImage source2 = sources[m_input2]; |
| 583 | Q_ASSERT(source1.depth() == 32); |
| 584 | Q_ASSERT(source2.depth() == 32); |
| 585 | |
| 586 | QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits); |
| 587 | QRect clipRectGlob = p->transform().mapRect(clipRect).toRect(); |
| 588 | |
| 589 | if (clipRectGlob.isEmpty()) |
| 590 | return QImage(); |
| 591 | |
| 592 | QImage result; |
| 593 | if (!QImageIOHandler::allocateImage(size: clipRectGlob.size(), format: QImage::Format_ARGB32_Premultiplied, image: &result)) { |
| 594 | qCWarning(lcSvgDraw) << "The requested filter buffer is too big, ignoring" ; |
| 595 | return QImage(); |
| 596 | } |
| 597 | result.setOffset(clipRectGlob.topLeft()); |
| 598 | result.fill(color: Qt::transparent); |
| 599 | |
| 600 | if (m_operator == Operator::Arithmetic) { |
| 601 | const qreal k1 = m_k.x(); |
| 602 | const qreal k2 = m_k.y(); |
| 603 | const qreal k3 = m_k.z(); |
| 604 | const qreal k4 = m_k.w(); |
| 605 | |
| 606 | for (int j = 0; j < result.height(); j++) { |
| 607 | int jj1 = j - source1.offset().y() + result.offset().y(); |
| 608 | int jj2 = j - source2.offset().y() + result.offset().y(); |
| 609 | |
| 610 | QRgb *resultLine = reinterpret_cast<QRgb *>(result.scanLine(j)); |
| 611 | QRgb *source1Line = nullptr; |
| 612 | QRgb *source2Line = nullptr; |
| 613 | |
| 614 | if (jj1 >= 0 && jj1 < source1.size().height()) |
| 615 | source1Line = reinterpret_cast<QRgb *>(source1.scanLine(jj1)); |
| 616 | if (jj2 >= 0 && jj2 < source2.size().height()) |
| 617 | source2Line = reinterpret_cast<QRgb *>(source2.scanLine(jj2)); |
| 618 | |
| 619 | for (int i = 0; i < result.width(); i++) { |
| 620 | int ii1 = i - source1.offset().x() + result.offset().x(); |
| 621 | int ii2 = i - source2.offset().x() + result.offset().x(); |
| 622 | |
| 623 | QVector4D s1 = QVector4D(0, 0, 0, 0); |
| 624 | QVector4D s2 = QVector4D(0, 0, 0, 0); |
| 625 | |
| 626 | if (ii1 >= 0 && ii1 < source1.size().width() && source1Line) { |
| 627 | QRgb pixel1 = source1Line[ii1]; |
| 628 | s1 = QVector4D(qRed(rgb: pixel1), |
| 629 | qGreen(rgb: pixel1), |
| 630 | qBlue(rgb: pixel1), |
| 631 | qAlpha(rgb: pixel1)); |
| 632 | } |
| 633 | |
| 634 | if (ii2 >= 0 && ii2 < source2.size().width() && source2Line) { |
| 635 | QRgb pixel2 = source2Line[ii2]; |
| 636 | s2 = QVector4D(qRed(rgb: pixel2), |
| 637 | qGreen(rgb: pixel2), |
| 638 | qBlue(rgb: pixel2), |
| 639 | qAlpha(rgb: pixel2)); |
| 640 | } |
| 641 | |
| 642 | int r = k1 * s1.x() * s2.x() / 255. + k2 * s1.x() + k3 * s2.x() + k4 * 255.; |
| 643 | int g = k1 * s1.y() * s2.y() / 255. + k2 * s1.y() + k3 * s2.y() + k4 * 255.; |
| 644 | int b = k1 * s1.z() * s2.z() / 255. + k2 * s1.z() + k3 * s2.z() + k4 * 255.; |
| 645 | int a = k1 * s1.w() * s2.w() / 255. + k2 * s1.w() + k3 * s2.w() + k4 * 255.; |
| 646 | |
| 647 | a = qBound(min: 0, val: a, max: 255); |
| 648 | resultLine[i] = qRgba(r: qBound(min: 0, val: r, max: a), |
| 649 | g: qBound(min: 0, val: g, max: a), |
| 650 | b: qBound(min: 0, val: b, max: a), |
| 651 | a); |
| 652 | } |
| 653 | } |
| 654 | } else { |
| 655 | QPainter proxyPainter(&result); |
| 656 | proxyPainter.drawImage(r: QRect(source1.offset() - result.offset(), source1.size()), image: source1); |
| 657 | |
| 658 | switch (m_operator) { |
| 659 | case Operator::In: |
| 660 | proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn); |
| 661 | break; |
| 662 | case Operator::Out: |
| 663 | proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationOut); |
| 664 | break; |
| 665 | case Operator::Xor: |
| 666 | proxyPainter.setCompositionMode(QPainter::CompositionMode_Xor); |
| 667 | break; |
| 668 | case Operator::Lighter: |
| 669 | proxyPainter.setCompositionMode(QPainter::CompositionMode_Lighten); |
| 670 | break; |
| 671 | case Operator::Atop: |
| 672 | proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationAtop); |
| 673 | break; |
| 674 | case Operator::Over: |
| 675 | proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationOver); |
| 676 | break; |
| 677 | case Operator::Arithmetic: // handled above |
| 678 | Q_UNREACHABLE(); |
| 679 | break; |
| 680 | } |
| 681 | proxyPainter.drawImage(r: QRect(source2.offset()-result.offset(), source2.size()), image: source2); |
| 682 | proxyPainter.end(); |
| 683 | } |
| 684 | |
| 685 | clipToTransformedBounds(buffer: &result, p, localRect: clipRect); |
| 686 | return result; |
| 687 | } |
| 688 | |
| 689 | bool QSvgFeComposite::requiresSourceAlpha() const |
| 690 | { |
| 691 | if (QSvgFeFilterPrimitive::requiresSourceAlpha()) |
| 692 | return true; |
| 693 | return m_input2 == QLatin1StringView("SourceAlpha" ); |
| 694 | } |
| 695 | |
| 696 | |
| 697 | QSvgFeFlood::QSvgFeFlood(QSvgNode *parent, QString input, QString result, |
| 698 | const QSvgRectF &rect, const QColor &color) |
| 699 | : QSvgFeFilterPrimitive(parent, input, result, rect) |
| 700 | , m_color(color) |
| 701 | { |
| 702 | |
| 703 | } |
| 704 | |
| 705 | QSvgNode::Type QSvgFeFlood::type() const |
| 706 | { |
| 707 | return QSvgNode::FeFlood; |
| 708 | } |
| 709 | |
| 710 | QImage QSvgFeFlood::apply(const QMap<QString, QImage> &, |
| 711 | QPainter *p, const QRectF &itemBounds, const QRectF &filterBounds, |
| 712 | QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits) const |
| 713 | { |
| 714 | |
| 715 | QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits); |
| 716 | QRect clipRectGlob = p->transform().mapRect(clipRect).toRect(); |
| 717 | |
| 718 | QImage result; |
| 719 | if (!QImageIOHandler::allocateImage(size: clipRectGlob.size(), format: QImage::Format_ARGB32_Premultiplied, image: &result)) { |
| 720 | qCWarning(lcSvgDraw) << "The requested filter buffer is too big, ignoring" ; |
| 721 | return QImage(); |
| 722 | } |
| 723 | result.setOffset(clipRectGlob.topLeft()); |
| 724 | result.fill(color: m_color); |
| 725 | |
| 726 | clipToTransformedBounds(buffer: &result, p, localRect: clipRect); |
| 727 | return result; |
| 728 | } |
| 729 | |
| 730 | QSvgFeUnsupported::QSvgFeUnsupported(QSvgNode *parent, QString input, QString result, |
| 731 | const QSvgRectF &rect) |
| 732 | : QSvgFeFilterPrimitive(parent, input, result, rect) |
| 733 | { |
| 734 | } |
| 735 | |
| 736 | QSvgNode::Type QSvgFeUnsupported::type() const |
| 737 | { |
| 738 | return QSvgNode::FeUnsupported; |
| 739 | } |
| 740 | |
| 741 | QImage QSvgFeUnsupported::apply(const QMap<QString, QImage> &, |
| 742 | QPainter *, const QRectF &, const QRectF &, |
| 743 | QtSvg::UnitTypes, QtSvg::UnitTypes) const |
| 744 | { |
| 745 | qCDebug(lcSvgDraw) << "Unsupported filter primitive should not be applied." ; |
| 746 | return QImage(); |
| 747 | } |
| 748 | |
| 749 | QT_END_NAMESPACE |
| 750 | |