Files
MapLibreParticleLab/platform/qt/src/qt_image.cpp
Tadej Novak aef3d4dd74 [qt] Deprecation warnings and Qt compatibility fixes (#118)
* [qt] Fix compilation of the sqlite wrapper

* [qt] Fix QImage-related warnings

* Update Qt redirection handling

Since 5.9 RedirectPolicyAttribute is preferred, and from 6.0 the
behavior of NoLessSafeRedirectPolicy is the default.

* [qt] Fix more deprecation warnings

Co-authored-by: Mårten Nordheim <morten242@gmail.com>
2021-07-16 09:52:48 +02:00

60 lines
1.6 KiB
C++

#include <mbgl/util/image.hpp>
#include <QBuffer>
#include <QByteArray>
#include <QImage>
namespace mbgl {
std::string encodePNG(const PremultipliedImage& pre) {
QImage image(pre.data.get(), pre.size.width, pre.size.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());
}
#if !defined(QT_IMAGE_DECODERS)
PremultipliedImage decodeJPEG(const uint8_t*, size_t);
#endif
PremultipliedImage decodeImage(const std::string& string) {
const uint8_t* data = reinterpret_cast<const uint8_t*>(string.data());
const size_t size = string.size();
#if !defined(QT_IMAGE_DECODERS)
if (size >= 2) {
uint16_t magic = ((data[0] << 8) | data[1]) & 0xffff;
if (magic == 0xFFD8) {
return decodeJPEG(data, size);
}
}
#endif
QImage image =
QImage::fromData(data, size)
.rgbSwapped()
.convertToFormat(QImage::Format_ARGB32_Premultiplied);
if (image.isNull()) {
throw std::runtime_error("Unsupported image type");
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
auto img = std::make_unique<uint8_t[]>(image.sizeInBytes());
memcpy(img.get(), image.constBits(), image.sizeInBytes());
#else
auto img = std::make_unique<uint8_t[]>(image.byteCount());
memcpy(img.get(), image.constBits(), image.byteCount());
#endif
return { { static_cast<uint32_t>(image.width()), static_cast<uint32_t>(image.height()) },
std::move(img) };
}
}