1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/* This file is part of the KDE project
* Copyright (c) 2009 Jan Hambrecht <jaham@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "BlendEffect.h"
#include "ColorChannelConversion.h"
#include <KoFilterEffectRenderContext.h>
#include <KoXmlWriter.h>
#include <KoXmlReader.h>
#include <klocalizedstring.h>
#include <QRect>
#include <QImage>
#include <math.h>
BlendEffect::BlendEffect()
: KoFilterEffect(BlendEffectId, i18n("Blend"))
, m_blendMode(Normal)
{
setRequiredInputCount(2);
setMaximalInputCount(2);
}
BlendEffect::BlendMode BlendEffect::blendMode() const
{
return m_blendMode;
}
void BlendEffect::setBlendMode(BlendMode blendMode)
{
m_blendMode = blendMode;
}
QImage BlendEffect::processImage(const QImage &image, const KoFilterEffectRenderContext &context) const
{
Q_UNUSED(context);
return image;
}
QImage BlendEffect::processImages(const QVector<QImage> &images, const KoFilterEffectRenderContext &context) const
{
int imageCount = images.count();
if (!imageCount)
return QImage();
QImage result = images[0];
if (images.count() != 2) {
return result;
}
const QRgb *src = (const QRgb*)images[1].constBits();
QRgb *dst = (QRgb*)result.bits();
int w = result.width();
qreal sa, sr, sg, sb;
qreal da, dr, dg, db;
int pixel = 0;
QRect roi = context.filterRegion().toRect();
for (int row = roi.top(); row < roi.bottom(); ++row) {
for (int col = roi.left(); col < roi.right(); ++col) {
pixel = row * w + col;
const QRgb &s = src[pixel];
QRgb &d = dst[pixel];
sa = fromIntColor[qAlpha(s)];
sr = fromIntColor[qRed(s)];
sg = fromIntColor[qGreen(s)];
sb = fromIntColor[qBlue(s)];
da = fromIntColor[qAlpha(d)];
dr = fromIntColor[qRed(d)];
dg = fromIntColor[qGreen(d)];
db = fromIntColor[qBlue(d)];
switch (m_blendMode) {
case Normal:
dr = (qreal(1.0) - da) * sr + dr;
dg = (qreal(1.0) - da) * sg + dg;
db = (qreal(1.0) - da) * sb + db;
break;
case Multiply:
dr = (qreal(1.0) - da) * sr + (qreal(1.0) - sa) * dr + dr * sr;
dg = (qreal(1.0) - da) * sg + (qreal(1.0) - sa) * dg + dg * sg;
db = (qreal(1.0) - da) * sb + (qreal(1.0) - sa) * db + db * sb;
break;
case Screen:
dr = sr + dr - dr * sr;
dg = sg + dg - dg * sg;
db = sb + db - db * sb;
break;
case Darken:
dr = qMin((qreal(1.0) - da) * sr + dr, (qreal(1.0) - sa) * dr + sr);
dg = qMin((qreal(1.0) - da) * sg + dg, (qreal(1.0) - sa) * dg + sg);
db = qMin((qreal(1.0) - da) * sb + db, (qreal(1.0) - sa) * db + sb);
break;
case Lighten:
dr = qMax((qreal(1.0) - da) * sr + dr, (qreal(1.0) - sa) * dr + sr);
dg = qMax((qreal(1.0) - da) * sg + dg, (qreal(1.0) - sa) * dg + sg);
db = qMax((qreal(1.0) - da) * sb + db, (qreal(1.0) - sa) * db + sb);
break;
}
da = qreal(1.0) - (qreal(1.0) - da) * (qreal(1.0) - sa);
d = qRgba(static_cast<quint8>(qBound(qreal(0.0), dr * qreal(255.0), qreal(255.0))),
static_cast<quint8>(qBound(qreal(0.0), dg * qreal(255.0), qreal(255.0))),
static_cast<quint8>(qBound(qreal(0.0), db * qreal(255.0), qreal(255.0))),
static_cast<quint8>(qBound(qreal(0.0), da * qreal(255.0), qreal(255.0))));
}
}
return result;
}
bool BlendEffect::load(const KoXmlElement &element, const KoFilterEffectLoadingContext &)
{
if (element.tagName() != id())
return false;
m_blendMode = Normal; // default blend mode
QString modeStr = element.attribute("mode");
if (!modeStr.isEmpty()) {
if (modeStr == "multiply")
m_blendMode = Multiply;
else if (modeStr == "screen")
m_blendMode = Screen;
else if (modeStr == "darken")
m_blendMode = Darken;
else if (modeStr == "lighten")
m_blendMode = Lighten;
}
if (element.hasAttribute("in2")) {
if (inputs().count() == 2)
setInput(1, element.attribute("in2"));
else
addInput(element.attribute("in2"));
}
return true;
}
void BlendEffect::save(KoXmlWriter &writer)
{
writer.startElement(BlendEffectId);
saveCommonAttributes(writer);
switch (m_blendMode) {
case Normal:
writer.addAttribute("mode", "normal");
break;
case Multiply:
writer.addAttribute("mode", "multiply");
break;
case Screen:
writer.addAttribute("mode", "screen");
break;
case Darken:
writer.addAttribute("mode", "darken");
break;
case Lighten:
writer.addAttribute("mode", "lighten");
break;
}
writer.addAttribute("in2", inputs().at(1));
writer.endElement();
}
|