kis_onion_skin_compositor.cpp 7.27 KB
Newer Older
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
/*
 *  Copyright (c) 2015 Jouni Pentikäinen <joupent@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kis_onion_skin_compositor.h"

#include "kis_paint_device.h"
#include "kis_painter.h"
#include "KoColor.h"
#include "KoColorSpace.h"
#include "KoCompositeOpRegistry.h"
26
#include "KoColorSpaceConstants.h"
27 28

#include "kis_image_config.h"
29 30
#include "kis_raster_keyframe_channel.h"

Boudewijn Rempt's avatar
Boudewijn Rempt committed
31
Q_GLOBAL_STATIC(KisOnionSkinCompositor, s_instance)
Dmitry Kazakov's avatar
Dmitry Kazakov committed
32

33 34
struct KisOnionSkinCompositor::Private
{
35 36
    int numberOfSkins = 0;
    int tintFactor = 0;
37 38 39 40
    QColor backwardTintColor;
    QColor forwardTintColor;
    QVector<int> backwardOpacities;
    QVector<int> forwardOpacities;
41
    int configSeqNo = 0;
42
    QList<int> colorLabelFilter;
43 44 45

    int skinOpacity(int offset)
    {
46 47
        const QVector<int> &bo = backwardOpacities;
        const QVector<int> &fo = forwardOpacities;
48

49
        return offset > 0 ? fo[qAbs(offset) - 1] : bo[qAbs(offset) - 1];
50 51 52 53 54 55
    }

    KisPaintDeviceSP setUpTintDevice(const QColor &tintColor, const KoColorSpace *colorSpace)
    {
        KisPaintDeviceSP tintDevice = new KisPaintDevice(colorSpace);
        KoColor color = KoColor(tintColor, colorSpace);
56
        tintDevice->setDefaultPixel(color);
57 58 59
        return tintDevice;
    }

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    KisKeyframeSP getNextFrameToComposite(KisKeyframeChannel *channel, KisKeyframeSP keyframe, bool backwards)
    {
        while (!keyframe.isNull()) {
            keyframe = backwards ? channel->previousKeyframe(keyframe) : channel->nextKeyframe(keyframe);
            if (colorLabelFilter.isEmpty()) {
                return keyframe;
            } else if (!keyframe.isNull()) {
                if (colorLabelFilter.contains(keyframe->colorLabel())) {
                    return keyframe;
                }
            }
        }
        return keyframe;
    }

76
    void tryCompositeFrame(KisRasterKeyframeChannel *keyframes, KisKeyframeSP keyframe, KisPainter &gcFrame, KisPainter &gcDest, KisPaintDeviceSP tintSource, int opacity, const QRect &rect)
77
    {
78
        if (keyframe.isNull() || opacity == OPACITY_TRANSPARENT_U8) return;
79

80
        keyframes->fetchFrame(keyframe, gcFrame.device());
81

82 83 84 85
        gcFrame.bitBlt(rect.topLeft(), tintSource, rect);

        gcDest.setOpacity(opacity);
        gcDest.bitBlt(rect.topLeft(), gcFrame.device(), rect);
86 87 88 89
    }

    void refreshConfig()
    {
90
        KisImageConfig config(true);
91 92 93 94 95 96

        numberOfSkins = config.numberOfOnionSkins();
        tintFactor = config.onionSkinTintFactor();
        backwardTintColor = config.onionSkinTintColorBackward();
        forwardTintColor = config.onionSkinTintColorForward();

97 98 99
        backwardOpacities.resize(numberOfSkins);
        forwardOpacities.resize(numberOfSkins);

100 101
        const int mainState = (int) config.onionSkinState(0);
        const qreal scaleFactor = mainState * config.onionSkinOpacity(0) / 255.0;
102

103
        for (int i = 0; i < numberOfSkins; i++) {
104 105 106 107 108
            int backwardState = (int) config.onionSkinState(-(i + 1));
            int forwardState = (int) config.onionSkinState(i + 1);

            backwardOpacities[i] = scaleFactor * backwardState * config.onionSkinOpacity(-(i + 1));
            forwardOpacities[i] = scaleFactor * forwardState * config.onionSkinOpacity(i + 1);
109
        }
110 111

        configSeqNo++;
112 113 114 115 116 117 118 119 120 121 122 123 124 125
    }
};

KisOnionSkinCompositor *KisOnionSkinCompositor::instance()
{
    return s_instance;
}

KisOnionSkinCompositor::KisOnionSkinCompositor()
    : m_d(new Private)
{
    m_d->refreshConfig();
}

Jouni Pentikäinen's avatar
Jouni Pentikäinen committed
126 127 128
KisOnionSkinCompositor::~KisOnionSkinCompositor()
{}

129 130 131 132 133
int KisOnionSkinCompositor::configSeqNo() const
{
    return m_d->configSeqNo;
}

134 135 136 137 138
void KisOnionSkinCompositor::setColorLabelFilter(QList<int> colors)
{
    m_d->colorLabelFilter = colors;
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152
void KisOnionSkinCompositor::composite(const KisPaintDeviceSP sourceDevice, KisPaintDeviceSP targetDevice, const QRect& rect)
{
    KisRasterKeyframeChannel *keyframes = sourceDevice->keyframeChannel();

    KisPaintDeviceSP frameDevice = new KisPaintDevice(sourceDevice->colorSpace());
    KisPainter gcFrame(frameDevice);
    QBitArray channelFlags = targetDevice->colorSpace()->channelFlags(true, false);
    gcFrame.setChannelFlags(channelFlags);
    gcFrame.setOpacity(m_d->tintFactor);

    KisPaintDeviceSP backwardTintDevice = m_d->setUpTintDevice(m_d->backwardTintColor, sourceDevice->colorSpace());
    KisPaintDeviceSP forwardTintDevice = m_d->setUpTintDevice(m_d->forwardTintColor, sourceDevice->colorSpace());

    KisPainter gcDest(targetDevice);
153
    gcDest.setCompositeOp(sourceDevice->colorSpace()->compositeOp(COMPOSITE_BEHIND));
154

155 156 157 158 159
    KisKeyframeSP keyframeBck;
    KisKeyframeSP keyframeFwd;

    int time = sourceDevice->defaultBounds()->currentTime();
    keyframeBck = keyframeFwd = keyframes->activeKeyframeAt(time);
160

161
    for (int offset = 1; offset <= m_d->numberOfSkins; offset++) {
162 163 164
        keyframeBck = m_d->getNextFrameToComposite(keyframes, keyframeBck, true);
        keyframeFwd = m_d->getNextFrameToComposite(keyframes, keyframeFwd, false);

165
        if (!keyframeBck.isNull()) {
166
            m_d->tryCompositeFrame(keyframes, keyframeBck, gcFrame, gcDest, backwardTintDevice, m_d->skinOpacity(-offset), rect);
167 168 169
        }

        if (!keyframeFwd.isNull()) {
170
            m_d->tryCompositeFrame(keyframes, keyframeFwd, gcFrame, gcDest, forwardTintDevice, m_d->skinOpacity(offset), rect);
171
        }
172 173 174 175
    }

}

176 177 178 179 180
QRect KisOnionSkinCompositor::calculateFullExtent(const KisPaintDeviceSP device)
{
    QRect rect;

    KisRasterKeyframeChannel *channel = device->keyframeChannel();
181 182
    if (!channel) return rect;

183 184 185 186 187 188 189 190 191 192
    KisKeyframeSP keyframe = channel->firstKeyframe();

    while (keyframe) {
        rect |= channel->frameExtents(keyframe);
        keyframe = channel->nextKeyframe(keyframe);
    }

    return rect;
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
QRect KisOnionSkinCompositor::calculateExtent(const KisPaintDeviceSP device)
{
    QRect rect;
    KisKeyframeSP keyframeBck;
    KisKeyframeSP keyframeFwd;

    KisRasterKeyframeChannel *channel = device->keyframeChannel();
    keyframeBck = keyframeFwd = channel->activeKeyframeAt(device->defaultBounds()->currentTime());

    for (int offset = 1; offset <= m_d->numberOfSkins; offset++) {
        if (!keyframeBck.isNull()) {
            keyframeBck = channel->previousKeyframe(keyframeBck);

            if (!keyframeBck.isNull()) {
                rect |= channel->frameExtents(keyframeBck);
            }
        }

        if (!keyframeFwd.isNull()) {
            keyframeFwd = channel->nextKeyframe(keyframeFwd);

            if (!keyframeFwd.isNull()) {
                rect |= channel->frameExtents(keyframeFwd);
            }
        }
    }

    return rect;
}

223 224 225
void KisOnionSkinCompositor::configChanged()
{
    m_d->refreshConfig();
226
    emit sigOnionSkinChanged();
227
}