krita
kis_perspectivetransform_worker.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kis_perspectivetransform_worker.h"
00021
00022 #include "kis_iterators_pixel.h"
00023 #include "kis_paint_device.h"
00024 #include "kis_perspective_math.h"
00025 #include "kis_progress_display_interface.h"
00026 #include "kis_random_sub_accessor.h"
00027 #include "kis_selection.h"
00028
00029 KisPerspectiveTransformWorker::KisPerspectiveTransformWorker(KisPaintDeviceSP dev, const KisPoint& topLeft, const KisPoint& topRight, const KisPoint& bottomLeft, const KisPoint& bottomRight, KisProgressDisplayInterface *progress)
00030 : KisProgressSubject(), m_dev(dev), m_cancelRequested(false), m_progress(progress)
00031
00032 {
00033 QRect m_r;
00034 if(m_dev->hasSelection())
00035 m_r = m_dev->selection()->selectedExactRect();
00036 else
00037 m_r = m_dev->exactBounds();
00038
00039
00040 kdDebug() << "r = " << m_r << endl;
00041
00042 double* b = KisPerspectiveMath::computeMatrixTransfoToPerspective(topLeft, topRight, bottomLeft, bottomRight, m_r);
00043 for(int i = 0; i < 3; i++)
00044 {
00045 for(int j = 0; j < 3; j++)
00046 {
00047 kdDebug() << "sol[" << 3*i+j << "]=" << b[3*i+j] << endl;
00048 m_matrix[i][j] = b[3*i+j];
00049 }
00050 }
00051 delete b;
00052 }
00053
00054
00055 KisPerspectiveTransformWorker::~KisPerspectiveTransformWorker()
00056 {
00057 }
00058
00059 double norm2(const KisPoint& p)
00060 {
00061 return sqrt(p.x() * p.x() + p.y() * p.y() );
00062 }
00063
00064 void KisPerspectiveTransformWorker::run()
00065 {
00066 kdDebug() << "r = " << m_r << endl;
00067
00068
00069 if(m_dev->hasSelection())
00070 {
00071 m_r = m_dev->selection()->selectedExactRect();
00072 }
00073 else
00074 {
00075 m_r = m_dev->exactBounds();
00076 }
00077
00078
00079 kdDebug() << "r = " << m_r << endl;
00080 KisRectIteratorPixel dstIt = m_dev->createRectIterator(m_r.x(), m_r.y(), m_r.width(), m_r.height(), true);
00081 KisPaintDeviceSP srcdev = new KisPaintDevice(*m_dev.data());
00082 {
00083 KisRandomSubAccessorPixel srcAcc = srcdev->createRandomSubAccessor();
00084
00085 if(m_progress)
00086 m_progress->setSubject(this, true, true);
00087 m_lastProgressReport = 0;
00088 m_progressStep = 0;
00089 m_progressTotalSteps = m_r.width() * m_r.height();
00090
00091 while(!dstIt.isDone())
00092 {
00093 if(dstIt.isSelected())
00094 {
00095 KisPoint p;
00096 double sf = ( dstIt.x() * m_matrix[2][0] + dstIt.y() * m_matrix[2][1] + 1.0);
00097 sf = (sf == 0.) ? 1. : 1./sf;
00098 p.setX( ( dstIt.x() * m_matrix[0][0] + dstIt.y() * m_matrix[0][1] + m_matrix[0][2] ) * sf );
00099 p.setY( ( dstIt.x() * m_matrix[1][0] + dstIt.y() * m_matrix[1][1] + m_matrix[1][2] ) * sf );
00100
00101 srcAcc.moveTo( p );
00102 srcAcc.sampledOldRawData( dstIt.rawData() );
00103
00104
00105
00106 } else {
00107
00108 }
00109 m_progressStep ++;
00110 if(m_lastProgressReport != (m_progressStep * 100) / m_progressTotalSteps)
00111 {
00112 m_lastProgressReport = (m_progressStep * 100) / m_progressTotalSteps;
00113 emit notifyProgress(m_lastProgressReport);
00114 }
00115 if (m_cancelRequested) {
00116 break;
00117 }
00118 ++dstIt;
00119 }
00120 }
00121 }
|