105{
106
107 int dx = p2.x() - p1.x();
108 int dy = p2.y() - p1.y();
109
110
111 int delta = 0;
112
113
114 double recip_r = 1.0 / sqrt( (double) (dx*dx + dy*dy) );
115
116
117 if( QABS(dx) > QABS(dy) )
118 {
119 delta = dy;
120 if(dx > 0) delta = -delta;
121 }
122
123 else
124 {
125 delta = dx;
126 if(dy < 0) delta = -delta;
127 }
128
129 double sinTheta = (delta * recip_r);
130 double theta = asin( sinTheta );
131 double cosTheta = cos( theta );
132
133
134 if( theta == 0 )
135 return NULL;
136
137
138 QImage originalImage( filename );
139
140
141 if( originalImage.depth() < 32 ) { originalImage = originalImage.convertDepth( 32, Qt::AutoColor ); }
142
143 QImage rotatedImage( originalImage.width(), originalImage.height(), originalImage.depth() );
144
145
146 QString statusMessage = qApp->translate( "correctImageTilt", "Correcting Tilt:" );
148 qApp->processEvents();
149
150
151 int updateIncrement = (int) ( 0.01 * originalImage.width() * originalImage.height() );
153
154
155 double xp, yp;
156
157 double w2 = 0.5 * rotatedImage.width();
158 double h2 = 0.5 * rotatedImage.height();
159
160 int x,y;
161 uchar* scanLine;
162 QRgb* rgb;
163 for( y=0; y<rotatedImage.height(); y++)
164 {
165
166 scanLine = rotatedImage.scanLine(y);
167 for( x=0; x<rotatedImage.width(); x++)
168 {
169
170 xp = cosTheta*(x-w2) + sinTheta*(y-h2) + w2;
171 yp = -sinTheta*(x-w2) + cosTheta*(y-h2) + h2;
172
173
174 rgb = ((QRgb*)scanLine+x);
176
177
180 {
183 qApp->processEvents();
184 }
185
186 }
187 }
188
189
190 double nTheta = -theta;
191 double sinNTheta = sin( nTheta );
192 double cosNTheta = cos( nTheta );
193
195 -sinNTheta*(-w2) + cosNTheta*(-h2) + h2 );
196
197 DPoint topRight =
DPoint( cosNTheta*(w2) + sinNTheta*(-h2) + w2,
198 -sinNTheta*(w2) + cosNTheta*(-h2) + h2 );
199
200 DPoint bottomLeft =
DPoint( cosNTheta*(-w2) + sinNTheta*(h2) + w2,
201 -sinNTheta*(-w2) + cosNTheta*(h2) + h2 );
202
204 -sinNTheta*(w2) + cosNTheta*(h2) + h2 );
205
206
207 DPoint top, bottom, left, right;
208 if( theta < 0 )
209 {
210 top = topRight;
211 bottom = bottomLeft;
214 }
215 else
216 {
219 left = bottomLeft;
220 right = topRight;
221 }
222
223
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 );
228
229
232
235
238
241
242
243 DPoint center( (
int)w2, (
int)h2 );
248
249
250 double minY =
MAX( safeTopLeft.
y(), safeTopRight.
y() );
251 double maxY =
MIN( safeBottomLeft.
y(), safeBottomRight.
y() );
252
253 double minX =
MAX( safeTopLeft.
x(), safeBottomLeft.
x() );
254 double maxX =
MIN( safeTopRight.
x(), safeBottomRight.
x() );
255
256
257
258
259 int xMin = (int) minX;
260 int xMax = (int) maxX;
261
262 int yMin = (int) minY;
263 int yMax = (int) maxY;
264
265 if( xMin < minX ) xMin++;
266 if( yMin < minY ) yMin++;
267
268
270 yMax - yMin + 1,
271 rotatedImage.depth() );
272
273
276
277 int x2,y2;
278 uchar* scanLine2;
279 QRgb* rgb2;
280
281 y2 = 0;
282 for( y=yMin; y<=yMax; y++, y2++)
283 {
284
285 scanLine = rotatedImage.scanLine(y);
287
288 x2 = 0;
289 for( x=xMin; x<=xMax; x++, x2++)
290 {
291 rgb = ((QRgb*)scanLine +x );
292 rgb2 = ((QRgb*)scanLine2+x2);
293 *rgb2 = *rgb;
294
295
298 {
301 qApp->processEvents();
302 }
303
304 }
305 }
306
307
309 qApp->processEvents();
310
311
313}
QRgb interpolatedPixelValue(double xp, double yp, QImage *image)
DPoint findTwoLineIntersection(DPoint p1, DPoint p2, DPoint p3, DPoint p4)