1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2006-09-15
7  * Description : Exiv2 library interface.
8  *               Tools for combining rotation operations.
9  *
10  * Copyright (C) 2006-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
11  * Copyright (C) 2006-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
12  *
13  * This program is free software; you can redistribute it
14  * and/or modify it under the terms of the GNU General
15  * Public License as published by the Free Software Foundation;
16  * either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * ============================================================ */
25 
26 #ifndef META_ENGINE_ROTATION_H
27 #define META_ENGINE_ROTATION_H
28 
29 // Qt includes
30 
31 #include <QMatrix>
32 
33 // Local includes
34 
35 #include "digikam_export.h"
36 #include "metaengine.h"
37 
38 namespace Digikam
39 {
40 
41 class DIGIKAM_EXPORT MetaEngineRotation
42 {
43 
44 public:
45 
46     /**
47      * This describes single transform primitives.
48      * Note some of the defined Exif rotation flags combine
49      * two of these actions.
50      * The enum values correspond to those defined
51      * as JXFORM_CODE in the often used the JPEG tool transupp.h.
52      */
53     enum TransformationAction
54     {
55         NoTransformation = 0, ///< no transformation
56         FlipHorizontal   = 1, ///< horizontal flip
57         FlipVertical     = 2, ///< vertical flip
58         Rotate90         = 5, ///< 90-degree clockwise rotation
59         Rotate180        = 6, ///< 180-degree rotation
60         Rotate270        = 7  ///< 270-degree clockwise (or 90 ccw)
61     };
62 
63 public:
64 
65     /**
66      * Constructs the identity matrix (the matrix describing no transformation)
67      */
68     MetaEngineRotation();
69 
70     /**
71      * Returns the matrix corresponding to the given TransformationAction
72      */
73     explicit MetaEngineRotation(TransformationAction action);
74 
75     /**
76      * Returns the matrix corresponding to the given TransformationAction
77      */
78     explicit MetaEngineRotation(MetaEngine::ImageOrientation exifOrientation);
79 
80     bool operator==(const MetaEngineRotation& ma)   const;
81     bool operator!=(const MetaEngineRotation& ma)   const;
82 
83     /**
84      * Returns true of this matrix describes no transformation (is the identity matrix)
85      */
86     bool isNoTransform()                            const;
87 
88     MetaEngineRotation& operator*=(const MetaEngineRotation& ma);
89 
90     /**
91      * Applies the given transform to this matrix
92      */
93     MetaEngineRotation& operator*=(TransformationAction action);
94 
95     /**
96      * Applies the given transform actions to this matrix
97      */
98     MetaEngineRotation& operator*=(QList<TransformationAction> actions);
99 
100     /**
101      * Applies the given Exif orientation flag to this matrix
102      */
103     MetaEngineRotation& operator*=(MetaEngine::ImageOrientation exifOrientation);
104 
105     MetaEngineRotation(int m11, int m12, int m21, int m22);
106 
107     /**
108      * Returns the actions described by this matrix. The order matters.
109      * Not all possible matrices are supported, but all those that can be combined
110      * by Exif rotation flags and the transform actions above.
111      * If isNoTransform() or the matrix is not supported returns an empty list.
112      */
113     QList<TransformationAction> transformations()   const;
114 
115     /**
116      * Returns the Exif orientation flag describing this matrix.
117      * Returns ORIENTATION_UNSPECIFIED if no flag matches this matrix.
118      */
119     MetaEngine::ImageOrientation exifOrientation()  const;
120 
121     /**
122      * Returns a QMatrix representing this matrix
123      */
124     QMatrix toMatrix()                              const;
125 
126     /**
127      * Returns a QMatrix for the given Exif orientation
128      */
129     static QMatrix toMatrix(MetaEngine::ImageOrientation orientation);
130 
131 protected:
132 
133     void set(int m11, int m12, int m21, int m22);
134 
135 protected:
136 
137     int m[2][2];
138 };
139 
140 } // namespace Digikam
141 
142 #endif // META_ENGINE_ROTATION_H
143