Krita Source Code Documentation
Loading...
Searching...
No Matches
ColorizeMask.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6#include "ColorizeMask.h"
9#include <kis_image.h>
10#include "Selection.h"
11#include <kis_selection.h>
12#include <KoColor.h>
14#include <kis_transaction.h>
16#include <kis_undo_stores.h>
17#include <kis_default_bounds.h>
18
19
20
21ColorizeMask::ColorizeMask(KisImageSP image, QString name, QObject *parent) :
22 Node(image, new KisColorizeMask(image,name), parent)
23{
24
25}
26
28 Node(image, mask, parent)
29{
30}
31
36
37
39{
40 QList<ManagedColor*> colorList;
41 const KisColorizeMask *mask = qobject_cast<const KisColorizeMask*>(this->node());
43
44 for (KoColor color : mask->keyStrokesColors().colors) {
45 colorList.append(new ManagedColor(color));
46 }
47
48 return colorList;
49}
50
52{
53 KisColorizeMaskSP mask = qobject_cast<KisColorizeMask*>(this->node().data());
55
66
68 delete mask->setColorSpace(mask->parent()->colorSpace());
69
71
72 for (int i = 0; i < colors.size(); i++) {
74 keyStroke.color = colors[i]->color();
75 keyStroke.dev = new KisPaintDevice(KoColorSpaceRegistry::instance()->alpha8());
76 keyStroke.dev->setDefaultBounds(new KisDefaultBounds(this->node()->image()));
77 keyStroke.isTransparent = transparentIndex == i;
78 // NOTE: the parent node link is initialized in
79 // setKeyStrokesDirect
80
81 keyStrokes.append(keyStroke);
82 }
83
84 mask->setKeyStrokesDirect(keyStrokes);
85}
86
87
89{
90 const KisColorizeMask *mask = qobject_cast<const KisColorizeMask*>(this->node());
92
94}
95
97{
98 const KoColor kc = color->color();
99 KisColorizeMask *mask = qobject_cast<KisColorizeMask*>(this->node().data());
101
102 mask->removeKeyStroke(kc);
103}
104
105QByteArray ColorizeMask::keyStrokePixelData(ManagedColor* color, int x, int y, int w, int h) const
106{
107 QByteArray ba;
108
109 if (!this->node()) return ba;
110
111 const KoColor kc = color->color();
112 const KisColorizeMask *mask = qobject_cast<const KisColorizeMask*>(this->node());
113 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(mask, QByteArray());
114
115 for (KisLazyFillTools::KeyStroke keystroke : mask->fetchKeyStrokesDirect()) {
116 if (kc == keystroke.color) {
117 KisPaintDeviceSP dev = keystroke.dev;
118
119 if (!dev) return ba;
120
121 ba.resize(w * h * dev->pixelSize());
122 dev->readBytes(reinterpret_cast<quint8*>(ba.data()), x, y, w, h);
123 return ba;
124 }
125 }
126
127 return ba;
128}
129
130bool ColorizeMask::setKeyStrokePixelData(QByteArray value, ManagedColor* color, int x, int y, int w, int h)
131{
132 if (!this->node()) return false;
133
134 const KoColor kc = color->color();
135 const KisColorizeMask *mask = qobject_cast<const KisColorizeMask*>(this->node());
137
138 for (KisLazyFillTools::KeyStroke keystroke : mask->fetchKeyStrokesDirect()) {
139 if (kc == keystroke.color) {
140 KisPaintDeviceSP dev = keystroke.dev;
141
142 if (!dev) return false;
143 if (value.length() < w * h * (int)dev->colorSpace()->pixelSize()) {
144 qWarning() << "ColorizeMask::setKeyStrokePixelData: not enough data to write to the paint device";
145 return false;
146 }
147 dev->writeBytes((const quint8*)value.constData(), x, y, w, h);
148 return true;
149 }
150 }
151
152 return false;
153}
154
156{
157 KisColorizeMask *mask = qobject_cast<KisColorizeMask*>(this->node().data());
159
161}
162
164{
165 const KisColorizeMask *mask = qobject_cast<const KisColorizeMask*>(this->node());
167
168 return mask->useEdgeDetection();
169}
170
172{
173 KisColorizeMask *mask = qobject_cast<KisColorizeMask*>(this->node().data());
175
177}
178
180{
181 const KisColorizeMask *mask = qobject_cast<const KisColorizeMask*>(this->node());
183
184 return mask->edgeDetectionSize();
185}
186
188{
189 KisColorizeMask *mask = qobject_cast<KisColorizeMask*>(this->node().data());
191
192 mask->setCleanUpAmount(value);
193}
194
196{
197 const KisColorizeMask *mask = qobject_cast<const KisColorizeMask*>(this->node());
199
200 return mask->cleanUpAmount();
201}
202
204{
205 KisColorizeMask *mask = qobject_cast<KisColorizeMask*>(this->node().data());
207
209}
210
212{
213 const KisColorizeMask *mask = qobject_cast<const KisColorizeMask*>(this->node());
215
216 return mask->limitToDeviceBounds();
217}
218
220{
221 KisColorizeMask *mask = qobject_cast<KisColorizeMask*>(this->node().data());
223
224 if (force) {
225 mask->forceRegenerateMask();
226 } else {
228 }
229}
230
232{
233 KisColorizeMask *mask = qobject_cast<KisColorizeMask*>(this->node().data());
235
236 mask->resetCache();
237}
238
240{
241 KisColorizeMask *mask = qobject_cast<KisColorizeMask*>(this->node().data());
243
245}
246
248{
249 const KisColorizeMask *mask = qobject_cast<const KisColorizeMask*>(this->node());
251
253
254 for (const KisBaseNode::Property &prop : props) {
256 return prop.state.toBool();
257 }
258 }
259
260 return false;
261}
262
264{
265 KisColorizeMask *mask = qobject_cast<KisColorizeMask*>(this->node().data());
267
269}
270
272{
273 const KisColorizeMask *mask = qobject_cast<const KisColorizeMask*>(this->node());
275
277
278 for (const KisBaseNode::Property &prop : props) {
280 return prop.state.toBool();
281 }
282 }
283
284 return false;
285}
286
287QString ColorizeMask::type() const
288{
289 return "colorizemask";
290}
float value(const T *src, size_t ch)
bool useEdgeDetection() const
useEdgeDetection
QByteArray keyStrokePixelData(ManagedColor *color, int x, int y, int w, int h) const
keyStrokePixelData reads the given rectangle from the keystroke image data and returns it as a byte a...
void setEditKeyStrokes(bool enabled)
setEditKeyStrokes Toggle Colorize Mask's edit keystrokes mode.
bool showOutput() const
showOutput Show output mode allows the user to see the result of the Colorize Mask's algorithm.
bool editKeyStrokes() const
editKeyStrokes Edit keystrokes mode allows the user to modify keystrokes on the active Colorize Mask.
void setEdgeDetectionSize(qreal value)
setEdgeDetectionSize Set the value to the thinnest line on the image.
int transparencyIndex() const
transparencyIndex Index of the transparent color.
qreal cleanUpAmount() const
cleanUpAmount
qreal edgeDetectionSize() const
edgeDetectionSize
void setShowOutput(bool enabled)
setShowOutput Toggle Colorize Mask's show output mode.
void setCleanUpAmount(qreal value)
setCleanUpAmount This will attempt to handle messy strokes that overlap the line art where they shoul...
QList< ManagedColor * > keyStrokesColors() const
keyStrokesColors Colors used in the Colorize Mask's keystrokes.
bool setKeyStrokePixelData(QByteArray value, ManagedColor *color, int x, int y, int w, int h)
setKeyStrokePixelData writes the given bytes, of which there must be enough, into the keystroke,...
void setLimitToDeviceBounds(bool value)
setLimitToDeviceBounds Limit the colorize mask to the combined layer bounds of the strokes and the li...
void updateMask(bool force=false)
updateMask Process the Colorize Mask's keystrokes and generate a projection of the computed colors.
bool limitToDeviceBounds() const
limitToDeviceBounds
void setUseEdgeDetection(bool value)
setUseEdgeDetection Activate this for line art with large solid areas, for example shadows on an obje...
void initializeKeyStrokeColors(QList< ManagedColor * > colors, int transparentIndex=-1)
initializeKeyStrokeColors Set the colors to use for the Colorize Mask's keystrokes.
virtual QString type() const override
type Krita has several types of nodes, split in layers and masks. Group layers can contain other laye...
~ColorizeMask() override
void removeKeyStroke(ManagedColor *color)
removeKeyStroke Remove a color from the Colorize Mask's keystrokes.
KisBaseNode::PropertyList sectionModelProperties() const override
bool limitToDeviceBounds() const
qreal cleanUpAmount() const
void setKeyStrokesDirect(const QList< KisLazyFillTools::KeyStroke > &strokes)
void setEdgeDetectionSize(qreal value)
void setLimitToDeviceBounds(bool value)
void setUseEdgeDetection(bool value)
bool useEdgeDetection() const
void removeKeyStroke(const KoColor &color)
KUndo2Command * setColorSpace(const KoColorSpace *dstColorSpace, KoColorConversionTransformation::Intent renderingIntent=KoColorConversionTransformation::internalRenderingIntent(), KoColorConversionTransformation::ConversionFlags conversionFlags=KoColorConversionTransformation::internalConversionFlags(), KoUpdater *progressUpdater=nullptr)
KeyStrokeColors keyStrokesColors() const
QList< KisLazyFillTools::KeyStroke > fetchKeyStrokesDirect() const
void setCleanUpAmount(qreal value)
qreal edgeDetectionSize() const
static void setNodePropertyAutoUndo(KisNodeSP node, const KoID &id, const QVariant &value, KisImageSP image)
quint32 pixelSize() const
void setDefaultBounds(KisDefaultBoundsBaseSP bounds)
const KoColorSpace * colorSpace() const
void readBytes(quint8 *data, qint32 x, qint32 y, qint32 w, qint32 h) const
void writeBytes(const quint8 *data, qint32 x, qint32 y, qint32 w, qint32 h)
virtual quint32 pixelSize() const =0
QString id() const
Definition KoID.cpp:63
The ManagedColor class is a class to handle colors that are color managed. A managed color is a color...
KoColor color() const
Definition Node.h:24
KisNodeSP node() const
Definition Node.cpp:827
KisImageSP image() const
Definition Node.cpp:822
friend class ColorizeMask
Definition Node.h:707
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
virtual const KoColorSpace * colorSpace() const =0
KisNodeWSP parent
Definition kis_node.cpp:86
static KoColorSpaceRegistry * instance()