Messagelib

imagescaling.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "imagescaling.h"
8#include "settings/messagecomposersettings.h"
9#include <QBuffer>
10#include <QImage>
11using namespace MessageComposer;
12
13class MessageComposer::ImageScalingPrivate
14{
15public:
16 ImageScalingPrivate() = default;
17
18 QImage mImage;
19 QBuffer mBuffer;
20 QString mName;
21 QByteArray mMimeType;
22};
23
24ImageScaling::ImageScaling()
25 : d(new MessageComposer::ImageScalingPrivate)
26{
27}
28
29ImageScaling::~ImageScaling() = default;
30
32{
33 if (!d->mImage.loadFromData(data)) {
34 return false;
35 }
36 return true;
37}
38
40{
41 if (d->mImage.isNull()) {
42 return false;
43 }
44 const int width = d->mImage.width();
45 const int height = d->mImage.height();
46 int newWidth = -1;
47 int newHeight = -1;
48 if (MessageComposer::MessageComposerSettings::self()->reduceImageToMaximum()) {
49 int maximumWidth = MessageComposer::MessageComposerSettings::self()->maximumWidth();
50 if (maximumWidth == -1) {
51 maximumWidth = MessageComposer::MessageComposerSettings::self()->customMaximumWidth();
52 }
53 int maximumHeight = MessageComposer::MessageComposerSettings::self()->maximumHeight();
54 if (maximumHeight == -1) {
55 maximumHeight = MessageComposer::MessageComposerSettings::self()->customMaximumHeight();
56 }
57 if (width > maximumWidth) {
58 newWidth = maximumWidth;
59 } else {
60 newWidth = width;
61 }
62 if (height > maximumHeight) {
63 newHeight = maximumHeight;
64 } else {
65 newHeight = height;
66 }
67 } else {
68 newHeight = height;
69 newWidth = width;
70 }
71 if (MessageComposer::MessageComposerSettings::self()->enlargeImageToMinimum()) {
72 int minimumWidth = MessageComposer::MessageComposerSettings::self()->minimumWidth();
73 if (minimumWidth == -1) {
74 minimumWidth = MessageComposer::MessageComposerSettings::self()->customMinimumWidth();
75 }
76
77 int minimumHeight = MessageComposer::MessageComposerSettings::self()->minimumHeight();
78 if (minimumHeight == -1) {
79 minimumHeight = MessageComposer::MessageComposerSettings::self()->customMinimumHeight();
80 }
81 if (newWidth < minimumWidth) {
82 newWidth = minimumWidth;
83 }
84 if (newHeight < minimumHeight) {
85 newHeight = minimumHeight;
86 }
87 }
88 if ((newHeight != height) || (newWidth != width)) {
89 d->mBuffer.open(QIODevice::WriteOnly);
90 d->mImage = d->mImage.scaled(newWidth,
91 newHeight,
92 MessageComposer::MessageComposerSettings::self()->keepImageRatio() ? Qt::KeepAspectRatio : Qt::IgnoreAspectRatio);
93
94 QByteArray format;
95 if (d->mMimeType == QByteArrayLiteral("image/jpeg")) {
96 format = QByteArrayLiteral("JPG");
97 } else if (d->mMimeType == QByteArrayLiteral("image/png")) {
98 format = QByteArrayLiteral("PNG");
99 } else {
100 format = MessageComposer::MessageComposerSettings::self()->writeFormat().toLocal8Bit();
101 if (format.isEmpty()) {
102 format = QByteArrayLiteral("PNG");
103 }
104 }
105 const bool result = d->mImage.save(&d->mBuffer, format.constData());
106 d->mBuffer.close();
107 return result;
108 } else {
109 return false;
110 }
111 return true;
112}
113
115{
116 if (d->mMimeType.isEmpty()) {
117 return {};
118 }
119 if ((d->mMimeType == QByteArrayLiteral("image/jpeg")) || (d->mMimeType == QByteArrayLiteral("image/png"))) {
120 return d->mMimeType;
121 } else {
122 // Add more mimetype if a day we add more saving format.
123 const QString type = MessageComposer::MessageComposerSettings::self()->writeFormat();
124 if (type == QLatin1StringView("JPG")) {
125 return QByteArrayLiteral("image/jpeg");
126 } else {
127 return QByteArrayLiteral("image/png");
128 }
129 }
130}
131
132void ImageScaling::setMimetype(const QByteArray &mimetype)
133{
134 d->mMimeType = mimetype;
135}
136
137void ImageScaling::setName(const QString &name)
138{
139 d->mName = name;
140}
141
143{
144 return d->mBuffer.data();
145}
146
147QString ImageScaling::generateNewName()
148{
149 if (d->mName.isEmpty()) {
150 return {};
151 }
152
153 // Don't rename it.
154 if ((d->mMimeType == QByteArrayLiteral("image/jpeg")) || (d->mMimeType == QByteArrayLiteral("image/png"))) {
155 return d->mName;
156 }
157 QString type = MessageComposer::MessageComposerSettings::self()->writeFormat();
158 if (type.isEmpty()) {
159 type = QStringLiteral("PNG");
160 }
161 if (d->mName.endsWith(QLatin1StringView(".png"))) {
162 if (type != QLatin1StringView("PNG")) {
163 d->mName.replace(QLatin1StringView(".png"), QLatin1StringView(".jpg"));
164 }
165 } else if (d->mName.endsWith(QLatin1StringView(".jpg"))) {
166 if (type != QLatin1StringView("JPG")) {
167 d->mName.replace(QLatin1StringView(".jpg"), QLatin1StringView(".png"));
168 }
169 } else {
170 if (type == QLatin1StringView("PNG")) {
171 d->mName += QLatin1StringView(".png");
172 } else {
173 d->mName += QLatin1StringView(".jpg");
174 }
175 }
176 return d->mName;
177}
QByteArray imageArray() const
imageArray
QByteArray mimetype() const
mimetype
bool loadImageFromData(const QByteArray &data)
loadImageFromData
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
const char * constData() const const
bool isEmpty() const const
KeepAspectRatio
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:53:08 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.