chalk

kis_perspectivetransform_worker.cpp
1/*
2 * This file is part of Chalk
3 *
4 * Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "kis_perspectivetransform_worker.h"
21
22#include "kis_iterators_pixel.h"
23#include "kis_paint_device.h"
24#include "kis_perspective_math.h"
25#include "kis_progress_display_interface.h"
26#include "kis_random_sub_accessor.h"
27#include "kis_selection.h"
28
29KisPerspectiveTransformWorker::KisPerspectiveTransformWorker(KisPaintDeviceSP dev, const KisPoint& topLeft, const KisPoint& topRight, const KisPoint& bottomLeft, const KisPoint& bottomRight, KisProgressDisplayInterface *progress)
30 : KisProgressSubject(), m_dev(dev), m_cancelRequested(false), m_progress(progress)
31
32{
33 TQRect m_r;
34 if(m_dev->hasSelection())
35 m_r = m_dev->selection()->selectedExactRect();
36 else
37 m_r = m_dev->exactBounds();
38/* if(m_dev->hasSelection())
39 m_dev->selection()->clear();*/
40 kdDebug() << "r = " << m_r << endl;
41
42 double* b = KisPerspectiveMath::computeMatrixTransfoToPerspective(topLeft, topRight, bottomLeft, bottomRight, m_r);
43 for(int i = 0; i < 3; i++)
44 {
45 for(int j = 0; j < 3; j++)
46 {
47 kdDebug() << "sol[" << 3*i+j << "]=" << b[3*i+j] << endl;
48 m_matrix[i][j] = b[3*i+j];
49 }
50 }
51 delete b;
52}
53
54
55KisPerspectiveTransformWorker::~KisPerspectiveTransformWorker()
56{
57}
58
59double norm2(const KisPoint& p)
60{
61 return sqrt(p.x() * p.x() + p.y() * p.y() );
62}
63
64void KisPerspectiveTransformWorker::run()
65{
66 kdDebug() << "r = " << m_r << endl;
67
68 //TODO: understand why my caching of the rect didn't work...
69 if(m_dev->hasSelection())
70 {
71 m_r = m_dev->selection()->selectedExactRect();
72 }
73 else
74 {
75 m_r = m_dev->exactBounds();
76 }
77// KisColorSpace * cs = m_dev->colorSpace();
78
79 kdDebug() << "r = " << m_r << endl;
80 KisRectIteratorPixel dstIt = m_dev->createRectIterator(m_r.x(), m_r.y(), m_r.width(), m_r.height(), true);
81 KisPaintDeviceSP srcdev = new KisPaintDevice(*m_dev.data());
82 { // ensure that the random sub accessor is deleted first
83 KisRandomSubAccessorPixel srcAcc = srcdev->createRandomSubAccessor();
84 // Initialise progress
85 if(m_progress)
86 m_progress->setSubject(this, true, true);
87 m_lastProgressReport = 0;
88 m_progressStep = 0;
89 m_progressTotalSteps = m_r.width() * m_r.height();
90 //Action
91 while(!dstIt.isDone())
92 {
93 if(dstIt.isSelected())
94 {
95 KisPoint p;
96 double sf = ( dstIt.x() * m_matrix[2][0] + dstIt.y() * m_matrix[2][1] + 1.0);
97 sf = (sf == 0.) ? 1. : 1./sf;
98 p.setX( ( dstIt.x() * m_matrix[0][0] + dstIt.y() * m_matrix[0][1] + m_matrix[0][2] ) * sf );
99 p.setY( ( dstIt.x() * m_matrix[1][0] + dstIt.y() * m_matrix[1][1] + m_matrix[1][2] ) * sf );
100
101 srcAcc.moveTo( p );
102 srcAcc.sampledOldRawData( dstIt.rawData() );
103
104 // TODO: Should set alpha = alpha*(1-selectedness)
105// cs->setAlpha( dstIt.rawData(), 255, 1);
106 } else {
107// cs->setAlpha( dstIt.rawData(), 0, 1);
108 }
109 m_progressStep ++;
110 if(m_lastProgressReport != (m_progressStep * 100) / m_progressTotalSteps)
111 {
112 m_lastProgressReport = (m_progressStep * 100) / m_progressTotalSteps;
113 emit notifyProgress(m_lastProgressReport);
114 }
115 if (m_cancelRequested) {
116 break;
117 }
118 ++dstIt;
119 }
120 }
121}
A paint device contains the actual pixel data and offers methods to read and write pixels.
A double-based point class that can return it's coordinates approximated to integers.
Definition kis_point.h:28