Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Implement saving vector masks to PSD
Wolthera van Hövell tot Westerflier59f97469f753
diff --git a/libs/psd/CMakeLists.txt b/libs/psd/CMakeLists.txtindex 9836976e2d..319119ddab 100644--- a/libs/psd/CMakeLists.txt+++ b/libs/psd/CMakeLists.txt@@ -15,6 +15,7 @@ target_link_libraries(kritapsdPUBLICkritaimagekritapsdutils+ kritaui)set_target_properties(kritapsd PROPERTIESdiff --git a/libs/psd/psd_additional_layer_info_block.cpp b/libs/psd/psd_additional_layer_info_block.cppindex 353585a976..5788d79e3b 100644--- a/libs/psd/psd_additional_layer_info_block.cpp+++ b/libs/psd/psd_additional_layer_info_block.cpp@@ -241,7 +241,7 @@ void PsdAdditionalLayerInfoBlock::readImpl(QIODevice &io)// read the flags.vectorMask.invert = flags & 1? true: false;vectorMask.notLink = flags & 2? true: false;- vectorMask.disable = flags & 3? true: false;+ vectorMask.disable = flags & 4? true: false;quint64 currentPos = 8;psd_path_sub_path currentPath;@@ -257,12 +257,12 @@ void PsdAdditionalLayerInfoBlock::readImpl(QIODevice &io)} else if (recordType == 7) {QRectF bounds;// unsure if there can be multiple clipboard records...- bounds.setTop(psdreadFixedFloatingPoint<byteOrder>(io));- bounds.setLeft(psdreadFixedFloatingPoint<byteOrder>(io));- bounds.setBottom(psdreadFixedFloatingPoint<byteOrder>(io));- bounds.setRight(psdreadFixedFloatingPoint<byteOrder>(io));+ bounds.setTop(psdreadFixedPoint<byteOrder>(io));+ bounds.setLeft(psdreadFixedPoint<byteOrder>(io));+ bounds.setBottom(psdreadFixedPoint<byteOrder>(io));+ bounds.setRight(psdreadFixedPoint<byteOrder>(io));vectorMask.path.clipBoardBounds = bounds;- vectorMask.path.clipBoardResolution = psdreadFixedFloatingPoint<byteOrder>(io);+ vectorMask.path.clipBoardResolution = psdreadFixedPoint<byteOrder>(io);dbgFile << "\trecord" << recordType << "top" << bounds << "res" << vectorMask.path.clipBoardResolution;io.skip(4);} else if (recordType == 0 || recordType == 3) {@@ -284,12 +284,12 @@ void PsdAdditionalLayerInfoBlock::readImpl(QIODevice &io)io.skip(22);} else {psd_path_node node;- node.control1.setY(psdreadFixedFloatingPoint<byteOrder>(io));- node.control1.setX(psdreadFixedFloatingPoint<byteOrder>(io));- node.node.setY(psdreadFixedFloatingPoint<byteOrder>(io));- node.node.setX(psdreadFixedFloatingPoint<byteOrder>(io));- node.control2.setY(psdreadFixedFloatingPoint<byteOrder>(io));- node.control2.setX(psdreadFixedFloatingPoint<byteOrder>(io));+ node.control1.setY(psdreadFixedPoint<byteOrder>(io));+ node.control1.setX(psdreadFixedPoint<byteOrder>(io));+ node.node.setY(psdreadFixedPoint<byteOrder>(io));+ node.node.setX(psdreadFixedPoint<byteOrder>(io));+ node.control2.setY(psdreadFixedPoint<byteOrder>(io));+ node.control2.setX(psdreadFixedPoint<byteOrder>(io));node.isSmooth = (recordType == 1 || recordType == 4);dbgFile << "\trecord" << recordType << "c1" << node.control1<< "node" << node.node << "c2" << node.control2;@@ -466,6 +466,18 @@ void PsdAdditionalLayerInfoBlock::writeFillLayerBlockEx(QIODevice &io, const QDo}}+void PsdAdditionalLayerInfoBlock::writeVmskBlockEx(QIODevice &io, psd_vector_mask mask)+{+ switch (m_header.byteOrder) {+ case psd_byte_order::psdLittleEndian:+ writeVectorMaskImpl<psd_byte_order::psdLittleEndian>(io, mask);+ break;+ default:+ writeVectorMaskImpl(io, mask);+ break;+ }+}+template<psd_byte_order byteOrder>void PsdAdditionalLayerInfoBlock::writePattBlockExImpl(QIODevice &io, const QDomDocument &patternsXmlDoc){@@ -528,3 +540,88 @@ void PsdAdditionalLayerInfoBlock::writeFillLayerBlockExImpl(QIODevice &io, constthrow e;}}++template<psd_byte_order byteOrder>+void PsdAdditionalLayerInfoBlock::writeVectorMaskImpl(QIODevice &io, psd_vector_mask mask)+{+ KisAslWriterUtils::writeFixedString<byteOrder>("8BIM", io);+ KisAslWriterUtils::writeFixedString<byteOrder>("vmsk", io);++ quint32 len = 8; //, 4 version, 4 flags+ len += 52; // 26 path record, 26 initial fill rule.+ Q_FOREACH(psd_path_sub_path subPath, mask.path.subPaths) {+ len += 26; // subpath record;+ len += subPath.nodes.size() * 26; //and one for each node.+ }++ SAFE_WRITE_EX(byteOrder, io, len);++ quint32 version = 3;+ SAFE_WRITE_EX(byteOrder, io, version);+ quint32 flags = 0;+ if (mask.invert) {+ flags |= 1;+ }+ if (mask.notLink) {+ flags |= 2;+ }+ if (mask.disable) {+ flags |= 4;+ }+ SAFE_WRITE_EX(byteOrder, io, flags);++ // start path records+ quint16 recordType = 6;+ quint32 zero = 0;+ SAFE_WRITE_EX(byteOrder, io, recordType);+ // 24 empty bits+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ // initial fill rule record+ recordType = 8;+ SAFE_WRITE_EX(byteOrder, io, recordType);+ quint16 fillType = mask.path.initialFillRecord;+ SAFE_WRITE_EX(byteOrder, io, fillType);+ // 22 empty bits+ const quint16 halfZero = 0;+ SAFE_WRITE_EX(byteOrder, io, halfZero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ // write the subpaths.+ Q_FOREACH(psd_path_sub_path subPath, mask.path.subPaths) {+ recordType = subPath.isClosed? 0: 3;+ quint16 length = subPath.nodes.size();+ qDebug() << "writing subpath" << subPath.nodes.size();+ SAFE_WRITE_EX(byteOrder, io, recordType);+ SAFE_WRITE_EX(byteOrder, io, length);+ // 22 empty bits+ SAFE_WRITE_EX(byteOrder, io, halfZero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);++ Q_FOREACH(psd_path_node node, subPath.nodes) {+ if (subPath.isClosed) {+ recordType = node.isSmooth? 1: 2;+ } else {+ recordType = node.isSmooth? 4: 5;+ }+ SAFE_WRITE_EX(byteOrder, io, recordType);+ psdwriteFixedPoint<byteOrder>(io, node.control1.y());+ psdwriteFixedPoint<byteOrder>(io, node.control1.x());+ psdwriteFixedPoint<byteOrder>(io, node.node.y());+ psdwriteFixedPoint<byteOrder>(io, node.node.x());+ psdwriteFixedPoint<byteOrder>(io, node.control2.y());+ psdwriteFixedPoint<byteOrder>(io, node.control2.x());+ }+ }+}diff --git a/libs/psd/psd_additional_layer_info_block.h b/libs/psd/psd_additional_layer_info_block.hindex 3eb2f3a30b..c7fcb96422 100644--- a/libs/psd/psd_additional_layer_info_block.h+++ b/libs/psd/psd_additional_layer_info_block.h@@ -659,12 +659,12 @@ struct KRITAPSD_EXPORT psd_path_node {QPointF control1;QPointF node;QPointF control2;- bool isSmooth;+ bool isSmooth {false};};struct KRITAPSD_EXPORT psd_path_sub_path {QList<psd_path_node> nodes;- bool isClosed;+ bool isClosed {false};};struct KRITAPSD_EXPORT psd_path {bool initialFillRecord;@@ -674,9 +674,9 @@ struct KRITAPSD_EXPORT psd_path {};struct KRITAPSD_EXPORT psd_vector_mask {- bool invert;- bool notLink;- bool disable;+ bool invert {false};+ bool notLink {false};+ bool disable {false};psd_path path;};@@ -706,6 +706,7 @@ public:void writeLclrBlockEx(QIODevice &io, const quint16 &labelColor);void writeFillLayerBlockEx(QIODevice &io, const QDomDocument &fillConfig, psd_fill_type type);+ void writeVmskBlockEx(QIODevice &io, psd_vector_mask mask);bool valid();@@ -751,6 +752,9 @@ private:template<psd_byte_order byteOrder = psd_byte_order::psdBigEndian>void writeFillLayerBlockExImpl(QIODevice &io, const QDomDocument &fillConfig, psd_fill_type type);+ template<psd_byte_order byteOrder = psd_byte_order::psdBigEndian>+ void writeVectorMaskImpl(QIODevice &io, psd_vector_mask mask);+private:ExtraLayerInfoBlockHandler m_layerInfoBlockHandler;UserMaskInfoBlockHandler m_userMaskBlockHandler;diff --git a/libs/psd/psd_layer_record.cpp b/libs/psd/psd_layer_record.cppindex bfd52ad8f1..40ff535207 100644--- a/libs/psd/psd_layer_record.cpp+++ b/libs/psd/psd_layer_record.cpp@@ -30,6 +30,7 @@#include <KoColorSpaceTraits.h>#include <KoPathShape.h>+#include <KoPathPoint.h>#include <asl/kis_asl_reader_utils.h>#include <asl/kis_asl_writer_utils.h>@@ -638,7 +639,10 @@ void PSDLayerRecord::writeImpl(QIODevice &io,}{- const quint8 maskFlags = 0; // nothing serious+ quint8 maskFlags = 0; // nothing serious+ if (!vectorMask.path.subPaths.isEmpty()) {+ maskFlags |= 8; // bit 3 = indicates that the user mask actually came from rendering other data+ }SAFE_WRITE_EX(byteOrder, io, maskFlags);const quint16 padding = 0; // 2-byte padding@@ -680,6 +684,11 @@ void PSDLayerRecord::writeImpl(QIODevice &io,additionalInfoBlock.writeFillLayerBlockEx(io, fillConfig, fillType);}+ // write 'vmsk' data block+ if (!vectorMask.path.subPaths.isEmpty()) {+ additionalInfoBlock.writeVmskBlockEx(io, vectorMask);+ }+}} catch (KisAslWriterUtils::ASLWriteException &e) {throw KisAslWriterUtils::ASLWriteException(PREPEND_METHOD(e.what()));@@ -938,7 +947,7 @@ KoPathShape *PSDLayerRecord::constructPathShape(psd_path path, double shapeWidth}if (subPath.isClosed) {shape->curveTo(tf.map(subPath.nodes.last().control2), tf.map(subPath.nodes.first().control1), tf.map(subPath.nodes.first().node));- shape->close();+ shape->closeMerge();}}if (shape->pointCount() > 0) {@@ -948,6 +957,29 @@ KoPathShape *PSDLayerRecord::constructPathShape(psd_path path, double shapeWidthreturn shape;}+void PSDLayerRecord::addPathShapeToPSDPath(psd_path &path, KoPathShape *shape, double shapeWidth, double shapeHeight)+{+ QTransform tf = QTransform::fromScale(shapeWidth, shapeHeight).inverted();+ tf = shape->absoluteTransformation()*tf;++ for (int i = 0; i < shape->subpathCount(); i++) {+ psd_path_sub_path subPath;+ subPath.isClosed = shape->isClosedSubpath(i);+ while(subPath.nodes.size() < shape->subpathPointCount(i)) {+ KoPathPoint *point = shape->pointByIndex(KoPathPointIndex(i, subPath.nodes.size()));+ psd_path_node node;+ node.control1 = tf.map(point->controlPoint1());+ node.control2 = tf.map(point->controlPoint2());+ node.node = tf.map(point->point());+ node.isSmooth = (point->properties().testFlag(KoPathPoint::IsSmooth)+ || point->properties().testFlag(KoPathPoint::IsSymmetric));+ subPath.nodes.append(node);+ }++ path.subPaths.append(subPath);+ }+}+QDebug operator<<(QDebug dbg, const PSDLayerRecord &layer){#ifndef NODEBUGdiff --git a/libs/psd/psd_layer_record.h b/libs/psd/psd_layer_record.hindex 4ecbb8966d..280c54bb60 100644--- a/libs/psd/psd_layer_record.h+++ b/libs/psd/psd_layer_record.h@@ -91,6 +91,14 @@ public:*/KoPathShape *constructPathShape(psd_path path, double shapeWidth, double shapeHeight);+ /**+ * @brief addPathShapeToPSDPath+ * add all KoPathShape subpaths to the given psd_path struct.+ * @param shapeWidth the image width in points+ * @param shapeHeight the image height in points+ */+ void addPathShapeToPSDPath(psd_path &path, KoPathShape *shape, double shapeWidth, double shapeHeight);+void write(QIODevice &io,KisPaintDeviceSP layerContentDevice,KisNodeSP onlyTransparencyMask,@@ -127,6 +135,8 @@ public:psd_fill_type fillType {psd_fill_solid_color};QDomDocument fillConfig;+ psd_vector_mask vectorMask;+struct LayerMaskData {qint32 top {0};qint32 left {0};diff --git a/libs/psd/psd_layer_section.cpp b/libs/psd/psd_layer_section.cppindex cd73127d08..a73176d7cb 100644--- a/libs/psd/psd_layer_section.cpp+++ b/libs/psd/psd_layer_section.cpp@@ -20,6 +20,11 @@#include <kis_node.h>#include <kis_paint_layer.h>#include <kis_painter.h>+#include <kis_selection.h>+#include <kis_shape_selection.h>+#include <kis_transparency_mask.h>++#include <KoPathShape.h>#include "kis_dom_utils.h"@@ -646,6 +651,9 @@ void PSDLayerMaskSection::writePsdImpl(QIODevice &io, KisNodeSP rootLayer, psd_cQString nodeName;KisPaintDeviceSP layerContentDevice;psd_section_type sectionType;+ psd_vector_mask vectorMask;+ double vectorWidth = rootLayer->image()? rootLayer->image()->width() / rootLayer->image()->xRes(): 1;+ double vectorHeight = rootLayer->image()? rootLayer->image()->height() / rootLayer->image()->yRes(): 1;if (item.type == FlattenedNode::RASTER_LAYER) {nodeIrrelevant = false;@@ -659,10 +667,42 @@ void PSDLayerMaskSection::writePsdImpl(QIODevice &io, KisNodeSP rootLayer, psd_cbool transparency = KisPainter::checkDeviceHasTransparency(node->paintDevice());bool semiOpacity = node->paintDevice()->defaultPixel().opacityU8() < OPACITY_OPAQUE_U8;if (transparency || semiOpacity) {+ KisSelectionSP selection = fillLayer->internalSelection();+ if(selection) {+ if(selection->hasNonEmptyShapeSelection()) {+ KisShapeSelection* shapeSelection = dynamic_cast<KisShapeSelection*>(selection->shapeSelection());+ if (shapeSelection) {+ Q_FOREACH(KoShape *shape, shapeSelection->shapes()) {+ KoPathShape *pathShape = dynamic_cast<KoPathShape*>(shape);+ if (pathShape){+ layerRecord->addPathShapeToPSDPath(vectorMask.path, pathShape, vectorWidth, vectorHeight);+ }+ }+ }+ }+ }layerContentDevice = node->original();onlyTransparencyMask = node;maskRect = onlyTransparencyMask->paintDevice()->exactBounds();}+ } else {+ KisTransparencyMask *mask = qobject_cast<KisTransparencyMask*>(onlyTransparencyMask.data());+ if (mask) {+ KisSelectionSP selection = mask->selection();+ if(selection) {+ if(selection->hasNonEmptyShapeSelection()) {+ KisShapeSelection* shapeSelection = dynamic_cast<KisShapeSelection*>(selection->shapeSelection());+ if (shapeSelection) {+ Q_FOREACH(KoShape *shape, shapeSelection->shapes()) {+ KoPathShape *pathShape = dynamic_cast<KoPathShape*>(shape);+ if (pathShape){+ layerRecord->addPathShapeToPSDPath(vectorMask.path, pathShape, vectorWidth, vectorHeight);+ }+ }+ }+ }+ }+ }}sectionType = psd_other;} else {@@ -727,6 +767,8 @@ void PSDLayerMaskSection::writePsdImpl(QIODevice &io, KisNodeSP rootLayer, psd_clayerRecord->fillType = fillType;layerRecord->fillConfig = fillConfig;+ layerRecord->vectorMask = vectorMask;+layerRecord->write(io, layerContentDevice, onlyTransparencyMask, maskRect, sectionType, stylesXmlDoc, node->inherits("KisGroupLayer"));}diff --git a/libs/psdutils/psd_utils.h b/libs/psdutils/psd_utils.hindex 1677110247..7ea7e566f5 100644--- a/libs/psdutils/psd_utils.h+++ b/libs/psdutils/psd_utils.h@@ -497,7 +497,7 @@ inline bool psd_read_blendmode(QIODevice &io, QString &blendModeKey)// Copied from Scribus decodePSDfloat in scimgdataloader.cpp by Franz Schmidtemplate<psd_byte_order byteOrder = psd_byte_order::psdBigEndian>-inline double psdreadFixedFloatingPoint(QIODevice &io)+inline double psdreadFixedPoint(QIODevice &io){quint32 data;double ret = 0.0;@@ -518,4 +518,21 @@ inline double psdreadFixedFloatingPoint(QIODevice &io)return ret;}+template<psd_byte_order byteOrder = psd_byte_order::psdBigEndian>+inline void psdwriteFixedPoint(QIODevice &io, double val)+{+ qint32 data;+ double max24 = 16777215.0;+ // val is bound to -16 to +16++ qint32 man = qint32(val);+ quint32 frac= quint32(fabs(val - man) * max24);+ data = (qAbs(man) << 24) | frac;+ if (val < 0) {+ data *= -1;+ };++ psdwrite<byteOrder>(io, data);+}+#endif // PSD_UTILS_Hdiff --git a/libs/psdutils/tests/psd_utils_test.cpp b/libs/psdutils/tests/psd_utils_test.cppindex d64d366b2f..d2192dc053 100644--- a/libs/psdutils/tests/psd_utils_test.cpp+++ b/libs/psdutils/tests/psd_utils_test.cpp@@ -362,4 +362,29 @@ void PSDUtilsTest::test_psdread_pascalstring()}}+void PSDUtilsTest::test_psdread_fixedpoint()+{+ {+ QBuffer buf;+ QVERIFY(buf.open(QBuffer::ReadWrite));+ const double s = -2.7;+ psdwriteFixedPoint<psd_byte_order::psdLittleEndian>(buf, s);+ buf.close();+ buf.open(QBuffer::ReadOnly);+ double r = psdreadFixedPoint<psd_byte_order::psdLittleEndian>(buf);+ QVERIFY(fabs(r-s) < 0.001);+ }++ {+ QBuffer buf;+ QVERIFY(buf.open(QBuffer::ReadWrite));+ const double s = -2.7;+ psdwriteFixedPoint(buf, s);+ buf.close();+ buf.open(QBuffer::ReadOnly);+ double r = psdreadFixedPoint(buf);+ QVERIFY(fabs(r-s) < 0.001);+ }+}+SIMPLE_TEST_MAIN(PSDUtilsTest)diff --git a/libs/psdutils/tests/psd_utils_test.h b/libs/psdutils/tests/psd_utils_test.hindex c131c0d919..7345918390 100644--- a/libs/psdutils/tests/psd_utils_test.h+++ b/libs/psdutils/tests/psd_utils_test.h@@ -25,6 +25,7 @@ private Q_SLOTS:void test_psdread_quint16();void test_psdread_quint32();void test_psdread_pascalstring();+ void test_psdread_fixedpoint();};#endifdiff --git a/plugins/impex/psd/psd_loader.cpp b/plugins/impex/psd/psd_loader.cppindex 0390c682ef..d8d40d8914 100644--- a/plugins/impex/psd/psd_loader.cpp+++ b/plugins/impex/psd/psd_loader.cpp@@ -399,22 +399,23 @@ KisImportExportErrorCode PSDLoader::decode(QIODevice &io)Q_FOREACH (ChannelInfo *channelInfo, layerRecord->channelInfoRecords) {if (channelInfo->channelId < -1) {const KisGeneratorLayer *fillLayer = qobject_cast<KisGeneratorLayer *>(newLayer.data());- KoPathShape *vectorMask;+ KoPathShape *vectorMask = new KoPathShape();if (layerRecord->infoBlocks.keys.contains("vmsk") || layerRecord->infoBlocks.keys.contains("vsms")) {double width = m_image->width() / m_image->xRes();double height = m_image->height() / m_image->yRes();vectorMask = layerRecord->constructPathShape(layerRecord->infoBlocks.vectorMask.path, width, height);vectorMask->setUserData(new KisShapeSelectionMarker);}+ bool hasVectorMask = vectorMask->pointCount() > 0 && layerRecord->infoBlocks.vectorMask.path.subPaths.size() > 0;if (fillLayer) {if (!layerRecord->readMask(io, fillLayer->paintDevice(), channelInfo)) {dbgFile << "failed reading masks for generator layer: " << layerRecord->layerName << layerRecord->error;}- if (vectorMask) {- KisShapeSelection* shapeSelection = new KisShapeSelection(m_doc->shapeController(), fillLayer->selection());- fillLayer->selection()->convertToVectorSelectionNoUndo(shapeSelection);+ if (hasVectorMask) {+ KisShapeSelection* shapeSelection = new KisShapeSelection(m_doc->shapeController(), fillLayer->internalSelection());+ fillLayer->internalSelection()->convertToVectorSelectionNoUndo(shapeSelection);shapeSelection->addShape(vectorMask);- fillLayer->selection()->updateProjection();+ fillLayer->internalSelection()->updateProjection();}} else {KisTransparencyMaskSP mask = new KisTransparencyMask(m_image, i18n("Transparency Mask"));@@ -422,7 +423,7 @@ KisImportExportErrorCode PSDLoader::decode(QIODevice &io)if (!layerRecord->readMask(io, mask->paintDevice(), channelInfo)) {dbgFile << "failed reading masks for layer: " << layerRecord->layerName << layerRecord->error;}- if (vectorMask) {+ if (hasVectorMask) {KisShapeSelection* shapeSelection = new KisShapeSelection(m_doc->shapeController(), mask->selection());mask->selection()->convertToVectorSelectionNoUndo(shapeSelection);shapeSelection->addShape(vectorMask);