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 for (int i = 0; i < numChannels; i++) {
57 const int idx = channelIndex[i];
58 dst[idx] =
60 (static_cast<composite_type>(dst[idx]) - baseColor[idx]) / dstOpacity + baseColor[idx]);
61 }
62}
63
64template<typename channel_type, typename composite_type>
65void applyToIterator(const int numChannels, const int *channelIndex,
67 int threshold, const KoColorSpace *cs)
68{
69 qreal thresholdF = threshold;
70 quint8 *baseColorData_uint8 = baseColor.data();
71 channel_type *baseColorData = reinterpret_cast<channel_type*>(baseColorData_uint8);
72
73 while (it.nextPixel()) {
74 channel_type *dst = reinterpret_cast<channel_type*>(it.rawData());
75 quint8 *dst_uint8 = it.rawData();
76
77 quint8 diff = cs->difference(baseColorData_uint8, dst_uint8);
78
79 qreal newOpacity = diff >= threshold ? 1.0 : diff / thresholdF;
80
81 if(newOpacity < cs->opacityF(dst_uint8)) {
82 cs->setOpacity(dst_uint8, newOpacity, 1);
83 }
84
85 inverseOver<channel_type, composite_type>(numChannels, channelIndex,
86 dst, baseColorData,
87 newOpacity);
88 }
89}
90
92 const QRect& rect,
93 const KisFilterConfigurationSP config,
94 KoUpdater* progressUpdater
95 ) const
96{
97 Q_ASSERT(device != 0);
99
100 QVariant value;
101 QColor cTA = (config->getProperty("targetcolor", value)) ? value.value<QColor>() : QColor(255, 255, 255);
102 int threshold = (config->getProperty("threshold", value)) ? value.toInt() : 1;
103
104 const KoColorSpace * cs = device->colorSpace();
105
106 KisSequentialIteratorProgress it(device, rect, progressUpdater);
107 KoColor baseColor(cTA, cs);
108
109 QVector<int> channelIndex;
111
112 const QList<KoChannelInfo*> channels = cs->channels();
113
114 for (int i = 0; i < channels.size(); i++) {
115 const KoChannelInfo *info = channels[i];
116
117 if (info->channelType() != KoChannelInfo::COLOR) continue;
118
119 KoChannelInfo::enumChannelValueType currentValueType =
120 info->channelValueType();
121
122 if (valueType != KoChannelInfo::OTHER &&
123 valueType != currentValueType) {
124
125 warnKrita << "Cannot apply a Color-to-Alpha filter to a heterogeneous colorspace";
126 return;
127 } else {
128 valueType = currentValueType;
129 }
130
131 channelIndex.append(i);
132 }
133
134 switch (valueType) {
136 applyToIterator<quint8, qint16>(channelIndex.size(), channelIndex.data(),
137 it, baseColor,
138 threshold, cs);
139 break;
141 applyToIterator<quint16, qint32>(channelIndex.size(), channelIndex.data(),
142 it, baseColor,
143 threshold, cs);
144 break;
146 applyToIterator<quint32, qint64>(channelIndex.size(), channelIndex.data(),
147 it, baseColor,
148 threshold, cs);
149 break;
150
152 applyToIterator<float, float>(channelIndex.size(), channelIndex.data(),
153 it, baseColor,
154 threshold, cs);
155 break;
157 applyToIterator<double, double>(channelIndex.size(), channelIndex.data(),
158 it, baseColor,
159 threshold, cs);
160 break;
162#ifdef HAVE_OPENEXR
163#include <half.h>
164 applyToIterator<half, half>(channelIndex.size(), channelIndex.data(),
165 it, baseColor,
166 threshold, cs);
167 break;
168
169#endif
170 case KoChannelInfo::INT8: /* !UNSUPPORTED! */
171 case KoChannelInfo::INT16: /* !UNSUPPORTED! */
172
174 warnKrita << "Color To Alpha: Unsupported channel type:" << valueType;
175 }
176}
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
#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:87
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)