74
74
ResizingBottomRight
77
PerspectiveWidgetPriv()
77
PerspectiveWidgetPriv() :
79
drawWhileMoving(true),
81
inverseTransformation(false),
82
validPerspective(true),
88
currentResizing(ResizingNone),
80
drawWhileMoving = true;
81
inverseTransformation = false;
82
validPerspective = true;
83
currentResizing = ResizingNone;
137
142
PerspectiveWidget::PerspectiveWidget(int w, int h, QWidget* parent)
138
: QWidget(parent), d(new PerspectiveWidgetPriv)
143
: QWidget(parent), d(new PerspectiveWidgetPriv)
140
145
setAttribute(Qt::WA_DeleteOnClose);
141
146
setMinimumSize(w, h);
186
191
float yFactor = (float)d->rect.height()/(float)(oldRect.height());
188
193
d->topLeftPoint = QPoint(lroundf(d->topLeftPoint.x()*xFactor),
189
lroundf(d->topLeftPoint.y()*yFactor));
194
lroundf(d->topLeftPoint.y()*yFactor));
190
195
d->topRightPoint = QPoint(lroundf(d->topRightPoint.x()*xFactor),
191
lroundf(d->topRightPoint.y()*yFactor));
196
lroundf(d->topRightPoint.y()*yFactor));
192
197
d->bottomLeftPoint = QPoint(lroundf(d->bottomLeftPoint.x()*xFactor),
193
lroundf(d->bottomLeftPoint.y()*yFactor));
198
lroundf(d->bottomLeftPoint.y()*yFactor));
194
199
d->bottomRightPoint = QPoint(lroundf(d->bottomRightPoint.x()*xFactor),
195
lroundf(d->bottomRightPoint.y()*yFactor));
200
lroundf(d->bottomRightPoint.y()*yFactor));
196
201
d->transformedCenter = QPoint(lroundf(d->transformedCenter.x()*xFactor),
197
lroundf(d->transformedCenter.y()*yFactor));
202
lroundf(d->transformedCenter.y()*yFactor));
199
204
d->spot.setX((int)((float)d->spot.x() * ( (float)d->width / (float)old_w)));
200
205
d->spot.setY((int)((float)d->spot.y() * ( (float)d->height / (float)old_h)));
362
367
d->bottomLeftCorner.setRect(d->bottomLeftPoint.x() + d->rect.topLeft().x(),
363
368
d->bottomLeftPoint.y() - 7 + d->rect.topLeft().y(), 8, 8);
364
369
d->bottomRightCorner.setRect(d->bottomRightPoint.x() - 7 + d->rect.topLeft().x(),
365
d->bottomRightPoint.y() - 7 + d->rect.topLeft().y(), 8, 8);
370
d->bottomRightPoint.y() - 7 + d->rect.topLeft().y(), 8, 8);
367
372
// Compute the grid array
467
472
if (!d->inverseTransformation)
469
int xspot = d->spot.x() + d->rect.x();
470
int yspot = d->spot.y() + d->rect.y();
471
p.setPen(QPen(Qt::white, d->guideSize, Qt::SolidLine));
472
p.drawLine(xspot, d->rect.top(), xspot, d->rect.bottom());
473
p.drawLine(d->rect.left(), yspot, d->rect.right(), yspot);
474
p.setPen(QPen(d->guideColor, d->guideSize, Qt::DotLine));
475
p.drawLine(xspot, d->rect.top(), xspot, d->rect.bottom());
476
p.drawLine(d->rect.left(), yspot, d->rect.right(), yspot);
474
int xspot = d->spot.x() + d->rect.x();
475
int yspot = d->spot.y() + d->rect.y();
476
p.setPen(QPen(Qt::white, d->guideSize, Qt::SolidLine));
477
p.drawLine(xspot, d->rect.top(), xspot, d->rect.bottom());
478
p.drawLine(d->rect.left(), yspot, d->rect.right(), yspot);
479
p.setPen(QPen(d->guideColor, d->guideSize, Qt::DotLine));
480
p.drawLine(xspot, d->rect.top(), xspot, d->rect.bottom());
481
p.drawLine(d->rect.left(), yspot, d->rect.right(), yspot);
485
490
QPoint PerspectiveWidget::buildPerspective(const QPoint& orignTopLeft, const QPoint& orignBottomRight,
486
const QPoint& transTopLeft, const QPoint& transTopRight,
487
const QPoint& transBottomLeft, const QPoint& transBottomRight,
488
DImg* orgImage, DImg* destImage,
491
const QPoint& transTopLeft, const QPoint& transTopRight,
492
const QPoint& transBottomLeft, const QPoint& transBottomRight,
493
DImg* orgImage, DImg* destImage,
491
496
Matrix matrix, transform;
513
518
scalex = scaley = 1.0;
515
520
if ((x2 - x1) > 0)
516
scalex = 1.0 / (double) (x2 - x1);
522
scalex = 1.0 / (double) (x2 - x1);
518
525
if ((y2 - y1) > 0)
519
scaley = 1.0 / (double) (y2 - y1);
527
scaley = 1.0 / (double) (y2 - y1);
521
530
// Determine the perspective transform that maps from
522
531
// the unit cube to the transformed coordinates
552
561
det2 = dx1 * dy2 - dy1 * dx2;
554
563
if (det1 == 0.0 && det2 == 0.0)
555
565
matrix.coeff[2][0] = 1.0;
557
569
matrix.coeff[2][0] = det1 / det2;
559
572
det1 = dx1 * dy3 - dy1 * dx3;
561
574
if (det1 == 0.0 && det2 == 0.0)
562
576
matrix.coeff[2][1] = 1.0;
564
580
matrix.coeff[2][1] = det1 / det2;
566
583
matrix.coeff[0][0] = tx2 - tx1 + matrix.coeff[2][0] * tx2;
567
584
matrix.coeff[0][1] = tx3 - tx1 + matrix.coeff[2][1] * tx3;
603
620
// Calculate the grid array points.
604
621
double newX, newY;
605
623
for (int i = 0 ; i < d->grid.size() ; ++i)
607
625
transform.transformPoint(d->grid.point(i).x(), d->grid.point(i).y(), &newX, &newY);
624
642
int x, y; // target coordinates
625
643
int u1, v1, u2, v2; // source bounding box
626
644
double uinc, vinc, winc; // increments in source coordinates
627
// pr horizontal target coordinate
645
// pr horizontal target coordinate
629
647
double u[5] = {0.0}; // source coordinates,
630
648
double v[5] = {0.0}; // 2
631
// / \ 0 is sample in the center of pixel
632
// 1 0 3 1..4 is offset 1 pixel in each
633
// \ / direction (in target space)
649
// / \ 0 is sample in the center of pixel
650
// 1 0 3 1..4 is offset 1 pixel in each
651
// \ / direction (in target space)
636
654
double tu[5],tv[5],tw[5]; // undivided source coordinates and divisor
685
705
for (y = y1; y < y2; ++y)
687
// set up inverse transform steps
707
// set up inverse transform steps
689
709
tu[0] = uinc * (x1 + 0.5) + m.coeff[0][1] * (y + 0.5) + m.coeff[0][2] - 0.5;
690
710
tv[0] = vinc * (x1 + 0.5) + m.coeff[1][1] * (y + 0.5) + m.coeff[1][2] - 0.5;
788
808
d->rect.contains( e->x(), e->y() ))
790
810
if ( d->topLeftCorner.contains( e->x(), e->y() ) )
791
812
d->currentResizing = PerspectiveWidgetPriv::ResizingTopLeft;
792
814
else if ( d->bottomRightCorner.contains( e->x(), e->y() ) )
793
816
d->currentResizing = PerspectiveWidgetPriv::ResizingBottomRight;
794
818
else if ( d->topRightCorner.contains( e->x(), e->y() ) )
795
820
d->currentResizing = PerspectiveWidgetPriv::ResizingTopRight;
796
822
else if ( d->bottomLeftCorner.contains( e->x(), e->y() ) )
797
824
d->currentResizing = PerspectiveWidgetPriv::ResizingBottomLeft;
800
828
d->spot.setX(e->x()-d->rect.x());
840
868
if (!d->rect.contains( pm ))
842
870
if (pm.x() > d->rect.right())
843
872
pm.setX(d->rect.right());
844
874
else if (pm.x() < d->rect.left())
845
876
pm.setX(d->rect.left());
847
879
if (pm.y() > d->rect.bottom())
848
881
pm.setY(d->rect.bottom());
849
883
else if (pm.y() < d->rect.top())
850
885
pm.setY(d->rect.top());
853
889
if ( d->currentResizing == PerspectiveWidgetPriv::ResizingTopLeft )
863
899
QRegion unusableArea(unusablePoints);
865
901
if ( unusableArea.contains(pm) && !d->inverseTransformation )
866
903
d->validPerspective = false;
868
906
d->topLeftPoint = pm - d->rect.topLeft();
869
907
setCursor( Qt::SizeFDiagCursor );
882
920
QRegion unusableArea(unusablePoints);
884
922
if ( unusableArea.contains(pm) && !d->inverseTransformation )
885
924
d->validPerspective = false;
887
927
d->topRightPoint = pm - d->rect.topLeft();
888
928
setCursor( Qt::SizeBDiagCursor );
901
941
QRegion unusableArea(unusablePoints);
903
943
if ( unusableArea.contains(pm) && !d->inverseTransformation )
904
945
d->validPerspective = false;
906
948
d->bottomLeftPoint = pm - d->rect.topLeft();
907
949
setCursor( Qt::SizeBDiagCursor );
920
962
QRegion unusableArea(unusablePoints);
922
964
if ( unusableArea.contains(pm) && !d->inverseTransformation )
923
966
d->validPerspective = false;
925
969
d->bottomRightPoint = pm - d->rect.topLeft();
926
970
setCursor( Qt::SizeFDiagCursor );
941
985
if ( d->topLeftCorner.contains( e->x(), e->y() ) ||
942
986
d->bottomRightCorner.contains( e->x(), e->y() ) )
943
988
setCursor( Qt::SizeFDiagCursor );
945
991
else if ( d->topRightCorner.contains( e->x(), e->y() ) ||
946
992
d->bottomLeftCorner.contains( e->x(), e->y() ) )
947
994
setCursor( Qt::SizeBDiagCursor );