diff --git a/src/document.cpp b/src/document.cpp index c43512dc..e1175b4c 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "document.h" #include "util.h" #include "snapshotxml.h" @@ -719,6 +720,10 @@ void Document::setNodeBoneMark(QUuid nodeId, BoneMark mark) void Document::updateTurnaround(const QImage &image) { turnaround = image; + turnaroundPngByteArray.clear(); + QBuffer pngBuffer(&turnaroundPngByteArray); + pngBuffer.open(QIODevice::WriteOnly); + turnaround.save(&pngBuffer, "PNG"); emit turnaroundChanged(); } diff --git a/src/documentwindow.cpp b/src/documentwindow.cpp index 4ec43de1..8ad85a65 100644 --- a/src/documentwindow.cpp +++ b/src/documentwindow.cpp @@ -1102,13 +1102,8 @@ void DocumentWindow::saveTo(const QString &saveAsFilename) if (modelXml.size() > 0) ds3Writer.add("model.xml", "model", &modelXml); - QByteArray imageByteArray; - QBuffer pngBuffer(&imageByteArray); - if (!m_document->turnaround.isNull()) { - pngBuffer.open(QIODevice::WriteOnly); - m_document->turnaround.save(&pngBuffer, "PNG"); - if (imageByteArray.size() > 0) - ds3Writer.add("canvas.png", "asset", &imageByteArray); + if (!m_document->turnaround.isNull() && m_document->turnaroundPngByteArray.size() > 0) { + ds3Writer.add("canvas.png", "asset", &m_document->turnaroundPngByteArray); } std::set imageIds; @@ -1133,15 +1128,11 @@ void DocumentWindow::saveTo(const QString &saveAsFilename) } for (const auto &imageId: imageIds) { - const QImage *image = ImageForever::get(imageId); - if (nullptr == image) + const QByteArray *pngByteArray = ImageForever::getPngByteArray(imageId); + if (nullptr == pngByteArray) continue; - QByteArray imageByteArray; - QBuffer pngBuffer(&imageByteArray); - pngBuffer.open(QIODevice::WriteOnly); - image->save(&pngBuffer, "PNG"); - if (imageByteArray.size() > 0) - ds3Writer.add("images/" + imageId.toString() + ".png", "asset", &imageByteArray); + if (pngByteArray->size() > 0) + ds3Writer.add("images/" + imageId.toString() + ".png", "asset", pngByteArray); } if (ds3Writer.save(filename)) { diff --git a/src/imageforever.cpp b/src/imageforever.cpp index d58a885f..e397104e 100644 --- a/src/imageforever.cpp +++ b/src/imageforever.cpp @@ -1,12 +1,14 @@ #include #include #include +#include #include "imageforever.h" struct ImageForeverItem { QImage *image; QUuid id; + QByteArray *imageByteArray; }; static std::map g_foreverMap; static std::map g_foreverCacheKeyToIdMap; @@ -21,6 +23,15 @@ const QImage *ImageForever::get(const QUuid &id) return findResult->second.image; } +const QByteArray *ImageForever::getPngByteArray(const QUuid &id) +{ + QMutexLocker locker(&g_mapMutex); + auto findResult = g_foreverMap.find(id); + if (findResult == g_foreverMap.end()) + return nullptr; + return findResult->second.imageByteArray; +} + QUuid ImageForever::add(const QImage *image, QUuid toId) { QMutexLocker locker(&g_mapMutex); @@ -35,7 +46,11 @@ QUuid ImageForever::add(const QImage *image, QUuid toId) if (g_foreverMap.find(newId) != g_foreverMap.end()) return newId; QImage *newImage = new QImage(*image); - g_foreverMap[newId] = {newImage, newId}; + QByteArray *imageByteArray = new QByteArray(); + QBuffer pngBuffer(imageByteArray); + pngBuffer.open(QIODevice::WriteOnly); + newImage->save(&pngBuffer, "PNG"); + g_foreverMap[newId] = {newImage, newId, imageByteArray}; g_foreverCacheKeyToIdMap[newImage->cacheKey()] = newId; return newId; } diff --git a/src/imageforever.h b/src/imageforever.h index 769f598c..e843f650 100644 --- a/src/imageforever.h +++ b/src/imageforever.h @@ -2,11 +2,13 @@ #define DUST3D_IMAGE_FOREVER_H #include #include +#include class ImageForever { public: static const QImage *get(const QUuid &id); + static const QByteArray *getPngByteArray(const QUuid &id); static QUuid add(const QImage *image, QUuid toId=QUuid()); }; diff --git a/src/skeletondocument.h b/src/skeletondocument.h index 6050a81d..bb3d1494 100644 --- a/src/skeletondocument.h +++ b/src/skeletondocument.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "bonemark.h" #include "theme.h" #include "meshloader.h" @@ -200,6 +201,7 @@ public: bool zlocked = false; bool radiusLocked = false; QImage turnaround; + QByteArray turnaroundPngByteArray; std::map partMap; std::map nodeMap; std::map edgeMap;