~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to imageplugins/transform/perspectivewidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-12-21 23:19:11 UTC
  • mfrom: (1.2.33 upstream) (3.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20101221231911-z9jip7s5aht1jqn9
Tags: 2:1.7.0-1ubuntu1
* Merge from Debian Experimental. Remaining Ubuntu changes:
  - Export .pot name and copy to plugins in debian/rules
  - Version build-depends on kipi-plugins-dev to ensure build is against the
    same version on all archs
* Drop debian/patches/kubuntu_01_linker.diff, incoporated upstream
* Remove patches directory and unused patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
        ResizingBottomRight
75
75
    };
76
76
 
77
 
    PerspectiveWidgetPriv()
 
77
    PerspectiveWidgetPriv() :
 
78
        antiAliasing(false),
 
79
        drawWhileMoving(true),
 
80
        drawGrid(false),
 
81
        inverseTransformation(false),
 
82
        validPerspective(true),
 
83
        data(0),
 
84
        width(0),
 
85
        height(0),
 
86
        origW(0),
 
87
        origH(0),
 
88
        currentResizing(ResizingNone),
 
89
        guideSize(1),
 
90
        guideColor(Qt::red),
 
91
        pixmap(0),
 
92
        iface(0)
78
93
    {
79
 
        drawGrid              = false;
80
 
        drawWhileMoving       = true;
81
 
        inverseTransformation = false;
82
 
        validPerspective      = true;
83
 
        currentResizing       = ResizingNone;
84
 
        guideColor            = Qt::red;
85
 
        guideSize             = 1;
86
 
        data                  = 0;
87
 
        pixmap                = 0;
88
 
        iface                 = 0;
89
94
    }
90
95
 
91
96
    bool        antiAliasing;
135
140
};
136
141
 
137
142
PerspectiveWidget::PerspectiveWidget(int w, int h, QWidget* parent)
138
 
                 : QWidget(parent), d(new PerspectiveWidgetPriv)
 
143
    : QWidget(parent), d(new PerspectiveWidgetPriv)
139
144
{
140
145
    setAttribute(Qt::WA_DeleteOnClose);
141
146
    setMinimumSize(w, h);
186
191
    float yFactor = (float)d->rect.height()/(float)(oldRect.height());
187
192
 
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));
198
203
 
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);
366
371
 
367
372
    // Compute the grid array
368
373
 
466
471
 
467
472
    if (!d->inverseTransformation)
468
473
    {
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);
477
482
    }
478
483
 
479
484
    p.end();
483
488
}
484
489
 
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,
489
 
                                           DColor background)
 
491
        const QPoint& transTopLeft, const QPoint& transTopRight,
 
492
        const QPoint& transBottomLeft, const QPoint& transBottomRight,
 
493
        DImg* orgImage, DImg* destImage,
 
494
        DColor background)
490
495
{
491
496
    Matrix matrix, transform;
492
497
    double  scalex;
513
518
    scalex = scaley = 1.0;
514
519
 
515
520
    if ((x2 - x1) > 0)
516
 
      scalex = 1.0 / (double) (x2 - x1);
 
521
    {
 
522
        scalex = 1.0 / (double) (x2 - x1);
 
523
    }
517
524
 
518
525
    if ((y2 - y1) > 0)
519
 
      scaley = 1.0 / (double) (y2 - y1);
 
526
    {
 
527
        scaley = 1.0 / (double) (y2 - y1);
 
528
    }
520
529
 
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;
553
562
 
554
563
        if (det1 == 0.0 && det2 == 0.0)
 
564
        {
555
565
            matrix.coeff[2][0] = 1.0;
 
566
        }
556
567
        else
 
568
        {
557
569
            matrix.coeff[2][0] = det1 / det2;
 
570
        }
558
571
 
559
572
        det1 = dx1 * dy3 - dy1 * dx3;
560
573
 
561
574
        if (det1 == 0.0 && det2 == 0.0)
 
575
        {
562
576
            matrix.coeff[2][1] = 1.0;
 
577
        }
563
578
        else
 
579
        {
564
580
            matrix.coeff[2][1] = det1 / det2;
 
581
        }
565
582
 
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;
602
619
 
603
620
    // Calculate the grid array points.
604
621
    double newX, newY;
 
622
 
605
623
    for (int i = 0 ; i < d->grid.size() ; ++i)
606
624
    {
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
628
646
 
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)
634
 
                                  //   4
 
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)
 
652
    //   4
635
653
 
636
654
    double tu[5],tv[5],tw[5];     // undivided source coordinates and divisor
