97QSvgFeColorMatrix::QSvgFeColorMatrix(QSvgNode *parent,
const QString &input,
const QString &result,
98 const QSvgRectF &rect, ColorShiftType type,
100 : QSvgFeFilterPrimitive(parent, input, result, rect)
104 if (type == ColorShiftType::Saturate) {
105 qreal s = qBound(0., m_matrix.data()[0], 1.);
109 m_matrix.data()[0+0*5] = 0.213f + 0.787f * s;
110 m_matrix.data()[1+0*5] = 0.715f - 0.715f * s;
111 m_matrix.data()[2+0*5] = 0.072f - 0.072f * s;
113 m_matrix.data()[0+1*5] = 0.213f - 0.213f * s;
114 m_matrix.data()[1+1*5] = 0.715f + 0.285f * s;
115 m_matrix.data()[2+1*5] = 0.072f - 0.072f * s;
117 m_matrix.data()[0+2*5] = 0.213f - 0.213f * s;
118 m_matrix.data()[1+2*5] = 0.715f - 0.715f * s;
119 m_matrix.data()[2+2*5] = 0.072f + 0.928f * s;
121 m_matrix.data()[3+3*5] = 1;
123 }
else if (type == ColorShiftType::HueRotate){
124 qreal angle = m_matrix.data()[0]/180.*M_PI;
125 qreal s = sin(angle);
126 qreal c = cos(angle);
131 m1.data()[0+0*3] = 0.213f;
132 m1.data()[1+0*3] = 0.715f;
133 m1.data()[2+0*3] = 0.072f;
135 m1.data()[0+1*3] = 0.213f;
136 m1.data()[1+1*3] = 0.715f;
137 m1.data()[2+1*3] = 0.072f;
139 m1.data()[0+2*3] = 0.213f;
140 m1.data()[1+2*3] = 0.715f;
141 m1.data()[2+2*3] = 0.072f;
144 m2.data()[0+0*3] = 0.787 * c;
145 m2.data()[1+0*3] = -0.715 * c;
146 m2.data()[2+0*3] = -0.072 * c;
148 m2.data()[0+1*3] = -0.213 * c;
149 m2.data()[1+1*3] = 0.285 * c;
150 m2.data()[2+1*3] = -0.072 * c;
152 m2.data()[0+2*3] = -0.213 * c;
153 m2.data()[1+2*3] = -0.715 * c;
154 m2.data()[2+2*3] = 0.928 * c;
157 m3.data()[0+0*3] = -0.213 * s;
158 m3.data()[1+0*3] = -0.715 * s;
159 m3.data()[2+0*3] = 0.928 * s;
161 m3.data()[0+1*3] = 0.143 * s;
162 m3.data()[1+1*3] = 0.140 * s;
163 m3.data()[2+1*3] = -0.283 * s;
165 m3.data()[0+2*3] = -0.787 * s;
166 m3.data()[1+2*3] = 0.715 * s;
167 m3.data()[2+2*3] = 0.072 * s;
169 QMatrix3x3 m = m1 + m2 + m3;
171 m_matrix.data()[0+0*5] = m.data()[0+0*3];
172 m_matrix.data()[1+0*5] = m.data()[1+0*3];
173 m_matrix.data()[2+0*5] = m.data()[2+0*3];
175 m_matrix.data()[0+1*5] = m.data()[0+1*3];
176 m_matrix.data()[1+1*5] = m.data()[1+1*3];
177 m_matrix.data()[2+1*5] = m.data()[2+1*3];
179 m_matrix.data()[0+2*5] = m.data()[0+2*3];
180 m_matrix.data()[1+2*5] = m.data()[1+2*3];
181 m_matrix.data()[2+2*5] = m.data()[2+2*3];
183 m_matrix.data()[3+3*5] = 1;
184 }
else if (type == ColorShiftType::LuminanceToAlpha){
187 m_matrix.data()[0+3*5] = 0.2125;
188 m_matrix.data()[1+3*5] = 0.7154;
189 m_matrix.data()[2+3*5] = 0.0721;
198QImage QSvgFeColorMatrix::apply(
const QMap<QString, QImage> &sources, QPainter *p,
199 const QRectF &itemBounds,
const QRectF &filterBounds,
200 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
202 if (!sources.contains(m_input))
204 QImage source = sources[m_input];
206 QRect clipRectGlob = globalSubRegion(p, itemBounds, filterBounds, primitiveUnits, filterUnits).toRect();
207 if (clipRectGlob.isEmpty())
211 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
212 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
215 result.setOffset(clipRectGlob.topLeft());
216 result.fill(Qt::transparent);
218 Q_ASSERT(source.depth() == 32);
220 for (
int i = 0; i < result.height(); i++) {
221 int sourceI = i - source.offset().y() + result.offset().y();
223 if (sourceI < 0 || sourceI >= source.height())
226 QRgb *sourceLine =
reinterpret_cast<QRgb *>(source.scanLine(sourceI));
227 QRgb *resultLine =
reinterpret_cast<QRgb *>(result.scanLine(i));
229 for (
int j = 0; j < result.width(); j++) {
230 int sourceJ = j - source.offset().x() + result.offset().x();
232 if (sourceJ < 0 || sourceJ >= source.width())
235 QRgb sourceColor = qUnpremultiply(sourceLine[sourceJ]);
236 qreal a = qAlpha(sourceColor);
237 qreal r = qRed(sourceColor);
238 qreal g = qGreen(sourceColor);
239 qreal b = qBlue(sourceColor);
241 qreal r2 = m_matrix.data()[0+0*5] * r +
242 m_matrix.data()[1+0*5] * g +
243 m_matrix.data()[2+0*5] * b +
244 m_matrix.data()[3+0*5] * a +
245 m_matrix.data()[4+0*5] * 255.;
246 qreal g2 = m_matrix.data()[0+1*5] * r +
247 m_matrix.data()[1+1*5] * g +
248 m_matrix.data()[2+1*5] * b +
249 m_matrix.data()[3+1*5] * a +
250 m_matrix.data()[4+1*5] * 255.;
251 qreal b2 = m_matrix.data()[0+2*5] * r +
252 m_matrix.data()[1+2*5] * g +
253 m_matrix.data()[2+2*5] * b +
254 m_matrix.data()[3+2*5] * a +
255 m_matrix.data()[4+2*5] * 255.;
256 qreal a2 = m_matrix.data()[0+3*5] * r +
257 m_matrix.data()[1+3*5] * g +
258 m_matrix.data()[2+3*5] * b +
259 m_matrix.data()[3+3*5] * a +
260 m_matrix.data()[4+3*5] * 255.;
262 QRgb rgba = qRgba(qBound(0,
int(r2), 255),
263 qBound(0,
int(g2), 255),
264 qBound(0,
int(b2), 255),
265 qBound(0,
int(a2), 255));
266 resultLine[j] = qPremultiply(rgba);
270 clipToTransformedBounds(&result, p, localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits));
290QImage QSvgFeGaussianBlur::apply(
const QMap<QString, QImage> &sources, QPainter *p,
291 const QRectF &itemBounds,
const QRectF &filterBounds,
292 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
294 if (!sources.contains(m_input))
296 QImage source = sources[m_input];
297 Q_ASSERT(source.depth() == 32);
299 if (m_stdDeviationX == 0 && m_stdDeviationY == 0)
302 const qreal scaleX = qHypot(p->transform().m11(), p->transform().m21());
303 const qreal scaleY = qHypot(p->transform().m12(), p->transform().m22());
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();
312 constexpr double sd = 3. * M_SQRT1_2 / M_2_SQRTPI;
313 const int dx = floor(sigma_x * sd + 0.5);
314 const int dy = floor(sigma_y * sd + 0.5);
316 const QTransform scaleXr = QTransform::fromScale(scaleX, scaleY);
317 const QTransform restXr = scaleXr.inverted() * p->transform();
319 QRect clipRectGlob = scaleXr.mapRect(localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits)).toRect();
320 if (clipRectGlob.isEmpty())
324 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &tempSource)) {
325 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
328 tempSource.setOffset(clipRectGlob.topLeft());
329 tempSource.fill(Qt::transparent);
330 QPainter copyPainter(&tempSource);
331 copyPainter.translate(-tempSource.offset());
332 copyPainter.setTransform(restXr.inverted(),
true);
333 copyPainter.drawImage(source.offset(), source);
336 QVarLengthArray<uint64_t, 32 * 32> buffer(tempSource.width() * tempSource.height());
338 const int sourceHeight = tempSource.height();
339 const int sourceWidth = tempSource.width();
340 QRgb *rawImage =
reinterpret_cast<QRgb *>(tempSource.bits());
345 for (
int m = 0; m < 3; m++) {
346 for (
int col = 0; col < 4 * 8; col += 8 ){
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;
353 buffer[i + j * sourceWidth] += buffer[(i - 1) + j * sourceWidth];
355 buffer[i + j * sourceWidth] += buffer[i + (j - 1) * sourceWidth];
357 buffer[i + j * sourceWidth] -= buffer[(i - 1) + (j - 1) * sourceWidth];
367 auto adjustD = [](
int d,
int iteration) {
369 std::pair<
int,
int> result;
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};
377 result = {d / 2 + 1, d / 2};
378 Q_ASSERT(result.first + result.second > 0);
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(0, i - dxleft);
387 const int i2 = qMin(sourceWidth - 1, i + dxright);
388 const int j1 = qMax(0, j - dytop);
389 const int j2 = qMin(sourceHeight - 1, j + dybottom);
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);
397 const unsigned int colorValue = colorValue64;
398 rawImage[i + j * sourceWidth] &= ~(0xff << col);
399 rawImage[i + j * sourceWidth] |= colorValue << col;
406 QRectF trueClipRectGlob = globalSubRegion(p, itemBounds, filterBounds, primitiveUnits, filterUnits);
409 if (!QImageIOHandler::allocateImage(trueClipRectGlob.toRect().size(), QImage::Format_ARGB32_Premultiplied, &result)) {
410 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
413 result.setOffset(trueClipRectGlob.toRect().topLeft());
414 result.fill(Qt::transparent);
415 QPainter transformPainter(&result);
416 transformPainter.setRenderHint(QPainter::Antialiasing,
true);
418 transformPainter.translate(-result.offset());
419 transformPainter.setTransform(restXr,
true);
420 transformPainter.drawImage(clipRectGlob.topLeft(), tempSource);
421 transformPainter.end();
423 clipToTransformedBounds(&result, p, localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits));
441QImage QSvgFeOffset::apply(
const QMap<QString, QImage> &sources, QPainter *p,
442 const QRectF &itemBounds,
const QRectF &filterBounds,
443 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
445 if (!sources.contains(m_input))
448 const QImage &source = sources[m_input];
450 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
451 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
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());
458 offset = p->transform().map(offset) - p->transform().map(QPoint(0, 0));
460 if (clipRectGlob.isEmpty())
464 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
465 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
468 result.setOffset(clipRectGlob.topLeft());
469 result.fill(Qt::transparent);
471 QPainter copyPainter(&result);
472 copyPainter.drawImage(source.offset()
473 - result.offset() + offset, source);
476 clipToTransformedBounds(&result, p, clipRect);
493QImage QSvgFeMerge::apply(
const QMap<QString, QImage> &sources, QPainter *p,
494 const QRectF &itemBounds,
const QRectF &filterBounds,
495 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
497 QList<QImage> mergeNodeResults;
498 for (
int i = 0; i < renderers().size(); i++) {
499 QSvgNode *child = renderers().at(i);
500 if (child->type() == QSvgNode::FeMergenode) {
501 QSvgFeMergeNode *filter =
static_cast<QSvgFeMergeNode*>(child);
502 mergeNodeResults.append(filter->apply(sources, p, itemBounds, filterBounds, primitiveUnits, filterUnits));
506 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
507 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
508 if (clipRectGlob.isEmpty())
512 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
513 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
516 result.setOffset(clipRectGlob.topLeft());
517 result.fill(Qt::transparent);
519 QPainter proxyPainter(&result);
520 for (
const QImage &i : mergeNodeResults) {
521 proxyPainter.drawImage(QRect(i.offset() - result.offset(), i.size()), i);
525 clipToTransformedBounds(&result, p, clipRect);
576QImage QSvgFeComposite::apply(
const QMap<QString, QImage> &sources, QPainter *p,
577 const QRectF &itemBounds,
const QRectF &filterBounds,
578 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
580 if (!sources.contains(m_input))
582 if (!sources.contains(m_input2))
584 QImage source1 = sources[m_input];
585 QImage source2 = sources[m_input2];
586 Q_ASSERT(source1.depth() == 32);
587 Q_ASSERT(source2.depth() == 32);
589 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
590 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
592 if (clipRectGlob.isEmpty())
596 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
597 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
600 result.setOffset(clipRectGlob.topLeft());
601 result.fill(Qt::transparent);
603 if (m_operator == Operator::Arithmetic) {
604 const qreal k1 = m_k.x();
605 const qreal k2 = m_k.y();
606 const qreal k3 = m_k.z();
607 const qreal k4 = m_k.w();
609 for (
int j = 0; j < result.height(); j++) {
610 int jj1 = j - source1.offset().y() + result.offset().y();
611 int jj2 = j - source2.offset().y() + result.offset().y();
613 QRgb *resultLine =
reinterpret_cast<QRgb *>(result.scanLine(j));
614 QRgb *source1Line =
nullptr;
615 QRgb *source2Line =
nullptr;
617 if (jj1 >= 0 && jj1 < source1.size().height())
618 source1Line =
reinterpret_cast<QRgb *>(source1.scanLine(jj1));
619 if (jj2 >= 0 && jj2 < source2.size().height())
620 source2Line =
reinterpret_cast<QRgb *>(source2.scanLine(jj2));
622 for (
int i = 0; i < result.width(); i++) {
623 int ii1 = i - source1.offset().x() + result.offset().x();
624 int ii2 = i - source2.offset().x() + result.offset().x();
626 QVector4D s1 = QVector4D(0, 0, 0, 0);
627 QVector4D s2 = QVector4D(0, 0, 0, 0);
629 if (ii1 >= 0 && ii1 < source1.size().width() && source1Line) {
630 QRgb pixel1 = source1Line[ii1];
631 s1 = QVector4D(qRed(pixel1),
637 if (ii2 >= 0 && ii2 < source2.size().width() && source2Line) {
638 QRgb pixel2 = source2Line[ii2];
639 s2 = QVector4D(qRed(pixel2),
645 int r = k1 * s1.x() * s2.x() / 255. + k2 * s1.x() + k3 * s2.x() + k4 * 255.;
646 int g = k1 * s1.y() * s2.y() / 255. + k2 * s1.y() + k3 * s2.y() + k4 * 255.;
647 int b = k1 * s1.z() * s2.z() / 255. + k2 * s1.z() + k3 * s2.z() + k4 * 255.;
648 int a = k1 * s1.w() * s2.w() / 255. + k2 * s1.w() + k3 * s2.w() + k4 * 255.;
650 a = qBound(0, a, 255);
651 resultLine[i] = qRgba(qBound(0, r, a),
658 QPainter proxyPainter(&result);
659 proxyPainter.drawImage(QRect(source1.offset() - result.offset(), source1.size()), source1);
661 switch (m_operator) {
663 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
666 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationOut);
669 proxyPainter.setCompositionMode(QPainter::CompositionMode_Xor);
671 case Operator::Lighter:
672 proxyPainter.setCompositionMode(QPainter::CompositionMode_Lighten);
675 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationAtop);
678 proxyPainter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
680 case Operator::Arithmetic:
684 proxyPainter.drawImage(QRect(source2.offset()-result.offset(), source2.size()), source2);
688 clipToTransformedBounds(&result, p, clipRect);
747QImage QSvgFeBlend::apply(
const QMap<QString, QImage> &sources, QPainter *p,
748 const QRectF &itemBounds,
const QRectF &filterBounds,
749 QtSvg::UnitTypes primitiveUnits, QtSvg::UnitTypes filterUnits)
const
751 if (!sources.contains(m_input))
753 if (!sources.contains(m_input2))
755 QImage source1 = sources[m_input];
756 QImage source2 = sources[m_input2];
757 Q_ASSERT(source1.depth() == 32);
758 Q_ASSERT(source2.depth() == 32);
760 QRectF clipRect = localSubRegion(itemBounds, filterBounds, primitiveUnits, filterUnits);
761 QRect clipRectGlob = p->transform().mapRect(clipRect).toRect();
764 if (!QImageIOHandler::allocateImage(clipRectGlob.size(), QImage::Format_ARGB32_Premultiplied, &result)) {
765 qCWarning(lcSvgDraw) <<
"The requested filter buffer is too big, ignoring";
768 result.setOffset(clipRectGlob.topLeft());
769 result.fill(Qt::transparent);
771 for (
int j = 0; j < result.height(); j++) {
772 int jj1 = j - source1.offset().y() + result.offset().y();
773 int jj2 = j - source2.offset().y() + result.offset().y();
775 QRgb *resultLine =
reinterpret_cast<QRgb *>(result.scanLine(j));
776 QRgb *source1Line =
nullptr;
777 QRgb *source2Line =
nullptr;
779 if (jj1 >= 0 && jj1 < source1.size().height())
780 source1Line =
reinterpret_cast<QRgb *>(source1.scanLine(jj1));
781 if (jj2 >= 0 && jj2 < source2.size().height())
782 source2Line =
reinterpret_cast<QRgb *>(source2.scanLine(jj2));
784 for (
int i = 0; i < result.width(); i++) {
785 int ii1 = i - source1.offset().x() + result.offset().x();
786 int ii2 = i - source2.offset().x() + result.offset().x();
788 QRgb pixel1 = (ii1 >= 0 && ii1 < source1.size().width() && source1Line) ?
789 source1Line[ii1] : qRgba(0, 0, 0, 0);
790 QRgb pixel2 = (ii2 >= 0 && ii2 < source2.size().width() && source2Line) ?
791 source2Line[ii2] : qRgba(0, 0, 0, 0);
793 qreal r = 0, g = 0, b = 0;
794 qreal red1 = qRed(pixel1);
795 qreal red2 = qRed(pixel2);
796 qreal green1 = qGreen(pixel1);
797 qreal green2 = qGreen(pixel2);
798 qreal blue1 = qBlue(pixel1);
799 qreal blue2 = qBlue(pixel2);
800 qreal alpha1 = qAlpha(pixel1) / 255.;
801 qreal alpha2 = qAlpha(pixel2) / 255.;
802 qreal a = 255 - (1 - alpha1) * (1 - alpha2) * 255.;
806 r = (1 - alpha1) * red2 + red1;
807 g = (1 - alpha1) * green2 + green1;
808 b = (1 - alpha1) * blue2 + blue1;
811 r = (1 - alpha1) * red2 + (1 - alpha2) * red1 + red1 * red2;
812 g = (1 - alpha1) * green2 + (1 - alpha2) * green1 + green1 * green2;
813 b = (1 - alpha1) * blue2 + (1 - alpha2) * blue1 + blue1 * blue2;
816 r = red2 + red1 - red1 * red2;
817 g = green2 + green1 - green1 * green2;
818 b = blue2 + blue1 - blue1 * blue2;
821 r = qMin((1 - alpha1) * red2 + red1, (1 - alpha2) * red1 + red2);
822 g = qMin((1 - alpha1) * green2 + green1, (1 - alpha2) * green1 + green2);
823 b = qMin((1 - alpha1) * blue2 + blue1, (1 - alpha2) * blue1 + blue2);
826 r = qMax((1 - alpha1) * red2 + red1, (1 - alpha2) * red1 + red2);
827 g = qMax((1 - alpha1) * green2 + green1, (1 - alpha2) * green1 + green2);
828 b = qMax((1 - alpha1) * blue2 + blue1, (1 - alpha2) * blue1 + blue2);
831 a = qBound(0., a, 255.);
832 resultLine[i] = qRgba(qBound(0., r, a),
838 clipToTransformedBounds(&result, p, clipRect);