KoColorSpace.cpp 15.8 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 24
#include <kdebug.h>

25
#include "KoColorSpace.h"
26
#include "KoChannelInfo.h"
27

28
#include "KoCompositeOp.h"
Cyrille Berger's avatar
Cyrille Berger committed
29
#include "KoColorTransformation.h"
30 31
#include "KoColorTransformationFactory.h"
#include "KoColorTransformationFactoryRegistry.h"
32
#include "KoColorConversionCache.h"
33 34
#include "KoColorConversionSystem.h"
#include "KoColorSpaceRegistry.h"
35
#include "KoColorProfile.h"
36
#include "KoCopyColorConversionTransformation.h"
37
#include "KoFallBackColorTransformation.h"
38

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

48

49 50 51 52
    mutable KoColorConversionTransformation* transfoToRGBA16;
    mutable KoColorConversionTransformation* transfoFromRGBA16;
    mutable KoColorConversionTransformation* transfoToLABA16;
    mutable KoColorConversionTransformation* transfoFromLABA16;
53

54 55
};

Stefan Nikolaus's avatar
Stefan Nikolaus committed
56 57 58 59 60
KoColorSpace::KoColorSpace()
    : d (new Private())
{
}

61
KoColorSpace::KoColorSpace(const QString &id, const QString &name, KoMixColorsOp* mixColorsOp, KoConvolutionOp* convolutionOp )
62
    : d (new Private())
63
{
64 65 66 67
    d->id = id;
    d->name = name;
    d->mixColorsOp = mixColorsOp;
    d->convolutionOp = convolutionOp;
68 69 70 71
    d->transfoToRGBA16 = 0;
    d->transfoFromRGBA16 = 0;
    d->transfoToLABA16 = 0;
    d->transfoFromLABA16 = 0;
72 73
}

74
KoColorSpace::~KoColorSpace()
75
{
Cyrille Berger's avatar
Cyrille Berger committed
76 77 78 79 80 81 82 83
    foreach(KoCompositeOp* op, d->compositeOps.values())
    {
        delete op;
    }
    foreach(KoChannelInfo * channel, d->channels)
    {
        delete channel;
    }
84
    KoColorSpaceRegistry::instance()->colorConversionCache()->colorSpaceIsDestroyed(this);
85 86 87 88 89
    delete d->mixColorsOp;
    delete d->convolutionOp;
    delete d;
}

90 91 92
bool KoColorSpace::operator==(const KoColorSpace& rhs) const {
    const KoColorProfile* p1 = rhs.profile();
    const KoColorProfile* p2 = profile();
Boudewijn Rempt's avatar
Boudewijn Rempt committed
93
    return id() == rhs.id() && ((p1 == p2) || (*p1 == *p2 ));
94 95
}

96 97 98 99
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::compositeOps() 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 177
    else {
        kWarning() << "Asking for non-existent composite operation " << id << ", returning " << COMPOSITE_OVER;
178
        return d->compositeOps.value( COMPOSITE_OVER );
179
    }
180 181 182 183 184
}

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

189
const KoColorConversionTransformation* KoColorSpace::toLabA16Converter() const
190
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
191
    if(!d->transfoToLABA16)
192
    {
193
        d->transfoToLABA16 = KoColorSpaceRegistry::instance()->colorConversionSystem()->createColorConverter(this, KoColorSpaceRegistry::instance()->lab16("") ) ;
194
    }
195
    return d->transfoToLABA16;
196
}
197

198
const KoColorConversionTransformation* KoColorSpace::fromLabA16Converter() const
199
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
200
    if(!d->transfoFromLABA16)
201
    {
202
        d->transfoFromLABA16 = KoColorSpaceRegistry::instance()->colorConversionSystem()->createColorConverter( KoColorSpaceRegistry::instance()->lab16("") , this ) ;
203
    }
204
    return d->transfoFromLABA16;
205
}
206
const KoColorConversionTransformation* KoColorSpace::toRgbA16Converter() const
207
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
208
    if(!d->transfoToRGBA16)
209
    {
210
        d->transfoToRGBA16 = KoColorSpaceRegistry::instance()->colorConversionSystem()->createColorConverter( this, KoColorSpaceRegistry::instance()->rgb16("") ) ;
211
    }
212
    return d->transfoToRGBA16;
213
}
214
const KoColorConversionTransformation* KoColorSpace::fromRgbA16Converter() const
215
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
216
    if(!d->transfoFromRGBA16)
217
    {
218
        d->transfoFromRGBA16 = KoColorSpaceRegistry::instance()->colorConversionSystem()->createColorConverter( KoColorSpaceRegistry::instance()->rgb16("") , this ) ;
219
    }
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    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);
241 242
}

243 244
KoColorConversionTransformation* KoColorSpace::createColorConverter(const KoColorSpace * dstColorSpace, KoColorConversionTransformation::Intent renderingIntent) const
{
245
    if( *this == *dstColorSpace)
246
    {
247
        return new KoCopyColorConversionTransformation(this);
248
    } else {
249
        return KoColorSpaceRegistry::instance()->colorConversionSystem()->createColorConverter( this, dstColorSpace, renderingIntent);
250
    }
251 252
}

