1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/* ============================================================
*
* This file is a part of digiKam project
* http://www.digikam.org
*
* Date : 2005-07-18
* Description : Free rotation threaded image filter.
*
* Copyright (C) 2004-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation;
* either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* ============================================================ */
// Degrees to radian convertion coeff (PI/180). To optimize computation.
#define DEG2RAD 0.017453292519943
// C++ includes.
#include <cmath>
#include <cstdlib>
// Local includes.
#include "dimg.h"
#include "dimgimagefilters.h"
#include "freerotation.h"
namespace DigikamFreeRotationImagesPlugin
{
FreeRotation::FreeRotation(Digikam::DImg *orgImage, TQObject *parent, double angle, bool antialiasing,
int autoCrop, TQColor backgroundColor, int orgW, int orgH)
: Digikam::DImgThreadedFilter(orgImage, parent, "FreeRotation")
{
m_angle = angle;
m_orgW = orgW;
m_orgH = orgH;
m_antiAlias = antialiasing;
m_autoCrop = autoCrop;
m_backgroundColor = backgroundColor;
initFilter();
}
void FreeRotation::filterImage(void)
{
int progress;
int w, h, nw, nh, j, i = 0;
int nNewHeight, nNewWidth;
int nhdx, nhdy, nhsx, nhsy;
double lfSin, lfCos, lfx, lfy;
int nWidth = m_orgImage.width();
int nHeight = m_orgImage.height();
uchar *pBits = m_orgImage.bits();
unsigned short *pBits16 = (unsigned short*)m_orgImage.bits();
// first of all, we need to calcule the sin and cos of the given angle
lfSin = sin (m_angle * -DEG2RAD);
lfCos = cos (m_angle * -DEG2RAD);
// now, we have to calc the new size for the destination image
if ((lfSin * lfCos) < 0)
{
nNewWidth = ROUND (fabs (nWidth * lfCos - nHeight * lfSin));
nNewHeight = ROUND (fabs (nWidth * lfSin - nHeight * lfCos));
}
else
{
nNewWidth = ROUND (fabs (nWidth * lfCos + nHeight * lfSin));
nNewHeight = ROUND (fabs (nWidth * lfSin + nHeight * lfCos));
}
// getting the destination's center position
nhdx = nNewWidth / 2;
nhdy = nNewHeight / 2;
// getting the source's center position
nhsx = nWidth / 2;
nhsy = nHeight / 2;
// now, we have to alloc a new image
bool sixteenBit = m_orgImage.sixteenBit();
m_destImage = Digikam::DImg(nNewWidth, nNewHeight, sixteenBit, m_orgImage.hasAlpha());
m_destImage.fill( Digikam::DColor(m_backgroundColor.rgb(), sixteenBit) );
uchar *pResBits = m_destImage.bits();
unsigned short *pResBits16 = (unsigned short *)m_destImage.bits();
Digikam::DImgImageFilters filters;
// main loop
for (h = 0; !m_cancel && (h < nNewHeight); h++)
{
nh = h - nhdy;
for (w = 0; !m_cancel && (w < nNewWidth); w++)
{
nw = w - nhdx;
i = setPosition (nNewWidth, w, h);
lfx = (double)nw * lfCos - (double)nh * lfSin + nhsx;
lfy = (double)nw * lfSin + (double)nh * lfCos + nhsy;
if (isInside (nWidth, nHeight, (int)lfx, (int)lfy))
{
if (m_antiAlias)
{
if (!sixteenBit)
filters.pixelAntiAliasing(pBits, nWidth, nHeight, lfx, lfy,
&pResBits[i+3], &pResBits[i+2],
&pResBits[i+1], &pResBits[i]);
else
filters.pixelAntiAliasing16(pBits16, nWidth, nHeight, lfx, lfy,
&pResBits16[i+3], &pResBits16[i+2],
&pResBits16[i+1], &pResBits16[i]);
}
else
{
j = setPosition (nWidth, (int)lfx, (int)lfy);
for (int p = 0 ; p < 4 ; p++)
{
if (!sixteenBit)
pResBits[i] = pBits[j];
else
pResBits16[i] = pBits16[j];
i++;
j++;
}
}
}
}
// Update the progress bar in dialog.
progress = (int)(((double)h * 100.0) / nNewHeight);
if (progress%5 == 0)
postProgress( progress );
}
// Compute the rotated destination image size using original image dimensions.
int W, H;
double absAngle = fabs(m_angle);
if (absAngle < 90.0)
{
W = (int)(m_orgW * cos(absAngle * DEG2RAD) + m_orgH * sin(absAngle * DEG2RAD));
H = (int)(m_orgH * cos(absAngle * DEG2RAD) + m_orgW * sin(absAngle * DEG2RAD));
}
else
{
H = (int)(m_orgW * cos((absAngle-90.0) * DEG2RAD) + m_orgH * sin((absAngle-90.0) * DEG2RAD));
W = (int)(m_orgH * cos((absAngle-90.0) * DEG2RAD) + m_orgW * sin((absAngle-90.0) * DEG2RAD));
}
// Auto-cropping destination image without black holes around.
TQRect autoCrop;
switch(m_autoCrop)
{
case WidestArea:
{
// 'Widest Area' method (by Renchi Raju).
autoCrop.setX( (int)(nHeight * sin(absAngle * DEG2RAD)) );
autoCrop.setY( (int)(nWidth * sin(absAngle * DEG2RAD)) );
autoCrop.setWidth( (int)(nNewWidth - 2*nHeight * sin(absAngle * DEG2RAD)) );
autoCrop.setHeight( (int)(nNewHeight - 2*nWidth * sin(absAngle * DEG2RAD)) );
if (!autoCrop.isValid())
{
m_destImage = Digikam::DImg(m_orgImage.width(), m_orgImage.height(),
m_orgImage.sixteenBit(), m_orgImage.hasAlpha());
m_destImage.fill( Digikam::DColor(m_backgroundColor.rgb(), sixteenBit) );
m_newSize = TQSize();
}
else
{
m_destImage = m_destImage.copy(autoCrop);
m_newSize.setWidth( (int)(W - 2*m_orgH * sin(absAngle * DEG2RAD)) );
m_newSize.setHeight( (int)(H - 2*m_orgW * sin(absAngle * DEG2RAD)) );
}
break;
}
case LargestArea:
{
// 'Largest Area' method (by Gerhard Kulzer).
float gamma = atan((float)nHeight / (float)nWidth);
if (absAngle < 90.0)
{
autoCrop.setWidth( (int)((float)nHeight / cos(absAngle*DEG2RAD) /
( tan(gamma) + tan(absAngle*DEG2RAD) )) );
autoCrop.setHeight( (int)((float)autoCrop.width() * tan(gamma)) );
}
else
{
autoCrop.setHeight( (int)((float)nHeight / cos((absAngle-90.0)*DEG2RAD) /
( tan(gamma) + tan((absAngle-90.0)*DEG2RAD) )) );
autoCrop.setWidth( (int)((float)autoCrop.height() * tan(gamma)) );
}
autoCrop.moveCenter( TQPoint(nNewWidth/2, nNewHeight/2));
if (!autoCrop.isValid())
{
m_destImage = Digikam::DImg(m_orgImage.width(), m_orgImage.height(),
m_orgImage.sixteenBit(), m_orgImage.hasAlpha());
m_destImage.fill( Digikam::DColor(m_backgroundColor.rgb(), sixteenBit) );
m_newSize = TQSize();
}
else
{
m_destImage = m_destImage.copy(autoCrop);
gamma = atan((float)m_orgH / (float)m_orgW);
if (absAngle < 90.0)
{
m_newSize.setWidth( (int)((float)m_orgH / cos(absAngle*DEG2RAD) /
( tan(gamma) + tan(absAngle*DEG2RAD) )) );
m_newSize.setHeight( (int)((float)m_newSize.width() * tan(gamma)) );
}
else
{
m_newSize.setHeight( (int)((float)m_orgH / cos((absAngle-90.0)*DEG2RAD) /
( tan(gamma) + tan((absAngle-90.0)*DEG2RAD) )) );
m_newSize.setWidth( (int)((float)m_newSize.height() * tan(gamma)) );
}
}
break;
}
default: // No auto croping.
{
m_newSize.setWidth( W );
m_newSize.setHeight( H );
break;
}
}
}
} // NameSpace DigikamFreeRotationImagesPlugin
|