Skip to content

armgit / krita

Public Imported from GitHub
0

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++

Load vector masks.

Wolthera van Hövell tot Westerflier45f4fd1119ca
diff --git a/libs/psd/psd_additional_layer_info_block.cpp b/libs/psd/psd_additional_layer_info_block.cpp
index 40425b78c4..353585a976 100644
--- a/libs/psd/psd_additional_layer_info_block.cpp
+++ b/libs/psd/psd_additional_layer_info_block.cpp
@@ -233,6 +233,74 @@ void PsdAdditionalLayerInfoBlock::readImpl(QIODevice &io)
} else if (key == "brst") {
} else if (key == "vmsk" || key == "vsms") { // If key is "vsms" then we are writing for (Photoshop CS6) and the document will have a "vscg" key
+ quint32 version; // ( = 3 for Photoshop 6.0)
+ psdread<byteOrder>(io, version);
+
+ quint32 flags;
+ psdread<byteOrder>(io, flags);
+ // read the flags.
+ vectorMask.invert = flags & 1? true: false;
+ vectorMask.notLink = flags & 2? true: false;
+ vectorMask.disable = flags & 3? true: false;
+
+ quint64 currentPos = 8;
+ psd_path_sub_path currentPath;
+ bool firstPath = true;
+
+ while (currentPos < blockSize) {
+ quint16 recordType;
+ psdread<byteOrder>(io, recordType);
+
+ if (recordType == 6) {
+ io.skip(24);
+ dbgFile << "\trecord" << recordType;
+ } 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));
+ vectorMask.path.clipBoardBounds = bounds;
+ vectorMask.path.clipBoardResolution = psdreadFixedFloatingPoint<byteOrder>(io);
+ dbgFile << "\trecord" << recordType << "top" << bounds << "res" << vectorMask.path.clipBoardResolution;
+ io.skip(4);
+ } else if (recordType == 0 || recordType == 3) {
+ quint16 length;
+ psdread<byteOrder>(io, length);
+ dbgFile << "\trecord" << recordType << "length" << length;
+ if (firstPath) {
+ currentPath.isClosed = (recordType == 0);
+ } else {
+ vectorMask.path.subPaths.append(currentPath);
+ currentPath = psd_path_sub_path();
+ currentPath.isClosed = (recordType == 0);
+ }
+ io.skip(22);
+ } else if (recordType == 8) {
+ quint16 length;
+ psdread<byteOrder>(io, length);
+ vectorMask.path.initialFillRecord = (length > 0);
+ 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.isSmooth = (recordType == 1 || recordType == 4);
+ dbgFile << "\trecord" << recordType << "c1" << node.control1
+ << "node" << node.node << "c2" << node.control2;
+ currentPath.nodes.append(node);
+ }
+
+ currentPos += 26;
+ }
+ if (!currentPath.nodes.isEmpty()) {
+ vectorMask.path.subPaths.append(currentPath);
+ }
} else if (key == "TySh") {
textData = KisAslReader::readTypeToolObjectSettings(io, textTransform, byteOrder);
diff --git a/libs/psd/psd_additional_layer_info_block.h b/libs/psd/psd_additional_layer_info_block.h
index 5ce10e858c..3eb2f3a30b 100644
--- a/libs/psd/psd_additional_layer_info_block.h
+++ b/libs/psd/psd_additional_layer_info_block.h
@@ -655,6 +655,31 @@ struct KRITAPSD_EXPORT psd_layer_type_shape {
}
};
+struct KRITAPSD_EXPORT psd_path_node {
+ QPointF control1;
+ QPointF node;
+ QPointF control2;
+ bool isSmooth;
+};
+
+struct KRITAPSD_EXPORT psd_path_sub_path {
+ QList<psd_path_node> nodes;
+ bool isClosed;
+};
+struct KRITAPSD_EXPORT psd_path {
+ bool initialFillRecord;
+ QRectF clipBoardBounds;
+ double clipBoardResolution;
+ QList<psd_path_sub_path> subPaths;
+};
+
+struct KRITAPSD_EXPORT psd_vector_mask {
+ bool invert;
+ bool notLink;
+ bool disable;
+ psd_path path;
+};
+
/**
* @brief The PsdAdditionalLayerInfoBlock class implements the Additional Layer Information block
*
@@ -699,6 +724,8 @@ public:
QTransform textTransform;
QDomDocument textData;
+ psd_vector_mask vectorMask;
+
psd_section_type sectionDividerType;
QString sectionDividerBlendMode;
diff --git a/libs/psd/psd_layer_record.cpp b/libs/psd/psd_layer_record.cpp
index 3d6ba3370e..bfd52ad8f1 100644
--- a/libs/psd/psd_layer_record.cpp
+++ b/libs/psd/psd_layer_record.cpp
@@ -29,6 +29,8 @@
#include <KoColorSpaceRegistry.h>
#include <KoColorSpaceTraits.h>
+#include <KoPathShape.h>
+
#include <asl/kis_asl_reader_utils.h>
#include <asl/kis_asl_writer_utils.h>
#include <asl/kis_offset_keeper.h>
@@ -912,6 +914,40 @@ bool PSDLayerRecord::readMask(QIODevice &io, KisPaintDeviceSP dev, ChannelInfo *
return true;
}
+KoPathShape *PSDLayerRecord::constructPathShape(psd_path path, double shapeWidth, double shapeHeight)
+{
+ KoPathShape *shape = new KoPathShape();
+ // psd paths are stored normalized.
+ QTransform tf = QTransform::fromScale(shapeWidth, shapeHeight);
+
+ QString nodeTypes;
+ Q_FOREACH(psd_path_sub_path subPath, path.subPaths) {
+ for (int i = 0; i < subPath.nodes.size(); i++) {
+ psd_path_node node = subPath.nodes.at(i);
+ if (i == 0) {
+ shape->moveTo(tf.map(node.node));
+ } else {
+ psd_path_node previousNode = subPath.nodes.at(i-1);
+ shape->curveTo(tf.map(previousNode.control2), tf.map(node.control1), tf.map(node.node));
+ }
+ if (node.isSmooth) {
+ nodeTypes.append("s");
+ } else {
+ nodeTypes.append("c");
+ }
+ }
+ 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();
+ }
+ }
+ if (shape->pointCount() > 0) {
+ shape->loadNodeTypes(nodeTypes);
+ }
+
+ return shape;
+}
+
QDebug operator<<(QDebug dbg, const PSDLayerRecord &layer)
{
#ifndef NODEBUG
diff --git a/libs/psd/psd_layer_record.h b/libs/psd/psd_layer_record.h
index 41801c3838..4ecbb8966d 100644
--- a/libs/psd/psd_layer_record.h
+++ b/libs/psd/psd_layer_record.h
@@ -22,6 +22,7 @@
#include "psd_header.h"
class QIODevice;
+class KoPathShape;
enum psd_layer_type {
psd_layer_type_normal,
@@ -80,6 +81,16 @@ public:
bool readPixelData(QIODevice &io, KisPaintDeviceSP device);
bool readMask(QIODevice &io, KisPaintDeviceSP dev, ChannelInfo *channel);
+ /**
+ * @brief constructPathShape
+ * create a KoPathshape based on a psd_path struct, used in vector masks and path resources.
+ * @param path a psd path struct.
+ * @param shapeWidth the image width in points
+ * @param shapeHeight the image height in points
+ * @return a KoPathShape.
+ */
+ KoPathShape *constructPathShape(psd_path path, double shapeWidth, double shapeHeight);
+
void write(QIODevice &io,
KisPaintDeviceSP layerContentDevice,
KisNodeSP onlyTransparencyMask,
diff --git a/libs/psdutils/psd_utils.h b/libs/psdutils/psd_utils.h
index 5972066cb0..1677110247 100644
--- a/libs/psdutils/psd_utils.h
+++ b/libs/psdutils/psd_utils.h
@@ -495,4 +495,27 @@ inline bool psd_read_blendmode(QIODevice &io, QString &blendModeKey)
return true;
}
+// Copied from Scribus decodePSDfloat in scimgdataloader.cpp by Franz Schmid
+template<psd_byte_order byteOrder = psd_byte_order::psdBigEndian>
+inline double psdreadFixedFloatingPoint(QIODevice &io)
+{
+ quint32 data;
+ double ret = 0.0;
+
+ if (psdread<byteOrder>(io, data)) {
+ char man = (data & 0xFF000000) >> 24;
+ if (man >= 0)
+ {
+ ret = (data & 0x00FFFFFF) / 16777215.0;
+ ret = (ret + man);
+ }
+ else
+ {
+ ret = (~data & 0x00FFFFFF) / 16777215.0;
+ ret = (ret + ~man) * -1;
+ }
+ }
+ return ret;
+}
+
#endif // PSD_UTILS_H
diff --git a/plugins/impex/psd/psd_loader.cpp b/plugins/impex/psd/psd_loader.cpp
index a674afd794..0390c682ef 100644
--- a/plugins/impex/psd/psd_loader.cpp
+++ b/plugins/impex/psd/psd_loader.cpp
@@ -18,6 +18,8 @@
#include <KoSvgTextShape.h>
#include <KoSvgTextShapeMarkupConverter.h>
#include <kis_shape_layer.h>
+#include <KoPathShape.h>
+#include <kis_shape_selection.h>
#include <kis_annotation.h>
#include <kis_types.h>
@@ -397,16 +399,35 @@ 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;
+ 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);
+ }
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);
+ shapeSelection->addShape(vectorMask);
+ fillLayer->selection()->updateProjection();
+ }
} else {
KisTransparencyMaskSP mask = new KisTransparencyMask(m_image, i18n("Transparency Mask"));
mask->initSelection(newLayer);
if (!layerRecord->readMask(io, mask->paintDevice(), channelInfo)) {
dbgFile << "failed reading masks for layer: " << layerRecord->layerName << layerRecord->error;
}
+ if (vectorMask) {
+ KisShapeSelection* shapeSelection = new KisShapeSelection(m_doc->shapeController(), mask->selection());
+ mask->selection()->convertToVectorSelectionNoUndo(shapeSelection);
+ shapeSelection->addShape(vectorMask);
+ mask->selection()->updateProjection();
+ }
m_image->addNode(mask, newLayer);
}
}