2
* This file is part of Krita
4
* Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include "kis_color_to_alpha.h"
22
#include <qcheckbox.h>
25
#include <kcolorbutton.h>
27
#include <KoProgressUpdater.h>
28
#include <KoUpdater.h>
30
#include <kis_iterators_pixel.h>
31
#include <kis_paint_device.h>
32
#include <kis_selection.h>
33
#include <filter/kis_filter_configuration.h>
34
#include <kis_processing_information.h>
36
#include "ui_wdgcolortoalphabase.h"
37
#include "kis_wdg_color_to_alpha.h"
39
KisFilterColorToAlpha::KisFilterColorToAlpha() : KisFilter(id(), categoryColors(), i18n("&Color to Alpha..."))
41
setSupportsPainting(true);
42
setSupportsPreview(true);
43
setSupportsIncrementalPainting(false);
44
setSupportsAdjustmentLayers(false);
45
setColorSpaceIndependence(FULLY_INDEPENDENT);
48
KisConfigWidget * KisFilterColorToAlpha::createConfigurationWidget(QWidget* parent, const KisPaintDeviceSP, const KisImageWSP image) const
51
return new KisWdgColorToAlpha(parent);
54
KisFilterConfiguration* KisFilterColorToAlpha::factoryConfiguration(const KisPaintDeviceSP) const
56
KisFilterConfiguration* config = new KisFilterConfiguration("colortoalpha", 1);
57
config->setProperty("targetcolor", QColor(255, 255, 255));
58
config->setProperty("threshold", 0);
62
void KisFilterColorToAlpha::process(KisConstProcessingInformation srcInfo,
63
KisProcessingInformation dstInfo,
65
const KisFilterConfiguration* config,
66
KoUpdater* progressUpdater
69
const KisPaintDeviceSP src = srcInfo.paintDevice();
70
KisPaintDeviceSP dst = dstInfo.paintDevice();
71
QPoint dstTopLeft = dstInfo.topLeft();
72
QPoint srcTopLeft = srcInfo.topLeft();
76
if (config == 0) config = new KisFilterConfiguration("colortoalpha", 1);
79
QColor cTA = (config->getProperty("targetcolor", value)) ? value.value<QColor>() : QColor(255, 255, 255);
80
int threshold = (config->getProperty("threshold", value)) ? value.toInt() : 0;
81
qreal thresholdF = threshold;
83
KisRectIteratorPixel dstIt = dst->createRectIterator(dstTopLeft.x(), dstTopLeft.y(), size.width(), size.height(), dstInfo.selection());
84
KisRectConstIteratorPixel srcIt = src->createRectConstIterator(srcTopLeft.x(), srcTopLeft.y(), size.width(), size.height(), srcInfo.selection());
86
int totalCost = size.width() * size.height() / 100;
87
if (totalCost == 0) totalCost = 1;
88
int currentProgress = 0;
90
const KoColorSpace * cs = src->colorSpace();
91
qint32 pixelsize = cs->pixelSize();
93
quint8* color = new quint8[pixelsize];
94
cs->fromQColor(cTA, color);
96
while (! srcIt.isDone()) {
97
if (srcIt.isSelected()) {
98
quint8 d = cs->difference(color, srcIt.oldRawData());
100
cs->setOpacity(dstIt.rawData(), 1.0, 1);
102
cs->setOpacity(dstIt.rawData(), d / thresholdF, 1);
105
if (progressUpdater) progressUpdater->setProgress((++currentProgress) / totalCost);