KoColorSpace.cpp 15.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 "KoColorTransformationFactory.h"
#include "KoColorTransformationFactoryRegistry.h"
31 32
#include "KoColorConversionSystem.h"
#include "KoColorSpaceRegistry.h"
33
#include "KoColorProfile.h"
34
#include "KoCopyColorConversionTransformation.h"
35
#include "KoFallBackColorTransformation.h"
36

37 38 39 40
struct KoColorSpace::Private {
    QString id;
    QString name;
    QHash<QString, KoCompositeOp *> compositeOps;
Adrian Page's avatar
q3--  
Adrian Page committed
41
    QList<KoChannelInfo *> channels;
42 43
    KoMixColorsOp* mixColorsOp;
    KoConvolutionOp* convolutionOp;
44 45
    QThreadStorage< QVector<quint8>* > conversionCache;

46 47
    mutable const KoColorSpace *lastUsedDstColorSpace;
    mutable KoColorConversionTransformation* lastUsedTransform;
48 49 50 51 52
    
    mutable KoColorConversionTransformation* transfoToRGBA16;
    mutable KoColorConversionTransformation* transfoFromRGBA16;
    mutable KoColorConversionTransformation* transfoToLABA16;
    mutable KoColorConversionTransformation* transfoFromLABA16;
53 54 55 56 57

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

58 59
};

Stefan Nikolaus's avatar
Stefan Nikolaus committed
60 61 62 63 64
KoColorSpace::KoColorSpace()
    : d (new Private())
{
}

65
KoColorSpace::KoColorSpace(const QString &id, const QString &name, KoMixColorsOp* mixColorsOp, KoConvolutionOp* convolutionOp )
66
    : d (new Private())
67
{
68 69 70 71
    d->id = id;
    d->name = name;
    d->mixColorsOp = mixColorsOp;
    d->convolutionOp = convolutionOp;
72 73
    d->lastUsedDstColorSpace = 0;
    d->lastUsedTransform = 0;
74 75 76 77
    d->transfoToRGBA16 = 0;
    d->transfoFromRGBA16 = 0;
    d->transfoToLABA16 = 0;
    d->transfoFromLABA16 = 0;
78 79
}

80
KoColorSpace::~KoColorSpace()
81
{
82 83 84 85 86
    delete d->mixColorsOp;
    delete d->convolutionOp;
    delete d;
}

87 88 89
bool KoColorSpace::operator==(const KoColorSpace& rhs) const {
    const KoColorProfile* p1 = rhs.profile();
    const KoColorProfile* p2 = profile();
90
    return id() == rhs.id() and ((p1 == p2) or (*p1 == *p2 ));
91 92
}

93 94 95 96
QString KoColorSpace::id() const {return d->id;}

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

Adrian Page's avatar
q3--  
Adrian Page committed
97
QList<KoChannelInfo *> KoColorSpace::channels() const
98 99 100 101
{
    return d->channels;
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
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;
}

118
QBitArray KoColorSpace::setChannelFlagsToPixelOrder(const QBitArray & origChannelFlags) const
119
{
120 121 122
    if ( origChannelFlags.isEmpty() ) return origChannelFlags;

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

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

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;
142 143
}

144 145 146
void KoColorSpace::addChannel(KoChannelInfo * ci)
{
    d->channels.push_back(ci);
147 148
}

149
quint8 *KoColorSpace::allocPixelBuffer(quint32 numPixels) const
150 151 152 153
{
    return new quint8[pixelSize()*numPixels];
}

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


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


KoConvolutionOp* KoColorSpace::convolutionOp() const {
    return d->convolutionOp;
167 168
}

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

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

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

237 238
KoColorConversionTransformation* KoColorSpace::createColorConverter(const KoColorSpace * dstColorSpace, KoColorConversionTransformation::Intent renderingIntent) const
{
239
    if( *this == *dstColorSpace)
240
    {
241
        return new KoCopyColorConversionTransformation(this);
242
    } else {
243
        return KoColorSpaceRegistry::instance()->colorConversionSystem()->createColorConverter( this, dstColorSpace, renderingIntent);
244
    }
245 246
}

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

        return true;
    }

    KoColorConversionTransformation* tf = 0;
262
#if 1
263
    if (d->lastUsedTransform != 0 && d->lastUsedDstColorSpace != 0) {
Cyrille Berger's avatar
Cyrille Berger committed
264
        if (*dstColorSpace == *d->lastUsedDstColorSpace) {
265 266
            tf = d->lastUsedTransform;
            }
267
    }
