KoColorSpace.cpp 8.1 KB
Newer Older
1 2 3
/*
 *  Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
 *
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 7
 * License as published by the Free Software Foundation; either
 * version 2 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
#include "KoColorSpace.h"
21 22 23

#include <kdebug.h>

24
#include "KoCompositeOp.h"
Cyrille Berger's avatar
Cyrille Berger committed
25
#include "KoColorTransformation.h"
26

27
KoColorSpace::KoColorSpace(const QString &id, const QString &name, KoColorSpaceRegistry * parent)
28
    : m_id(id)
29
    , m_name( name )
30
    , m_parent( parent )
31 32
    , m_mixColorsOp( 0 )
    , m_convolutionOp( 0 )
33
{
Laurent Montel's avatar
Laurent Montel committed
34
    //m_dcop = 0;
35 36
}

37
KoColorSpace::~KoColorSpace()
38
{
Laurent Montel's avatar
Laurent Montel committed
39
    //delete m_dcop;
40 41
}

42
quint8 *KoColorSpace::allocPixelBuffer(quint32 numPixels) const
43 44 45 46
{
    return new quint8[pixelSize()*numPixels];
}

47
QList<KoCompositeOp*> KoColorSpace::userVisiblecompositeOps() const
48 49 50 51
{
    return m_compositeOps.values();
}

Cyrille Berger's avatar
Cyrille Berger committed
52
const KoCompositeOp * KoColorSpace::compositeOp(const QString & id) const
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
{
    if ( m_compositeOps.contains( id ) )
        return m_compositeOps.value( id );
    else
        return m_compositeOps.value( COMPOSITE_OVER );
}

void KoColorSpace::addCompositeOp(const KoCompositeOp * op)
{
    if ( op->colorSpace()->id() == id()) {
        m_compositeOps.insert( op->id(), const_cast<KoCompositeOp*>( op ) );
    }
}


68
bool KoColorSpace::convertPixelsTo(const quint8 * src,
69
                                   quint8 * dst,
Cyrille Berger's avatar
Cyrille Berger committed
70
                                   const KoColorSpace * dstColorSpace,
71
                                   quint32 numPixels,
Cyrille Berger's avatar
Cyrille Berger committed
72
                                   qint32 renderingIntent) const
73
{
74
    Q_UNUSED(renderingIntent);
75
    // 4 channels: labA, 2 bytes per lab channel
76
    quint8 *pixels = new quint8[sizeof(quint16)*4*numPixels];
77 78 79 80 81 82 83
    toLabA16(src, pixels,numPixels);
    dstColorSpace->fromLabA16(pixels, dst,numPixels);

    delete [] pixels;

    return true;
}
84 85 86 87 88 89 90 91 92 93 94 95

void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
                          KoColorSpace * srcSpace,
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
                          const QString & op,
Cyrille Berger's avatar
Cyrille Berger committed
96
                          const QBitArray & channelFlags) const
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
{
    if ( m_compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, m_compositeOps.value( op ), channelFlags);
    }
    else {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, m_compositeOps.value( COMPOSITE_OVER ), channelFlags);
    }

}

void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
                          KoColorSpace * srcSpace,
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
Cyrille Berger's avatar
Cyrille Berger committed
117
                          const QString& op) const
118 119 120 121 122 123 124 125
{
    if ( m_compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, m_compositeOps.value( op ));
    }
    else {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, m_compositeOps.value( COMPOSITE_OVER ) );
    }
}
126 127 128 129 130 131 132 133 134 135 136 137

void KoColorSpace::bitBlt(quint8 *dst,
                                   qint32 dststride,
                                   KoColorSpace * srcSpace,
                                   const quint8 *src,
                                   qint32 srcRowStride,
                                   const quint8 *srcAlphaMask,
                                   qint32 maskRowStride,
                                   quint8 opacity,
                                   qint32 rows,
                                   qint32 cols,
                                   const KoCompositeOp * op,
Cyrille Berger's avatar
Cyrille Berger committed
138
                                   const QBitArray & channelFlags) const
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
{
    if (rows <= 0 || cols <= 0)
        return;

    if (this != srcSpace) {
        quint32 len = pixelSize() * rows * cols;

        // If our conversion cache is too small, extend it.
        if (!m_conversionCache.resize( len, Q3GArray::SpeedOptim )) {
            kWarning() << "Could not allocate enough memory for the conversion!\n";
            // XXX: We should do a slow, pixel by pixel bitblt here...
            abort();
        }

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
                                      m_conversionCache.data() + row * cols * pixelSize(), this,
                                      cols);
        }

        // The old srcRowStride is no longer valid because we converted to the current cs
        srcRowStride = cols * pixelSize();

        op->composite( dst, dststride,
                       m_conversionCache.data(), srcRowStride,
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity, channelFlags );

    }
    else {
        op->composite( dst, dststride,
                       src, srcRowStride,
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity, channelFlags );
    }
}

178
// XXX: I don't want this code duplication, but also don't want an
179 180 181 182 183 184 185 186 187 188 189 190
//      extra function call in this critical section of code. What to
//      do?
void KoColorSpace::bitBlt(quint8 *dst,
                                   qint32 dststride,
                                   KoColorSpace * srcSpace,
                                   const quint8 *src,
                                   qint32 srcRowStride,
                                   const quint8 *srcAlphaMask,
                                   qint32 maskRowStride,
                                   quint8 opacity,
                                   qint32 rows,
                                   qint32 cols,
Cyrille Berger's avatar
Cyrille Berger committed
191
                                   const KoCompositeOp * op) const
192 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 223 224 225 226 227 228 229
{
    if (rows <= 0 || cols <= 0)
        return;

    if (this != srcSpace) {
        quint32 len = pixelSize() * rows * cols;

        // If our conversion cache is too small, extend it.
        if (!m_conversionCache.resize( len, Q3GArray::SpeedOptim )) {
            kWarning() << "Could not allocate enough memory for the conversion!\n";
            // XXX: We should do a slow, pixel by pixel bitblt here...
            abort();
        }

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
                                      m_conversionCache.data() + row * cols * pixelSize(), this,
                                      cols);
        }

        // The old srcRowStride is no longer valid because we converted to the current cs
        srcRowStride = cols * pixelSize();

        op->composite( dst, dststride,
                       m_conversionCache.data(), srcRowStride,
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity);

    }
    else {
        op->composite( dst, dststride,
                       src,srcRowStride,
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity);
    }
}