104QSvgFeColorMatrix::QSvgFeColorMatrix(QSvgNode *parent,
const QString &input,
const QString &result,
105 const QSvgRectF &rect, ColorShiftType type,
106 const Matrix &matrix)
107 : QSvgFeFilterPrimitive(parent, input, result, rect)
111 constexpr qreal v1[] = {0.213, 0.213, 0.213,
113 0.072, 0.072, 0.072};
114 static const Matrix3x3 m1(v1);
116 constexpr qreal v2[] = { 0.787, -0.213, -0.213,
117 -0.715, 0.285, -0.715,
118 -0.072, -0.072, 0.928};
119 static const Matrix3x3 m2(v2);
121 constexpr qreal v3[] = {-0.213, 0.143, -0.787,
122 -0.715, 0.140, 0.715,
123 0.928, -0.283, 0.072};
124 static const Matrix3x3 m3(v3);
126 if (type == ColorShiftType::Saturate) {
127 qreal s = qBound(0., m_matrix.data()[0], 1.);
131 const Matrix3x3 m = m1 + m2 * s;
133 for (
int j = 0; j < 3; ++j)
134 for (
int i = 0; i < 3; ++i)
135 m_matrix.data()[i+j*5] = m.data()[i+j*3];
136 m_matrix.data()[3+3*5] = 1;
138 }
else if (type == ColorShiftType::HueRotate){
139 qreal angle = m_matrix.data()[0]/180.*M_PI;
140 qreal s = sin(angle);
141 qreal c = cos(angle);
145 const Matrix3x3 m = m1 + m2 * c + m3 * s;
147 for (
int j = 0; j < 3; ++j)
148 for (
int i = 0; i < 3; ++i)
149 m_matrix.data()[i+j*5] = m.data()[i+j*3];
150 m_matrix.data()[3+3*5] = 1;
151 }
else if (type == ColorShiftType::LuminanceToAlpha){
154 m_matrix.data()[0+3*5] = 0.2125;
155 m_matrix.data()[1+3*5] = 0.7154;
156 m_matrix.data()[2+3*5] = 0.0721;
168QImage QSvgFeColorMatrix::apply(
const QMap<QString, QImage> &sources, QPainter *p,
169 const QRectF &itemBounds,
const QRectF &filterBounds,
170 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
172 if (!sources.contains(m_input))
174 QImage source = sources[m_input];
176 QRect clipRectGlob = globalSubRegion(p, itemBounds, filterBounds, primitiveUnits, filterUnits).toRect();
177 if (clipRectGlob.isEmpty())
181 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
182 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
185 result.setOffset(clipRectGlob.topLeft());
186 result.fill(Qt::transparent);
188 Q_ASSERT(source.depth() == 32);
190 const Matrix transposedMatrix = m_matrix.transposed();
192 for (
int i = 0; i < result.height(); i++) {
193 int sourceI = i - source.offset().y() + result.offset().y();
195 if (sourceI < 0 || sourceI >= source.height())
198 QRgb *sourceLine =
reinterpret_cast<QRgb *>(source.scanLine(sourceI));
199 QRgb *resultLine =
reinterpret_cast<QRgb *>(result.scanLine(i));
201 for (
int j = 0; j < result.width(); j++) {
202 int sourceJ = j - source.offset().x() + result.offset().x();
204 if (sourceJ < 0 || sourceJ >= source.width())
207 QRgb sourceColor = qUnpremultiply(sourceLine[sourceJ]);
208 const qreal values[] = {qreal(qRed(sourceColor)),
209 qreal(qGreen(sourceColor)),
210 qreal(qBlue(sourceColor)),
211 qreal(qAlpha(sourceColor)),
213 const QGenericMatrix<1, 5, qreal> sourceVector(values);
214 const QGenericMatrix<1, 5, qreal> resultVector = transposedMatrix * sourceVector;
216 QRgb rgba = qRgba(qBound(0,
int(resultVector(0, 0)), 255),
217 qBound(0,
int(resultVector(1, 0)), 255),
218 qBound(0,
int(resultVector(2, 0)), 255),
219 qBound(0,
int(resultVector(3, 0)), 255));
220 resultLine[j] = qPremultiply(rgba);
224 clipToTransformedBounds(&result, p, localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits));
310QImage QSvgFeGaussianBlur::apply(
const QMap<QString, QImage> &sources, QPainter *p,
311 const QRectF &itemBounds,
const QRectF &filterBounds,
312 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
314 if (!sources.contains(m_input))
316 QImage source = sources[m_input];
317 Q_ASSERT(source.depth() == 32);
319 if (m_stdDeviationX == 0 && m_stdDeviationY == 0)
322 const qreal scaleX = qHypot(p->transform().m11(), p->transform().m21());
323 const qreal scaleY = qHypot(p->transform().m12(), p->transform().m22());
325 qreal sigma_x = scaleX * m_stdDeviationX;
326 qreal sigma_y = scaleY * m_stdDeviationY;
327 if (primitiveUnits == QtSvg::UnitTypes::objectBoundingBox) {
328 sigma_x *= itemBounds.width();
329 sigma_y *= itemBounds.height();
332 constexpr double sd = 3. * M_SQRT1_2 / M_2_SQRTPI;
333 const int dx = floor(sigma_x * sd + 0.5);
334 const int dy = floor(sigma_y * sd + 0.5);
336 const QTransform scaleXr = QTransform::fromScale(scaleX, scaleY);
337 const QTransform restXr = scaleXr.inverted() * p->transform();
339 QRect clipRectGlob = scaleXr.mapRect(localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits)).toRect();
340 if (clipRectGlob.isEmpty())
344 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &tempSource)) {
345 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
348 tempSource.setOffset(clipRectGlob.topLeft());
349 tempSource.fill(Qt::transparent);
350 QPainter copyPainter(&tempSource);
351 copyPainter.translate(-tempSource.offset());
352 copyPainter.setTransform(restXr.inverted(),
true);
353 copyPainter.drawImage(source.offset(), source);
356 QVarLengthArray<ColorValues, 32 * 32> buffer(tempSource.width() * tempSource.height());
358 const int sourceHeight = tempSource.height();
359 const int sourceWidth = tempSource.width();
360 QRgb *rawImage =
reinterpret_cast<QRgb *>(tempSource.bits());
365 for (
int m = 0; m < 3; m++) {
372 auto adjustD = [](
int d,
int iteration) {
374 std::pair<
int,
int> result;
376 result = {d / 2 + 1, d / 2};
377 else if (iteration == 0)
378 result = {d / 2 + 1, d / 2 - 1};
379 else if (iteration == 1)
380 result = {d / 2, d / 2};
382 result = {d / 2 + 1, d / 2};
383 Q_ASSERT(result.first + result.second > 0);
387 const auto [dxleft, dxright] = adjustD(dx, m);
388 const auto [dytop, dybottom] = adjustD(dy, m);
392 for (
int j = 0; j < sourceHeight; j++) {
393 for (
int i = 0; i < sourceWidth; i++) {
394 ColorValues colorValues(rawImage[i + j * sourceWidth]);
396 colorValues += buffer[(i - 1) + j * sourceWidth];
398 colorValues += buffer[i + (j - 1) * sourceWidth];
400 colorValues -= buffer[(i - 1) + (j - 1) * sourceWidth];
401 buffer[i + j * sourceWidth] = colorValues;
405 for (
int j = 0; j < sourceHeight; j++) {
406 const int j1 = qMax(0, j - dytop);
407 const int j2 = qMin(sourceHeight - 1, j + dybottom);
408 for (
int i = 0; i < sourceWidth; i++) {
409 const int i1 = qMax(0, i - dxleft);
410 const int i2 = qMin(sourceWidth - 1, i + dxright);
411 ColorValues colorValues = buffer[i2 + j2 * sourceWidth]
412 - buffer[i1 + j2 * sourceWidth]
413 - buffer[i2 + j1 * sourceWidth]
414 + buffer[i1 + j1 * sourceWidth];
415 colorValues /= uint64_t(dxleft + dxright) * uint64_t(dytop + dybottom);
416 rawImage[i + j * sourceWidth] = colorValues.toRgb();
421 QRectF trueClipRectGlob = globalSubRegion(p, itemBounds, filterBounds, primitiveUnits, filterUnits);
424 if (!QImageIOHandler::allocateImage(trueClipRectGlob.toRect().size(), QImage::Format_ARGB32_Premultiplied, &result)) {
425 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
428 result.setOffset(trueClipRectGlob.toRect().topLeft());
429 result.fill(Qt::transparent);
430 QPainter transformPainter(&result);
431 transformPainter.setRenderHint(QPainter::Antialiasing,
true);
433 transformPainter.translate(-result.offset());
434 transformPainter.setTransform(restXr,
true);
435 transformPainter.drawImage(clipRectGlob.topLeft(), tempSource);
436 transformPainter.end();
438 clipToTransformedBounds(&result, p, localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits));
459QImage QSvgFeOffset::apply(
const QMap<QString, QImage> &sources, QPainter *p,
460 const QRectF &itemBounds,
const QRectF &filterBounds,
461 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
463 if (!sources.contains(m_input))
466 const QImage &source = sources[m_input];
468 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
469 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
471 QPoint offset(m_dx, m_dy);
472 if (primitiveUnits == QtSvg::UnitTypes::objectBoundingBox) {
473 offset = QPoint(m_dx * itemBounds.width(),
474 m_dy * itemBounds.height());
476 offset = p->transform().map(offset) - p->transform().map(QPoint(0, 0));
478 if (clipRectGlob.isEmpty())
482 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
483 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
486 result.setOffset(clipRectGlob.topLeft());
487 result.fill(Qt::transparent);
489 QPainter copyPainter(&result);
490 copyPainter.drawImage(source.offset()
491 - result.offset() + offset, source);
494 clipToTransformedBounds(&result, p, clipRect);
514QImage QSvgFeMerge::apply(
const QMap<QString, QImage> &sources, QPainter *p,
515 const QRectF &itemBounds,
const QRectF &filterBounds,
516 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
518 QList<QImage> mergeNodeResults;
519 for (
const auto &node : renderers()) {
520 if (node->type() == QSvgNode::FeMergenode) {
521 const QSvgFeMergeNode &filter =
static_cast<
const QSvgFeMergeNode&>(*node);
522 mergeNodeResults.append(filter.apply(sources, p, itemBounds, filterBounds, primitiveUnits, filterUnits));
526 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
527 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
528 if (clipRectGlob.isEmpty())
532 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
533 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
536 result.setOffset(clipRectGlob.topLeft());
537 result.fill(Qt::transparent);
539 QPainter proxyPainter(&result);
540 for (
const QImage &i : mergeNodeResults) {
541 proxyPainter.drawImage(QRect(i.offset() - result.offset(), i.size()), i);
545 clipToTransformedBounds(&result, p, clipRect);
601QImage QSvgFeComposite::apply(
const QMap<QString, QImage> &sources, QPainter *p,
602 const QRectF &itemBounds,
const QRectF &filterBounds,
603 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
605 if (!sources.contains(m_input))
607 if (!sources.contains(m_input2))
609 QImage source1 = sources[m_input];
610 QImage source2 = sources[m_input2];
611 Q_ASSERT(source1.depth() == 32);
612 Q_ASSERT(source2.depth() == 32);
614 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
615 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
617 if (clipRectGlob.isEmpty())
621 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
622 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
625 result.setOffset(clipRectGlob.topLeft());
626 result.fill(Qt::transparent);
628 if (m_operator == Operator::Arithmetic) {
629 const qreal k1 = m_k.x();
630 const qreal k2 = m_k.y();
631 const qreal k3 = m_k.z();
632 const qreal k4 = m_k.w();
634 for (
int j = 0; j < result.height(); j++) {
635 int jj1 = j - source1.offset().y() + result.offset().y();
636 int jj2 = j - source2.offset().y() + result.offset().y();
638 QRgb *resultLine =
reinterpret_cast<QRgb *>(result.scanLine(j));
639 QRgb *source1Line =
nullptr;
640 QRgb *source2Line =
nullptr;
642 if (jj1 >= 0 && jj1 < source1.size().height())
643 source1Line =
reinterpret_cast<QRgb *>(source1.scanLine(jj1));
644 if (jj2 >= 0 && jj2 < source2.size().height())
645 source2Line =
reinterpret_cast<QRgb *>(source2.scanLine(jj2));
647 for (
int i = 0; i < result.width(); i++) {
648 int ii1 = i - source1.offset().x() + result.offset().x();
649 int ii2 = i - source2.offset().x() + result.offset().x();
651 QVector4D s1 = QVector4D(0, 0, 0, 0);
652 QVector4D s2 = QVector4D(0, 0, 0, 0);
654 if (ii1 >= 0 && ii1 < source1.size().width() && source1Line) {
655 QRgb pixel1 = source1Line[ii1];
656 s1 = QVector4D(qRed(pixel1),
662 if (ii2 >= 0 && ii2 < source2.size().width() && source2Line) {
663 QRgb pixel2 = source2Line[ii2];
664 s2 = QVector4D(qRed(pixel2),
670 int r = k1 * s1.x() * s2.x() / 255. + k2 * s1.x() + k3 * s2.x() + k4 * 255.;
671 int g = k1 * s1.y() * s2.y() / 255. + k2 * s1.y() + k3 * s2.y() + k4 * 255.;
672 int b = k1 * s1.z() * s2.z() / 255. + k2 * s1.z() + k3 * s2.z() + k4 * 255.;
673 int a = k1 * s1.w() * s2.w() / 255. + k2 * s1.w() + k3 * s2.w() + k4 * 255.;
675 a = qBound(0, a, 255);
676 resultLine[i] = qRgba(qBound(0, r, a),
683 QPainter proxyPainter(&result);
684 proxyPainter.drawImage(QRect(source1.offset() - result.offset(), source1.size()), source1);
686 switch (m_operator) {
688 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
691 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationOut);
694 proxyPainter.setCompositionMode(QPainter::CompositionMode_Xor);
696 case Operator::Lighter:
697 proxyPainter.setCompositionMode(QPainter::CompositionMode_Lighten);
700 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationAtop);
703 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
705 case Operator::Arithmetic:
709 proxyPainter.drawImage(QRect(source2.offset()-result.offset(), source2.size()), source2);
713 clipToTransformedBounds(&result, p, clipRect);
778QImage QSvgFeBlend::apply(
const QMap<QString, QImage> &sources, QPainter *p,
779 const QRectF &itemBounds,
const QRectF &filterBounds,
780 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
782 if (!sources.contains(m_input))
784 if (!sources.contains(m_input2))
786 QImage source1 = sources[m_input];
787 QImage source2 = sources[m_input2];
788 Q_ASSERT(source1.depth() == 32);
789 Q_ASSERT(source2.depth() == 32);
791 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
792 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
795 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
796 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
799 result.setOffset(clipRectGlob.topLeft());
800 result.fill(Qt::transparent);
802 for (
int j = 0; j < result.height(); j++) {
803 int jj1 = j - source1.offset().y() + result.offset().y();
804 int jj2 = j - source2.offset().y() + result.offset().y();
806 QRgb *resultLine =
reinterpret_cast<QRgb *>(result.scanLine(j));
807 QRgb *source1Line =
nullptr;
808 QRgb *source2Line =
nullptr;
810 if (jj1 >= 0 && jj1 < source1.size().height())
811 source1Line =
reinterpret_cast<QRgb *>(source1.scanLine(jj1));
812 if (jj2 >= 0 && jj2 < source2.size().height())
813 source2Line =
reinterpret_cast<QRgb *>(source2.scanLine(jj2));
815 for (
int i = 0; i < result.width(); i++) {
816 int ii1 = i - source1.offset().x() + result.offset().x();
817 int ii2 = i - source2.offset().x() + result.offset().x();
819 QRgb pixel1 = (ii1 >= 0 && ii1 < source1.size().width() && source1Line) ?
820 source1Line[ii1] : qRgba(0, 0, 0, 0);
821 QRgb pixel2 = (ii2 >= 0 && ii2 < source2.size().width() && source2Line) ?
822 source2Line[ii2] : qRgba(0, 0, 0, 0);
824 qreal r = 0, g = 0, b = 0;
825 qreal red1 = qRed(pixel1);
826 qreal red2 = qRed(pixel2);
827 qreal green1 = qGreen(pixel1);
828 qreal green2 = qGreen(pixel2);
829 qreal blue1 = qBlue(pixel1);
830 qreal blue2 = qBlue(pixel2);
831 qreal alpha1 = qAlpha(pixel1) / 255.;
832 qreal alpha2 = qAlpha(pixel2) / 255.;
833 qreal a = 255 - (1 - alpha1) * (1 - alpha2) * 255.;
837 r = (1 - alpha1) * red2 + red1;
838 g = (1 - alpha1) * green2 + green1;
839 b = (1 - alpha1) * blue2 + blue1;
842 r = (1 - alpha1) * red2 + (1 - alpha2) * red1 + red1 * red2 / 255.;
843 g = (1 - alpha1) * green2 + (1 - alpha2) * green1 + green1 * green2 / 255.;
844 b = (1 - alpha1) * blue2 + (1 - alpha2) * blue1 + blue1 * blue2 / 255.;
847 r = red2 + red1 - red1 * red2 / 255.;
848 g = green2 + green1 - green1 * green2 / 255.;
849 b = blue2 + blue1 - blue1 * blue2 / 255.;
852 r = qMin((1 - alpha1) * red2 + red1, (1 - alpha2) * red1 + red2);
853 g = qMin((1 - alpha1) * green2 + green1, (1 - alpha2) * green1 + green2);
854 b = qMin((1 - alpha1) * blue2 + blue1, (1 - alpha2) * blue1 + blue2);
857 r = qMax((1 - alpha1) * red2 + red1, (1 - alpha2) * red1 + red2);
858 g = qMax((1 - alpha1) * green2 + green1, (1 - alpha2) * green1 + green2);
859 b = qMax((1 - alpha1) * blue2 + blue1, (1 - alpha2) * blue1 + blue2);
862 a = qBound(0., a, 255.);
863 resultLine[i] = qRgba(qBound(0., r, a),
869 clipToTransformedBounds(&result, p, clipRect);