Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[raster] Fix pixelated rendering of raster on high DPI screens
  • Loading branch information
nirvn committed May 17, 2023
1 parent 74849da commit a8fbea9
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/core/raster/qgsrasterdrawer.cpp
Expand Up @@ -69,7 +69,7 @@ void QgsRasterDrawer::draw( QPainter *p, QgsRasterViewPort *viewPort, const QgsM

// last pipe filter has only 1 band
const int bandNumber = 1;
mIterator->startRasterRead( bandNumber, viewPort->mWidth, viewPort->mHeight, viewPort->mDrawnExtent, feedback );
mIterator->startRasterRead( bandNumber, viewPort->mWidth * p->device()->devicePixelRatio(), viewPort->mHeight * p->device()->devicePixelRatio(), viewPort->mDrawnExtent, feedback );

//number of cols/rows in output pixels
int nCols = 0;
Expand Down Expand Up @@ -148,9 +148,11 @@ void QgsRasterDrawer::drawImage( QPainter *p, QgsRasterViewPort *viewPort, const
return;
}

//top left position in device coords
const QPoint tlPoint = QPoint( std::floor( viewPort->mTopLeftPoint.x() + topLeftCol / mDpiScaleFactor ), std::floor( viewPort->mTopLeftPoint.y() + topLeftRow / mDpiScaleFactor ) );
const double devicePixelRatio = p->device()->devicePixelRatio();

//top left position in device coords
const QPoint tlPoint = QPoint( std::floor( viewPort->mTopLeftPoint.x() + topLeftCol / mDpiScaleFactor / devicePixelRatio ),
std::floor( viewPort->mTopLeftPoint.y() + topLeftRow / mDpiScaleFactor / devicePixelRatio ) );
const QgsScopedQPainterState painterState( p );
p->setRenderHint( QPainter::Antialiasing, false );

Expand All @@ -174,11 +176,14 @@ void QgsRasterDrawer::drawImage( QPainter *p, QgsRasterViewPort *viewPort, const
}
}

p->drawImage( tlPoint, mDpiScaleFactor != 1.0 ? img.scaledToHeight( std::ceil( img.height() / mDpiScaleFactor ) ) : img );
p->drawImage( QRect( tlPoint.x(), tlPoint.y(),
std::ceil( img.width() / mDpiScaleFactor / devicePixelRatio ),
std::ceil( img.height() / mDpiScaleFactor / devicePixelRatio ) ),
img );

#if 0
// For debugging:
QRectF br = QRectF( tlPoint, img.size() );
QRectF br = QRectF( tlPoint, img.size() / mDpiScaleFactor / devicePixelRatio );
QPointF c = br.center();
double rad = std::max( br.width(), br.height() ) / 10;
p->drawRoundedRect( br, rad, rad );
Expand Down

0 comments on commit a8fbea9

Please sign in to comment.