Krita Source Code Documentation
Loading...
Searching...
No Matches
GridIterationTools::QImagePolygonOp Struct Reference

#include <kis_grid_interpolation_tools.h>

Public Member Functions

void fastCopyArea (QPolygonF areaToCopy)
 
void fastCopyArea (QRect areaToCopy)
 
void fastCopyArea (QRect areaToCopy, bool lazy)
 
void finalize ()
 
void operator() (const QPolygonF &srcPolygon, const QPolygonF &dstPolygon)
 
void operator() (const QPolygonF &srcPolygon, const QPolygonF &dstPolygon, const QPolygonF &clipDstPolygon)
 
 QImagePolygonOp (const QImage &srcImage, QImage &dstImage, const QPointF &srcImageOffset, const QPointF &dstImageOffset)
 
void setCanMergeRects (bool canMergeRects)
 

Public Attributes

QImage & m_dstImage
 
QPointF m_dstImageOffset
 
QRect m_dstImageRect
 
const qreal m_epsilon {0.1}
 
const QImage & m_srcImage
 
QPointF m_srcImageOffset
 
QRect m_srcImageRect
 

Private Attributes

bool m_canMergeRects {true}
 
QVector< QRect > m_rectsToCopy
 

Detailed Description

Definition at line 355 of file kis_grid_interpolation_tools.h.

Constructor & Destructor Documentation

◆ QImagePolygonOp()

GridIterationTools::QImagePolygonOp::QImagePolygonOp ( const QImage & srcImage,
QImage & dstImage,
const QPointF & srcImageOffset,
const QPointF & dstImageOffset )
inline

Member Function Documentation

◆ fastCopyArea() [1/3]

void GridIterationTools::QImagePolygonOp::fastCopyArea ( QPolygonF areaToCopy)
inline

Definition at line 368 of file kis_grid_interpolation_tools.h.

368 {
369 QRect boundRect = areaToCopy.boundingRect().toAlignedRect();
370
371 if (boundRect.isEmpty()) return;
372
373 bool isItRect = KisAlgebra2D::isPolygonRect(areaToCopy, m_epsilon); // no need for lower tolerance
374 if (isItRect) {
375 fastCopyArea(boundRect);
376 return;
377 }
378
379 // this can possibly be optimized with scanlining the polygon
380 // (use intersectLineConvexPolygon to get a line at every height)
381 // but it doesn't matter much because in the vast majority of cases
382 // it should go straight to the rect area copying
383
384 for (int y = boundRect.top(); y <= boundRect.bottom(); y++) {
385 for (int x = boundRect.left(); x <= boundRect.right(); x++) {
386 QPointF dstPoint = QPointF(x, y);
387 QPointF srcPoint = dstPoint;
388
389 if (areaToCopy.containsPoint(middlePoint(srcPoint), Qt::OddEvenFill)) {
390
391 // about srcPoint/dstPoint hell please see a
392 // comment in PaintDevicePolygonOp::operator() ()
393
394 srcPoint -= m_dstImageOffset;
396
397 QPoint srcPointI = srcPoint.toPoint();
398 QPoint dstPointI = dstPoint.toPoint();
399
400 if (!m_dstImageRect.contains(srcPointI)) continue;
401 if (!m_srcImageRect.contains(dstPointI)) continue;
402
403 m_dstImage.setPixel(srcPointI, m_srcImage.pixel(dstPointI));
404 }
405
406 }
407 }
408
409 }
QPointF dstPoint
QPointF middlePoint(int x, int y)
bool isPolygonRect(const Polygon &poly, Difference tolerance)

References dstPoint, fastCopyArea(), KisAlgebra2D::isPolygonRect(), m_dstImage, m_dstImageOffset, m_dstImageRect, m_epsilon, m_srcImage, m_srcImageOffset, m_srcImageRect, and GridIterationTools::middlePoint().

◆ fastCopyArea() [2/3]

void GridIterationTools::QImagePolygonOp::fastCopyArea ( QRect areaToCopy)
inline

◆ fastCopyArea() [3/3]

void GridIterationTools::QImagePolygonOp::fastCopyArea ( QRect areaToCopy,
bool lazy )
inline

