[graphics/krita] libs/ui/tool: Add checks to avoid 0 sized sample rect

Emmet O'Neill <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 702a6509491ad9e3500aa61f954838bfec09cc96 by Emmet O'Neill, on behalf of Le Tien Dat.
Committed on 13/08/2026 at 23:16.
Pushed by emmetoneill into branch 'master'.

Add checks to avoid 0 sized sample rect

Avoid situations when zoom and preview scale both so large that the division become 0

M  +12   -6    libs/ui/tool/KisAsyncColorSamplerHelper.cpp

https://invent.kde.org/graphics/krita/-/commit/702a6509491ad9e3500aa61f954838bfec09cc96

diff --git a/libs/ui/tool/KisAsyncColorSamplerHelper.cpp b/libs/ui/tool/KisAsyncColorSamplerHelper.cpp
index b0e8d6acc24..a342c67e339 100644
--- a/libs/ui/tool/KisAsyncColorSamplerHelper.cpp
+++ b/libs/ui/tool/KisAsyncColorSamplerHelper.cpp
@@ -570,6 +570,7 @@ void KisAsyncColorSamplerHelper::paintCircle(QPainter &gc,
     // Draw zoom preview
     if (m_d->circleZoomPreviewScale > 1) {
         QRectF sampleDocRectF = m_d->previewDocRect;
+        // Sample a rect with size that is (zoomPreviewScale) times smaller than (previewDocRect size)
         sampleDocRectF.setSize(sampleDocRectF.size() / m_d->circleZoomPreviewScale);
         sampleDocRectF.moveCenter(m_d->previewDocRect.center());
 
@@ -586,10 +587,12 @@ void KisAsyncColorSamplerHelper::paintCircle(QPainter &gc,
 void KisAsyncColorSamplerHelper::paintCircleCanvasPreview(QPainter &gc, const QRectF &viewRectF, const QRectF &sampleDocRectF, const QPainterPath &clip) {
     gc.save();
 
-    QRectF sampleCanvasRectF = m_d->canvas->image()->documentToPixel(sampleDocRectF);
+    QRect sampleCanvasRect = m_d->canvas->image()->documentToPixel(sampleDocRectF).toRect();
+    // In case zoom and scale is both so large that sample size becomes 0
+    if (sampleCanvasRect.isEmpty()) sampleCanvasRect.setSize(QSize(1, 1));
 
-    // Copy a piece of canvas image with size = (previewDocRect size) / (zoom preview scale)
-    QImage canvasPreview = m_d->canvas->image()->convertToQImage(sampleCanvasRectF.toRect(), nullptr);
+
+    QImage canvasPreview = m_d->canvas->image()->convertToQImage(sampleCanvasRect, nullptr);
     gc.setCompositionMode(QPainter::CompositionMode_SourceOver);
 
     gc.setClipPath(clip);
@@ -631,20 +634,23 @@ void KisAsyncColorSamplerHelper::paintCircleReferenceImagePreview(QPainter &gc,
         // image.convertTo(QImage::Format_ARGB32); Do this in KisReferenceImage to avoid having to copy? and convert
 
         QPointF sampleRefPointF = refImage->documentToPixel(sampleDocRectF.center());
-        QRectF shapeRectF = refImage->documentToShape(sampleDocRectF);
+
         qreal xScale = refImage->boundingRect().width() / image.width();
         qreal yScale = refImage->boundingRect().height() / image.height();
 
-        QRectF sampleRefRectF = QRectF(sampleRefPointF, QSizeF(shapeRectF.width() / xScale, shapeRectF.height() / yScale));
+        QRectF sampleRefRectF = QRectF(sampleRefPointF, QSizeF(sampleDocRectF.width() / xScale, sampleDocRectF.height() / yScale));
         sampleRefRectF.moveCenter(sampleRefPointF);
 
+        QRect sampleRefRect = sampleRefRectF.toRect();
+        if (sampleRefRect.isEmpty()) sampleRefRect.setSize(QSize(1, 1));
+
         // Rotate the painter, draw, rotate back is seemingly easier than
         // trying to rotate the image and the shenanighens that follow
         gc.translate(viewRectF.center());
         gc.rotate(refImage->rotation());
         gc.translate(-viewRectF.center());
 
-        gc.drawImage(viewRectF, image, sampleRefRectF.toRect());
+        gc.drawImage(viewRectF, image, sampleRefRect);
 
         gc.restore();
     }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.