1 /*
2     SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "rotationjob_p.h"
8 
9 #include <QTransform>
10 
11 using namespace Okular;
12 
RotationJob(const QImage & image,Rotation oldRotation,Rotation newRotation,DocumentObserver * observer)13 RotationJob::RotationJob(const QImage &image, Rotation oldRotation, Rotation newRotation, DocumentObserver *observer)
14     : ThreadWeaver::QObjectDecorator(new RotationJobInternal(image, oldRotation, newRotation))
15     , mObserver(observer)
16     , m_pd(nullptr)
17     , mRect(NormalizedRect())
18     , mIsPartialUpdate(false)
19 {
20 }
21 
setPage(PagePrivate * pd)22 void RotationJob::setPage(PagePrivate *pd)
23 {
24     m_pd = pd;
25 }
26 
setRect(const NormalizedRect & rect)27 void RotationJob::setRect(const NormalizedRect &rect)
28 {
29     mRect = rect;
30 }
31 
setIsPartialUpdate(bool partialUpdate)32 void RotationJob::setIsPartialUpdate(bool partialUpdate)
33 {
34     mIsPartialUpdate = partialUpdate;
35 }
36 
observer() const37 DocumentObserver *RotationJob::observer() const
38 {
39     return mObserver;
40 }
41 
page() const42 PagePrivate *RotationJob::page() const
43 {
44     return m_pd;
45 }
46 
rect() const47 NormalizedRect RotationJob::rect() const
48 {
49     return mRect;
50 }
51 
isPartialUpdate() const52 bool RotationJob::isPartialUpdate() const
53 {
54     return mIsPartialUpdate;
55 }
56 
rotationMatrix(Rotation from,Rotation to)57 QTransform RotationJob::rotationMatrix(Rotation from, Rotation to)
58 {
59     QTransform matrix;
60 
61     if (from == Rotation0) {
62         if (to == Rotation90)
63             matrix.rotate(90);
64         else if (to == Rotation180)
65             matrix.rotate(180);
66         else if (to == Rotation270)
67             matrix.rotate(270);
68     } else if (from == Rotation90) {
69         if (to == Rotation180)
70             matrix.rotate(90);
71         else if (to == Rotation270)
72             matrix.rotate(180);
73         else if (to == Rotation0)
74             matrix.rotate(270);
75     } else if (from == Rotation180) {
76         if (to == Rotation270)
77             matrix.rotate(90);
78         else if (to == Rotation0)
79             matrix.rotate(180);
80         else if (to == Rotation90)
81             matrix.rotate(270);
82     } else if (from == Rotation270) {
83         if (to == Rotation0)
84             matrix.rotate(90);
85         else if (to == Rotation90)
86             matrix.rotate(180);
87         else if (to == Rotation180)
88             matrix.rotate(270);
89     }
90 
91     return matrix;
92 }
93 
RotationJobInternal(const QImage & image,Rotation oldRotation,Rotation newRotation)94 RotationJobInternal::RotationJobInternal(const QImage &image, Rotation oldRotation, Rotation newRotation)
95     : mImage(image)
96     , mOldRotation(oldRotation)
97     , mNewRotation(newRotation)
98 {
99 }
100 
image() const101 QImage RotationJobInternal::image() const
102 {
103     return mRotatedImage;
104 }
105 
rotation() const106 Rotation RotationJobInternal::rotation() const
107 {
108     return mNewRotation;
109 }
110 
run(ThreadWeaver::JobPointer self,ThreadWeaver::Thread * thread)111 void RotationJobInternal::run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread)
112 {
113     Q_UNUSED(self);
114     Q_UNUSED(thread);
115 
116     if (mOldRotation == mNewRotation) {
117         mRotatedImage = mImage;
118         return;
119     }
120 
121     const QTransform matrix = RotationJob::rotationMatrix(mOldRotation, mNewRotation);
122 
123     mRotatedImage = mImage.transformed(matrix);
124 }
125 
126 #include "moc_rotationjob_p.cpp"
127