99QSvgFeColorMatrix::QSvgFeColorMatrix(QSvgNode *parent,
const QString &input,
const QString &result,
100 const QSvgRectF &rect, ColorShiftType type,
101 const Matrix &matrix)
102 : QSvgFeFilterPrimitive(parent, input, result, rect)
106 constexpr qreal v1[] = {0.213, 0.213, 0.213,
108 0.072, 0.072, 0.072};
109 static const Matrix3x3 m1(v1);
111 constexpr qreal v2[] = { 0.787, -0.213, -0.213,
112 -0.715, 0.285, -0.715,
113 -0.072, -0.072, 0.928};
114 static const Matrix3x3 m2(v2);
116 constexpr qreal v3[] = {-0.213, 0.143, -0.787,
117 -0.715, 0.140, 0.715,
118 0.928, -0.283, 0.072};
119 static const Matrix3x3 m3(v3);
121 if (type == ColorShiftType::Saturate) {
122 qreal s = qBound(0., m_matrix.data()[0], 1.);
126 const Matrix3x3 m = m1 + m2 * s;
128 for (
int j = 0; j < 3; ++j)
129 for (
int i = 0; i < 3; ++i)
130 m_matrix.data()[i+j*5] = m.data()[i+j*3];
131 m_matrix.data()[3+3*5] = 1;
133 }
else if (type == ColorShiftType::HueRotate){
134 qreal angle = m_matrix.data()[0]/180.*M_PI;
135 qreal s = sin(angle);
136 qreal c = cos(angle);
140 const Matrix3x3 m = m1 + m2 * c + m3 * s;
142 for (
int j = 0; j < 3; ++j)
143 for (
int i = 0; i < 3; ++i)
144 m_matrix.data()[i+j*5] = m.data()[i+j*3];
145 m_matrix.data()[3+3*5] = 1;
146 }
else if (type == ColorShiftType::LuminanceToAlpha){
149 m_matrix.data()[0+3*5] = 0.2125;
150 m_matrix.data()[1+3*5] = 0.7154;
151 m_matrix.data()[2+3*5] = 0.0721;
160QImage QSvgFeColorMatrix::apply(
const QMap<QString, QImage> &sources, QPainter *p,
161 const QRectF &itemBounds,
const QRectF &filterBounds,
162 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
164 if (!sources.contains(m_input))
166 QImage source = sources[m_input];
168 QRect clipRectGlob = globalSubRegion(p, itemBounds, filterBounds, primitiveUnits, filterUnits).toRect();
169 if (clipRectGlob.isEmpty())
173 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
174 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
177 result.setOffset(clipRectGlob.topLeft());
178 result.fill(Qt::transparent);
180 Q_ASSERT(source.depth() == 32);
182 const Matrix transposedMatrix = m_matrix.transposed();
184 for (
int i = 0; i < result.height(); i++) {
185 int sourceI = i - source.offset().y() + result.offset().y();
187 if (sourceI < 0 || sourceI >= source.height())
190 QRgb *sourceLine =
reinterpret_cast<QRgb *>(source.scanLine(sourceI));
191 QRgb *resultLine =
reinterpret_cast<QRgb *>(result.scanLine(i));
193 for (
int j = 0; j < result.width(); j++) {
194 int sourceJ = j - source.offset().x() + result.offset().x();
196 if (sourceJ < 0 || sourceJ >= source.width())
199 QRgb sourceColor = qUnpremultiply(sourceLine[sourceJ]);
200 const qreal values[] = {qreal(qRed(sourceColor)),
201 qreal(qGreen(sourceColor)),
202 qreal(qBlue(sourceColor)),
203 qreal(qAlpha(sourceColor)),
205 const QGenericMatrix<1, 5, qreal> sourceVector(values);
206 const QGenericMatrix<1, 5, qreal> resultVector = transposedMatrix * sourceVector;
208 QRgb rgba = qRgba(qBound(0,
int(resultVector(0, 0)), 255),
209 qBound(0,
int(resultVector(1, 0)), 255),
210 qBound(0,
int(resultVector(2, 0)), 255),
211 qBound(0,
int(resultVector(3, 0)), 255));
212 resultLine[j] = qPremultiply(rgba);
216 clipToTransformedBounds(&result, p, localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits));
236QImage QSvgFeGaussianBlur::apply(
const QMap<QString, QImage> &sources, QPainter *p,
237 const QRectF &itemBounds,
const QRectF &filterBounds,
238 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
240 if (!sources.contains(m_input))
242 QImage source = sources[m_input];
243 Q_ASSERT(source.depth() == 32);
245 if (m_stdDeviationX == 0 && m_stdDeviationY == 0)
248 const qreal scaleX = qHypot(p->transform().m11(), p->transform().m21());
249 const qreal scaleY = qHypot(p->transform().m12(), p->transform().m22());
251 qreal sigma_x = scaleX * m_stdDeviationX;
252 qreal sigma_y = scaleY * m_stdDeviationY;
253 if (primitiveUnits == QtSvg::UnitTypes::objectBoundingBox) {
254 sigma_x *= itemBounds.width();
255 sigma_y *= itemBounds.height();
258 constexpr double sd = 3. * M_SQRT1_2 / M_2_SQRTPI;
259 const int dx = floor(sigma_x * sd + 0.5);
260 const int dy = floor(sigma_y * sd + 0.5);
262 const QTransform scaleXr = QTransform::fromScale(scaleX, scaleY);
263 const QTransform restXr = scaleXr.inverted() * p->transform();
265 QRect clipRectGlob = scaleXr.mapRect(localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits)).toRect();
266 if (clipRectGlob.isEmpty())
270 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &tempSource)) {
271 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
274 tempSource.setOffset(clipRectGlob.topLeft());
275 tempSource.fill(Qt::transparent);
276 QPainter copyPainter(&tempSource);
277 copyPainter.translate(-tempSource.offset());
278 copyPainter.setTransform(restXr.inverted(),
true);
279 copyPainter.drawImage(source.offset(), source);
282 QVarLengthArray<uint64_t, 32 * 32> buffer(tempSource.width() * tempSource.height());
284 const int sourceHeight = tempSource.height();
285 const int sourceWidth = tempSource.width();
286 QRgb *rawImage =
reinterpret_cast<QRgb *>(tempSource.bits());
291 for (
int m = 0; m < 3; m++) {
298 auto adjustD = [](
int d,
int iteration) {
300 std::pair<
int,
int> result;
302 result = {d / 2 + 1, d / 2};
303 else if (iteration == 0)
304 result = {d / 2 + 1, d / 2 - 1};
305 else if (iteration == 1)
306 result = {d / 2, d / 2};
308 result = {d / 2 + 1, d / 2};
309 Q_ASSERT(result.first + result.second > 0);
313 const auto [dxleft, dxright] = adjustD(dx, m);
314 const auto [dytop, dybottom] = adjustD(dy, m);
316 for (
int col = 0; col < 4 * 8; col += 8 ){
319 for (
int j = 0; j < sourceHeight; j++) {
320 for (
int i = 0; i < sourceWidth; i++) {
321 buffer[i + j * sourceWidth] = (rawImage[i + j * sourceWidth] >> col) & 0xff;
323 buffer[i + j * sourceWidth] += buffer[(i - 1) + j * sourceWidth];
325 buffer[i + j * sourceWidth] += buffer[i + (j - 1) * sourceWidth];
327 buffer[i + j * sourceWidth] -= buffer[(i - 1) + (j - 1) * sourceWidth];
331 for (
int j = 0; j < sourceHeight; j++) {
332 const int j1 = qMax(0, j - dytop);
333 const int j2 = qMin(sourceHeight - 1, j + dybottom);
334 for (
int i = 0; i < sourceWidth; i++) {
335 const int i1 = qMax(0, i - dxleft);
336 const int i2 = qMin(sourceWidth - 1, i + dxright);
338 uint64_t colorValue64 = buffer[i2 + j2 * sourceWidth];
339 colorValue64 -= buffer[i1 + j2 * sourceWidth];
340 colorValue64 -= buffer[i2 + j1 * sourceWidth];
341 colorValue64 += buffer[i1 + j1 * sourceWidth];
342 colorValue64 /= uint64_t(dxleft + dxright) * uint64_t(dytop + dybottom);
344 const unsigned int colorValue = colorValue64;
345 rawImage[i + j * sourceWidth] &= ~(0xff << col);
346 rawImage[i + j * sourceWidth] |= colorValue << col;
353 QRectF trueClipRectGlob = globalSubRegion(p, itemBounds, filterBounds, primitiveUnits, filterUnits);
356 if (!QImageIOHandler::allocateImage(trueClipRectGlob.toRect().size(), QImage::Format_ARGB32_Premultiplied, &result)) {
357 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
360 result.setOffset(trueClipRectGlob.toRect().topLeft());
361 result.fill(Qt::transparent);
362 QPainter transformPainter(&result);
363 transformPainter.setRenderHint(QPainter::Antialiasing,
true);
365 transformPainter.translate(-result.offset());
366 transformPainter.setTransform(restXr,
true);
367 transformPainter.drawImage(clipRectGlob.topLeft(), tempSource);
368 transformPainter.end();
370 clipToTransformedBounds(&result, p, localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits));
388QImage QSvgFeOffset::apply(
const QMap<QString, QImage> &sources, QPainter *p,
389 const QRectF &itemBounds,
const QRectF &filterBounds,
390 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
392 if (!sources.contains(m_input))
395 const QImage &source = sources[m_input];
397 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
398 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
400 QPoint offset(m_dx, m_dy);
401 if (primitiveUnits == QtSvg::UnitTypes::objectBoundingBox) {
402 offset = QPoint(m_dx * itemBounds.width(),
403 m_dy * itemBounds.height());
405 offset = p->transform().map(offset) - p->transform().map(QPoint(0, 0));
407 if (clipRectGlob.isEmpty())
411 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
412 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
415 result.setOffset(clipRectGlob.topLeft());
416 result.fill(Qt::transparent);
418 QPainter copyPainter(&result);
419 copyPainter.drawImage(source.offset()
420 - result.offset() + offset, source);
423 clipToTransformedBounds(&result, p, clipRect);
440QImage QSvgFeMerge::apply(
const QMap<QString, QImage> &sources, QPainter *p,
441 const QRectF &itemBounds,
const QRectF &filterBounds,
442 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
444 QList<QImage> mergeNodeResults;
445 for (
int i = 0; i < renderers().size(); i++) {
446 QSvgNode *child = renderers().at(i);
447 if (child->type() == QSvgNode::FeMergenode) {
448 QSvgFeMergeNode *filter =
static_cast<QSvgFeMergeNode*>(child);
449 mergeNodeResults.append(filter->apply(sources, p, itemBounds, filterBounds, primitiveUnits, filterUnits));
453 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
454 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
455 if (clipRectGlob.isEmpty())
459 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
460 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
463 result.setOffset(clipRectGlob.topLeft());
464 result.fill(Qt::transparent);
466 QPainter proxyPainter(&result);
467 for (
const QImage &i : mergeNodeResults) {
468 proxyPainter.drawImage(QRect(i.offset() - result.offset(), i.size()), i);
472 clipToTransformedBounds(&result, p, clipRect);
523QImage QSvgFeComposite::apply(
const QMap<QString, QImage> &sources, QPainter *p,
524 const QRectF &itemBounds,
const QRectF &filterBounds,
525 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
527 if (!sources.contains(m_input))
529 if (!sources.contains(m_input2))
531 QImage source1 = sources[m_input];
532 QImage source2 = sources[m_input2];
533 Q_ASSERT(source1.depth() == 32);
534 Q_ASSERT(source2.depth() == 32);
536 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
537 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
539 if (clipRectGlob.isEmpty())
543 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
544 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
547 result.setOffset(clipRectGlob.topLeft());
548 result.fill(Qt::transparent);
550 if (m_operator == Operator::Arithmetic) {
551 const qreal k1 = m_k.x();
552 const qreal k2 = m_k.y();
553 const qreal k3 = m_k.z();
554 const qreal k4 = m_k.w();
556 for (
int j = 0; j < result.height(); j++) {
557 int jj1 = j - source1.offset().y() + result.offset().y();
558 int jj2 = j - source2.offset().y() + result.offset().y();
560 QRgb *resultLine =
reinterpret_cast<QRgb *>(result.scanLine(j));
561 QRgb *source1Line =
nullptr;
562 QRgb *source2Line =
nullptr;
564 if (jj1 >= 0 && jj1 < source1.size().height())
565 source1Line =
reinterpret_cast<QRgb *>(source1.scanLine(jj1));
566 if (jj2 >= 0 && jj2 < source2.size().height())
567 source2Line =
reinterpret_cast<QRgb *>(source2.scanLine(jj2));
569 for (
int i = 0; i < result.width(); i++) {
570 int ii1 = i - source1.offset().x() + result.offset().x();
571 int ii2 = i - source2.offset().x() + result.offset().x();
573 QVector4D s1 = QVector4D(0, 0, 0, 0);
574 QVector4D s2 = QVector4D(0, 0, 0, 0);
576 if (ii1 >= 0 && ii1 < source1.size().width() && source1Line) {
577 QRgb pixel1 = source1Line[ii1];
578 s1 = QVector4D(qRed(pixel1),
584 if (ii2 >= 0 && ii2 < source2.size().width() && source2Line) {
585 QRgb pixel2 = source2Line[ii2];
586 s2 = QVector4D(qRed(pixel2),
592 int r = k1 * s1.x() * s2.x() / 255. + k2 * s1.x() + k3 * s2.x() + k4 * 255.;
593 int g = k1 * s1.y() * s2.y() / 255. + k2 * s1.y() + k3 * s2.y() + k4 * 255.;
594 int b = k1 * s1.z() * s2.z() / 255. + k2 * s1.z() + k3 * s2.z() + k4 * 255.;
595 int a = k1 * s1.w() * s2.w() / 255. + k2 * s1.w() + k3 * s2.w() + k4 * 255.;
597 a = qBound(0, a, 255);
598 resultLine[i] = qRgba(qBound(0, r, a),
605 QPainter proxyPainter(&result);
606 proxyPainter.drawImage(QRect(source1.offset() - result.offset(), source1.size()), source1);
608 switch (m_operator) {
610 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
613 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationOut);
616 proxyPainter.setCompositionMode(QPainter::CompositionMode_Xor);
618 case Operator::Lighter:
619 proxyPainter.setCompositionMode(QPainter::CompositionMode_Lighten);
622 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationAtop);
625 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
627 case Operator::Arithmetic:
631 proxyPainter.drawImage(QRect(source2.offset()-result.offset(), source2.size()), source2);
635 clipToTransformedBounds(&result, p, clipRect);
694QImage QSvgFeBlend::apply(
const QMap<QString, QImage> &sources, QPainter *p,
695 const QRectF &itemBounds,
const QRectF &filterBounds,
696 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
698 if (!sources.contains(m_input))
700 if (!sources.contains(m_input2))
702 QImage source1 = sources[m_input];
703 QImage source2 = sources[m_input2];
704 Q_ASSERT(source1.depth() == 32);
705 Q_ASSERT(source2.depth() == 32);
707 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
708 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
711 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
712 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
715 result.setOffset(clipRectGlob.topLeft());
716 result.fill(Qt::transparent);
718 for (
int j = 0; j < result.height(); j++) {
719 int jj1 = j - source1.offset().y() + result.offset().y();
720 int jj2 = j - source2.offset().y() + result.offset().y();
722 QRgb *resultLine =
reinterpret_cast<QRgb *>(result.scanLine(j));
723 QRgb *source1Line =
nullptr;
724 QRgb *source2Line =
nullptr;
726 if (jj1 >= 0 && jj1 < source1.size().height())
727 source1Line =
reinterpret_cast<QRgb *>(source1.scanLine(jj1));
728 if (jj2 >= 0 && jj2 < source2.size().height())
729 source2Line =
reinterpret_cast<QRgb *>(source2.scanLine(jj2));
731 for (
int i = 0; i < result.width(); i++) {
732 int ii1 = i - source1.offset().x() + result.offset().x();
733 int ii2 = i - source2.offset().x() + result.offset().x();
735 QRgb pixel1 = (ii1 >= 0 && ii1 < source1.size().width() && source1Line) ?
736 source1Line[ii1] : qRgba(0, 0, 0, 0);
737 QRgb pixel2 = (ii2 >= 0 && ii2 < source2.size().width() && source2Line) ?
738 source2Line[ii2] : qRgba(0, 0, 0, 0);
740 qreal r = 0, g = 0, b = 0;
741 qreal red1 = qRed(pixel1);
742 qreal red2 = qRed(pixel2);
743 qreal green1 = qGreen(pixel1);
744 qreal green2 = qGreen(pixel2);
745 qreal blue1 = qBlue(pixel1);
746 qreal blue2 = qBlue(pixel2);
747 qreal alpha1 = qAlpha(pixel1) / 255.;
748 qreal alpha2 = qAlpha(pixel2) / 255.;
749 qreal a = 255 - (1 - alpha1) * (1 - alpha2) * 255.;
753 r = (1 - alpha1) * red2 + red1;
754 g = (1 - alpha1) * green2 + green1;
755 b = (1 - alpha1) * blue2 + blue1;
758 r = (1 - alpha1) * red2 + (1 - alpha2) * red1 + red1 * red2;
759 g = (1 - alpha1) * green2 + (1 - alpha2) * green1 + green1 * green2;
760 b = (1 - alpha1) * blue2 + (1 - alpha2) * blue1 + blue1 * blue2;
763 r = red2 + red1 - red1 * red2;
764 g = green2 + green1 - green1 * green2;
765 b = blue2 + blue1 - blue1 * blue2;
768 r = qMin((1 - alpha1) * red2 + red1, (1 - alpha2) * red1 + red2);
769 g = qMin((1 - alpha1) * green2 + green1, (1 - alpha2) * green1 + green2);
770 b = qMin((1 - alpha1) * blue2 + blue1, (1 - alpha2) * blue1 + blue2);
773 r = qMax((1 - alpha1) * red2 + red1, (1 - alpha2) * red1 + red2);
774 g = qMax((1 - alpha1) * green2 + green1, (1 - alpha2) * green1 + green2);
775 b = qMax((1 - alpha1) * blue2 + blue1, (1 - alpha2) * blue1 + blue2);
778 a = qBound(0., a, 255.);
779 resultLine[i] = qRgba(qBound(0., r, a),
785 clipToTransformedBounds(&result, p, clipRect);