637
655
 
652
670
    newData     = destImage->bits();
653
671
 
654
672
    if (sixteenBit)
 
673
    {
655
674
        background.convertToSixteenBit();
 
675
    }
656
676
 
657
677
    //destImage->fill(background);
658
678
 
684
704
 
685
705
    for (y = y1; y < y2; ++y)
686
706
    {
687
 
       // set up inverse transform steps
 
707
        // set up inverse transform steps
688
708
 
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() ))
789
809
    {
790
810
        if ( d->topLeftCorner.contains( e->x(), e->y() ) )
 
811
        {
791
812
            d->currentResizing = PerspectiveWidgetPriv::ResizingTopLeft;
 
813
        }
792
814
        else if ( d->bottomRightCorner.contains( e->x(), e->y() ) )
 
815
        {
793
816
            d->currentResizing = PerspectiveWidgetPriv::ResizingBottomRight;
 
817
        }
794
818
        else if ( d->topRightCorner.contains( e->x(), e->y() ) )
 
819
        {
795
820
            d->currentResizing = PerspectiveWidgetPriv::ResizingTopRight;
 
821
        }
796
822
        else if ( d->bottomLeftCorner.contains( e->x(), e->y() ) )
 
823
        {
797
824
            d->currentResizing = PerspectiveWidgetPriv::ResizingBottomLeft;
 
825
        }
798
826
        else
799
827
        {
800
828
            d->spot.setX(e->x()-d->rect.x());
840
868
            if (!d->rect.contains( pm ))
841
869
            {
842
870
                if (pm.x() > d->rect.right())
 
871
                {
843
872
                    pm.setX(d->rect.right());
 
873
                }
844
874
                else if (pm.x() < d->rect.left())
 
875
                {
845
876
                    pm.setX(d->rect.left());
 
877
                }
846
878
 
847
879
                if (pm.y() > d->rect.bottom())
 
880
                {
848
881
                    pm.setY(d->rect.bottom());
 
882
                }
849
883
                else if (pm.y() < d->rect.top())
 
884
                {
850
885
                    pm.setY(d->rect.top());
 
886
                }
851
887
            }
852
888
 
853
889
            if ( d->currentResizing == PerspectiveWidgetPriv::ResizingTopLeft )
863
899
                QRegion unusableArea(unusablePoints);
864
900
 
865
901
                if ( unusableArea.contains(pm) && !d->inverseTransformation )
 
902
                {
866
903
                    d->validPerspective = false;
 
904
                }
867
905
 
868
906
                d->topLeftPoint = pm - d->rect.topLeft();
869
907
                setCursor( Qt::SizeFDiagCursor );
882
920
                QRegion unusableArea(unusablePoints);
883
921
 
884
922
                if ( unusableArea.contains(pm) && !d->inverseTransformation )
 
923
                {
885
924
                    d->validPerspective = false;
 
925
                }
886
926
 
887
927
                d->topRightPoint = pm - d->rect.topLeft();
888
928
                setCursor( Qt::SizeBDiagCursor );
901
941
                QRegion unusableArea(unusablePoints);
902
942
 
903
943
                if ( unusableArea.contains(pm) && !d->inverseTransformation )
 
944
                {
904
945
                    d->validPerspective = false;
 
946
                }
905
947
 
906
948
                d->bottomLeftPoint = pm - d->rect.topLeft();
907
949
                setCursor( Qt::SizeBDiagCursor );
920
962
                QRegion unusableArea(unusablePoints);
921
963
 
922
964
                if ( unusableArea.contains(pm) && !d->inverseTransformation )
 
965
                {
923
966
                    d->validPerspective = false;
 
967
                }
924
968
 
925
969
                d->bottomRightPoint = pm - d->rect.topLeft();
926
970
                setCursor( Qt::SizeFDiagCursor );
940
984
    {
941
985
        if ( d->topLeftCorner.contains( e->x(), e->y() ) ||
942
986
             d->bottomRightCorner.contains( e->x(), e->y() ) )
 
987
        {
943
988
            setCursor( Qt::SizeFDiagCursor );
 
989
        }
944
990
 
945
991
        else if ( d->topRightCorner.contains( e->x(), e->y() ) ||
946
992
                  d->bottomLeftCorner.contains( e->x(), e->y() ) )
 
993
        {
947
994
            setCursor( Qt::SizeBDiagCursor );
 
995
        }
948
996
        else
 
997
        {
949
998
            unsetCursor();
 
999
        }
950
1000
    }
951
1001
}
952
1002