KoCompositeOp.cpp 5.74 KB
Newer Older
1 2 3
/*
 *  Copyright (c) 2005 Adrian Page <adrian@pagenet.plus.com>
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9 10 11
 * 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
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15 16 17 18
 * 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.
*/
19

20 21
#include "KoCompositeOp.h"

22
#include <klocalizedstring.h>
23 24
#include <KoID.h>
#include <QList>
25

26
#include "KoColorSpace.h"
27
#include "KoColorSpaceMaths.h"
28

29 30
QString KoCompositeOp::categoryColor()
{
31 32
    return i18n("Color");
}
33 34

QString KoCompositeOp::categoryArithmetic() { return i18n("Arithmetic"); }
35 36
QString KoCompositeOp::categoryBinary()     { return i18n("Binary");     }
QString KoCompositeOp::categoryModulo()     { return i18n("Modulo");     }
37 38 39 40 41 42 43 44 45
QString KoCompositeOp::categoryNegative()   { return i18n("Negative");   }
QString KoCompositeOp::categoryLight()      { return i18n("Lighten");    }
QString KoCompositeOp::categoryDark()       { return i18n("Darken");     }
QString KoCompositeOp::categoryHSY()        { return i18n("HSY");        }
QString KoCompositeOp::categoryHSI()        { return i18n("HSI");        }
QString KoCompositeOp::categoryHSL()        { return i18n("HSL");        }
QString KoCompositeOp::categoryHSV()        { return i18n("HSV");        }
QString KoCompositeOp::categoryMix()        { return i18n("Mix");        }
QString KoCompositeOp::categoryMisc()       { return i18n("Misc");       }
46
QString KoCompositeOp::categoryQuadratic()  { return i18n("Quadratic");  }
47

48
KoCompositeOp::ParameterInfo::ParameterInfo()
49 50 51
    : opacity(1.0f)
    , flow(1.0f)
    , lastOpacity(&opacity)
52 53 54 55 56 57 58 59 60 61 62 63 64 65
{
}

KoCompositeOp::ParameterInfo::ParameterInfo(const ParameterInfo &rhs)
{
    copy(rhs);
}

KoCompositeOp::ParameterInfo& KoCompositeOp::ParameterInfo::operator=(const ParameterInfo &rhs)
{
    copy(rhs);
    return *this;
}

66 67 68 69 70 71 72 73 74 75 76 77
void KoCompositeOp::ParameterInfo::setOpacityAndAverage(float _opacity, float _averageOpacity)
{
    if (qFuzzyCompare(_opacity, _averageOpacity)) {
        opacity = _opacity;
        lastOpacity = &opacity;
    } else {
        opacity = _opacity;
        _lastOpacityData = _averageOpacity;
        lastOpacity = &_lastOpacityData;
    }
}

78 79
void KoCompositeOp::ParameterInfo::copy(const ParameterInfo &rhs)
{
80 81 82 83 84 85 86 87 88 89 90 91 92
    dstRowStart = rhs.dstRowStart;
    dstRowStride = rhs.dstRowStride;
    srcRowStart = rhs.srcRowStart;
    srcRowStride = rhs.srcRowStride;
    maskRowStart = rhs.maskRowStart;
    maskRowStride = rhs.maskRowStride;
    rows = rhs.rows;
    cols = rhs.cols;
    opacity = rhs.opacity;
    flow = rhs.flow;
    _lastOpacityData = rhs._lastOpacityData;
    channelFlags = rhs.channelFlags;

93 94 95 96
    lastOpacity = rhs.lastOpacity == &rhs.opacity ?
        &opacity : &_lastOpacityData;
}

97 98 99 100 101 102 103 104 105 106 107 108 109
void KoCompositeOp::ParameterInfo::updateOpacityAndAverage(float value) {
    const float exponent = 0.1;

    opacity = value;

    if (*lastOpacity < opacity) {
        lastOpacity = &opacity;
    } else {
        _lastOpacityData = exponent * opacity + (1.0 - exponent) * (*lastOpacity);
        lastOpacity = &_lastOpacityData;
    }
}

110
struct Q_DECL_HIDDEN KoCompositeOp::Private {
111
    const KoColorSpace * colorSpace;
112 113
    QString id;
    QString description;
114
    QString category;
115 116 117 118
    QBitArray defaultChannelFlags;
};

KoCompositeOp::KoCompositeOp() : d(new Private)
119 120 121 122
{

}

123 124 125 126
KoCompositeOp::~KoCompositeOp()
{
    delete d;
}
127

128
KoCompositeOp::KoCompositeOp(const KoColorSpace * cs, const QString& id,  const QString& description, const QString & category)
129
        : d(new Private)
130
{
131 132 133
    d->colorSpace = cs;
    d->id = id;
    d->description = description;
134
    d->category = category;
135
    if (d->category.isEmpty()) {
136 137
        d->category = categoryMisc();
    }
138 139
}

140 141 142 143
void KoCompositeOp::composite(quint8 *dstRowStart, qint32 dstRowStride,
                              const quint8 *srcRowStart, qint32 srcRowStride,
                              const quint8 *maskRowStart, qint32 maskRowStride,
                              qint32 rows, qint32 numColumns,
144
                              quint8 opacity, const QBitArray& channelFlags) const
145 146 147 148 149 150 151 152 153 154
{
    KoCompositeOp::ParameterInfo params;
    params.dstRowStart   = dstRowStart;
    params.dstRowStride  = dstRowStride;
    params.srcRowStart   = srcRowStart;
    params.srcRowStride  = srcRowStride;
    params.maskRowStart  = maskRowStart;
    params.maskRowStride = maskRowStride;
    params.rows          = rows;
    params.cols          = numColumns;
155 156
    params.opacity       = float(opacity) / 255.0f;
    params.flow          = 1.0f;
157 158 159 160 161
    params.channelFlags  = channelFlags;
    composite(params);
}

void KoCompositeOp::composite(const KoCompositeOp::ParameterInfo& params) const
162
{
163 164
    using namespace Arithmetic;

165 166 167 168
    composite(params.dstRowStart           , params.dstRowStride ,
              params.srcRowStart           , params.srcRowStride ,
              params.maskRowStart          , params.maskRowStride,
              params.rows                  , params.cols         ,
169
              scale<quint8>(params.opacity), params.channelFlags );
170 171
}

172

173 174 175 176 177
QString KoCompositeOp::category() const
{
    return d->category;
}

178 179 180 181 182 183 184 185 186 187
QString KoCompositeOp::id() const
{
    return d->id;
}

QString KoCompositeOp::description() const
{
    return d->description;
}

188
const KoColorSpace * KoCompositeOp::colorSpace() const
189 190 191
{
    return d->colorSpace;
}