From 2af3863b5716b290c852e2dae93ca023cb6bf0f4 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Tue, 4 Aug 2015 15:36:18 +0300 Subject: [PATCH] [Qt] Use QImage for decompressing images Needs to explicitly define the target platform, otherwise it will use linux/osx. --- platform/qt/src/image.cpp | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 platform/qt/src/image.cpp diff --git a/platform/qt/src/image.cpp b/platform/qt/src/image.cpp new file mode 100644 index 000000000..60ea84e50 --- /dev/null +++ b/platform/qt/src/image.cpp @@ -0,0 +1,45 @@ +#include + +#include +#include +#include + +namespace mbgl { + +std::string encodePNG(const PremultipliedImage& pre) { + QImage image(pre.data.get(), pre.width, pre.height, + QImage::Format_ARGB32_Premultiplied); + + QByteArray array; + QBuffer buffer(&array); + + buffer.open(QIODevice::WriteOnly); + image.rgbSwapped().save(&buffer, "PNG"); + + return std::string(array.constData(), array.size()); +} + + +PremultipliedImage decodeImage(const std::string& data) { + QImage image = + QImage::fromData(reinterpret_cast(data.data()), data.size()) + .rgbSwapped() + .convertToFormat(QImage::Format_ARGB32_Premultiplied); + + if (image.isNull()) { + throw std::runtime_error("Unsupported image type"); + } + + PremultipliedImage pre; + pre.width = image.width(); + pre.height = image.height(); + + auto img = std::make_unique(image.byteCount()); + memcpy(img.get(), image.constBits(), image.byteCount()); + + pre.data = std::move(img); + + return pre; +} + +}