KoColorSpace.cpp 16.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

24
#include "KoColorSpace.h"
25
#include "KoChannelInfo.h"
Boudewijn Rempt's avatar
Boudewijn Rempt committed
26
#include "DebugPigment.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
#include "KoColorConversionCache.h"
32 33
#include "KoColorConversionSystem.h"
#include "KoColorSpaceRegistry.h"
34
#include "KoColorProfile.h"
35
#include "KoCopyColorConversionTransformation.h"
36
#include "KoFallBackColorTransformation.h"
37
#include "KoUniqueNumberForIdServer.h"
38

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

49

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

55 56
};

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

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

76
KoColorSpace::~KoColorSpace()
77
{
78
    qDeleteAll(d->compositeOps);
Cyrille Berger's avatar
Cyrille Berger committed
79 80 81 82
    foreach(KoChannelInfo * channel, d->channels)
    {
        delete channel;
    }
83 84 85 86
    KoColorConversionCache* cache = KoColorSpaceRegistry::instance()->colorConversionCache();
    if (cache) {
        cache->colorSpaceIsDestroyed(this);
    }
87 88 89 90 91
    delete d->mixColorsOp;
    delete d->convolutionOp;
    delete d;
}

92 93 94
bool KoColorSpace::operator==(const KoColorSpace& rhs) const {
    const KoColorProfile* p1 = rhs.profile();
    const KoColorProfile* p2 = profile();
95
    return d->idNumber == rhs.d->idNumber && ((p1 == p2) || (*p1 == *p2 ));
96 97
}

98 99 100 101
QString KoColorSpace::id() const {return d->id;}

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

Adrian Page's avatar
q3--  
Adrian Page committed
102
QList<KoChannelInfo *> KoColorSpace::channels() const
103 104 105 106
{
    return d->channels;
}

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

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

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

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

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;
147 148
}

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

154
quint8 *KoColorSpace::allocPixelBuffer(quint32 numPixels, bool clear, quint8 defaultvalue) const
155
{
156 157 158 159 160 161 162 163
    if (numPixels == 0) {
        return 0;
    }
    quint8* buf = new quint8[pixelSize()*numPixels];
    if (clear) {
        memset(buf, defaultvalue, pixelSize() * numPixels);
    }
    return buf;
164 165
}

166
QList<KoCompositeOp*> KoColorSpace::compositeOps() const
167
{
168 169 170 171 172 173 174 175 176 177 178
    return d->compositeOps.values();
}


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


KoConvolutionOp* KoColorSpace::convolutionOp() const {
    return d->convolutionOp;
179 180
}

Cyrille Berger's avatar
Cyrille Berger committed
181
const KoCompositeOp * KoColorSpace::compositeOp(const QString & id) const
182
{
183 184
    if ( d->compositeOps.contains( id ) )
        return d->compositeOps.value( id );
185
    else {
Boudewijn Rempt's avatar
Boudewijn Rempt committed
186
        warnPigment << "Asking for non-existent composite operation " << id << ", returning " << COMPOSITE_OVER;
187
        return d->compositeOps.value( COMPOSITE_OVER );
188
    }
189 190 191 192 193
}

void KoColorSpace::addCompositeOp(const KoCompositeOp * op)
{
    if ( op->colorSpace()->id() == id()) {
194
        d->compositeOps.insert( op->id(), const_cast<KoCompositeOp*>( op ) );
195 196 197
    }
}

198
const KoColorConversionTransformation* KoColorSpace::toLabA16Converter() const
199
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
200
    if(!d->transfoToLABA16)
201
    {
202
        d->transfoToLABA16 = KoColorSpaceRegistry::instance()->colorConversionSystem()->createColorConverter(this, KoColorSpaceRegistry::instance()->lab16("") ) ;
203
    }
204
    return d->transfoToLABA16;
205
}
206

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

252 253
KoColorConversionTransformation* KoColorSpace::createColorConverter(const KoColorSpace * dstColorSpace, KoColorConversionTransformation::Intent renderingIntent) const
{
254
    if( *this == *dstColorSpace)
255
    {
256
        return new KoCopyColorConversionTransformation(this);
257
    } else {
258
        return KoColorSpaceRegistry::instance()->colorConversionSystem()->createColorConverter( this, dstColorSpace, renderingIntent);
259
    }
260 261
}

262
bool KoColorSpace::convertPixelsTo(const quint8 * src,
263 264 265 266
        quint8 * dst,
        const KoColorSpace * dstColorSpace,
        quint32 numPixels,
        KoColorConversionTransformation::Intent renderingIntent) const
267
{
268 269 270 271 272 273 274
    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);
    }
275 276
    return true;
}
277

278

279 280
void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
281
                          const KoColorSpace * srcSpace,
282 283 284 285 286 287 288 289
                          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
290
                          const QBitArray & channelFlags) const
291
{
292 293
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ), channelFlags);
294 295
    }
    else {
296
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ), channelFlags);
297 298 299 300 301 302
    }

}

void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
303
                          const KoColorSpace * srcSpace,
304 305 306 307 308 309 310
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
Cyrille Berger's avatar
Cyrille Berger committed
311
                          const QString& op) const
312
{
313 314
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ));
315 316
    }
    else {
317
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ) );
318 319
    }
}
320 321

void KoColorSpace::bitBlt(quint8 *dst,
322
                          qint32 dstRowStride,
323
                          const KoColorSpace * srcSpace,
324 325 326 327 328 329 330 331 332
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
                          const KoCompositeOp * op,
                          const QBitArray & channelFlags) const
333
{
Cyrille Berger's avatar
Cyrille Berger committed
334
    Q_ASSERT_X(*op->colorSpace() == *this, "KoColorSpace::bitBlt", QString("Composite op is for color space %1 (%2) while this is %3 (%4)" ).arg( op->colorSpace()->id() ).arg(op->colorSpace()->profile()->name()).arg(id()).arg(profile()->name()).toLatin1() );
335

336 337 338
    if (rows <= 0 || cols <= 0)
        return;

339
    if (!(*this == *srcSpace)) {
340

341 342 343
	quint32 conversionBufferStride = cols * pixelSize();
        QVector<quint8> * conversionCache = 
	    threadLocalConversionCache(rows * conversionBufferStride);
344

345
	quint8* conversionData = conversionCache->data();
346 347 348

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
349 350
                                      conversionData + row * conversionBufferStride,
				      this, cols);
351 352
        }

353 354
        op->composite( dst, dstRowStride,
                       conversionData, conversionBufferStride,
355 356 357 358 359 360
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity, channelFlags );

    }
    else {
361
        op->composite( dst, dstRowStride,
362 363 364 365 366 367 368
                       src, srcRowStride,
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity, channelFlags );
    }
}

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

386 387 388 389
    if (rows <= 0 || cols <= 0)
        return;

    if (this != srcSpace) {
390 391 392
	quint32 conversionBufferStride = cols * pixelSize();
        QVector<quint8> * conversionCache = 
	    threadLocalConversionCache(rows * conversionBufferStride);
393

394
        quint8* conversionData = conversionCache->data();
395 396 397

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
398
                                      conversionData + row * conversionBufferStride, this,
399 400 401
                                      cols);
        }

402 403
        op->composite( dst, dstRowStride,
                       conversionData, conversionBufferStride,
404 405 406 407 408 409
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity);

    }
    else {
410 411
        op->composite( dst, dstRowStride,
                       src, srcRowStride,
412
                       srcAlphaMask, maskRowStride,
413
                       rows, cols,
414 415 416
                       opacity);
    }
}
417 418 419 420 421

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

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

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
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;
}