Definition at line 415 of file kis_grid_interpolation_tools.h.

415 {
416 if (lazy) {
417 m_rectsToCopy.append(areaToCopy.adjusted(0, 0, -1, -1));
418 return;
419 }
420
421 // only handling saved offsets
422 QRect srcArea = areaToCopy.translated(-m_srcImageOffset.toPoint());
423 QRect dstArea = areaToCopy.translated(-m_dstImageOffset.toPoint());
424
425 srcArea = srcArea.intersected(m_srcImageRect);
426 dstArea = dstArea.intersected(m_dstImageRect);
427
428 // it might look pointless but it cuts off unneeded areas on both rects based on where they end up
429 // since *I know* they are the same rectangle before translation
430 // TODO: I'm pretty sure this logic is correct, but let's check it when I'm less sleepy
431 QRect srcAreaUntranslated = srcArea.translated(m_srcImageOffset.toPoint());
432 QRect dstAreaUntranslated = dstArea.translated(m_dstImageOffset.toPoint());
433
434 QRect actualCopyArea = srcAreaUntranslated.intersected(dstAreaUntranslated);
435 srcArea = actualCopyArea.translated(-m_srcImageOffset.toPoint());
436 dstArea = actualCopyArea.translated(-m_dstImageOffset.toPoint());
437
438 int bytesPerPixel = m_srcImage.sizeInBytes()/m_srcImage.height()/m_srcImage.width();
439
440 int srcX = srcArea.left()*bytesPerPixel;
441 int dstX = dstArea.left()*bytesPerPixel;
442
443 for (int srcY = srcArea.top(); srcY <= srcArea.bottom(); ++srcY) {
444
445 int dstY = dstArea.top() + srcY - srcArea.top();
446 const uchar *srcLine = m_srcImage.constScanLine(srcY);
447 uchar *dstLine = m_dstImage.scanLine(dstY);
448 memcpy(dstLine + dstX, srcLine + srcX, srcArea.width()*bytesPerPixel);
449
450 }
451 }

References m_dstImage, m_dstImageOffset, m_dstImageRect, m_rectsToCopy, m_srcImage, m_srcImageOffset, and m_srcImageRect.

◆ finalize()

void GridIterationTools::QImagePolygonOp::finalize ( )
inline

Definition at line 513 of file kis_grid_interpolation_tools.h.

513 {
514
516
517 for (QVector<QRect>::iterator it = m_rectsToCopy.begin(); it < end; it++) {
518 QRect areaToCopy = *it;
519 fastCopyArea(areaToCopy.adjusted(0, 0, 1, 1), false);
520 }
521
523 }
static QVector< QRect >::iterator mergeSparseRects(QVector< QRect >::iterator beginIt, QVector< QRect >::iterator endIt)
merge a set of rectangles into a smaller set of bigger rectangles

References fastCopyArea(), m_rectsToCopy, and KisRegion::mergeSparseRects().

◆ operator()() [1/2]

void GridIterationTools::QImagePolygonOp::operator() ( const QPolygonF & srcPolygon,
const QPolygonF & dstPolygon )
inline

Definition at line 453 of file kis_grid_interpolation_tools.h.

453 {
454 this->operator() (srcPolygon, dstPolygon, dstPolygon);
455 }
void operator()(const QPolygonF &srcPolygon, const QPolygonF &dstPolygon)

References operator()().

◆ operator()() [2/2]

void GridIterationTools::QImagePolygonOp::operator() ( const QPolygonF & srcPolygon,
const QPolygonF & dstPolygon,
const QPolygonF & clipDstPolygon )
inline

Definition at line 457 of file kis_grid_interpolation_tools.h.

