KoColorSpace.cpp 14.7 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 37 38 39 40 41 42 43
class KoCopyConversionTransformation : public KoColorConversionTransformation {
    public:
        KoCopyConversionTransformation(const KoColorSpace* cs) : KoColorConversionTransformation(cs, cs)
        {
        }
        virtual void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const
        {
            memcpy(dstU8, srcU8, nPixels * srcColorSpace()->pixelSize());
        }
};


44 45 46 47 48
struct KoColorSpace::Private {
    QString id;
    QString name;
    QHash<QString, KoCompositeOp *> compositeOps;
    KoColorSpaceRegistry * parent;
Adrian Page's avatar
q3--  
Adrian Page committed
49
    QList<KoChannelInfo *> channels;
50 51
    KoMixColorsOp* mixColorsOp;
    KoConvolutionOp* convolutionOp;
52 53
    QThreadStorage< QVector<quint8>* > conversionCache;

54 55
    mutable const KoColorSpace *lastUsedDstColorSpace;
    mutable KoColorConversionTransformation* lastUsedTransform;
56 57 58 59 60
    
    mutable KoColorConversionTransformation* transfoToRGBA16;
    mutable KoColorConversionTransformation* transfoFromRGBA16;
    mutable KoColorConversionTransformation* transfoToLABA16;
    mutable KoColorConversionTransformation* transfoFromLABA16;
61 62 63 64 65

// cmsHTRANSFORM is a void *, so this should work.
    typedef QMap<const KoColorSpace *, KoColorConversionTransformation*>  TransformMap;
    mutable TransformMap transforms; // Cache for existing transforms

66 67
};

Stefan Nikolaus's avatar
Stefan Nikolaus committed
68 69 70 71 72
KoColorSpace::KoColorSpace()
    : d (new Private())
{
}

73
KoColorSpace::KoColorSpace(const QString &id, const QString &name, KoColorSpaceRegistry * parent, KoMixColorsOp* mixColorsOp, KoConvolutionOp* convolutionOp )
74
    : d (new Private())
75
{
76 77 78 79 80
    d->id = id;
    d->name = name;
    d->parent = parent;
    d->mixColorsOp = mixColorsOp;
    d->convolutionOp = convolutionOp;
81 82
    d->lastUsedDstColorSpace = 0;
    d->lastUsedTransform = 0;
83 84 85 86
    d->transfoToRGBA16 = 0;
    d->transfoFromRGBA16 = 0;
    d->transfoToLABA16 = 0;
    d->transfoFromLABA16 = 0;
87 88
}

89
KoColorSpace::~KoColorSpace()
90
{
91 92 93 94 95 96 97 98 99
    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
100
QList<KoChannelInfo *> KoColorSpace::channels() const
101 102 103 104
{
    return d->channels;
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
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;
}

121
QBitArray KoColorSpace::setChannelFlagsToPixelOrder(const QBitArray & origChannelFlags) const
122
{
123 124 125
    if ( origChannelFlags.isEmpty() ) return origChannelFlags;

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

128 129 130 131
        KoChannelInfo * channel = d->channels.at( i );
        orderedChannelFlags.setBit( channel->pos(), origChannelFlags.testBit( i ) );
    }
    return orderedChannelFlags;
132 133 134 135 136 137 138 139 140 141 142 143 144
}

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;
145 146
}

147 148 149
void KoColorSpace::addChannel(KoChannelInfo * ci)
{
    d->channels.push_back(ci);
150 151
}

152
quint8 *KoColorSpace::allocPixelBuffer(quint32 numPixels) const
153 154 155 156
{
    return new quint8[pixelSize()*numPixels];
}

157
QList<KoCompositeOp*> KoColorSpace::userVisiblecompositeOps() const
158
{
159 160 161 162 163 164 165 166 167 168 169
    return d->compositeOps.values();
}


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


KoConvolutionOp* KoColorSpace::convolutionOp() const {
    return d->convolutionOp;
170 171
}

Cyrille Berger's avatar
Cyrille Berger committed
172
const KoCompositeOp * KoColorSpace::compositeOp(const QString & id) const
173
{
174 175
    if ( d->compositeOps.contains( id ) )
        return d->compositeOps.value( id );
176
    else
177
        return d->compositeOps.value( COMPOSITE_OVER );
178 179 180 181 182
}

void KoColorSpace::addCompositeOp(const KoCompositeOp * op)
{
    if ( op->colorSpace()->id() == id()) {
183
        d->compositeOps.insert( op->id(), const_cast<KoCompositeOp*>( op ) );
184 185 186
    }
}

187
const KoColorConversionTransformation* KoColorSpace::toLabA16Converter() const
188 189 190 191 192
{
    if(not d->transfoToLABA16)
    {
        d->transfoToLABA16 = d->parent->colorConversionSystem()->createColorConverter(this, KoColorSpaceRegistry::instance()->lab16("") ) ;
    }
193
    return d->transfoToLABA16;
194
}
195
const KoColorConversionTransformation* KoColorSpace::fromLabA16Converter() const
196 197 198 199 200
{
    if(not d->transfoFromLABA16)
    {
        d->transfoFromLABA16 = d->parent->colorConversionSystem()->createColorConverter( KoColorSpaceRegistry::instance()->lab16("") , this ) ;
    }
201
    return d->transfoFromLABA16;
202
}
203
const KoColorConversionTransformation* KoColorSpace::toRgbA16Converter() const
204 205 206 207 208
{
    if(not d->transfoToRGBA16)
    {
        d->transfoToRGBA16 = d->parent->colorConversionSystem()->createColorConverter( this, KoColorSpaceRegistry::instance()->rgb16("") ) ;
    }
209
    return d->transfoToRGBA16;
210
}
211
const KoColorConversionTransformation* KoColorSpace::fromRgbA16Converter() const
212 213 214 215 216
{
    if(not d->transfoFromRGBA16)
    {
        d->transfoFromRGBA16 = d->parent->colorConversionSystem()->createColorConverter( KoColorSpaceRegistry::instance()->rgb16("") , this ) ;
    }
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    return d->transfoFromRGBA16;
}

