krita

kis_perspectivetransform_worker.cpp

00001 /*
00002  * This file is part of Krita
00003  *
00004  *  Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 /*    if(m_dev->hasSelection())
00039         m_dev->selection()->clear();*/
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     //TODO: understand why my caching of the rect didn't work...
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 //    KisColorSpace * cs = m_dev->colorSpace();
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     { // ensure that the random sub accessor is deleted first
00083         KisRandomSubAccessorPixel srcAcc = srcdev->createRandomSubAccessor();
00084         // Initialise progress
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         //Action
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                 // TODO: Should set alpha = alpha*(1-selectedness)
00105 //                 cs->setAlpha( dstIt.rawData(), 255, 1);
00106             } else {
00107 //                 cs->setAlpha( dstIt.rawData(), 0, 1);
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 }
KDE Home | KDE Accessibility Home | Description of Access Keys