KoColorSpace.cpp 11.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 <QThreadStorage>
#include <QByteArray>
21
#include <QBitArray>
22

23
#include "KoColorSpace.h"
24
#include "KoChannelInfo.h"
25 26
#include <kdebug.h>

27
#include "KoCompositeOp.h"
Cyrille Berger's avatar
Cyrille Berger committed
28
#include "KoColorTransformation.h"
29 30
#include "KoColorConversionSystem.h"
#include "KoColorSpaceRegistry.h"
31

32 33 34 35 36
struct KoColorSpace::Private {
    QString id;
    QString name;
    QHash<QString, KoCompositeOp *> compositeOps;
    KoColorSpaceRegistry * parent;
Adrian Page's avatar
q3--  
Adrian Page committed
37
    QList<KoChannelInfo *> channels;
38 39
    KoMixColorsOp* mixColorsOp;
    KoConvolutionOp* convolutionOp;
40 41
    QThreadStorage< QVector<quint8>* > conversionCache;

42 43
};

Stefan Nikolaus's avatar
Stefan Nikolaus committed
44 45 46 47 48
KoColorSpace::KoColorSpace()
    : d (new Private())
{
}

49
KoColorSpace::KoColorSpace(const QString &id, const QString &name, KoColorSpaceRegistry * parent, KoMixColorsOp* mixColorsOp, KoConvolutionOp* convolutionOp )
50
    : d (new Private())
51
{
52 53 54 55 56
    d->id = id;
    d->name = name;
    d->parent = parent;
    d->mixColorsOp = mixColorsOp;
    d->convolutionOp = convolutionOp;
57 58
}

59
KoColorSpace::~KoColorSpace()
60
{
61 62 63 64 65 66 67 68 69
    delete d->mixColorsOp;
    delete d->convolutionOp;
    delete d;
}

QString KoColorSpace::id() const {return d->id;}

QString KoColorSpace::name() const {return d->name;}

Adrian Page's avatar
q3--  
Adrian Page committed
70
QList<KoChannelInfo *> KoColorSpace::channels() const
71 72 73 74
{
    return d->channels;
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
QBitArray KoColorSpace::channelFlags(bool color, bool alpha, bool substance, bool substrate) const
{
    QBitArray ba( d->channels.size() );
    if ( !color && !alpha && !substance && !substrate ) return ba;

    for ( int i = 0; i < d->channels.size(); ++i ) {
        KoChannelInfo * channel = d->channels.at( i );
        if ( ( color && channel->channelType() == KoChannelInfo::COLOR ) ||
             ( alpha && channel->channelType() == KoChannelInfo::ALPHA ) ||
             ( substrate && channel->channelType() == KoChannelInfo::SUBSTRATE ) ||
             ( substance && channel->channelType() == KoChannelInfo::SUBSTANCE ) )
            ba.setBit( i, true );
    }
    return ba;
}

91
QBitArray KoColorSpace::setChannelFlagsToPixelOrder(const QBitArray & origChannelFlags) const
92
{
93 94 95
    if ( origChannelFlags.isEmpty() ) return origChannelFlags;

    QBitArray orderedChannelFlags( origChannelFlags.size() );
96
    for ( int i = 0; i < origChannelFlags.size(); ++i ) {
97

98 99 100 101
        KoChannelInfo * channel = d->channels.at( i );
        orderedChannelFlags.setBit( channel->pos(), origChannelFlags.testBit( i ) );
    }
    return orderedChannelFlags;
102 103 104 105 106 107 108 109 110 111 112 113 114
}

QBitArray KoColorSpace::setChannelFlagsToColorSpaceOrder( const QBitArray & origChannelFlags ) const
{
    if ( origChannelFlags.isEmpty() ) return origChannelFlags;

    QBitArray orderedChannelFlags( origChannelFlags.size() );
    for ( int i = 0; i < orderedChannelFlags.size(); ++i )
    {
        KoChannelInfo * channel = d->channels.at( i );
        orderedChannelFlags.setBit( i, origChannelFlags.testBit( channel->pos() ) );
    }
    return orderedChannelFlags;
115 116
}

117 118 119
void KoColorSpace::addChannel(KoChannelInfo * ci)
{
    d->channels.push_back(ci);
120 121
}

122
quint8 *KoColorSpace::allocPixelBuffer(quint32 numPixels) const
123 124 125 126
{
    return new quint8[pixelSize()*numPixels];
}

127
QList<KoCompositeOp*> KoColorSpace::userVisiblecompositeOps() const
128
{
129 130 131 132 133 134 135 136 137 138 139
    return d->compositeOps.values();
}


KoMixColorsOp* KoColorSpace::mixColorsOp() const {
    return d->mixColorsOp;
}


KoConvolutionOp* KoColorSpace::convolutionOp() const {
    return d->convolutionOp;
140 141
}

Cyrille Berger's avatar
Cyrille Berger committed
142
const KoCompositeOp * KoColorSpace::compositeOp(const QString & id) const
143
{
144 145
    if ( d->compositeOps.contains( id ) )
        return d->compositeOps.value( id );
146
    else
147
        return d->compositeOps.value( COMPOSITE_OVER );
148 149 150 151 152
}

void KoColorSpace::addCompositeOp(const KoCompositeOp * op)
{
    if ( op->colorSpace()->id() == id()) {
153
        d->compositeOps.insert( op->id(), const_cast<KoCompositeOp*>( op ) );
154 155 156
    }
}

157 158
KoColorConversionTransformation* KoColorSpace::createColorConverter(const KoColorSpace * dstColorSpace, KoColorConversionTransformation::Intent renderingIntent) const
{
159 160
    return d->parent->colorConversionSystem()->createColorConverter( this, dstColorSpace, renderingIntent);
//     return new KoColorConversionTransformation(this, dstColorSpace, renderingIntent);
161 162
}

163
bool KoColorSpace::convertPixelsTo(const quint8 * src,
164
                                   quint8 * dst,
Cyrille Berger's avatar
Cyrille Berger committed
165
                                   const KoColorSpace * dstColorSpace,
166
                                   quint32 numPixels,
167
                                   KoColorConversionTransformation::Intent renderingIntent) const
168
{
169
    Q_UNUSED(renderingIntent);
170

171 172 173 174 175 176
    if (*this == *dstColorSpace) {
        memmove(dst, src, numPixels*pixelSize());
    } else {
        KoColorConversionTransformation* transfo =new KoColorConversionTransformation(this, dstColorSpace, renderingIntent);
        transfo->transform(src, dst, numPixels);
    }
177 178 179

    return true;
}
180 181 182 183 184 185 186 187 188 189 190 191

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
192
                          const QBitArray & channelFlags) const
