PixelBuffer: versions to create independent copies into QImage

This commit is contained in:
Matthias Koefferlein 2022-05-04 21:16:10 +02:00
parent f51fac5f0f
commit 5fa984324e
3 changed files with 57 additions and 1 deletions

View File

@ -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
}

View File

@ -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:

View File

@ -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