253
bool KoColorSpace::convertPixelsTo(const quint8 * src,
254 255 256 257
        quint8 * dst,
        const KoColorSpace * dstColorSpace,
        quint32 numPixels,
        KoColorConversionTransformation::Intent renderingIntent) const
258
{
259 260 261 262 263 264 265
    if( *this == *dstColorSpace)
    {
        memcpy( dst, src, numPixels * sizeof(quint8) * pixelSize());
    } else {
        KoCachedColorConversionTransformation cct = KoColorSpaceRegistry::instance()->colorConversionCache()->cachedConverter(this, dstColorSpace, renderingIntent);
        cct.transformation()->transform(src, dst, numPixels);
    }
266 267
    return true;
}
268

269

270 271
void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
272
                          const KoColorSpace * srcSpace,
273 274 275 276 277 278 279 280
                          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
281
                          const QBitArray & channelFlags) const
282
{
283 284
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ), channelFlags);
285 286
    }
    else {
287
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ), channelFlags);
288 289 290 291 292 293
    }

}

void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
294
                          const KoColorSpace * srcSpace,
295 296 297 298 299 300 301
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
Cyrille Berger's avatar
Cyrille Berger committed
302
                          const QString& op) const
303
{
304 305
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ));
306 307
    }
    else {
308
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ) );
309 310
    }
}
311 312

void KoColorSpace::bitBlt(quint8 *dst,
313
                          qint32 dststride,
314
                          const KoColorSpace * srcSpace,
315 316 317 318 319 320 321 322 323
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
                          const KoCompositeOp * op,
                          const QBitArray & channelFlags) const
324
{
Cyrille Berger's avatar
Cyrille Berger committed
325
    Q_ASSERT_X(*op->colorSpace() == *this, "KoColorSpace::bitBlt", QString("Composite op is for color space %1 while this is %2" ).arg( op->colorSpace()->id() ).arg(id()).toLatin1() );
326

327 328 329
    if (rows <= 0 || cols <= 0)
        return;

330
    if (!(*this == *srcSpace)) {
331

332 333
        quint32 len = pixelSize() * rows * cols;

334 335
        QVector<quint8> * conversionCache = threadLocalConversionCache(len);
        quint8* conversionData = conversionCache->data();
336 337 338

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
339
                                      conversionData + row * cols * pixelSize(), this,
340 341 342 343 344 345 346
                                      cols);
        }

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

        op->composite( dst, dststride,
347
                       conversionData, srcRowStride,
348 349 350 351 352 353 354 355 356 357 358 359 360 361
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity, channelFlags );

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

362
// XXX: I don't want this code duplication, but also don't want an
363 364 365
//      extra function call in this critical section of code. What to
//      do?
void KoColorSpace::bitBlt(quint8 *dst,
366
                          qint32 dststride,
367
                          const KoColorSpace * srcSpace,
368 369 370 371 372 373 374 375
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
                          const KoCompositeOp * op) const
376
{
377
    Q_ASSERT(*op->colorSpace() == *this);
378

379 380 381 382 383 384
    if (rows <= 0 || cols <= 0)
        return;

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

385 386
        QVector<quint8> * conversionCache = threadLocalConversionCache(len);
        quint8* conversionData = conversionCache->data();
387 388 389

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
390
                                      conversionData + row * cols * pixelSize(), this,
391 392 393 394 395 396 397
                                      cols);
        }

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

        op->composite( dst, dststride,
398
                       conversionData, srcRowStride,
399 400 401 402 403 404 405 406 407 408 409 410 411
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity);

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

QVector<quint8> * KoColorSpace::threadLocalConversionCache(quint32 size) const
{
    QVector<quint8> * ba = 0;
    if ( !d->conversionCache.hasLocalData() ) {
417
        ba = new QVector<quint8>( size, '0' );
418 419 420 421 422 423 424 425 426
        d->conversionCache.setLocalData( ba );
    }
    else {
        ba = d->conversionCache.localData();
        if ( ( quint8 )ba->size() < size )
            ba->resize( size );
    }
    return ba;
}
427

Boudewijn Rempt's avatar
Boudewijn Rempt committed
428
KoColorTransformation* KoColorSpace::createColorTransformation( const QString & id, const QHash<QString, QVariant> & parameters) const
429 430
{
    KoColorTransformationFactory* factory = KoColorTransformationFactoryRegistry::instance()->get( id );
Boudewijn Rempt's avatar
Boudewijn Rempt committed
431
    if(!factory) return 0;
432 433 434 435 436 437 438
    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
439
        // TODO use the color conversion cache
440 441 442 443 444 445 446 447 448
        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);
    }
}
449

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
QImage KoColorSpace::convertToQImage(const quint8 *data, qint32 width, qint32 height,
        const KoColorProfile *dstProfile,
        KoColorConversionTransformation::Intent renderingIntent) const

{
    QImage img = QImage(width, height, QImage::Format_ARGB32);

    const KoColorSpace * dstCS = KoColorSpaceRegistry::instance()->rgb8(dstProfile);

    if (data)
        this->convertPixelsTo(const_cast<quint8 *>(data), img.bits(), dstCS, width * height, renderingIntent);

    return img;
}