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 "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

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

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

53 54
};

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

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

73
KoColorSpace::~KoColorSpace()
74
{
75
    KoColorSpaceRegistry::instance()->colorConversionCache()->colorSpaceIsDestroyed(this);
76 77 78 79 80
    delete d->mixColorsOp;
    delete d->convolutionOp;
    delete d;
}

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

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

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

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

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

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

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

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

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

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

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

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


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


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

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

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

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

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

241
bool KoColorSpace::convertPixelsTo(const quint8 * src,
242 243 244 245
        quint8 * dst,
        const KoColorSpace * dstColorSpace,
        quint32 numPixels,
        KoColorConversionTransformation::Intent renderingIntent) const
246
{
247 248
    KoCachedColorConversionTransformation cct = KoColorSpaceRegistry::instance()->colorConversionCache()->cachedConverter(this, dstColorSpace, renderingIntent);
    cct.transformation()->transform(src, dst, numPixels);
249 250
    return true;
}
251

252

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

}

void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
277
                          const KoColorSpace * srcSpace,
278 279 280 281 282 283 284
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
Cyrille Berger's avatar
Cyrille Berger committed
285
                          const QString& op) const
286
{
287 288
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ));
289 290
    }
    else {
291
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ) );
292 293
    }
}
294 295

void KoColorSpace::bitBlt(quint8 *dst,
296
                          qint32 dststride,
297
                          const KoColorSpace * srcSpace,
298 299 300 301 302 303 304 305 306
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
                          const KoCompositeOp * op,
                          const QBitArray & channelFlags) const
307
{
308
    Q_ASSERT(*op->colorSpace() == *this);
309

310 311 312 313 314 315
    if (rows <= 0 || cols <= 0)
        return;

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

316 317
        QVector<quint8> * conversionCache = threadLocalConversionCache(len);
        quint8* conversionData = conversionCache->data();
318 319 320

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
321
                                      conversionData + row * cols * pixelSize(), this,
322 323 324 325 326 327 328
                                      cols);
        }

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

        op->composite( dst, dststride,
329
                       conversionData, srcRowStride,
330 331 332 333 334 335 336 337 338 339 340 341 342 343
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity, channelFlags );

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

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

361 362 363 364 365 366
    if (rows <= 0 || cols <= 0)
        return;

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

367 368
        QVector<quint8> * conversionCache = threadLocalConversionCache(len);
        quint8* conversionData = conversionCache->data();
369 370 371

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
372
                                      conversionData + row * cols * pixelSize(), this,
373 374 375 376 377 378 379
                                      cols);
        }

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

        op->composite( dst, dststride,
380
                       conversionData, srcRowStride,
381 382 383 384 385 386 387 388 389 390 391 392 393
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity);

    }
    else {
        op->composite( dst, dststride,
                       src,srcRowStride,
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity);
    }
}
394 395 396 397 398

QVector<quint8> * KoColorSpace::threadLocalConversionCache(quint32 size) const
{
    QVector<quint8> * ba = 0;
    if ( !d->conversionCache.hasLocalData() ) {
399
        ba = new QVector<quint8>( size, '0' );
400 401 402 403 404 405 406 407 408
        d->conversionCache.setLocalData( ba );
    }
    else {
        ba = d->conversionCache.localData();
        if ( ( quint8 )ba->size() < size )
            ba->resize( size );
    }
    return ba;
}
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

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