kis_onion_skin_compositor.cpp 6.07 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 29

#include "kis_image_config.h"
#include "kis_config_notifier.h"
30 31
#include "kis_raster_keyframe_channel.h"

Dmitry Kazakov's avatar
Dmitry Kazakov committed
32 33
Q_GLOBAL_STATIC(KisOnionSkinCompositor, s_instance);

34 35 36 37 38 39 40 41 42 43 44 45

struct KisOnionSkinCompositor::Private
{
    int numberOfSkins;
    int tintFactor;
    QColor backwardTintColor;
    QColor forwardTintColor;
    QVector<int> backwardOpacities;
    QVector<int> forwardOpacities;

    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 56 57 58 59
    }

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

60
    void compositeFrame(KisRasterKeyframeChannel *keyframes, KisKeyframeSP keyframe, KisPainter &gcFrame, KisPainter &gcDest, KisPaintDeviceSP tintSource, int opacity, const QRect &rect)
61
    {
62
        if (keyframe.isNull() || opacity == OPACITY_TRANSPARENT_U8) return;
63

64
        keyframes->fetchFrame(keyframe, gcFrame.device());
65

66 67 68 69
        gcFrame.bitBlt(rect.topLeft(), tintSource, rect);

        gcDest.setOpacity(opacity);
        gcDest.bitBlt(rect.topLeft(), gcFrame.device(), rect);
70 71 72 73 74 75 76 77 78 79 80
    }

    void refreshConfig()
    {
        KisImageConfig config;

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

81 82 83 84 85 86 87
        backwardOpacities.resize(numberOfSkins);
        forwardOpacities.resize(numberOfSkins);

        for (int i = 0; i < numberOfSkins; i++) {
            backwardOpacities[i] = config.onionSkinOpacity(-(i + 1));
            forwardOpacities[i] = config.onionSkinOpacity(i + 1);
        }
88 89 90 91 92 93 94 95 96 97 98 99 100 101
    }
};

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

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

Jouni Pentikäinen's avatar
Jouni Pentikäinen committed
102 103 104
KisOnionSkinCompositor::~KisOnionSkinCompositor()
{}

105 106 107 108 109 110 111 112 113 114 115 116 117 118
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);
119
    gcDest.setCompositeOp(sourceDevice->colorSpace()->compositeOp(COMPOSITE_BEHIND));
120

121 122 123 124 125
    KisKeyframeSP keyframeBck;
    KisKeyframeSP keyframeFwd;

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

127
    for (int offset = 1; offset <= m_d->numberOfSkins; offset++) {
128 129 130 131 132 133 134 135 136
        if (!keyframeBck.isNull()) {
            keyframeBck = keyframes->previousKeyframe(keyframeBck);
            m_d->compositeFrame(keyframes, keyframeBck, gcFrame, gcDest, backwardTintDevice, m_d->skinOpacity(-offset), rect);
        }

        if (!keyframeFwd.isNull()) {
            keyframeFwd = keyframes->nextKeyframe(keyframeFwd);
            m_d->compositeFrame(keyframes, keyframeFwd, gcFrame, gcDest, forwardTintDevice, m_d->skinOpacity(offset), rect);
        }
137 138 139 140
    }

}

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
QRect KisOnionSkinCompositor::calculateFullExtent(const KisPaintDeviceSP device)
{
    QRect rect;

    KisRasterKeyframeChannel *channel = device->keyframeChannel();
    KisKeyframeSP keyframe = channel->firstKeyframe();

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

    return rect;
}

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 185
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;
}

186 187 188
void KisOnionSkinCompositor::configChanged()
{
    m_d->refreshConfig();
189
    emit sigOnionSkinChanged();
190
}