00102 {
00103
00104
int dx = p2.x() - p1.x();
00105
int dy = p2.y() - p1.y();
00106
00107
00108
int delta = 0;
00109
00110
00111
double recip_r = 1.0 / sqrt( (
double) (dx*dx + dy*dy) );
00112
00113
00114
if( QABS(dx) > QABS(dy) )
00115 {
00116 delta = dy;
00117
if(dx > 0) delta = -delta;
00118 }
00119
00120
else
00121 {
00122 delta = dx;
00123
if(dy < 0) delta = -delta;
00124 }
00125
00126
double sinTheta = (delta * recip_r);
00127
double theta = asin( sinTheta );
00128
double cosTheta = cos( theta );
00129
00130
00131
if( theta == 0 )
00132
return NULL;
00133
00134
00135 QImage originalImage( filename );
00136
00137
00138
if( originalImage.depth() < 32 ) { originalImage = originalImage.convertDepth( 32, Qt::AutoColor ); }
00139
00140 QImage rotatedImage( originalImage.width(), originalImage.height(), originalImage.depth() );
00141
00142
00143 QString statusMessage = qApp->translate(
"correctImageTilt",
"Correcting Tilt:" );
00144
status->
showProgressBar( statusMessage, 200 );
00145 qApp->processEvents();
00146
00147
00148
int updateIncrement = (
int) ( 0.01 * originalImage.width() * originalImage.height() );
00149
int newProgress = 0;
00150
00151
00152
double xp, yp;
00153
00154
double w2 = 0.5 * rotatedImage.width();
00155
double h2 = 0.5 * rotatedImage.height();
00156
00157
int x,y;
00158 uchar* scanLine;
00159 QRgb* rgb;
00160
for( y=0; y<rotatedImage.height(); y++)
00161 {
00162
00163 scanLine = rotatedImage.scanLine(y);
00164
for( x=0; x<rotatedImage.width(); x++)
00165 {
00166
00167 xp = cosTheta*(x-w2) + sinTheta*(y-h2) + w2;
00168 yp = -sinTheta*(x-w2) + cosTheta*(y-h2) + h2;
00169
00170
00171 rgb = ((QRgb*)scanLine+x);
00172 *rgb =
interpolatedPixelValue( xp, yp, &originalImage);
00173
00174
00175
newProgress++;
00176
if(
newProgress >=
updateIncrement)
00177 {
00178
newProgress = 0;
00179
status->
incrementProgress();
00180 qApp->processEvents();
00181 }
00182
00183 }
00184 }
00185
00186
00187
double nTheta = -theta;
00188
double sinNTheta = sin( nTheta );
00189
double cosNTheta = cos( nTheta );
00190
00191
DPoint topLeft =
DPoint( cosNTheta*(-w2) + sinNTheta*(-h2) + w2,
00192 -sinNTheta*(-w2) + cosNTheta*(-h2) + h2 );
00193
00194 DPoint topRight = DPoint( cosNTheta*(w2) + sinNTheta*(-h2) + w2,
00195 -sinNTheta*(w2) + cosNTheta*(-h2) + h2 );
00196
00197 DPoint bottomLeft = DPoint( cosNTheta*(-w2) + sinNTheta*(h2) + w2,
00198 -sinNTheta*(-w2) + cosNTheta*(h2) + h2 );
00199
00200 DPoint
bottomRight = DPoint( cosNTheta*(w2) + sinNTheta*(h2) + w2,
00201 -sinNTheta*(w2) + cosNTheta*(h2) + h2 );
00202
00203
00204 DPoint top, bottom, left, right;
00205
if( theta < 0 )
00206 {
00207 top = topRight;
00208 bottom = bottomLeft;
00209 left =
topLeft;
00210 right =
bottomRight;
00211 }
00212
else
00213 {
00214 top =
topLeft;
00215 bottom =
bottomRight;
00216 left = bottomLeft;
00217 right = topRight;
00218 }
00219
00220
00221 DPoint trueTopLeft ( 0, 0 );
00222 DPoint trueTopRight ( rotatedImage.width()-1, 0 );
00223 DPoint trueBottomLeft ( 0, rotatedImage.height()-1 );
00224 DPoint trueBottomRight( rotatedImage.width()-1, rotatedImage.height()-1 );
00225
00226
00227 DPoint topEdgeL =
findTwoLineIntersection( left, top, trueTopLeft, trueTopRight );
00228 DPoint topEdgeR =
findTwoLineIntersection( top, right, trueTopLeft, trueTopRight );
00229
00230 DPoint bottomEdgeL =
findTwoLineIntersection( left, bottom, trueBottomLeft, trueBottomRight );
00231 DPoint bottomEdgeR =
findTwoLineIntersection( bottom, right, trueBottomLeft, trueBottomRight );
00232
00233 DPoint leftEdgeT =
findTwoLineIntersection( left, top, trueTopLeft, trueBottomLeft );
00234 DPoint leftEdgeB =
findTwoLineIntersection( left, bottom, trueTopLeft, trueBottomLeft );
00235
00236 DPoint rightEdgeT =
findTwoLineIntersection( right, top, trueTopRight, trueBottomRight );
00237 DPoint rightEdgeB =
findTwoLineIntersection( right, bottom, trueTopRight, trueBottomRight );
00238
00239
00240 DPoint center( (
int)w2, (
int)h2 );
00241 DPoint safeTopLeft =
findTwoLineIntersection( center, trueTopLeft, leftEdgeT, topEdgeL );
00242 DPoint safeTopRight =
findTwoLineIntersection( center, trueTopRight, rightEdgeT, topEdgeR );
00243 DPoint safeBottomLeft =
findTwoLineIntersection( center, trueBottomLeft, leftEdgeB, bottomEdgeL );
00244 DPoint safeBottomRight =
findTwoLineIntersection( center, trueBottomRight, rightEdgeB, bottomEdgeR );
00245
00246
00247
double minY = QMAX( safeTopLeft.
y(), safeTopRight.
y() );
00248
double maxY = QMIN( safeBottomLeft.
y(), safeBottomRight.
y() );
00249
00250
double minX = QMAX( safeTopLeft.
x(), safeBottomLeft.
x() );
00251
double maxX = QMIN( safeTopRight.
x(), safeBottomRight.
x() );
00252
00253
00254
00255
00256
int xMin = (
int) minX;
00257
int xMax = (
int) maxX;
00258
00259
int yMin = (
int) minY;
00260
int yMax = (
int) maxY;
00261
00262
if( xMin < minX ) xMin++;
00263
if( yMin < minY ) yMin++;
00264
00265
00266 QImage*
editedImage =
new QImage( xMax - xMin + 1,
00267 yMax - yMin + 1,
00268 rotatedImage.depth() );
00269
00270
00271
updateIncrement = (
int) ( 0.01 *
editedImage->width() *
editedImage->height() );
00272
newProgress = 0;
00273
00274
int x2,y2;
00275 uchar* scanLine2;
00276 QRgb* rgb2;
00277
00278 y2 = 0;
00279
for( y=yMin; y<=yMax; y++, y2++)
00280 {
00281
00282 scanLine = rotatedImage.scanLine(y);
00283 scanLine2 =
editedImage->scanLine(y2);
00284
00285 x2 = 0;
00286
for( x=xMin; x<=xMax; x++, x2++)
00287 {
00288 rgb = ((QRgb*)scanLine +x );
00289 rgb2 = ((QRgb*)scanLine2+x2);
00290 *rgb2 = *rgb;
00291
00292
00293
newProgress++;
00294
if(
newProgress >=
updateIncrement)
00295 {
00296
newProgress = 0;
00297
status->
incrementProgress();
00298 qApp->processEvents();
00299 }
00300
00301 }
00302 }
00303
00304
00305
status->
setStatus(
"" );
00306 qApp->processEvents();
00307
00308
00309
return editedImage;
00310 }