forked from linuxdeepin/dde-services
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageeffectprocessor.cpp
More file actions
135 lines (109 loc) · 4.23 KB
/
Copy pathimageeffectprocessor.cpp
File metadata and controls
135 lines (109 loc) · 4.23 KB
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
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "imageeffectprocessor.h"
#include <QDebug>
#include <QPainter>
#include <QImageReader>
#include <QColor>
#include <QImage>
#include <DGuiApplicationHelper>
// pixmix algorithm parameters
#define PIXMIX_MATRIX 16 // Image sample size
#define PIXMIX_OPACITY 90 // Color opacity
#define PIXMIX_SATURATION 50 // Saturation
#define PIXMIX_BRIGHTNESS -60 // Brightness
#define PIXMIX_MAX_DIM 7680 // 8K UHD long side cap
DGUI_USE_NAMESPACE
ImageEffectProcessor::ImageEffectProcessor(QObject *parent)
: QObject(parent)
{
}
ImageEffectProcessor::~ImageEffectProcessor()
{
}
QImage ImageEffectProcessor::applyEffect(const QString &imagePath, EffectType effectType)
{
switch (effectType) {
case PixmixEffect:
return processPixmixEffect(imagePath);
default:
qWarning() << "Unsupported effect type:" << effectType;
return QImage();
}
}
QImage ImageEffectProcessor::applyPixmixEffect(const QString &imagePath)
{
return processPixmixEffect(imagePath);
}
bool ImageEffectProcessor::isSupportedEffect(EffectType effectType)
{
switch (effectType) {
case PixmixEffect:
return true;
default:
return false;
}
}
ImageEffectProcessor::EffectType ImageEffectProcessor::effectTypeFromString(const QString &effectName)
{
if (effectName.isEmpty() || effectName == "pixmix") {
return PixmixEffect;
}
qWarning() << "Unknown effect name:" << effectName << ", fallback to PixmixEffect";
return PixmixEffect;
}
QImage ImageEffectProcessor::processPixmixEffect(const QString &imagePath)
{
QImageReader reader(imagePath);
if (!reader.canRead()) {
qWarning() << "Cannot read image:" << imagePath;
return QImage();
}
QSize originalSize = reader.size();
if (!originalSize.isEmpty()
&& (originalSize.width() > PIXMIX_MAX_DIM || originalSize.height() > PIXMIX_MAX_DIM)) {
QSize decodeSize = originalSize;
decodeSize.scale(PIXMIX_MAX_DIM, PIXMIX_MAX_DIM, Qt::KeepAspectRatio);
reader.setScaledSize(decodeSize);
qDebug() << "Decoding image at" << decodeSize
<< "(original:" << originalSize << ")";
}
QImage originalImage = reader.read();
if (originalImage.isNull()) {
qWarning() << "Failed to load image:" << imagePath << reader.errorString();
return QImage();
}
QColor averageColor = calculateAverageColor(originalImage, PIXMIX_MATRIX);
QColor adjustedColor = DGuiApplicationHelper::adjustColor(averageColor,
0, // Hue shift
PIXMIX_SATURATION, // Saturation
PIXMIX_BRIGHTNESS, // Brightness
0, 0, 0, 0);
QImage resultImage = originalImage.convertToFormat(QImage::Format_ARGB32_Premultiplied);
QPainter painter(&resultImage);
painter.setRenderHints(painter.renderHints() | QPainter::SmoothPixmapTransform);
adjustedColor.setAlpha(int(PIXMIX_OPACITY * 1.0 / 100 * 255));
painter.fillRect(resultImage.rect(), adjustedColor);
painter.end();
qDebug() << "Applied pixmix effect - original color:" << averageColor
<< "adjusted color:" << adjustedColor
<< "opacity:" << PIXMIX_OPACITY << "%";
return resultImage;
}
QColor ImageEffectProcessor::calculateAverageColor(const QImage &image, int sampleSize)
{
QImage sampledImage = image.scaled(sampleSize, sampleSize,
Qt::IgnoreAspectRatio, Qt::FastTransformation);
int totalR = 0, totalG = 0, totalB = 0;
for (int i = 0; i < sampleSize; ++i) {
for (int j = 0; j < sampleSize; ++j) {
QRgb rgb = sampledImage.pixel(i, j);
totalR += qRed(rgb);
totalG += qGreen(rgb);
totalB += qBlue(rgb);
}
}
int pixelCount = sampleSize * sampleSize;
return QColor(totalR / pixelCount, totalG / pixelCount, totalB / pixelCount);
}