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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2006-09-15
 * Description : Exiv2 library interface.
 *               Tools for combining rotation operations.
 *
 * SPDX-FileCopyrightText: 2006-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2006-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

// Local includes

#include "metaengine_rotation.h"

// KDE includes

#include <klocalizedstring.h>

namespace Digikam
{

/**
 * If the picture is displayed according to the exif orientation tag,
 * the user will request rotating operations relative to what he sees,
 * and that is the picture rotated according to the EXIF tag.
 * So the operation requested and the given EXIF angle must be combined.
 * E.g. if orientation is "6" (rotate 90 clockwiseto show correctly)
 * and the user selects 180 clockwise, the operation is 270.
 * If the user selected 270, the operation would be None (and clearing the exif tag).
 *
 * This requires to describe the transformations in a model which
 * cares for both composing (180+90=270) and eliminating (180+180=no action),
 * as well as the non-commutative nature of the operations (vflip+90 is not 90+vflip)
 *
 * All 2D transformations can be described by a 2x3 matrix, see QWMetaEngineRotation.
 * All transformations needed here - rotate 90, 180, 270, flipV, flipH -
 * can be described in a 2x2 matrix with the values 0,1,-1
 * (because flipping is expressed by changing the sign only,
 *  and sine and cosine of 90, 180 and 270 are either 0,1 or -1).
 *
 *  x' = m11 x + m12 y
 *  y' = m21 x + m22 y
 *
 * Moreover, all combinations of these rotate/flip operations result in one of the eight
 * matrices defined below. This did not proof that mathematically, but empirically.
 */

namespace Matrix
{

static const MetaEngineRotation identity               ( 1,  0,  0,  1);
static const MetaEngineRotation rotate90               ( 0,  1, -1,  0);
static const MetaEngineRotation rotate180              (-1,  0,  0, -1);
static const MetaEngineRotation rotate270              ( 0, -1,  1,  0);
static const MetaEngineRotation flipHorizontal         (-1,  0,  0,  1);
static const MetaEngineRotation flipVertical           ( 1,  0,  0, -1);
static const MetaEngineRotation rotate90flipHorizontal ( 0,  1,  1,  0);    ///< first rotate, then flip
static const MetaEngineRotation rotate90flipVertical   ( 0, -1, -1,  0);    ///< first rotate, then flip

MetaEngineRotation matrix(MetaEngineRotation::TransformationAction action)
{
    switch (action)
    {
        case MetaEngineRotation::NoTransformation:
        {
            return identity;
        }

        case MetaEngineRotation::FlipHorizontal:
        {
            return flipHorizontal;
        }

        case MetaEngineRotation::FlipVertical:
        {
            return flipVertical;
        }

        case MetaEngineRotation::Rotate90:
        {
            return rotate90;
        }

        case MetaEngineRotation::Rotate180:
        {
            return rotate180;
        }

        case MetaEngineRotation::Rotate270:
        {
            return rotate270;
        }

        default:
        {
            break;
        }
    }

    return identity;
}

MetaEngineRotation matrix(MetaEngine::ImageOrientation exifOrientation)
{
    switch (exifOrientation)
    {
        case MetaEngine::ORIENTATION_NORMAL:
        {
            return identity;
        }

        case MetaEngine::ORIENTATION_HFLIP:
        {
            return flipHorizontal;
        }

        case MetaEngine::ORIENTATION_ROT_180:
        {
            return rotate180;
        }

        case MetaEngine::ORIENTATION_VFLIP:
        {
            return flipVertical;
        }

        case MetaEngine::ORIENTATION_ROT_90_HFLIP:
        {
            return rotate90flipHorizontal;
        }

        case MetaEngine::ORIENTATION_ROT_90:
        {
            return rotate90;
        }

        case MetaEngine::ORIENTATION_ROT_90_VFLIP:
        {
            return rotate90flipVertical;
        }

        case MetaEngine::ORIENTATION_ROT_270:
        {
            return rotate270;
        }

        case MetaEngine::ORIENTATION_UNSPECIFIED:
        {
            return identity;
        }
    }

    return identity;
}

} // namespace Matrix

MetaEngineRotation::MetaEngineRotation()
{
    set(1, 0, 0, 1);
}

MetaEngineRotation::MetaEngineRotation(TransformationAction action)
{
    *this = Matrix::matrix(action);
}

MetaEngineRotation::MetaEngineRotation(MetaEngine::ImageOrientation exifOrientation)
{
    *this = Matrix::matrix(exifOrientation);
}

MetaEngineRotation::MetaEngineRotation(int m11, int m12, int m21, int m22)
{
    set(m11, m12, m21, m22);
}

void MetaEngineRotation::set(int m11, int m12, int m21, int m22)
{
    m[0][0] = m11;
    m[0][1] = m12;
    m[1][0] = m21;
    m[1][1] = m22;
}

bool MetaEngineRotation::isNoTransform() const
{
    return (*this == Matrix::identity);
}

MetaEngineRotation& MetaEngineRotation::operator*=(const MetaEngineRotation& ma)
{
    set(
        ma.m[0][0]*m[0][0] + ma.m[0][1]*m[1][0],  ma.m[0][0]*m[0][1] + ma.m[0][1]*m[1][1],
        ma.m[1][0]*m[0][0] + ma.m[1][1]*m[1][0],  ma.m[1][0]*m[0][1] + ma.m[1][1]*m[1][1]
       );

    return *this;
}

bool MetaEngineRotation::operator==(const MetaEngineRotation& ma) const
{
    return (
            (m[0][0] == ma.m[0][0]) &&
            (m[0][1] == ma.m[0][1]) &&
            (m[1][0] == ma.m[1][0]) &&
            (m[1][1] == ma.m[1][1])
           );
}

bool MetaEngineRotation::operator!=(const MetaEngineRotation& ma) const
{
    return !(*this==ma);
}

MetaEngineRotation& MetaEngineRotation::operator*=(TransformationAction action)
{
    return (*this *= Matrix::matrix(action));
}

MetaEngineRotation& MetaEngineRotation::operator*=(const QList<TransformationAction>& actions)
{
    for (const TransformationAction& action : EXIV2_AS_CONST(actions))
    {
        *this *= Matrix::matrix(action);
    }

    return *this;
}

MetaEngineRotation& MetaEngineRotation::operator*=(MetaEngine::ImageOrientation exifOrientation)<--- Shadow argument
{
    return (*this *= Matrix::matrix(exifOrientation));
}

/**
 * Converts the mathematically correct description
 * into the primitive operations that can be carried out losslessly.
 */
QList<MetaEngineRotation::TransformationAction> MetaEngineRotation::transformations() const
{
    QList<TransformationAction> transforms;

    if      (*this == Matrix::rotate90)
    {
        transforms << Rotate90;
    }
    else if (*this == Matrix::rotate180)
    {
        transforms << Rotate180;
    }
    else if (*this == Matrix::rotate270)
    {
        transforms << Rotate270;
    }
    else if (*this == Matrix::flipHorizontal)
    {
        transforms << FlipHorizontal;
    }
    else if (*this == Matrix::flipVertical)
    {
        transforms << FlipVertical;
    }
    else if (*this == Matrix::rotate90flipHorizontal)
    {
        // first rotate, then flip!

        transforms << Rotate90;
        transforms << FlipHorizontal;
    }
    else if (*this == Matrix::rotate90flipVertical)
    {
        // first rotate, then flip!

        transforms << Rotate90;
        transforms << FlipVertical;
    }

    return transforms;
}

MetaEngine::ImageOrientation MetaEngineRotation::exifOrientation() const
{
    if (*this == Matrix::identity)
    {
        return MetaEngine::ORIENTATION_NORMAL;
    }

    if      (*this == Matrix::rotate90)
    {
        return MetaEngine::ORIENTATION_ROT_90;
    }
    else if (*this == Matrix::rotate180)
    {
        return MetaEngine::ORIENTATION_ROT_180;
    }
    else if (*this == Matrix::rotate270)
    {
        return MetaEngine::ORIENTATION_ROT_270;
    }
    else if (*this == Matrix::flipHorizontal)
    {
        return MetaEngine::ORIENTATION_HFLIP;
    }
    else if (*this == Matrix::flipVertical)
    {
        return MetaEngine::ORIENTATION_VFLIP;
    }
    else if (*this == Matrix::rotate90flipHorizontal)
    {
        return MetaEngine::ORIENTATION_ROT_90_HFLIP;
    }
    else if (*this == Matrix::rotate90flipVertical)
    {
        return MetaEngine::ORIENTATION_ROT_90_VFLIP;
    }

    return MetaEngine::ORIENTATION_UNSPECIFIED;
}

QTransform MetaEngineRotation::toTransform() const
{
    return toTransform(exifOrientation());
}

QTransform MetaEngineRotation::toTransform(MetaEngine::ImageOrientation orientation)
{
    QTransform matrix;

    switch (orientation)
    {
        case MetaEngine::ORIENTATION_NORMAL:
        case MetaEngine::ORIENTATION_UNSPECIFIED:
        {
            break;
        }

        case MetaEngine::ORIENTATION_HFLIP:
        {
            matrix.scale(-1, 1);
            break;
        }

        case MetaEngine::ORIENTATION_ROT_180:
        {
            matrix.rotate(180);
            break;
        }

        case MetaEngine::ORIENTATION_VFLIP:
        {
            matrix.scale(1, -1);
            break;
        }

        case MetaEngine::ORIENTATION_ROT_90_HFLIP:
        {
            matrix.scale(-1, 1);
            matrix.rotate(90);
            break;
        }

        case MetaEngine::ORIENTATION_ROT_90:
        {
            matrix.rotate(90);
            break;
        }

        case MetaEngine::ORIENTATION_ROT_90_VFLIP:
        {
            matrix.scale(1, -1);
            matrix.rotate(90);
            break;
        }

        case MetaEngine::ORIENTATION_ROT_270:
        {
            matrix.rotate(270);
            break;
        }
    }

    return matrix;
}

/**
 * Converts a MetaEngineRotation::TransformationAction to a QString description.
 */
QString MetaEngineRotation::transformationActionToString(TransformationAction action)
{
    switch (action)
    {
        case NoTransformation:
        {
            return i18n("No Transformation");
        }

        case FlipHorizontal:
        {
            return i18n("Flip Horizontal");
        }

        case FlipVertical:
        {
            return i18n("Flip Vertical");
        }

        case Rotate90:
        {
            return i18n("Rotate 90°");
        }

        case Rotate180:
        {
            return i18n("Rotate 180°");
        }

        case Rotate270:
        {
            return i18n("Rotate 270°");
        }

        case RotateAuto:
        {
            return i18n("Auto Rotation");
        }

        default:
        {
            return i18n("Unknown Transformation");
        }
    }
}

} // namespace Digikam