summaryrefslogtreecommitdiffstats
path: root/chalk/core/kis_perspectivetransform_worker.cpp
blob: 637a61deeac912db38261799061f128ebe813d37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * This file is part of Chalk
 *
 *  Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kis_perspectivetransform_worker.h"

#include "kis_iterators_pixel.h"
#include "kis_paint_device.h"
#include "kis_perspective_math.h"
#include "kis_progress_display_interface.h"
#include "kis_random_sub_accessor.h"
#include "kis_selection.h"

KisPerspectiveTransformWorker::KisPerspectiveTransformWorker(KisPaintDeviceSP dev, const KisPoint& topLeft, const KisPoint& topRight, const KisPoint& bottomLeft, const KisPoint& bottomRight, KisProgressDisplayInterface *progress)
    : KisProgressSubject(), m_dev(dev), m_cancelRequested(false), m_progress(progress)

{
    TQRect m_r;
    if(m_dev->hasSelection())
        m_r = m_dev->selection()->selectedExactRect();
    else
        m_r = m_dev->exactBounds();
/*    if(m_dev->hasSelection())
        m_dev->selection()->clear();*/
    kdDebug() << "r = " << m_r << endl;

    double* b = KisPerspectiveMath::computeMatrixTransfoToPerspective(topLeft, topRight, bottomLeft, bottomRight, m_r);
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            kdDebug() << "sol[" << 3*i+j << "]=" << b[3*i+j] << endl;
            m_matrix[i][j] = b[3*i+j];
        }
    }
    delete b;
}


KisPerspectiveTransformWorker::~KisPerspectiveTransformWorker()
{
}

double norm2(const KisPoint& p)
{
    return sqrt(p.x() * p.x() + p.y() * p.y() );
}

void KisPerspectiveTransformWorker::run()
{
    kdDebug() << "r = " << m_r << endl;

    //TODO: understand why my caching of the rect didn't work...
    if(m_dev->hasSelection())
    {
        m_r = m_dev->selection()->selectedExactRect();
    }
    else
    {
        m_r = m_dev->exactBounds();
    }
//    KisColorSpace * cs = m_dev->colorSpace();

    kdDebug() << "r = " << m_r << endl;
    KisRectIteratorPixel dstIt = m_dev->createRectIterator(m_r.x(), m_r.y(), m_r.width(), m_r.height(), true);
    KisPaintDeviceSP srcdev = new KisPaintDevice(*m_dev.data());
    { // ensure that the random sub accessor is deleted first
        KisRandomSubAccessorPixel srcAcc = srcdev->createRandomSubAccessor();
        // Initialise progress
        if(m_progress)
            m_progress->setSubject(this, true, true);
        m_lastProgressReport = 0;
        m_progressStep = 0;
        m_progressTotalSteps = m_r.width() * m_r.height();
        //Action
        while(!dstIt.isDone())
        {
            if(dstIt.isSelected())
            {
                KisPoint p;
                double sf = ( dstIt.x() * m_matrix[2][0] + dstIt.y() * m_matrix[2][1] + 1.0);
                sf = (sf == 0.) ? 1. : 1./sf;
                p.setX( ( dstIt.x() * m_matrix[0][0] + dstIt.y() * m_matrix[0][1] + m_matrix[0][2] ) * sf );
                p.setY( ( dstIt.x() * m_matrix[1][0] + dstIt.y() * m_matrix[1][1] + m_matrix[1][2] ) * sf );

                srcAcc.moveTo( p );
                srcAcc.sampledOldRawData( dstIt.rawData() );

                // TODO: Should set alpha = alpha*(1-selectedness)
//                 cs->setAlpha( dstIt.rawData(), 255, 1);
            } else {
//                 cs->setAlpha( dstIt.rawData(), 0, 1);
            }
            m_progressStep ++;
            if(m_lastProgressReport != (m_progressStep * 100) / m_progressTotalSteps)
            {
                m_lastProgressReport = (m_progressStep * 100) / m_progressTotalSteps;
                emit notifyProgress(m_lastProgressReport);
            }
            if (m_cancelRequested) {
                break;
            }
            ++dstIt;
        }
    }
}