Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_color_to_alpha.cpp
Go to the documentation of this file.
1/*
2 * This file is part of Krita
3 *
4 * SPDX-FileCopyrightText: 2006 Cyrille Berger <cberger@cberger.net>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
10#include <QCheckBox>
11#include <QSpinBox>
12
13#include <KoColorSpaceMaths.h>
14#include <KoConfig.h>
15#include <KoUpdater.h>
16
18#include <kis_paint_device.h>
19#include <kis_selection.h>
23
24#include "ui_wdgcolortoalphabase.h"
26#include <kis_iterator_ng.h>
28
37
39{
40 return new KisWdgColorToAlpha(parent);
41}
42
44{
45 KisFilterConfigurationSP config = factoryConfiguration(resourcesInterface);
46 config->setProperty("targetcolor", QColor(255, 255, 255));
47 config->setProperty("threshold", 100);
48 return config;
49}
50
51template<typename channel_type, typename composite_type>
52inline void inverseOver(const int numChannels, const int *channelIndex,
53 channel_type *dst, const channel_type *baseColor,
54 qreal dstOpacity)
55{
56 if (qFuzzyIsNull(dstOpacity)) {
57 return;
58 }
59
60 for (int i = 0; i < numChannels; i++) {
61 const int idx = channelIndex[i];
62 dst[idx] =
64 (static_cast<composite_type>(dst[idx]) - baseColor[idx]) / dstOpacity + baseColor[idx]);
65 }
66}
67
68template<typename channel_type, typename composite_type>
69void applyToIterator(const int numChannels, const int *channelIndex,
71 int threshold, const KoColorSpace *cs)
72{
73 qreal thresholdF = threshold;
74 quint8 *baseColorData_uint8 = baseColor.data();
75 channel_type *baseColorData = reinterpret_cast<channel_type*>(baseColorData_uint8);
76
77 while (it.nextPixel()) {
78 channel_type *dst = reinterpret_cast<channel_type*>(it.rawData());
79 quint8 *dst_uint8 = it.rawData();
80
81 quint8 diff = cs->difference(baseColorData_uint8, dst_uint8);
82
83 qreal newOpacity = diff >= threshold ? 1.0 : diff / thresholdF;
84
85 if(newOpacity < cs->opacityF(dst_uint8)) {
86 cs->setOpacity(dst_uint8, newOpacity, 1);
87 }
88
89 inverseOver<channel_type, composite_type>(numChannels, channelIndex,
90 dst, baseColorData,
91 newOpacity);
92 }
93}
94
96 const QRect& rect,
97 const KisFilterConfigurationSP config,
98 KoUpdater* progressUpdater
99 ) const
100{
101 Q_ASSERT(device != 0);
103
104 QVariant value;
105 QColor cTA = (config->getProperty("targetcolor", value)) ? value.value<QColor>() : QColor(255, 255, 255);
106 int threshold = (config->getProperty("threshold", value)) ? value.toInt() : 1;
107
108 const KoColorSpace * cs = device->colorSpace();
109
110 KisSequentialIteratorProgress it(device, rect, progressUpdater);
111 KoColor baseColor(cTA, cs);
112
113 QVector<int> channelIndex;
115
116 const QList<KoChannelInfo*> channels = cs->channels();
117
118 for (int i = 0; i < channels.size(); i++) {
119 const KoChannelInfo *info = channels[i];
120
121 if (info->channelType() != KoChannelInfo::COLOR) continue;
122
123 KoChannelInfo::enumChannelValueType currentValueType =
124 info->channelValueType();
125
126 if (valueType != KoChannelInfo::OTHER &&
127 valueType != currentValueType) {
128
129 warnKrita << "Cannot apply a Color-to-Alpha filter to a heterogeneous colorspace";
130 return;
131 } else {
132 valueType = currentValueType;
133 }
134
135 channelIndex.append(i);
136 }
137
138 switch (valueType) {
140 applyToIterator<quint8, qint16>(channelIndex.size(), channelIndex.data(),
141 it, baseColor,
142 threshold, cs);
143 break;
145 applyToIterator<quint16, qint32>(channelIndex.size(), channelIndex.data(),
146 it, baseColor,
147 threshold, cs);
148 break;
150 applyToIterator<quint32, qint64>(channelIndex.size(), channelIndex.data(),
151 it, baseColor,
152 threshold, cs);
153 break;
154
156 applyToIterator<float, float>(channelIndex.size(), channelIndex.data(),
157 it, baseColor,
158 threshold, cs);
159 break;
161 applyToIterator<double, double>(channelIndex.size(), channelIndex.data(),
162 it, baseColor,
163 threshold, cs);
164 break;
166#ifdef HAVE_OPENEXR
167#include <half.h>
168 applyToIterator<half, half>(channelIndex.size(), channelIndex.data(),
169 it, baseColor,
170 threshold, cs);
171 break;
172
173#endif
174 case KoChannelInfo::INT8: /* !UNSUPPORTED! */
175 case KoChannelInfo::INT16: /* !UNSUPPORTED! */
176
178 warnKrita << "Color To Alpha: Unsupported channel type:" << valueType;
179 }
180}
float value(const T *src, size_t ch)
@ FULLY_INDEPENDENT
KisFilterConfigurationSP defaultConfiguration(KisResourcesInterfaceSP resourcesInterface) const override
KisConfigWidget * createConfigurationWidget(QWidget *parent, const KisPaintDeviceSP dev, bool useForMasks) const override
void processImpl(KisPaintDeviceSP device, const QRect &rect, const KisFilterConfigurationSP config, KoUpdater *progressUpdater) const override
void setSupportsLevelOfDetail(bool value)
const KoColorSpace * colorSpace() const
ALWAYS_INLINE quint8 * rawData()
@ COLOR
The channel represents a color.
enumChannelValueType
enum to define the value of the channel
@ UINT8
use this for an unsigned integer 8bits channel
@ UINT16
use this for an integer 16bits channel
@ OTHER
Use this if the channel is neither an integer or a float.
@ INT16
use this for an integer 16bits channel
@ INT8
use this for an integer 8bits channel
@ FLOAT32
use this for a float 32bits channel
@ FLOAT16
use this for a float 16bits channel
@ UINT32
use this for an unsigned integer 21bits channel
@ FLOAT64
use this for a float 64bits channel
enumChannelType channelType() const
enumChannelValueType channelValueType() const
static dst_compositetype clamp(dst_compositetype val)
virtual quint8 difference(const quint8 *src1, const quint8 *src2) const =0
virtual void setOpacity(quint8 *pixels, quint8 alpha, qint32 nPixels) const =0
QList< KoChannelInfo * > channels
quint8 * data()
Definition KoColor.h:144
static bool qFuzzyIsNull(half h)
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
void applyToIterator(const int numChannels, const int *channelIndex, KisSequentialIteratorProgress &it, KoColor baseColor, int threshold, const KoColorSpace *cs)
void inverseOver(const int numChannels, const int *channelIndex, channel_type *dst, const channel_type *baseColor, qreal dstOpacity)
#define warnKrita
Definition kis_debug.h:94
const KoID FiltersCategoryColorId("color_filters", ki18nc("The category of color transfer filters, like color to alpha. Noun.", "Colors"))
virtual KisFilterConfigurationSP factoryConfiguration(KisResourcesInterfaceSP resourcesInterface) const
void setSupportsAdjustmentLayers(bool v)
void setSupportsPainting(bool v)
void setColorSpaceIndependence(ColorSpaceIndependence v)