qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
exiftransform.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2020 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "exiftransform.h"
21 
22 #include <libexif/exif-loader.h>
23 
24 #include <QDebug>
25 
26 namespace ExifTransform
27 {
28  Orientation getOrientation(QByteArray imageData)
29  {
30  auto data = imageData.constData();
31  auto size = imageData.size();
32 
33  ExifData* exifData = exif_data_new_from_data(reinterpret_cast<const unsigned char*>(data), size);
34 
35  if (!exifData) {
36  return Orientation::TopLeft;
37  }
38 
39  const ExifByteOrder byteOrder = exif_data_get_byte_order(exifData);
40  const ExifEntry* const exifEntry = exif_data_get_entry(exifData, EXIF_TAG_ORIENTATION);
41 
42  if (!exifEntry) {
43  exif_data_free(exifData);
44  return Orientation::TopLeft;
45  }
46 
47  const int orientation = exif_get_short(exifEntry->data, byteOrder);
48  exif_data_free(exifData);
49 
50  switch (orientation){
51  case 1:
52  return Orientation::TopLeft;
53  case 2:
54  return Orientation::TopRight;
55  case 3:
57  case 4:
59  case 5:
60  return Orientation::LeftTop;
61  case 6:
62  return Orientation::RightTop;
63  case 7:
65  case 8:
67  default:
68  qWarning() << "Invalid exif orientation";
69  return Orientation::TopLeft;
70  }
71  }
72 
73  QImage applyTransformation(QImage image, Orientation orientation)
74  {
75  QTransform exifTransform;
76  switch (orientation) {
78  break;
80  image = image.mirrored(1, 0);
81  break;
83  exifTransform.rotate(180);
84  break;
86  image = image.mirrored(0, 1);
87  break;
89  exifTransform.rotate(-90);
90  image = image.mirrored(1, 0);
91  break;
93  exifTransform.rotate(90);
94  break;
96  exifTransform.rotate(90);
97  image = image.mirrored(1, 0);
98  break;
100  exifTransform.rotate(-90);
101  break;
102  }
103  image = image.transformed(exifTransform);
104  return image;
105  }
106 };
ExifTransform::Orientation::LeftBottom
@ LeftBottom
ExifTransform::Orientation::TopRight
@ TopRight
ExifTransform
Definition: exiftransform.cpp:26
ExifTransform::Orientation::TopLeft
@ TopLeft
ExifTransform::Orientation::RightTop
@ RightTop
ExifTransform::applyTransformation
QImage applyTransformation(QImage image, Orientation orientation)
Definition: exiftransform.cpp:73
ExifTransform::Orientation::LeftTop
@ LeftTop
ExifTransform::getOrientation
Orientation getOrientation(QByteArray imageData)
Definition: exiftransform.cpp:28
ExifTransform::Orientation
Orientation
Definition: exiftransform.h:26
exiftransform.h
ExifTransform::Orientation::RightBottom
@ RightBottom
ExifTransform::Orientation::BottomRight
@ BottomRight
ExifTransform::Orientation::BottomLeft
@ BottomLeft