106QSvgFeColorMatrix::QSvgFeColorMatrix(QSvgNode *parent,
const QString &input,
const QString &result,
107 const QSvgRectF &rect, ColorShiftType type,
108 const Matrix &matrix)
109 : QSvgFeFilterPrimitive(parent, input, result, rect)
113 constexpr qreal v1[] = {0.213, 0.213, 0.213,
115 0.072, 0.072, 0.072};
116 static const Matrix3x3 m1(v1);
118 constexpr qreal v2[] = { 0.787, -0.213, -0.213,
119 -0.715, 0.285, -0.715,
120 -0.072, -0.072, 0.928};
121 static const Matrix3x3 m2(v2);
123 constexpr qreal v3[] = {-0.213, 0.143, -0.787,
124 -0.715, 0.140, 0.715,
125 0.928, -0.283, 0.072};
126 static const Matrix3x3 m3(v3);
128 if (type == ColorShiftType::Saturate) {
129 qreal s = qBound(0., m_matrix.data()[0], 1.);
133 const Matrix3x3 m = m1 + m2 * s;
135 for (
int j = 0; j < 3; ++j)
136 for (
int i = 0; i < 3; ++i)
137 m_matrix.data()[i+j*5] = m.data()[i+j*3];
138 m_matrix.data()[3+3*5] = 1;
140 }
else if (type == ColorShiftType::HueRotate){
141 qreal angle = m_matrix.data()[0]/180.*M_PI;
142 qreal s = sin(angle);
143 qreal c = cos(angle);
147 const Matrix3x3 m = m1 + m2 * c + m3 * s;
149 for (
int j = 0; j < 3; ++j)
150 for (
int i = 0; i < 3; ++i)
151 m_matrix.data()[i+j*5] = m.data()[i+j*3];
152 m_matrix.data()[3+3*5] = 1;
153 }
else if (type == ColorShiftType::LuminanceToAlpha){
156 m_matrix.data()[0+3*5] = 0.2125;
157 m_matrix.data()[1+3*5] = 0.7154;
158 m_matrix.data()[2+3*5] = 0.0721;
170QImage QSvgFeColorMatrix::apply(
const QMap<QString, QImage> &sources, QPainter *p,
171 const QRectF &itemBounds,
const QRectF &filterBounds,
172 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
174 if (!sources.contains(m_input))
176 QImage source = sources[m_input];
178 QRect clipRectGlob = globalSubRegion(p, itemBounds, filterBounds, primitiveUnits, filterUnits).toRect();
179 if (clipRectGlob.isEmpty())
183 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
184 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
187 result.setOffset(clipRectGlob.topLeft());
188 result.fill(Qt::transparent);
190 Q_ASSERT(source.depth() == 32);
192 const Matrix transposedMatrix = m_matrix.transposed();
194 for (
int i = 0; i < result.height(); i++) {
195 int sourceI = i - source.offset().y() + result.offset().y();
197 if (sourceI < 0 || sourceI >= source.height())
200 QRgb *sourceLine =
reinterpret_cast<QRgb *>(source.scanLine(sourceI));
201 QRgb *resultLine =
reinterpret_cast<QRgb *>(result.scanLine(i));
203 for (
int j = 0; j < result.width(); j++) {
204 int sourceJ = j - source.offset().x() + result.offset().x();
206 if (sourceJ < 0 || sourceJ >= source.width())
209 QRgb sourceColor = qUnpremultiply(sourceLine[sourceJ]);
210 const qreal values[] = {qreal(qRed(sourceColor)),
211 qreal(qGreen(sourceColor)),
212 qreal(qBlue(sourceColor)),
213 qreal(qAlpha(sourceColor)),
215 const QGenericMatrix<1, 5, qreal> sourceVector(values);
216 const QGenericMatrix<1, 5, qreal> resultVector = transposedMatrix * sourceVector;
218 QRgb rgba = qRgba(qBound(0,
int(resultVector(0, 0)), 255),
219 qBound(0,
int(resultVector(1, 0)), 255),
220 qBound(0,
int(resultVector(2, 0)), 255),
221 qBound(0,
int(resultVector(3, 0)), 255));
222 resultLine[j] = qPremultiply(rgba);
226 clipToTransformedBounds(&result, p, localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits));
312QImage QSvgFeGaussianBlur::apply(
const QMap<QString, QImage> &sources, QPainter *p,
313 const QRectF &itemBounds,
const QRectF &filterBounds,
314 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
316 if (!sources.contains(m_input))
318 QImage source = sources[m_input];
319 Q_ASSERT(source.depth() == 32);
321 if (m_stdDeviationX == 0 && m_stdDeviationY == 0)
324 const qreal scaleX = qHypot(p->transform().m11(), p->transform().m21());
325 const qreal scaleY = qHypot(p->transform().m12(), p->transform().m22());
327 qreal sigma_x = scaleX * m_stdDeviationX;
328 qreal sigma_y = scaleY * m_stdDeviationY;
329 if (primitiveUnits == QtSvg::UnitTypes::objectBoundingBox) {
330 sigma_x *= itemBounds.width();
331 sigma_y *= itemBounds.height();
334 constexpr double sd = 3. * M_SQRT1_2 / M_2_SQRTPI;
335 const int dx = floor(sigma_x * sd + 0.5);
336 const int dy = floor(sigma_y * sd + 0.5);
338 const QTransform scaleXr = QTransform::fromScale(scaleX, scaleY);
339 const QTransform restXr = scaleXr.inverted() * p->transform();
341 QRect clipRectGlob = scaleXr.mapRect(localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits)).toRect();
342 if (clipRectGlob.isEmpty())
346 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &tempSource)) {
347 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
350 tempSource.setOffset(clipRectGlob.topLeft());
351 tempSource.fill(Qt::transparent);
352 QPainter copyPainter(&tempSource);
353 copyPainter.translate(-tempSource.offset());
354 copyPainter.setTransform(restXr.inverted(),
true);
355 copyPainter.drawImage(source.offset(), source);
358 const auto buffer = std::make_unique<ColorValues[]>(tempSource.width() * tempSource.height());
360 const int sourceHeight = tempSource.height();
361 const int sourceWidth = tempSource.width();
362 QRgb *rawImage =
reinterpret_cast<QRgb *>(tempSource.bits());
367 for (
int m = 0; m < 3; m++) {
374 auto adjustD = [](
int d,
int iteration) {
376 std::pair<
int,
int> result;
378 result = {d / 2 + 1, d / 2};
379 else if (iteration == 0)
380 result = {d / 2 + 1, d / 2 - 1};
381 else if (iteration == 1)
382 result = {d / 2, d / 2};
384 result = {d / 2 + 1, d / 2};
385 Q_ASSERT(result.first + result.second > 0);
389 const auto [dxleft, dxright] = adjustD(dx, m);
390 const auto [dytop, dybottom] = adjustD(dy, m);
394 for (
int j = 0; j < sourceHeight; j++) {
395 for (
int i = 0; i < sourceWidth; i++) {
396 ColorValues colorValues(rawImage[i + j * sourceWidth]);
398 colorValues += buffer[(i - 1) + j * sourceWidth];
400 colorValues += buffer[i + (j - 1) * sourceWidth];
402 colorValues -= buffer[(i - 1) + (j - 1) * sourceWidth];
403 buffer[i + j * sourceWidth] = colorValues;
407 for (
int j = 0; j < sourceHeight; j++) {
408 const int j1 = qMax(0, j - dytop);
409 const int j2 = qMin(sourceHeight - 1, j + dybottom);
410 for (
int i = 0; i < sourceWidth; i++) {
411 const int i1 = qMax(0, i - dxleft);
412 const int i2 = qMin(sourceWidth - 1, i + dxright);
413 ColorValues colorValues = buffer[i2 + j2 * sourceWidth]
414 - buffer[i1 + j2 * sourceWidth]
415 - buffer[i2 + j1 * sourceWidth]
416 + buffer[i1 + j1 * sourceWidth];
417 colorValues /= uint64_t(dxleft + dxright) * uint64_t(dytop + dybottom);
418 rawImage[i + j * sourceWidth] = colorValues.toRgb();
423 QRectF trueClipRectGlob = globalSubRegion(p, itemBounds, filterBounds, primitiveUnits, filterUnits);
426 if (!QImageIOHandler::allocateImage(trueClipRectGlob.toRect().size(), QImage::Format_ARGB32_Premultiplied, &result)) {
427 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
430 result.setOffset(trueClipRectGlob.toRect().topLeft());
431 result.fill(Qt::transparent);
432 QPainter transformPainter(&result);
433 transformPainter.setRenderHint(QPainter::Antialiasing,
true);
435 transformPainter.translate(-result.offset());
436 transformPainter.setTransform(restXr,
true);
437 transformPainter.drawImage(clipRectGlob.topLeft(), tempSource);
438 transformPainter.end();
440 clipToTransformedBounds(&result, p, localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits));
461QImage QSvgFeOffset::apply(
const QMap<QString, QImage> &sources, QPainter *p,
462 const QRectF &itemBounds,
const QRectF &filterBounds,
463 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
465 if (!sources.contains(m_input))
468 const QImage &source = sources[m_input];
470 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
471 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
473 QPoint offset(m_dx, m_dy);
474 if (primitiveUnits == QtSvg::UnitTypes::objectBoundingBox) {
475 offset = QPoint(m_dx * itemBounds.width(),
476 m_dy * itemBounds.height());
478 offset = p->transform().map(offset) - p->transform().map(QPoint(0, 0));
480 if (clipRectGlob.isEmpty())
484 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
485 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
488 result.setOffset(clipRectGlob.topLeft());
489 result.fill(Qt::transparent);
491 QPainter copyPainter(&result);
492 copyPainter.drawImage(source.offset()
493 - result.offset() + offset, source);
496 clipToTransformedBounds(&result, p, clipRect);
516QImage QSvgFeMerge::apply(
const QMap<QString, QImage> &sources, QPainter *p,
517 const QRectF &itemBounds,
const QRectF &filterBounds,
518 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
520 QList<QImage> mergeNodeResults;
521 for (
const auto &node : renderers()) {
522 if (node->type() == QSvgNode::FeMergenode) {
523 const QSvgFeMergeNode &filter =
static_cast<
const QSvgFeMergeNode&>(*node);
524 mergeNodeResults.append(filter.apply(sources, p, itemBounds, filterBounds, primitiveUnits, filterUnits));
528 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
529 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
530 if (clipRectGlob.isEmpty())
534 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
535 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
538 result.setOffset(clipRectGlob.topLeft());
539 result.fill(Qt::transparent);
541 QPainter proxyPainter(&result);
542 for (
const QImage &i : mergeNodeResults) {
543 proxyPainter.drawImage(QRect(i.offset() - result.offset(), i.size()), i);
547 clipToTransformedBounds(&result, p, clipRect);
603QImage QSvgFeComposite::apply(
const QMap<QString, QImage> &sources, QPainter *p,
604 const QRectF &itemBounds,
const QRectF &filterBounds,
605 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
607 if (!sources.contains(m_input))
609 if (!sources.contains(m_input2))
611 QImage source1 = sources[m_input];
612 QImage source2 = sources[m_input2];
613 Q_ASSERT(source1.depth() == 32);
614 Q_ASSERT(source2.depth() == 32);
616 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
617 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
619 if (clipRectGlob.isEmpty())
623 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
624 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
627 result.setOffset(clipRectGlob.topLeft());
628 result.fill(Qt::transparent);
630 if (m_operator == Operator::Arithmetic) {
631 const qreal k1 = m_k.x();
632 const qreal k2 = m_k.y();
633 const qreal k3 = m_k.z();
634 const qreal k4 = m_k.w();
636 for (
int j = 0; j < result.height(); j++) {
637 int jj1 = j - source1.offset().y() + result.offset().y();
638 int jj2 = j - source2.offset().y() + result.offset().y();
640 QRgb *resultLine =
reinterpret_cast<QRgb *>(result.scanLine(j));
641 QRgb *source1Line =
nullptr;
642 QRgb *source2Line =
nullptr;
644 if (jj1 >= 0 && jj1 < source1.size().height())
645 source1Line =
reinterpret_cast<QRgb *>(source1.scanLine(jj1));
646 if (jj2 >= 0 && jj2 < source2.size().height())
647 source2Line =
reinterpret_cast<QRgb *>(source2.scanLine(jj2));
649 for (
int i = 0; i < result.width(); i++) {
650 int ii1 = i - source1.offset().x() + result.offset().x();
651 int ii2 = i - source2.offset().x() + result.offset().x();
653 QVector4D s1 = QVector4D(0, 0, 0, 0);
654 QVector4D s2 = QVector4D(0, 0, 0, 0);
656 if (ii1 >= 0 && ii1 < source1.size().width() && source1Line) {
657 QRgb pixel1 = source1Line[ii1];
658 s1 = QVector4D(qRed(pixel1),
664 if (ii2 >= 0 && ii2 < source2.size().width() && source2Line) {
665 QRgb pixel2 = source2Line[ii2];
666 s2 = QVector4D(qRed(pixel2),
672 int r = k1 * s1.x() * s2.x() / 255. + k2 * s1.x() + k3 * s2.x() + k4 * 255.;
673 int g = k1 * s1.y() * s2.y() / 255. + k2 * s1.y() + k3 * s2.y() + k4 * 255.;
674 int b = k1 * s1.z() * s2.z() / 255. + k2 * s1.z() + k3 * s2.z() + k4 * 255.;
675 int a = k1 * s1.w() * s2.w() / 255. + k2 * s1.w() + k3 * s2.w() + k4 * 255.;
677 a = qBound(0, a, 255);
678 resultLine[i] = qRgba(qBound(0, r, a),
685 QPainter proxyPainter(&result);
686 proxyPainter.drawImage(QRect(source1.offset() - result.offset(), source1.size()), source1);
688 switch (m_operator) {
690 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
693 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationOut);
696 proxyPainter.setCompositionMode(QPainter::CompositionMode_Xor);
698 case Operator::Lighter:
699 proxyPainter.setCompositionMode(QPainter::CompositionMode_Lighten);
702 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationAtop);
705 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
707 case Operator::Arithmetic:
711 proxyPainter.drawImage(QRect(source2.offset()-result.offset(), source2.size()), source2);
715 clipToTransformedBounds(&result, p, clipRect);
780QImage QSvgFeBlend::apply(
const QMap<QString, QImage> &sources, QPainter *p,
781 const QRectF &itemBounds,
const QRectF &filterBounds,
782 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
784 if (!sources.contains(m_input))
786 if (!sources.contains(m_input2))
788 QImage source1 = sources[m_input];
789 QImage source2 = sources[m_input2];
790 Q_ASSERT(source1.depth() == 32);
791 Q_ASSERT(source2.depth() == 32);
793 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
794 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
797 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
798 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
801 result.setOffset(clipRectGlob.topLeft());
802 result.fill(Qt::transparent);
804 for (
int j = 0; j < result.height(); j++) {
805 int jj1 = j - source1.offset().y() + result.offset().y();
806 int jj2 = j - source2.offset().y() + result.offset().y();
808 QRgb *resultLine =
reinterpret_cast<QRgb *>(result.scanLine(j));
809 QRgb *source1Line =
nullptr;
810 QRgb *source2Line =
nullptr;
812 if (jj1 >= 0 && jj1 < source1.size().height())
813 source1Line =
reinterpret_cast<QRgb *>(source1.scanLine(jj1));
814 if (jj2 >= 0 && jj2 < source2.size().height())
815 source2Line =
reinterpret_cast<QRgb *>(source2.scanLine(jj2));
817 for (
int i = 0; i < result.width(); i++) {
818 int ii1 = i - source1.offset().x() + result.offset().x();
819 int ii2 = i - source2.offset().x() + result.offset().x();
821 QRgb pixel1 = (ii1 >= 0 && ii1 < source1.size().width() && source1Line) ?
822 source1Line[ii1] : qRgba(0, 0, 0, 0);
823 QRgb pixel2 = (ii2 >= 0 && ii2 < source2.size().width() && source2Line) ?
824 source2Line[ii2] : qRgba(0, 0, 0, 0);
826 qreal r = 0, g = 0, b = 0;
827 qreal red1 = qRed(pixel1);
828 qreal red2 = qRed(pixel2);
829 qreal green1 = qGreen(pixel1);
830 qreal green2 = qGreen(pixel2);
831 qreal blue1 = qBlue(pixel1);
832 qreal blue2 = qBlue(pixel2);
833 qreal alpha1 = qAlpha(pixel1) / 255.;
834 qreal alpha2 = qAlpha(pixel2) / 255.;
835 qreal a = 255 - (1 - alpha1) * (1 - alpha2) * 255.;
839 r = (1 - alpha1) * red2 + red1;
840 g = (1 - alpha1) * green2 + green1;
841 b = (1 - alpha1) * blue2 + blue1;
844 r = (1 - alpha1) * red2 + (1 - alpha2) * red1 + red1 * red2 / 255.;
845 g = (1 - alpha1) * green2 + (1 - alpha2) * green1 + green1 * green2 / 255.;
846 b = (1 - alpha1) * blue2 + (1 - alpha2) * blue1 + blue1 * blue2 / 255.;
849 r = red2 + red1 - red1 * red2 / 255.;
850 g = green2 + green1 - green1 * green2 / 255.;
851 b = blue2 + blue1 - blue1 * blue2 / 255.;
854 r = qMin((1 - alpha1) * red2 + red1, (1 - alpha2) * red1 + red2);
855 g = qMin((1 - alpha1) * green2 + green1, (1 - alpha2) * green1 + green2);
856 b = qMin((1 - alpha1) * blue2 + blue1, (1 - alpha2) * blue1 + blue2);
859 r = qMax((1 - alpha1) * red2 + red1, (1 - alpha2) * red1 + red2);
860 g = qMax((1 - alpha1) * green2 + green1, (1 - alpha2) * green1 + green2);
861 b = qMax((1 - alpha1) * blue2 + blue1, (1 - alpha2) * blue1 + blue2);
864 a = qBound(0., a, 255.);
865 resultLine[i] = qRgba(qBound(0., r, a),
871 clipToTransformedBounds(&result, p, clipRect);