From 4d83e5ed5f314c1272b5cfa2a25ac72ed2cbcd24 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sat, 15 Jun 2024 17:29:13 +0200 Subject: [PATCH] Editor plugin: Image bugfixing --- ...fpagecontenteditorcontentstreambuilder.cpp | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Pdf4QtLibCore/sources/pdfpagecontenteditorcontentstreambuilder.cpp b/Pdf4QtLibCore/sources/pdfpagecontenteditorcontentstreambuilder.cpp index 50e26bf..c4597d7 100644 --- a/Pdf4QtLibCore/sources/pdfpagecontenteditorcontentstreambuilder.cpp +++ b/Pdf4QtLibCore/sources/pdfpagecontenteditorcontentstreambuilder.cpp @@ -21,6 +21,7 @@ #include "pdfstreamfilters.h" #include "pdfpainterutils.h" +#include #include #include @@ -711,7 +712,21 @@ void PDFPageContentEditorContentStreamBuilder::writeImage(QTextStream& stream, c QImage codedImage = image; codedImage = codedImage.convertToFormat(QImage::Format_RGB888); - QByteArray decodedStream(reinterpret_cast(image.constBits()), image.sizeInBytes()); + QByteArray decodedStream; + QBuffer buffer(&decodedStream); + if (buffer.open(QIODevice::WriteOnly)) + { + int width = codedImage.width(); + int bytesPerLine = codedImage.bytesPerLine(); + + for (int scanLineIndex = 0; scanLineIndex < codedImage.height(); ++scanLineIndex) + { + const uchar* scanline = codedImage.constScanLine(scanLineIndex); + buffer.write((const char*)scanline, qMin(3 * width, bytesPerLine)); + } + + buffer.close(); + } // Compress the content stream QByteArray compressedData = PDFFlateDecodeFilter::compress(decodedStream); @@ -818,7 +833,11 @@ void PDFPageContentEditorContentStreamBuilder::writeImage(const QImage& image, QTextStream stream(&m_outputContent, QDataStream::WriteOnly | QDataStream::Append); stream << "q" << Qt::endl; - QTransform imageTransform(rectangle.width(), 0, 0, -rectangle.height(), rectangle.left(), rectangle.bottom()); + QSizeF rectangleSize = QSizeF(image.size()).scaled(rectangle.size(), Qt::KeepAspectRatio); + QRectF transformedRectangle(QPointF(), rectangleSize); + transformedRectangle.moveCenter(rectangle.center()); + + QTransform imageTransform(transformedRectangle.width(), 0, 0, transformedRectangle.height(), transformedRectangle.left(), transformedRectangle.top()); PDFReal m11 = imageTransform.m11(); PDFReal m12 = imageTransform.m12();