268 269
    if (not tf) {
        if (!d->transforms.contains(dstColorSpace)) {
270
#endif    // XXX: Should we clear the transform cache if it gets too big?
271
            tf = this->createColorConverter(dstColorSpace, renderingIntent);
272
#if 1
273 274 275 276 277 278 279 280 281
            d->transforms[dstColorSpace] = tf;
        }
        else {
            tf = d->transforms[dstColorSpace];
        }

        d->lastUsedTransform = tf;
        d->lastUsedDstColorSpace = dstColorSpace;
    }
282
#endif
283 284
    tf->transform(src, dst, numPixels);

285 286
    return true;
}
287

288

289 290
void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
291
                          const KoColorSpace * srcSpace,
292 293 294 295 296 297 298 299
                          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
300
                          const QBitArray & channelFlags) const
301
{
302 303
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ), channelFlags);
304 305
    }
    else {
306
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ), channelFlags);
307 308 309 310 311 312
    }

}

void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
313
                          const KoColorSpace * srcSpace,
314 315 316 317 318 319 320
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
Cyrille Berger's avatar
Cyrille Berger committed
321
                          const QString& op) const
322
{
323 324
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ));
325 326
    }
    else {
327
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ) );
328 329
    }
}
330 331

void KoColorSpace::bitBlt(quint8 *dst,
332
                          qint32 dststride,
333
                          const KoColorSpace * srcSpace,
334 335 336 337 338 339 340 341 342
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
                          const KoCompositeOp * op,
                          const QBitArray & channelFlags) const
343
{
344
    Q_ASSERT(*op->colorSpace() == *this);
345

346 347 348 349 350 351
    if (rows <= 0 || cols <= 0)
        return;

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

352 353
        QVector<quint8> * conversionCache = threadLocalConversionCache(len);
        quint8* conversionData = conversionCache->data();
354 355 356

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
357
                                      conversionData + row * cols * pixelSize(), this,
358 359 360 361 362 363 364
                                      cols);
        }

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

        op->composite( dst, dststride,
365
                       conversionData, srcRowStride,
366 367 368 369 370 371 372 373 374 375 376 377 378 379
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity, channelFlags );

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

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

397 398 399 400 401 402
    if (rows <= 0 || cols <= 0)
        return;

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

403 404
        QVector<quint8> * conversionCache = threadLocalConversionCache(len);
        quint8* conversionData = conversionCache->data();
405 406 407

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
408
                                      conversionData + row * cols * pixelSize(), this,
409 410 411 412 413 414 415
                                      cols);
        }

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

        op->composite( dst, dststride,
416
                       conversionData, srcRowStride,
417 418 419 420 421 422 423 424 425 426 427 428 429
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity);

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

QVector<quint8> * KoColorSpace::threadLocalConversionCache(quint32 size) const
{
    QVector<quint8> * ba = 0;
    if ( !d->conversionCache.hasLocalData() ) {
435
        ba = new QVector<quint8>( size, '0' );
436 437 438 439 440 441 442 443 444
        d->conversionCache.setLocalData( ba );
    }
    else {
        ba = d->conversionCache.localData();
        if ( ( quint8 )ba->size() < size )
            ba->resize( size );
    }
    return ba;
}
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465

KoColorTransformation* KoColorSpace::createColorTransformation( QString id, QHash<QString, QVariant> parameters) const
{
    KoColorTransformationFactory* factory = KoColorTransformationFactoryRegistry::instance()->get( id );
    if(not factory) return 0;
    QPair<KoID, KoID> model( colorModelId(), colorDepthId() );
    QList< QPair<KoID, KoID> > models = factory->supportedModels();
    if(models.contains(model))
    {
        return factory->createTransformation( this, parameters);
    } else {
        // Find the best solution
        KoColorConversionTransformation* csToFallBack = 0;
        KoColorConversionTransformation* fallBackToCs = 0;
        KoColorSpaceRegistry::instance()->colorConversionSystem()->createColorConverters(this, models, csToFallBack, fallBackToCs);
        Q_ASSERT(csToFallBack);
        Q_ASSERT(fallBackToCs);
        KoColorTransformation* transfo = factory->createTransformation(fallBackToCs->srcColorSpace(), parameters);
        return new KoFallBackColorTransformation( csToFallBack, fallBackToCs, transfo);
    }
}
466