diff --git a/src/core/colorquantizer.cpp b/src/core/colorquantizer.cpp index b97cf676..abe6af8e 100644 --- a/src/core/colorquantizer.cpp +++ b/src/core/colorquantizer.cpp @@ -17,18 +17,14 @@ ColorQuantizerOperation::ColorQuantizerOperation(QUrl* source, qreal depth, qrea , maxDepth(depth) , rescaleSize(rescaleSize) { setAutoDelete(false); - colors = QList(); } void ColorQuantizerOperation::quantizeImage(const QAtomicInteger& shouldCancel) { - if (shouldCancel.loadAcquire()) return; + if (shouldCancel.loadAcquire() || source->isEmpty()) return; colors.clear(); - if (source->isEmpty()) return; - auto image = QImage(source->toLocalFile()); - if ((image.width() > rescaleSize || image.height() > rescaleSize) && rescaleSize > 0) { image = image.scaled( static_cast(rescaleSize), @@ -59,7 +55,7 @@ void ColorQuantizerOperation::quantizeImage(const QAtomicInteger& shouldCa auto endTime = QDateTime::currentDateTime(); auto milliseconds = startTime.msecsTo(endTime); - qDebug() << "Color Quantization took: " << milliseconds << "ms"; + qCDebug(logColorQuantizer) << "Color Quantization took: " << milliseconds << "ms"; } QList ColorQuantizerOperation::quantization( @@ -95,8 +91,8 @@ QList ColorQuantizerOperation::quantization( auto dominantChannel = findBiggestColorRange(rgbValues); std::ranges::sort(rgbValues, [dominantChannel](const auto& a, const auto& b) { - if (dominantChannel == "r") return a.red() < b.red(); - else if (dominantChannel == "g") return a.green() < b.green(); + if (dominantChannel == 'r') return a.red() < b.red(); + else if (dominantChannel == 'g') return a.green() < b.green(); return a.blue() < b.blue(); }); @@ -112,7 +108,7 @@ QList ColorQuantizerOperation::quantization( return result; } -QChar ColorQuantizerOperation::findBiggestColorRange(const QList& rgbValues) { +char ColorQuantizerOperation::findBiggestColorRange(const QList& rgbValues) { if (rgbValues.isEmpty()) return 'r'; auto rMin = 255; @@ -203,8 +199,8 @@ void ColorQuantizer::setRescaleSize(int rescaleSize) { void ColorQuantizer::operationFinished(const QList& result) { bColors = result; - emit this->colorsChanged(); this->liveOperation = nullptr; + emit this->colorsChanged(); } void ColorQuantizer::quantizeAsync() { @@ -212,12 +208,14 @@ void ColorQuantizer::quantizeAsync() { qCDebug(logColorQuantizer) << "Starting color quantization asynchronously"; this->liveOperation = new ColorQuantizerOperation(&mSource, mDepth, mRescaleSize); + QObject::connect( this->liveOperation, &ColorQuantizerOperation::done, this, &ColorQuantizer::operationFinished ); + QThreadPool::globalInstance()->start(this->liveOperation); } diff --git a/src/core/colorquantizer.hpp b/src/core/colorquantizer.hpp index 0e9461a1..13d3ae54 100644 --- a/src/core/colorquantizer.hpp +++ b/src/core/colorquantizer.hpp @@ -27,7 +27,7 @@ private slots: void finished(); private: - static QChar findBiggestColorRange(const QList& rgbValues); + static char findBiggestColorRange(const QList& rgbValues); void quantizeImage(const QAtomicInteger& shouldCancel = false); QList quantization( @@ -67,7 +67,7 @@ class ColorQuantizer /// Access the colors resulting from the color quantization performed. /// > [!NOTE] The amount of colors returned from the quantization is determined by /// > the property depth, specifically 2ⁿ where n is the depth. - Q_PROPERTY(QList colors READ default BINDABLE bindableColors); + Q_PROPERTY(QList colors READ default NOTIFY colorsChanged BINDABLE bindableColors); /// Path to the image you'd like to run the color quantization on. Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged);