diff --git a/src/laybasic/laybasic/layPixelBuffer.cc b/src/laybasic/laybasic/layPixelBuffer.cc index 28a831f01..ad041d1bb 100644 --- a/src/laybasic/laybasic/layPixelBuffer.cc +++ b/src/laybasic/laybasic/layPixelBuffer.cc @@ -170,6 +170,14 @@ PixelBuffer::to_image () const { return QImage ((const uchar *) data (), m_width, m_height, m_transparent ? QImage::Format_ARGB32 : QImage::Format_RGB32); } + +QImage +PixelBuffer::to_image_copy () const +{ + QImage img (m_width, m_height, m_transparent ? QImage::Format_ARGB32 : QImage::Format_RGB32); + memcpy (img.bits (), data (), img.sizeInBytes ()); + return img; +} #endif void @@ -364,6 +372,14 @@ BitmapBuffer::to_image () const img.setColor (1, 0xffffffff); return img; } + +QImage +BitmapBuffer::to_image_copy () const +{ + QImage img (m_width, m_height, QImage::Format_MonoLSB); + memcpy (img.bits (), data (), img.sizeInBytes ()); + return img; +} #endif } diff --git a/src/laybasic/laybasic/layPixelBuffer.h b/src/laybasic/laybasic/layPixelBuffer.h index 64aee0315..c78cfbf66 100644 --- a/src/laybasic/laybasic/layPixelBuffer.h +++ b/src/laybasic/laybasic/layPixelBuffer.h @@ -168,8 +168,19 @@ public: #if defined(HAVE_QT) /** * @brief Produces a QImage object from the image + * + * NOTE: this version creates a reference, i.e. the QImage is valid only + * during the lifetime of the PixelBuffer. */ QImage to_image () const; + + /** + * @brief Produces a QImage object from the image + * + * NOTE: this version creates a copy and the QImage is independent of the + * PixelBuffer. + */ + QImage to_image_copy () const; #endif /** @@ -352,9 +363,20 @@ public: #if defined(HAVE_QT) /** - * @brief Produces a QMonoImage object from the image + * @brief Produces a QImage object from the image + * + * NOTE: this version creates a reference, i.e. the QImage is valid only + * during the lifetime of the BitmapBuffer. */ QImage to_image () const; + + /** + * @brief Produces a QImage object from the image + * + * NOTE: this version creates a copy and the QImage is independent of the + * BitmapBuffer. + */ + QImage to_image_copy () const; #endif private: diff --git a/src/laybasic/unit_tests/layPixelBufferTests.cc b/src/laybasic/unit_tests/layPixelBufferTests.cc index 9e180568d..9f79bc8f0 100644 --- a/src/laybasic/unit_tests/layPixelBufferTests.cc +++ b/src/laybasic/unit_tests/layPixelBufferTests.cc @@ -237,6 +237,15 @@ TEST(2) tl::info << "PNG file read from " << au; EXPECT_EQ (compare_images (qimg, au), true); + + qimg = img.to_image_copy (); + img.fill (false); + + tmp = tmp_file ("test2.png"); + qimg.save (tl::to_qstring (tmp)); + tl::info << "PNG file written to " << tmp; + + EXPECT_EQ (compare_images (qimg, au), true); } #endif @@ -428,6 +437,15 @@ TEST(12) tl::info << "PNG file read from " << au; EXPECT_EQ (compare_images_mono (qimg.convertToFormat (QImage::Format_Mono), au), true); + + qimg = img.to_image_copy (); + img.fill (false); + + tmp = tmp_file ("test2.png"); + qimg.save (tl::to_qstring (tmp)); + tl::info << "PNG file written to " << tmp; + + EXPECT_EQ (compare_images_mono (qimg.convertToFormat (QImage::Format_Mono), au), true); } #endif