void KoColorSpace::toLabA16(const quint8 * src, quint8 * dst, quint32 nPixels) const
{
    toLabA16Converter()->transform( src, dst, nPixels);
}

void KoColorSpace::fromLabA16(const quint8 * src, quint8 * dst, quint32 nPixels) const
{
    fromLabA16Converter()->transform( src, dst, nPixels);
}

void KoColorSpace::toRgbA16(const quint8 * src, quint8 * dst, quint32 nPixels) const
{
    toRgbA16Converter()->transform( src, dst, nPixels);
}

void KoColorSpace::fromRgbA16(const quint8 * src, quint8 * dst, quint32 nPixels) const
{
    fromRgbA16Converter()->transform( src, dst, nPixels);
238 239
}

240 241
KoColorConversionTransformation* KoColorSpace::createColorConverter(const KoColorSpace * dstColorSpace, KoColorConversionTransformation::Intent renderingIntent) const
{
242 243 244 245 246 247
    if( this == dstColorSpace)
    {
        return new KoCopyConversionTransformation(this);
    } else {
        return d->parent->colorConversionSystem()->createColorConverter( this, dstColorSpace, renderingIntent);
    }
248 249
}

250
bool KoColorSpace::convertPixelsTo(const quint8 * src,
251 252 253 254
        quint8 * dst,
        const KoColorSpace * dstColorSpace,
        quint32 numPixels,
        KoColorConversionTransformation::Intent renderingIntent) const
255
{
256 257 258 259 260 261 262 263 264 265
    if (dstColorSpace->id() == this->id()
        && dstColorSpace->profile() == profile())
    {
        if (src!= dst)
            memcpy (dst, src, numPixels * this->pixelSize());

        return true;
    }

    KoColorConversionTransformation* tf = 0;
266

267 268 269 270 271
    if (d->lastUsedTransform != 0 && d->lastUsedDstColorSpace != 0) {
        if (dstColorSpace->id() == d->lastUsedDstColorSpace->id() &&
            dstColorSpace->profile() == d->lastUsedDstColorSpace->profile()) {
            tf = d->lastUsedTransform;
            }
272
    }
273

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    if (not tf) {

        if (!d->transforms.contains(dstColorSpace)) {
    // XXX: Should we clear the transform cache if it gets too big?
            tf = this->createColorConverter(dstColorSpace, renderingIntent);
            d->transforms[dstColorSpace] = tf;
        }
        else {
            tf = d->transforms[dstColorSpace];
        }

        d->lastUsedTransform = tf;
        d->lastUsedDstColorSpace = dstColorSpace;
    }
    tf->transform(src, dst, numPixels);

290 291
    return true;
}
292

293

294 295 296 297 298 299 300 301 302 303 304
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
305
                          const QBitArray & channelFlags) const
306
{
307 308
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ), channelFlags);
309 310
    }
    else {
311
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ), channelFlags);
312 313 314 315 316 317 318 319 320 321 322 323 324 325
    }

}

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
326
                          const QString& op) const
327
{
328 329
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ));
330 331
    }
    else {
332
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ) );
333 334
    }
}
335 336

void KoColorSpace::bitBlt(quint8 *dst,
337 338 339 340 341 342 343 344 345 346 347
                          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
348
{
349 350
    Q_ASSERT(op->colorSpace() == this);

351 352 353 354 355 356
    if (rows <= 0 || cols <= 0)
        return;

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

357 358
        QVector<quint8> * conversionCache = threadLocalConversionCache(len);
        quint8* conversionData = conversionCache->data();
359 360 361

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
362
                                      conversionData + row * cols * pixelSize(), this,
363 364 365 366 367 368 369
                                      cols);
        }

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

        op->composite( dst, dststride,
370
                       conversionData, srcRowStride,
371 372 373 374 375 376 377 378 379 380 381 382 383 384
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity, channelFlags );

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

385
// XXX: I don't want this code duplication, but also don't want an
386 387 388
//      extra function call in this critical section of code. What to
//      do?
void KoColorSpace::bitBlt(quint8 *dst,
389 390 391 392 393 394 395 396 397 398
                          qint32 dststride,
                          KoColorSpace * srcSpace,
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
                          const KoCompositeOp * op) const
399
{
400 401
    Q_ASSERT(op->colorSpace() == this);

402 403 404 405 406 407
    if (rows <= 0 || cols <= 0)
        return;

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

408 409
        QVector<quint8> * conversionCache = threadLocalConversionCache(len);
        quint8* conversionData = conversionCache->data();
410 411 412

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
413
                                      conversionData + row * cols * pixelSize(), this,
414 415 416 417 418 419 420
                                      cols);
        }

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

        op->composite( dst, dststride,
421
                       conversionData, srcRowStride,
422 423 424 425 426 427 428 429 430 431 432 433 434
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity);

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

QVector<quint8> * KoColorSpace::threadLocalConversionCache(quint32 size) const
{
    QVector<quint8> * ba = 0;
    if ( !d->conversionCache.hasLocalData() ) {
440
        ba = new QVector<quint8>( size, '0' );
441 442 443 444 445 446 447 448 449
        d->conversionCache.setLocalData( ba );
    }
    else {
        ba = d->conversionCache.localData();
        if ( ( quint8 )ba->size() < size )
            ba->resize( size );
    }
    return ba;
}