457 {
458 QRect boundRect = clipDstPolygon.boundingRect().toAlignedRect();
459
460 bool samePolygon = m_dstImage.format() == m_srcImage.format() && KisAlgebra2D::fuzzyPointCompare(srcPolygon, dstPolygon, m_epsilon);
461
462 if (samePolygon) {
463 // we can use clipDstPolygon here, because it will be smaller than dstPolygon and srcPolygon, because of how IncompletePolicy works
464 // we could also calculate intersection here if we're worried whether that fact is always true
465 fastCopyArea(clipDstPolygon);
466 return;
467 }
468
469 KisFourPointInterpolatorBackward interp(srcPolygon, dstPolygon);
470
471 for (int y = boundRect.top(); y <= boundRect.bottom(); y++) {
472 interp.setY(y);
473 for (int x = boundRect.left(); x <= boundRect.right(); x++) {
474
475 QPointF srcPoint(x, y);
476 if (clipDstPolygon.containsPoint(middlePoint(srcPoint), Qt::OddEvenFill)) {
477
478 interp.setX(srcPoint.x());
479 QPointF dstPoint = interp.getValue();
480
481 // about srcPoint/dstPoint hell please see a
482 // comment in PaintDevicePolygonOp::operator() ()
483
484 srcPoint -= m_dstImageOffset;
486
487 QPoint srcPointI = srcPoint.toPoint();
488 QPoint dstPointI = dstPoint.toPoint();
489
490 if (!m_dstImageRect.contains(srcPointI)) continue;
491 if (!m_srcImageRect.contains(dstPointI)) continue;
492
493 m_dstImage.setPixel(srcPointI, m_srcImage.pixel(dstPointI));
494 }
495 }
496 }
497
498#ifdef DEBUG_PAINTING_POLYGONS
499 QPainter gc(&m_dstImage);
500 gc.setPen(Qt::red);
501 gc.setOpacity(0.5);
502
503 gc.setBrush(Qt::green);
504 gc.drawPolygon(clipDstPolygon.translated(-m_dstImageOffset));
505
506 gc.setBrush(Qt::blue);
507 //gc.drawPolygon(dstPolygon.translated(-m_dstImageOffset));
508
509#endif /* DEBUG_PAINTING_POLYGONS */
510
511 }
qreal interp(qreal r, qreal a, qreal b)
private functions
bool fuzzyPointCompare(const QPointF &p1, const QPointF &p2)

References dstPoint, fastCopyArea(), KisAlgebra2D::fuzzyPointCompare(), interp(), m_dstImage, m_dstImageOffset, m_dstImageRect, m_epsilon, m_srcImage, m_srcImageOffset, m_srcImageRect, and GridIterationTools::middlePoint().

◆ setCanMergeRects()

void GridIterationTools::QImagePolygonOp::setCanMergeRects ( bool canMergeRects)
inline

Definition at line 525 of file kis_grid_interpolation_tools.h.

525 {
526 m_canMergeRects = canMergeRects;
527 }

References m_canMergeRects.

Member Data Documentation

◆ m_canMergeRects

bool GridIterationTools::QImagePolygonOp::m_canMergeRects {true}
private

Definition at line 541 of file kis_grid_interpolation_tools.h.

541{true};

◆ m_dstImage

QImage& GridIterationTools::QImagePolygonOp::m_dstImage

Definition at line 531 of file kis_grid_interpolation_tools.h.

◆ m_dstImageOffset

QPointF GridIterationTools::QImagePolygonOp::m_dstImageOffset

Definition at line 533 of file kis_grid_interpolation_tools.h.

◆ m_dstImageRect

QRect GridIterationTools::QImagePolygonOp::m_dstImageRect

Definition at line 536 of file kis_grid_interpolation_tools.h.

◆ m_epsilon

const qreal GridIterationTools::QImagePolygonOp::m_epsilon {0.1}

Definition at line 538 of file kis_grid_interpolation_tools.h.

538{0.1};

◆ m_rectsToCopy

QVector<QRect> GridIterationTools::QImagePolygonOp::m_rectsToCopy
private

Definition at line 542 of file kis_grid_interpolation_tools.h.

◆ m_srcImage

const QImage& GridIterationTools::QImagePolygonOp::m_srcImage

Definition at line 530 of file kis_grid_interpolation_tools.h.

◆ m_srcImageOffset

QPointF GridIterationTools::QImagePolygonOp::m_srcImageOffset

Definition at line 532 of file kis_grid_interpolation_tools.h.

◆ m_srcImageRect

QRect GridIterationTools::QImagePolygonOp::m_srcImageRect

Definition at line 535 of file kis_grid_interpolation_tools.h.


The documentation for this struct was generated from the following file: