Skip to content

Commit afa5298

Browse files
core/colorquant: add imageRect option for cropping image
1 parent 9662234 commit afa5298

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/core/colorquantizer.cpp

Lines changed: 29 additions & 3 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
this->setAutoDelete(false);
3239
}
@@ -37,6 +44,11 @@ void ColorQuantizerOperation::quantizeImage(const QAtomicInteger<bool>& shouldCa
3744
this->colors.clear();
3845

3946
auto image = QImage(this->source->toLocalFile());
47+
48+
if (this->imageRect.isValid()) {
49+
image = image.copy(this->imageRect);
50+
}
51+
4052
if ((image.width() > this->rescaleSize || image.height() > this->rescaleSize)
4153
&& this->rescaleSize > 0)
4254
{
@@ -202,6 +214,15 @@ void ColorQuantizer::setDepth(qreal depth) {
202214
}
203215
}
204216

217+
void ColorQuantizer::setImageRect(QRect imageRect) {
218+
if (this->mImageRect != imageRect) {
219+
this->mImageRect = imageRect;
220+
emit this->imageRectChanged();
221+
222+
if (this->componentCompleted) this->quantizeAsync();
223+
}
224+
}
225+
205226
void ColorQuantizer::setRescaleSize(int rescaleSize) {
206227
if (this->mRescaleSize != rescaleSize) {
207228
this->mRescaleSize = rescaleSize;
@@ -221,8 +242,13 @@ void ColorQuantizer::quantizeAsync() {
221242
if (this->liveOperation) this->cancelAsync();
222243

223244
qCDebug(logColorQuantizer) << "Starting color quantization asynchronously";
224-
this->liveOperation =
225-
new ColorQuantizerOperation(&this->mSource, this->mDepth, this->mRescaleSize);
245+
246+
this->liveOperation = new ColorQuantizerOperation(
247+
&this->mSource,
248+
this->mDepth,
249+
this->mImageRect,
250+
this->mRescaleSize
251+
);
226252

227253
QObject::connect(
228254
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 this->mDepth; }
98103
void setDepth(qreal depth);
99104

105+
[[nodiscard]] QRect imageRect() const { return this->mImageRect; }
106+
void setImageRect(QRect imageRect);
107+
100108
[[nodiscard]] qreal rescaleSize() const { return this->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;
120130
qreal mRescaleSize = 0;
121131

122132
Q_OBJECT_BINDABLE_PROPERTY(

0 commit comments

Comments
 (0)