Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_qmic_import_tools.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2013 Dmitry Kazakov <dimula73@gmail.com>
3 * SPDX-FileCopyrightText: 2013 Lukáš Tvrdý <lukast.dev@gmail.com>
4 * SPDX-FileCopyrightText: 2022 L. E. Segovia <amy@amyspark.me>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include <QRegularExpression>
10#include <QRegExp>
11
17#include <kis_command_utils.h>
18#include <kis_layer_utils.h>
19#include <kis_node.h>
20#include <kis_paint_layer.h>
21#include <kis_painter.h>
22#include <kis_selection.h>
23#include <kis_types.h>
24
25#include "gmic.h"
27#include "kis_qmic_interface.h"
29
31{
32[[nodiscard]] KUndo2Command *
34 KisNode *node,
35 KisSelectionSP selection)
36{
37 dbgPlugins << "KisQmicImportTools::applyLayerNameChanges";
38
39 auto *cmd = new KisCommandUtils::CompositeCommand();
40
41 dbgPlugins << "Layer name: " << srcGmicImage.m_layerName;
42
43 {
44 const QRegExp modeRe(R"(mode\‍(\s*([^)]*)\s*\))");
45 if (modeRe.indexIn(srcGmicImage.m_layerName) != -1) {
46 QString modeStr = modeRe.cap(1).trimmed();
47 auto translatedMode =
49 dbgPlugins << "Detected mode: " << modeStr << " => "
50 << translatedMode;
51 if (!translatedMode.isNull()) {
52 cmd->addCommand(
53 new KisNodeCompositeOpCommand(node, translatedMode));
54 }
55 }
56 }
57
58 {
59 const QRegExp opacityRe(R"(opacity\‍(\s*([^)]*)\s*\))");
60
61 if (opacityRe.indexIn(srcGmicImage.m_layerName) != -1) {
62 const auto opacity = opacityRe.cap(1).toFloat();
63 dbgPlugins << "Detected opacity: " << opacity
64 << std::lround(opacity / 100.f * 255.f);
65 cmd->addCommand(
66 new KisNodeOpacityCommand(node,
67 static_cast<quint8>(std::lround(
68 float(opacity * 255) / 100.f))));
69 }
70 }
71
72 {
73 const QRegExp nameRe(R"(name\‍(\s*([^)]*)\s*\))");
74
75 if (nameRe.indexIn(srcGmicImage.m_layerName) != -1) {
76 const auto name = nameRe.cap(1);
77 dbgPlugins << "Detected layer name: " << name;
78 cmd->addCommand(new KisNodeRenameCommand(node, node->name(), name));
79 // apply command
80 }
81 }
82
83 if (!selection) {
84 // Some GMic filters encode layer position into the layer name.
85 // E.g. from extract foreground: "name([unnamed]
86 // [foreground]),pos(55,35)"
87 const QRegularExpression positionPattern(
88 R"(pos\‍(\s*(-?\d*)[^)](-?\d*)\s*\))");
89 const QRegularExpressionMatch match =
90 positionPattern.match(srcGmicImage.m_layerName);
91 if (match.hasMatch()) {
92 const auto x = match.captured(1).toInt();
93 const auto y = match.captured(2).toInt();
94 const QPoint oldPos(node->x(), node->y());
95 const QPoint newPos(x, y);
96 dbgPlugins << "Detected layer position: " << oldPos << newPos
97 << node->paintDevice()->exactBounds();
98 cmd->addCommand(new KisNodeMoveCommand2(node, oldPos, newPos));
99 }
100 }
101
102 return cmd;
103}
104
105void gmicImageToPaintDevice(const KisQMicImage &srcGmicImage,
107 KisSelectionSP selection,
108 const QRect &dstRect)
109{
110 dbgPlugins << "KisQmicImportTools::gmicImageToPaintDevice()" << dstRect;
111
112 if (selection) {
114 KisQmicSimpleConvertor::convertFromGmicFast(srcGmicImage, src, 255.0f);
115 KisPainter painter(dst, selection);
117 painter.bitBlt(dstRect.topLeft(),
118 src,
119 QRect(QPoint(0, 0), dstRect.size()));
120 } else {
121 KisQmicSimpleConvertor::convertFromGmicFast(srcGmicImage, dst, 255.0f);
122 }
123}
124
126inputNodes(KisImageSP image, InputLayerMode inputMode, KisNodeSP currentNode)
127{
128 /*
129 ACTIVE_LAYER,
130 ALL_LAYERS,
131 ACTIVE_LAYER_BELOW_LAYER,
132 ACTIVE_LAYER_ABOVE_LAYER,
133 ALL_VISIBLE_LAYERS,
134 ALL_INVISIBLE_LAYERS,
135 ALL_VISIBLE_LAYERS_DECR,
136 ALL_INVISIBLE_DECR,
137 ALL_DECR
138 */
139 const auto isAvailable = [](KisNodeSP node) -> bool {
140 auto *paintLayer = dynamic_cast<KisPaintLayer *>(node.data());
141 return paintLayer && paintLayer->visible(false);
142 };
143
144 KisNodeListSP result(new QList<KisNodeSP>());
145 switch (inputMode) {
147 break;
148 }
150 if (isAvailable(currentNode)) {
151 result->prepend(currentNode);
152 }
153 break; // drop down in case of one more layer modes
154 }
155 case InputLayerMode::All: {
156 result = [&]() {
159 image->root(),
160 [&](KisNodeSP item) {
161 auto *paintLayer =
162 dynamic_cast<KisPaintLayer *>(item.data());
163 if (paintLayer) {
164 r->prepend(item);
165 }
166 });
167 return r;
168 }();
169 break;
170 }
172 if (isAvailable(currentNode)) {
173 result->prepend(currentNode);
174 if (isAvailable(currentNode->prevSibling())) {
175 result->prepend(currentNode->prevSibling());
176 }
177 }
178 break;
179 }
181 if (isAvailable(currentNode)) {
182 result->prepend(currentNode);
183 if (isAvailable(currentNode->nextSibling())) {
184 result->prepend(currentNode->nextSibling());
185 }
186 }
187 break;
188 }
191 const bool visibility = (inputMode == InputLayerMode::AllInvisible);
192
193 result = [&]() {
196 image->root(),
197 [&](KisNodeSP item) {
198 auto *paintLayer =
199 dynamic_cast<KisPaintLayer *>(item.data());
200 if (paintLayer
201 && paintLayer->visible(false) == visibility) {
202 r->prepend(item);
203 }
204 });
205 return r;
206 }();
207 break;
208 }
210 default: {
211 qWarning()
212 << "Inputmode" << static_cast<int>(inputMode)
213 << "must be specified by GMic or is not implemented in Krita";
214 break;
215 }
216 }
217 return result;
218}
219} // namespace KisQmicImportTools
const QString COMPOSITE_COPY
The command for setting the composite op.
The command for setting the node opacity.
The command for setting the node's name.
QRect exactBounds() const
const KoColorSpace * colorSpace() const
void bitBlt(qint32 dstX, qint32 dstY, const KisPaintDeviceSP srcDev, qint32 srcX, qint32 srcY, qint32 srcWidth, qint32 srcHeight)
void setCompositeOpId(const KoCompositeOp *op)
static void convertFromGmicFast(const KisQMicImage &gmicImage, KisPaintDeviceSP dst, float gmicUnitValue)
Fast versions.
static QString stringToBlendingMode(QString str)
InputLayerMode
Definition gmic.h:21
#define dbgPlugins
Definition kis_debug.h:51
void recursiveApplyNodes(NodePointer node, Functor func)
KUndo2Command * applyLayerNameChanges(const KisQMicImage &srcGmicImage, KisNode *node, KisSelectionSP selection)
void gmicImageToPaintDevice(const KisQMicImage &srcGmicImage, KisPaintDeviceSP dst, KisSelectionSP selection, const QRect &dstRect)
KisNodeListSP inputNodes(KisImageSP image, InputLayerMode inputMode, KisNodeSP currentNode)
virtual qint32 y() const
virtual qint32 x() const
virtual KisPaintDeviceSP paintDevice() const =0
QString name() const
virtual bool visible(bool recursive=false) const
KisNodeSP prevSibling() const
Definition kis_node.cpp:402
KisNodeSP nextSibling() const
Definition kis_node.cpp:408