14 #include <qapplication.h> 17 #define MIN(x,y) ((x) < (y) ? (x) : (y)) 18 #define MAX(x,y) ((x) < (y) ? (x) : (y)) 23 #include "../../gui/statusWidget.h" 107 int dx = p2.x() - p1.x();
108 int dy = p2.y() - p1.y();
114 double recip_r = 1.0 / sqrt( (
double) (dx*dx + dy*dy) );
117 if( QABS(dx) > QABS(dy) )
120 if(dx > 0) delta = -delta;
126 if(dy < 0) delta = -delta;
129 double sinTheta = (delta * recip_r);
130 double theta = asin( sinTheta );
131 double cosTheta = cos( theta );
138 QImage originalImage( filename );
141 if( originalImage.depth() < 32 ) { originalImage = originalImage.convertDepth( 32, Qt::AutoColor ); }
143 QImage rotatedImage( originalImage.width(), originalImage.height(), originalImage.depth() );
146 QString statusMessage = qApp->translate(
"correctImageTilt",
"Correcting Tilt:" );
148 qApp->processEvents();
151 int updateIncrement = (int) ( 0.01 * originalImage.width() * originalImage.height() );
157 double w2 = 0.5 * rotatedImage.width();
158 double h2 = 0.5 * rotatedImage.height();
163 for( y=0; y<rotatedImage.height(); y++)
166 scanLine = rotatedImage.scanLine(y);
167 for( x=0; x<rotatedImage.width(); x++)
170 xp = cosTheta*(x-w2) + sinTheta*(y-h2) + w2;
171 yp = -sinTheta*(x-w2) + cosTheta*(y-h2) + h2;
174 rgb = ((QRgb*)scanLine+x);
179 if(newProgress >= updateIncrement)
183 qApp->processEvents();
190 double nTheta = -theta;
191 double sinNTheta = sin( nTheta );
192 double cosNTheta = cos( nTheta );
195 -sinNTheta*(-w2) + cosNTheta*(-h2) + h2 );
197 DPoint topRight =
DPoint( cosNTheta*(w2) + sinNTheta*(-h2) + w2,
198 -sinNTheta*(w2) + cosNTheta*(-h2) + h2 );
200 DPoint bottomLeft =
DPoint( cosNTheta*(-w2) + sinNTheta*(h2) + w2,
201 -sinNTheta*(-w2) + cosNTheta*(h2) + h2 );
204 -sinNTheta*(w2) + cosNTheta*(h2) + h2 );
207 DPoint top, bottom, left, right;
224 DPoint trueTopLeft ( 0, 0 );
225 DPoint trueTopRight ( rotatedImage.width()-1, 0 );
226 DPoint trueBottomLeft ( 0, rotatedImage.height()-1 );
227 DPoint trueBottomRight( rotatedImage.width()-1, rotatedImage.height()-1 );
243 DPoint center( (
int)w2, (
int)h2 );
250 double minY =
MAX( safeTopLeft.
y(), safeTopRight.
y() );
251 double maxY =
MIN( safeBottomLeft.
y(), safeBottomRight.
y() );
253 double minX =
MAX( safeTopLeft.
x(), safeBottomLeft.
x() );
254 double maxX =
MIN( safeTopRight.
x(), safeBottomRight.
x() );
259 int xMin = (int) minX;
260 int xMax = (int) maxX;
262 int yMin = (int) minY;
263 int yMax = (int) maxY;
265 if( xMin < minX ) xMin++;
266 if( yMin < minY ) yMin++;
271 rotatedImage.depth() );
274 updateIncrement = (int) ( 0.01 * editedImage->width() * editedImage->height() );
282 for( y=yMin; y<=yMax; y++, y2++)
285 scanLine = rotatedImage.scanLine(y);
286 scanLine2 = editedImage->scanLine(y2);
289 for( x=xMin; x<=xMax; x++, x2++)
291 rgb = ((QRgb*)scanLine +x );
292 rgb2 = ((QRgb*)scanLine2+x2);
297 if(newProgress >= updateIncrement)
301 qApp->processEvents();
309 qApp->processEvents();
320 if(xp < 0 || xp >= image->width() ||
321 yp < 0 || yp >= image->height() )
322 return qRgb( 0, 0, 0 );
328 uchar* scanLine1 = image->scanLine( y );
331 if( y < image->
height() - 1 )
332 scanLine2 = image->scanLine( y+1 );
334 scanLine2 = scanLine1;
338 p1 = *((QRgb*)scanLine1+x);
339 p3 = *((QRgb*)scanLine2+x);
341 if( x < image->
width() - 1)
343 p2 = *((QRgb*)scanLine1+x+1);
344 p4 = *((QRgb*)scanLine2+x+1);
353 double alphaY = yp - y;
354 double alphaX = xp - x;
364 double alpha2 = 1.0-alpha;
365 return qRgb( (
int)
MAX(
MIN( 255, alpha2*qRed (Qt::color1) + alpha*qRed(color2) ), 0 ),
366 (
int)
MAX(
MIN( 255, alpha2*qGreen(Qt::color1) + alpha*qGreen(color2) ), 0 ),
367 (
int)
MAX(
MIN( 255, alpha2*qBlue (Qt::color1) + alpha*qBlue(color2) ), 0 ) );
378 if(p1.
x() == p2.
x() &&
386 if( p1.
x() == p3.
x() )
387 {
return DPoint( p1.
x(), p1.
y() ); }
391 {
return DPoint( -32000, -32000 ); }
403 else if( p1.
x() == p2.
x() )
405 double slopeB = ((double) (p4.
y() - p3.
y()) ) / (p4.
x() - p3.
x());
407 double yInterceptB = p3.
y() - slopeB*p3.
x();
410 return DPoint( p2.
x(), slopeB*p2.
x() + yInterceptB );
417 else if( p4.
x() == p3.
x() )
419 double slopeA = ((double) (p2.
y() - p1.
y()) ) / (p2.
x() - p1.
x());
421 double yInterceptA = p1.
y() - slopeA*p1.
x();
424 return DPoint( p4.
x(), slopeA*p4.
x() + yInterceptA );
431 double slopeA = ((double) (p2.
y() - p1.
y()) ) / (p2.
x() - p1.
x());
432 double slopeB = ((double) (p4.
y() - p3.
y()) ) / (p4.
x() - p3.
x());
433 double yInterceptA = p1.
y() - slopeA*p1.
x();
434 double yInterceptB = p3.
y() - slopeB*p3.
x();
445 double x = (yInterceptB - yInterceptA) / (slopeA - slopeB);
446 return DPoint( x, (slopeA * x) + yInterceptA );
QRgb interpolatedPixelValue(double xp, double yp, QImage *image)
QImage * correctImageTilt(QString filename, QPoint p1, QPoint p2, StatusWidget *status)
QRgb blendColors(QRgb color1, QRgb color2, double alpha)
DPoint findTwoLineIntersection(DPoint p1, DPoint p2, DPoint p3, DPoint p4)