Smooth image drawing

This commit is contained in:
Jakub Melka 2019-07-06 16:27:36 +02:00
parent 9ed17fc8ca
commit 4239b6686a
4 changed files with 35 additions and 7 deletions

View File

@ -348,7 +348,7 @@ PDFDrawWidgetProxy::PDFDrawWidgetProxy(QObject* parent) :
m_widget(nullptr),
m_horizontalScrollbar(nullptr),
m_verticalScrollbar(nullptr),
m_features(PDFRenderer::Antialiasing | PDFRenderer::TextAntialiasing)
m_features(PDFRenderer::getDefaultFeatures())
{
m_controller = new PDFDrawSpaceController(this);
connect(m_controller, &PDFDrawSpaceController::drawSpaceChanged, this, &PDFDrawWidgetProxy::update);

View File

@ -101,17 +101,46 @@ void PDFPainter::performImagePainting(const QImage& image)
m_painter->save();
// TODO: Draw smooth images
QMatrix imageTransform(1.0 / image.width(), 0, 0, 1.0 / image.height(), 0, 0);
QImage adjustedImage = image;
if (m_features.testFlag(PDFRenderer::SmoothImages))
{
// Test, if we can use smooth images. We can use them under following conditions:
// 1) Transformed rectangle is not skewed or deformed (so vectors (0, 1) and (1, 0) are orthogonal)
// 2) Image enlargement is not too big (so we doesn't run out of memory)
QMatrix matrix = m_painter->worldMatrix();
QLineF mappedWidthVector = matrix.map(QLineF(0, 0, 1, 0));
QLineF mappedHeightVector = matrix.map(QLineF(0, 0, 0, 1));
qreal angle = mappedWidthVector.angleTo(mappedHeightVector);
if (qFuzzyCompare(angle, 90.0))
{
// Image is not skewed, so we test enlargement factor
const int newWidth = mappedWidthVector.length();
const int newHeight = mappedHeightVector.length();
const int newPixels = newWidth * newHeight;
const int oldPixels = image.width() * image.height();
if (newPixels < oldPixels * 8)
{
QSize size = adjustedImage.size();
QSize adjustedImageSize = size.scaled(newWidth, newHeight, Qt::KeepAspectRatio);
adjustedImage = adjustedImage.scaled(adjustedImageSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
}
}
QMatrix imageTransform(1.0 / adjustedImage.width(), 0, 0, 1.0 / adjustedImage.height(), 0, 0);
QMatrix worldMatrix = imageTransform * m_painter->worldMatrix();
// Because Qt uses opposite axis direction than PDF, then we must transform the y-axis
// to the opposite (so the image is then unchanged)
worldMatrix.translate(0, image.height());
worldMatrix.translate(0, adjustedImage.height());
worldMatrix.scale(1, -1);
m_painter->setWorldMatrix(worldMatrix);
m_painter->drawImage(0, 0, image);
m_painter->drawImage(0, 0, adjustedImage);
m_painter->restore();
}

View File

@ -32,7 +32,6 @@ PDFRenderer::PDFRenderer(const PDFDocument* document, const PDFFontCache* fontCa
}
// TODO: Dodelat rotovani stranek
// TODO: Dodelat obrazky - SMOOTH
// TODO: Clipovani na stranku
QList<PDFRenderError> PDFRenderer::render(QPainter* painter, const QRectF& rectangle, size_t pageIndex) const

View File

@ -49,7 +49,7 @@ class PDFViewerSettings : public QObject
public:
inline explicit PDFViewerSettings(QObject* parent) :
QObject(parent),
m_features(pdf::PDFRenderer::Antialiasing | pdf::PDFRenderer::TextAntialiasing)
m_features(pdf::PDFRenderer::getDefaultFeatures())
{
}