193
{
194 195
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ), channelFlags);
196 197
    }
    else {
198
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ), channelFlags);
199 200 201 202 203 204 205 206 207 208 209 210 211 212
    }

}

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
213
                          const QString& op) const
214
{
215 216
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ));
217 218
    }
    else {
219
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ) );
220 221
    }
}
222 223

void KoColorSpace::bitBlt(quint8 *dst,
224 225 226 227 228 229 230 231 232 233 234
                          qint32 dststride,
                          KoColorSpace * srcSpace,
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
                          const KoCompositeOp * op,
                          const QBitArray & channelFlags) const
235
{
236 237
    Q_ASSERT(op->colorSpace() == this);

238 239 240 241 242 243
    if (rows <= 0 || cols <= 0)
        return;

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

244 245
        QVector<quint8> * conversionCache = threadLocalConversionCache(len);
        quint8* conversionData = conversionCache->data();
246 247 248

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
249
                                      conversionData + row * cols * pixelSize(), this,
250 251 252 253 254 255 256
                                      cols);
        }

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

        op->composite( dst, dststride,
257
                       conversionData, srcRowStride,
258 259 260 261 262 263 264 265 266 267 268 269 270 271
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity, channelFlags );

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

272
// XXX: I don't want this code duplication, but also don't want an
273 274 275
//      extra function call in this critical section of code. What to
//      do?
void KoColorSpace::bitBlt(quint8 *dst,
276 277 278 279 280 281 282 283 284 285
                          qint32 dststride,
                          KoColorSpace * srcSpace,
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
                          const KoCompositeOp * op) const
286
{
287 288
    Q_ASSERT(op->colorSpace() == this);

289 290 291 292 293 294
    if (rows <= 0 || cols <= 0)
        return;

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

295 296
        QVector<quint8> * conversionCache = threadLocalConversionCache(len);
        quint8* conversionData = conversionCache->data();
297 298 299

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
300
                                      conversionData + row * cols * pixelSize(), this,
301 302 303 304 305 306 307
                                      cols);
        }

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

        op->composite( dst, dststride,
308
                       conversionData, srcRowStride,
309 310 311 312 313 314 315 316 317 318 319 320 321
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity);

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

QVector<quint8> * KoColorSpace::threadLocalConversionCache(quint32 size) const
{
    QVector<quint8> * ba = 0;
    if ( !d->conversionCache.hasLocalData() ) {
327
        ba = new QVector<quint8>( size, '0' );
328 329 330 331 332 333 334 335 336
        d->conversionCache.setLocalData( ba );
    }
    else {
        ba = d->conversionCache.localData();
        if ( ( quint8 )ba->size() < size )
            ba->resize( size );
    }
    return ba;
}