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 SAI blend modes for PSD files
Carsten Hartenfels58bb34556d17
diff --git a/libs/psd/psd_additional_layer_info_block.cpp b/libs/psd/psd_additional_layer_info_block.cppindex 21c06fbd8c..8e93fa1496 100644--- a/libs/psd/psd_additional_layer_info_block.cpp+++ b/libs/psd/psd_additional_layer_info_block.cpp@@ -319,6 +319,9 @@ void PsdAdditionalLayerInfoBlock::readImpl(QIODevice &io)} else if (key == "shmd") {} else if (key == "lyvr") {} else if (key == "tsly") {+ // "Transparency shapes layer". Paint Tool SAI sets+ // this to 0 when one of its blend modes is used.+ psdread<byteOrder>(io, transparencyShapesLayer);} else if (key == "lmgm") {} else if (key == "vmgm") {} else if (key == "plLd") { // Replaced by SoLd in CS3@@ -365,6 +368,12 @@ void PsdAdditionalLayerInfoBlock::readImpl(QIODevice &io)}} else if (key == "FXid") {} else if (key == "FEid") {+ } else if (key == "iOpa") {+ // Not from the PSD spec, the Python module psd_tools calls this+ // "blend fill opacity". PaintTool SAI sets this value to the layer+ // opacity when using one of its blend modes and leaves the regular+ // layer opacity at 255.+ psdread<byteOrder>(io, blendFillOpacity);}}}@@ -494,6 +503,18 @@ void PsdAdditionalLayerInfoBlock::writeLclrBlockEx(QIODevice &io, const quint16}}+void PsdAdditionalLayerInfoBlock::writeSaiBlendBlockEx(QIODevice &io, quint8 opacity)+{+ switch (m_header.byteOrder) {+ case psd_byte_order::psdLittleEndian:+ writeSaiBlendBlockExImpl<psd_byte_order::psdLittleEndian>(io, opacity);+ break;+ default:+ writeSaiBlendBlockExImpl(io, opacity);+ break;+ }+}+void PsdAdditionalLayerInfoBlock::writeFillLayerBlockEx(QIODevice &io, const QDomDocument &fillConfig, psd_fill_type type){switch (m_header.byteOrder) {@@ -606,6 +627,32 @@ void PsdAdditionalLayerInfoBlock::writeLclrBlockExImpl(QIODevice &io, const quin}+template<psd_byte_order byteOrder>+void PsdAdditionalLayerInfoBlock::writeSaiBlendBlockExImpl(QIODevice &io, quint8 opacity)+{+ // Paint Tool SAI blend mode marker. The regular layer opacity+ // is set to 255, tsly to 0 and iOpa to the actual opacity.+ // They're both 4 bytes long and have zeroes for padding.+ const quint32 len = 4;+ const quint8 zero = 0;+ // Transparency shapes layer 0, three bytes of padding.+ KisAslWriterUtils::writeFixedString<byteOrder>("8BIM", io);+ KisAslWriterUtils::writeFixedString<byteOrder>("tsly", io);+ SAFE_WRITE_EX(byteOrder, io, len);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ // Blend shape opacity, three bytes of padding.+ KisAslWriterUtils::writeFixedString<byteOrder>("8BIM", io);+ KisAslWriterUtils::writeFixedString<byteOrder>("iOpa", io);+ SAFE_WRITE_EX(byteOrder, io, len);+ SAFE_WRITE_EX(byteOrder, io, opacity);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+ SAFE_WRITE_EX(byteOrder, io, zero);+}+template<psd_byte_order byteOrder>void PsdAdditionalLayerInfoBlock::writeFillLayerBlockExImpl(QIODevice &io, const QDomDocument &fillConfig, psd_fill_type type){diff --git a/libs/psd/psd_additional_layer_info_block.h b/libs/psd/psd_additional_layer_info_block.hindex 69f8d8340a..f912dfd581 100644--- a/libs/psd/psd_additional_layer_info_block.h+++ b/libs/psd/psd_additional_layer_info_block.h@@ -1366,6 +1366,7 @@ public:void writePattBlockEx(QIODevice &io, const QDomDocument &patternsXmlDoc);void writeLspfBlockEx(QIODevice &io, const bool lock);void writeLclrBlockEx(QIODevice &io, const quint16 &labelColor);+ void writeSaiBlendBlockEx(QIODevice &io, quint8 opacity);void writeFillLayerBlockEx(QIODevice &io, const QDomDocument &fillConfig, psd_fill_type type);void writeVmskBlockEx(QIODevice &io, psd_vector_mask mask);@@ -1401,6 +1402,8 @@ public:psd_section_type sectionDividerType;QString sectionDividerBlendMode;+ quint8 transparencyShapesLayer = 1;+ quint8 blendFillOpacity = 0;private:template<psd_byte_order byteOrder = psd_byte_order::psdBigEndian>@@ -1424,6 +1427,9 @@ private:template<psd_byte_order byteOrder = psd_byte_order::psdBigEndian>void writeLclrBlockExImpl(QIODevice &io, const quint16 &lclr);+ template<psd_byte_order byteOrder = psd_byte_order::psdBigEndian>+ void writeSaiBlendBlockExImpl(QIODevice &io, quint8 opacity);+template<psd_byte_order byteOrder = psd_byte_order::psdBigEndian>void writeFillLayerBlockExImpl(QIODevice &io, const QDomDocument &fillConfig, psd_fill_type type);diff --git a/libs/psd/psd_layer_record.cpp b/libs/psd/psd_layer_record.cppindex 62c286971e..7b77a19ee0 100644--- a/libs/psd/psd_layer_record.cpp+++ b/libs/psd/psd_layer_record.cpp@@ -514,6 +514,13 @@ bool PSDLayerRecord::readImpl(QIODevice &io)userLocked = infoBlocks.userLock;}labelColor = kritaColorLabelIndex(infoBlocks.labelColor);++ // Paint Tool SAI blend mode handling. It sets tsly to 0,+ // the layer opacity to 255 and iOpa to the actual opacity.+ if (infoBlocks.keys.contains("tsly") && infoBlocks.keys.contains("iOpa") && infoBlocks.transparencyShapesLayer == 0) {+ opacity = infoBlocks.blendFillOpacity;+ saiBlendMode = true;+ }}return valid();@@ -600,7 +607,10 @@ void PSDLayerRecord::writeImpl(QIODevice &io,KisAslWriterUtils::writeFixedString<byteOrder>("8BIM", io);KisAslWriterUtils::writeFixedString<byteOrder>(blendModeKey, io);- SAFE_WRITE_EX(byteOrder, io, opacity);+ // For Paint Tool SAI blend modes, the regular layer+ // opacity is set to 255 and the real opacity is in the+ // iOpa block instead.+ SAFE_WRITE_EX(byteOrder, io, saiBlendMode ? quint8(255) : opacity);SAFE_WRITE_EX(byteOrder, io, clipping); // unused// visibility and protection@@ -708,6 +718,10 @@ void PSDLayerRecord::writeImpl(QIODevice &io,additionalInfoBlock.writeVectorOriginationDataEx(io, vectorOriginationData);}+ // write 'tsly' and 'iOpa' data blocks+ if (saiBlendMode) {+ additionalInfoBlock.writeSaiBlendBlockEx(io, opacity);+ }}} catch (KisAslWriterUtils::ASLWriteException &e) {throw KisAslWriterUtils::ASLWriteException(PREPEND_METHOD(e.what()));diff --git a/libs/psd/psd_layer_record.h b/libs/psd/psd_layer_record.hindex 81e2da935d..b6be61a65e 100644--- a/libs/psd/psd_layer_record.h+++ b/libs/psd/psd_layer_record.h@@ -131,6 +131,7 @@ public:bool visible {true};bool irrelevant {false};bool userLocked {false};+ bool saiBlendMode {false};int labelColor {0};diff --git a/libs/psd/psd_layer_section.cpp b/libs/psd/psd_layer_section.cppindex 52863ec470..324597aa76 100644--- a/libs/psd/psd_layer_section.cpp+++ b/libs/psd/psd_layer_section.cpp@@ -959,6 +959,7 @@ void PSDLayerMaskSection::writePsdImpl(QIODevice &io, KisNodeSP rootLayer, psd_c}layerRecord->blendModeKey = composite_op_to_psd_blendmode(nodeCompositeOp);+ layerRecord->saiBlendMode = composite_op_is_sai(nodeCompositeOp);layerRecord->isPassThrough = nodeIsPassThrough;layerRecord->opacity = nodeOpacity;layerRecord->clipping = nodeClipping;@@ -1139,6 +1140,7 @@ void PSDLayerMaskSection::writeTiffImpl(QIODevice &io, KisNodeSP rootLayer, psd_}layerRecord->blendModeKey = composite_op_to_psd_blendmode(nodeCompositeOp);+ layerRecord->saiBlendMode = composite_op_is_sai(nodeCompositeOp);layerRecord->isPassThrough = nodeIsPassThrough;layerRecord->opacity = nodeOpacity;layerRecord->clipping = nodeClipping;diff --git a/libs/psdutils/psd.cpp b/libs/psdutils/psd.cppindex 26deadf6f9..f8775578ae 100644--- a/libs/psdutils/psd.cpp+++ b/libs/psdutils/psd.cpp@@ -56,7 +56,7 @@ QPair<QString, QString> psd_colormode_to_colormodelid(psd_color_mode colormode,return colorSpaceId;}-QString psd_blendmode_to_composite_op(const QString &blendmode)+QString psd_blendmode_to_composite_op(const QString &blendmode, bool sai){// 'pass' = pass throughif (blendmode == "pass")@@ -75,10 +75,10 @@ QString psd_blendmode_to_composite_op(const QString &blendmode)return COMPOSITE_MULT;// 'idiv' = color burnif (blendmode == "idiv")- return COMPOSITE_BURN;+ return sai ? COMPOSITE_BURN_SAI : COMPOSITE_BURN;// 'lbrn' = linear burnif (blendmode == "lbrn")- return COMPOSITE_LINEAR_BURN;+ return sai ? COMPOSITE_SHADE_SAI : COMPOSITE_LINEAR_BURN;// 'dkCl' = darker colorif (blendmode == "dkCl")return COMPOSITE_DARKER_COLOR;@@ -90,10 +90,10 @@ QString psd_blendmode_to_composite_op(const QString &blendmode)return COMPOSITE_SCREEN;// 'div ' = color dodgeif (blendmode == "div ")- return COMPOSITE_DODGE;+ return sai ? COMPOSITE_DODGE_SAI : COMPOSITE_DODGE;// 'lddg' = linear dodgeif (blendmode == "lddg")- return COMPOSITE_LINEAR_DODGE;+ return sai ? COMPOSITE_LUMINOSITY_SAI : COMPOSITE_LINEAR_DODGE;// 'lgCl' = lighter colorif (blendmode == "lgCl")return COMPOSITE_LIGHTER_COLOR;@@ -108,19 +108,19 @@ QString psd_blendmode_to_composite_op(const QString &blendmode)return COMPOSITE_HARD_LIGHT;// 'vLit' = vivid lightif (blendmode == "vLit")- return COMPOSITE_VIVID_LIGHT;+ return sai ? COMPOSITE_BURN_DODGE_SAI : COMPOSITE_VIVID_LIGHT;// 'lLit' = linear lightif (blendmode == "lLit")- return COMPOSITE_LINEAR_LIGHT;+ return sai ? COMPOSITE_SHADE_SHINE_SAI : COMPOSITE_LINEAR_LIGHT;// 'pLit' = pin lightif (blendmode == "pLit")return COMPOSITE_PIN_LIGHT;// 'hMix' = hard mixif (blendmode == "hMix")- return COMPOSITE_HARD_MIX_PHOTOSHOP;+ return sai ? COMPOSITE_HARD_MIX_SAI : COMPOSITE_HARD_MIX_PHOTOSHOP;// 'diff' = differenceif (blendmode == "diff")- return COMPOSITE_DIFF;+ return sai ? COMPOSITE_DIFFERENCE_SAI : COMPOSITE_DIFF;// 'smud' = exclusionif (blendmode == "smud")return COMPOSITE_EXCLUSION;@@ -167,9 +167,13 @@ QString composite_op_to_psd_blendmode(const QString &compositeop)// 'idiv' = color burnif (compositeop == COMPOSITE_BURN)return "idiv";+ if (compositeop == COMPOSITE_BURN_SAI)+ return "idiv";// 'lbrn' = linear burnif (compositeop == COMPOSITE_LINEAR_BURN)return "lbrn";+ if (compositeop == COMPOSITE_SHADE_SAI)+ return "lbrn";// 'dkCl' = darker colorif (compositeop == COMPOSITE_DARKER_COLOR)return "dkCl";@@ -182,9 +186,13 @@ QString composite_op_to_psd_blendmode(const QString &compositeop)// 'div ' = color dodgeif (compositeop == COMPOSITE_DODGE)return "div ";+ if (compositeop == COMPOSITE_DODGE_SAI)+ return "div ";// 'lddg' = linear dodgeif (compositeop == COMPOSITE_LINEAR_DODGE)return "lddg";+ if (compositeop == COMPOSITE_LUMINOSITY_SAI)+ return "lddg";// 'lgCl' = lighter colorif (compositeop == COMPOSITE_LIGHTER_COLOR)return "lgCl";@@ -202,18 +210,26 @@ QString composite_op_to_psd_blendmode(const QString &compositeop)// 'vLit' = vivid lightif (compositeop == COMPOSITE_VIVID_LIGHT)return "vLit";+ if (compositeop == COMPOSITE_BURN_DODGE_SAI)+ return "vLit";// 'lLit' = linear lightif (compositeop == COMPOSITE_LINEAR_LIGHT)return "lLit";+ if (compositeop == COMPOSITE_SHADE_SHINE_SAI)+ return "lLit";// 'pLit' = pin lightif (compositeop == COMPOSITE_PIN_LIGHT)return "pLit";// 'hMix' = hard mixif (compositeop == COMPOSITE_HARD_MIX_PHOTOSHOP)return "hMix";+ if (compositeop == COMPOSITE_HARD_MIX_SAI)+ return "hMix";// 'diff' = differenceif (compositeop == COMPOSITE_DIFF)return "diff";+ if (compositeop == COMPOSITE_DIFFERENCE_SAI)+ return "diff";// 'smud' = exclusionif (compositeop == COMPOSITE_EXCLUSION)return "smud";@@ -240,6 +256,18 @@ QString composite_op_to_psd_blendmode(const QString &compositeop)return "norm";}+bool composite_op_is_sai(const QString &compositeop)+{+ return compositeop == COMPOSITE_BURN_DODGE_SAI+ || compositeop == COMPOSITE_BURN_SAI+ || compositeop == COMPOSITE_DIFFERENCE_SAI+ || compositeop == COMPOSITE_DODGE_SAI+ || compositeop == COMPOSITE_HARD_MIX_SAI+ || compositeop == COMPOSITE_SHADE_SAI+ || compositeop == COMPOSITE_SHADE_SHINE_SAI+ || compositeop == COMPOSITE_LUMINOSITY_SAI;+}+QPoint psd_layer_effects_shadow_base::calculateOffset(const psd_layer_effects_context *context) const{Q_UNUSED(context);diff --git a/libs/psdutils/psd.h b/libs/psdutils/psd.hindex 03797ca717..899b1bf07a 100644--- a/libs/psdutils/psd.h+++ b/libs/psdutils/psd.h@@ -1301,8 +1301,9 @@ QPair<QString, QString> KRITAPSDUTILS_EXPORT psd_colormode_to_colormodelid(psd_c/*** Convert the Photoshop blend mode strings to Pigment compositeop id's*/-QString KRITAPSDUTILS_EXPORT psd_blendmode_to_composite_op(const QString &blendmode);+QString KRITAPSDUTILS_EXPORT psd_blendmode_to_composite_op(const QString &blendmode, bool sai = false);QString KRITAPSDUTILS_EXPORT composite_op_to_psd_blendmode(const QString &compositeOp);+bool KRITAPSDUTILS_EXPORT composite_op_is_sai(const QString &compositeOp);#if defined(__clang__) && __clang_major__ < 10diff --git a/plugins/impex/psd/psd_loader.cpp b/plugins/impex/psd/psd_loader.cppindex 0b3a0da4c3..a493538d68 100644--- a/plugins/impex/psd/psd_loader.cpp+++ b/plugins/impex/psd/psd_loader.cpp@@ -307,7 +307,7 @@ KisImportExportErrorCode PSDLoader::decode(QIODevice &io)groupLayer->setCollapsed(true);}- QString compositeOp = psd_blendmode_to_composite_op(layerRecord->infoBlocks.sectionDividerBlendMode);+ QString compositeOp = psd_blendmode_to_composite_op(layerRecord->infoBlocks.sectionDividerBlendMode, layerRecord->saiBlendMode);// Krita doesn't support pass-through blend// mode. Instead it is just a property of a group@@ -550,7 +550,7 @@ KisImportExportErrorCode PSDLoader::decode(QIODevice &io)return ImportExportCodes::FileFormatIncorrect;}}- layer->setCompositeOpId(psd_blendmode_to_composite_op(layerRecord->blendModeKey));+ layer->setCompositeOpId(psd_blendmode_to_composite_op(layerRecord->blendModeKey, layerRecord->saiBlendMode));layer->setColorLabelIndex(layerRecord->labelColor);