dsvg imageformats plugin sans dtk dependency

This commit is contained in:
2023-03-02 23:26:09 +08:00
parent 383edf5627
commit fbe90c3485
13 changed files with 714 additions and 21 deletions

View File

@ -0,0 +1,17 @@
set (plugin pimg_sai)
set (PLUGIN_SOURCES
sai_p.h
sai.cpp
)
# {{{ KCM style
set(CMAKE_SHARED_MODULE_PREFIX "")
unset(CMAKE_LIBRARY_OUTPUT_DIRECTORY)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
# }}}
add_library(${plugin} MODULE ${PLUGIN_SOURCES})
set_property(TARGET ${plugin} APPEND PROPERTY AUTOGEN_TARGET_DEPENDS "sai.json")
set_target_properties(${plugin} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/imageformats")
target_link_libraries(${plugin} Qt5::Gui sai)

96
imageformats/sai/sai.cpp Normal file
View File

@ -0,0 +1,96 @@
#include "sai_p.h"
#include "sai.hpp"
#include <QFile>
#include <QDebug>
#include <QImage>
SAIHandler::SAIHandler()
{
}
bool SAIHandler::canRead() const
{
if (canRead(device())) {
setFormat("sai");
return true;
}
return false;
}
bool SAIHandler::read(QImage *image)
{
QFile * file = qobject_cast<QFile*>(device());
if (!file) {
return false;
}
sai::Document saiDoc(file->fileName().toUtf8().data());
if (!saiDoc.IsOpen()) {
qDebug() << "isOpen false";
return false;
}
std::uint32_t width, height;
std::unique_ptr<std::uint8_t[]> PixelData;
std::tie(PixelData, width, height) = saiDoc.GetThumbnail();
// std::uint32_t aw, ah;
// std::tie(aw, ah) = saiDoc.GetCanvasSize();
// qDebug() << width << height << aw << ah;
if( PixelData == nullptr || !width || !height ) {
return false;
}
const unsigned char * bytes = PixelData.get();
QImage img(bytes, static_cast<int>(width), static_cast<int>(height), QImage::Format_RGB32);
*image = img;
return true;
}
bool SAIHandler::write(const QImage &image)
{
Q_UNUSED(image)
return false;
}
bool SAIHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("SAIHandler::canRead() called with no device");
return false;
}
// FIXME: check
return true;
}
QImageIOPlugin::Capabilities SAIPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "sai") {
return Capabilities(CanRead);
}
if (!format.isEmpty()) {
return {};
}
if (!device->isOpen()) {
return {};
}
Capabilities cap;
if (device->isReadable() && SAIHandler::canRead(device)) {
cap |= CanRead;
}
return cap;
}
QImageIOHandler *SAIPlugin::create(QIODevice *device, const QByteArray &format) const
{
QImageIOHandler *handler = new SAIHandler;
handler->setDevice(device);
handler->setFormat(format);
return handler;
}

View File

@ -0,0 +1,4 @@
{
"Keys": [ "sai" ],
"MimeTypes": []
}

25
imageformats/sai/sai_p.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include <QImageIOPlugin>
class SAIHandler : public QImageIOHandler
{
public:
SAIHandler();
bool canRead() const override;
bool read(QImage *image) override;
bool write(const QImage &image) override;
static bool canRead(QIODevice *device);
};
class SAIPlugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "sai.json")
public:
Capabilities capabilities(QIODevice *device, const QByteArray &format) const override;
QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const override;
};