KoColorSpace.cpp 15 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
{
76
    KoColorSpaceRegistry::instance()->colorConversionCache()->colorSpaceIsDestroyed(this);
77 78 79 80 81
    delete d->mixColorsOp;
    delete d->convolutionOp;
    delete d;
}

82 83 84
bool KoColorSpace::operator==(const KoColorSpace& rhs) const {
    const KoColorProfile* p1 = rhs.profile();
    const KoColorProfile* p2 = profile();
85
    return id() == rhs.id() and ((p1 == p2) or (*p1 == *p2 ));
86 87
}

88 89 90 91
QString KoColorSpace::id() const {return d->id;}

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

Adrian Page's avatar
q3--  
Adrian Page committed
92
QList<KoChannelInfo *> KoColorSpace::channels() const
93 94 95 96
{
    return d->channels;
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
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;
}

113
QBitArray KoColorSpace::setChannelFlagsToPixelOrder(const QBitArray & origChannelFlags) const
114
{
115 116 117
    if ( origChannelFlags.isEmpty() ) return origChannelFlags;

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

120 121 122 123
        KoChannelInfo * channel = d->channels.at( i );
        orderedChannelFlags.setBit( channel->pos(), origChannelFlags.testBit( i ) );
    }
    return orderedChannelFlags;
124 125 126 127 128 129 130 131 132 133 134 135 136
}

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;
137 138
}

139 140 141
void KoColorSpace::addChannel(KoChannelInfo * ci)
{
    d->channels.push_back(ci);
142 143
}

144
quint8 *KoColorSpace::allocPixelBuffer(quint32 numPixels) const
145 146 147 148
{
    return new quint8[pixelSize()*numPixels];
}

149
QList<KoCompositeOp*> KoColorSpace::compositeOps() const
150
{
151 152 153 154 155 156 157 158 159 160 161
    return d->compositeOps.values();
}


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


KoConvolutionOp* KoColorSpace::convolutionOp() const {
    return d->convolutionOp;
162 163
}

Cyrille Berger's avatar
Cyrille Berger committed
164
const KoCompositeOp * KoColorSpace::compositeOp(const QString & id) const
165
{
166 167
    if ( d->compositeOps.contains( id ) )
        return d->compositeOps.value( id );
168
    else
169
        return d->compositeOps.value( COMPOSITE_OVER );
170 171 172 173 174
}

void KoColorSpace::addCompositeOp(const KoCompositeOp * op)
{
    if ( op->colorSpace()->id() == id()) {
175
        d->compositeOps.insert( op->id(), const_cast<KoCompositeOp*>( op ) );
176 177 178
    }
}

179
const KoColorConversionTransformation* KoColorSpace::toLabA16Converter() const
180 181 182
{
    if(not d->transfoToLABA16)
    {
183
        d->transfoToLABA16 = KoColorSpaceRegistry::instance()->colorConversionSystem()->createColorConverter(this, KoColorSpaceRegistry::instance()->lab16("") ) ;
184
    }
185
    return d->transfoToLABA16;
186
}
187

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

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

243
bool KoColorSpace::convertPixelsTo(const quint8 * src,
244 245 246 247
        quint8 * dst,
        const KoColorSpace * dstColorSpace,
        quint32 numPixels,
        KoColorConversionTransformation::Intent renderingIntent) const
248
{
249 250 251 252 253 254 255
    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);
    }
256 257
    return true;
}
258

259

260 261
void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
262
                          const KoColorSpace * srcSpace,
263 264 265 266 267 268 269 270
                          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
271
                          const QBitArray & channelFlags) const
272
{
273 274
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ), channelFlags);
275 276
    }
    else {
277
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ), channelFlags);
278 279 280 281 282 283
    }

}

void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
284
                          const KoColorSpace * srcSpace,
285 286 287 288 289 290 291
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
Cyrille Berger's avatar
Cyrille Berger committed
292
                          const QString& op) const
293
{
294 295
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ));
296 297
    }
    else {
298
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ) );
299 300
    }
}
301 302

void KoColorSpace::bitBlt(quint8 *dst,
303
                          qint32 dststride,
304
                          const KoColorSpace * srcSpace,
305 306 307 308 309 310 311 312 313
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
                          const KoCompositeOp * op,
                          const QBitArray & channelFlags) const
314
{
Cyrille Berger's avatar
Cyrille Berger committed
315
    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() );
316

317 318 319
    if (rows <= 0 || cols <= 0)
        return;

320 321
    if (!(*this == *srcSpace)) {
        
322 323
        quint32 len = pixelSize() * rows * cols;

324 325
        QVector<quint8> * conversionCache = threadLocalConversionCache(len);
        quint8* conversionData = conversionCache->data();
326 327 328

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
329
                                      conversionData + row * cols * pixelSize(), this,
330 331 332 333 334 335 336
                                      cols);
        }

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

        op->composite( dst, dststride,
337
                       conversionData, srcRowStride,
338 339 340 341 342 343 344 345 346 347 348 349 350 351
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity, channelFlags );

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

352
// XXX: I don't want this code duplication, but also don't want an
353 354 355
//      extra function call in this critical section of code. What to
//      do?
void KoColorSpace::bitBlt(quint8 *dst,
356
                          qint32 dststride,
357
                          const KoColorSpace * srcSpace,
358 359 360 361 362 363 364 365
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
                          const KoCompositeOp * op) const
366
{
367
    Q_ASSERT(*op->colorSpace() == *this);
368

369 370 371 372 373 374
    if (rows <= 0 || cols <= 0)
        return;

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

375 376
        QVector<quint8> * conversionCache = threadLocalConversionCache(len);
        quint8* conversionData = conversionCache->data();
377 378 379

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
380
                                      conversionData + row * cols * pixelSize(), this,
381 382 383 384 385 386 387
                                      cols);
        }

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

        op->composite( dst, dststride,
388
                       conversionData, srcRowStride,
389 390 391 392 393 394 395 396 397 398 399 400 401
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity);

    }
    else {
        op->composite( dst, dststride,
                       src,srcRowStride,
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity);
    }
}
402 403 404 405 406

QVector<quint8> * KoColorSpace::threadLocalConversionCache(quint32 size) const
{
    QVector<quint8> * ba = 0;
    if ( !d->conversionCache.hasLocalData() ) {
407
        ba = new QVector<quint8>( size, '0' );
408 409 410 411 412 413 414 415 416
        d->conversionCache.setLocalData( ba );
    }
    else {
        ba = d->conversionCache.localData();
        if ( ( quint8 )ba->size() < size )
            ba->resize( size );
    }
    return ba;
}
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

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);
    }
}
438