Skip to content

Commit 1ba1d82

Browse files
core/colorquant: add imageRect option for cropping image
1 parent 49646e4 commit 1ba1d82

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/core/colorquantizer.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <qnumeric.h>
1414
#include <qobject.h>
1515
#include <qqmllist.h>
16+
#include <qrect.h>
1617
#include <qrgb.h>
1718
#include <qthreadpool.h>
1819
#include <qtmetamacros.h>
@@ -24,9 +25,15 @@ namespace {
2425
QS_LOGGING_CATEGORY(logColorQuantizer, "quickshell.colorquantizer", QtWarningMsg);
2526
}
2627

27-
ColorQuantizerOperation::ColorQuantizerOperation(QUrl* source, qreal depth, qreal rescaleSize)
28+
ColorQuantizerOperation::ColorQuantizerOperation(
29+
QUrl* source,
30+
qreal depth,
31+
QRect imageRect,
32+
qreal rescaleSize
33+
)
2834
: source(source)
2935
, maxDepth(depth)
36+
, imageRect(imageRect)
3037
, rescaleSize(rescaleSize) {
3138
setAutoDelete(false);
3239
}
@@ -37,6 +44,11 @@ void ColorQuantizerOperation::quantizeImage(const QAtomicInteger<bool>& shouldCa
3744
colors.clear();
3845

3946
auto image = QImage(source->toLocalFile());
47+
48+
if (imageRect.isValid()) {
49+
image = image.copy(imageRect);
50+
}
51+
4052
if ((image.width() > rescaleSize || image.height() > rescaleSize) && rescaleSize > 0) {
4153
image = image.scaled(
4254
static_cast<int>(rescaleSize),
@@ -200,6 +212,15 @@ void ColorQuantizer::setDepth(qreal depth) {
200212
}
201213
}
202214

215+
void ColorQuantizer::setImageRect(QRect imageRect) {
216+
if (mImageRect != imageRect) {
217+
mImageRect = imageRect;
218+
emit this->imageRectChanged();
219+
220+
if (this->componentCompleted) quantizeAsync();
221+
}
222+
}
223+
203224
void ColorQuantizer::setRescaleSize(int rescaleSize) {
204225
if (mRescaleSize != rescaleSize) {
205226
mRescaleSize = rescaleSize;
@@ -219,7 +240,7 @@ void ColorQuantizer::quantizeAsync() {
219240
if (this->liveOperation) this->cancelAsync();
220241

221242
qCDebug(logColorQuantizer) << "Starting color quantization asynchronously";
222-
this->liveOperation = new ColorQuantizerOperation(&mSource, mDepth, mRescaleSize);
243+
this->liveOperation = new ColorQuantizerOperation(&mSource, mDepth, mImageRect, mRescaleSize);
223244

224245
QObject::connect(
225246
this->liveOperation,

src/core/colorquantizer.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <qproperty.h>
66
#include <qqmlintegration.h>
77
#include <qqmlparserstatus.h>
8+
#include <qrect.h>
89
#include <qrunnable.h>
910
#include <qtmetamacros.h>
1011
#include <qtypes.h>
@@ -16,7 +17,7 @@ class ColorQuantizerOperation
1617
Q_OBJECT;
1718

1819
public:
19-
explicit ColorQuantizerOperation(QUrl* source, qreal depth, qreal rescaleSize);
20+
explicit ColorQuantizerOperation(QUrl* source, qreal depth, QRect imageRect, qreal rescaleSize);
2021

2122
void run() override;
2223
void tryCancel();
@@ -44,6 +45,7 @@ private slots:
4445
QList<QColor> colors;
4546
QUrl* source;
4647
qreal maxDepth;
48+
QRect imageRect;
4749
qreal rescaleSize;
4850
};
4951

@@ -78,6 +80,9 @@ class ColorQuantizer
7880
/// binary split of the color space
7981
Q_PROPERTY(qreal depth READ depth WRITE setDepth NOTIFY depthChanged);
8082

83+
/// Rectangle that the source image is cropped to.
84+
Q_PROPERTY(QRect imageRect READ imageRect WRITE setImageRect NOTIFY imageRectChanged);
85+
8186
/// The size to rescale the image to, when rescaleSize is 0 then no scaling will be done.
8287
/// > [!NOTE] Results from color quantization doesn't suffer much when rescaling, it's
8388
/// > reccommended to rescale, otherwise the quantization process will take much longer.
@@ -97,13 +102,17 @@ class ColorQuantizer
97102
[[nodiscard]] qreal depth() const { return mDepth; }
98103
void setDepth(qreal depth);
99104

105+
[[nodiscard]] QRect imageRect() const { return mImageRect; }
106+
void setImageRect(QRect imageRect);
107+
100108
[[nodiscard]] qreal rescaleSize() const { return mRescaleSize; }
101109
void setRescaleSize(int rescaleSize);
102110

103111
signals:
104112
void colorsChanged();
105113
void sourceChanged();
106114
void depthChanged();
115+
void imageRectChanged();
107116
void rescaleSizeChanged();
108117

109118
public slots:
@@ -117,6 +126,7 @@ public slots:
117126
ColorQuantizerOperation* liveOperation = nullptr;
118127
QUrl mSource;
119128
qreal mDepth = 0;
129+
QRect mImageRect = QRect();
120130
qreal mRescaleSize = 0;
121131

122132
Q_OBJECT_BINDABLE_PROPERTY(

0 commit comments

Comments
 (0)