KoColorSpace.cpp 10.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 "KoColorSpace.h"
21 22 23

#include <kdebug.h>

24
#include "KoCompositeOp.h"
Cyrille Berger's avatar
Cyrille Berger committed
25
#include "KoColorTransformation.h"
26

27 28 29 30 31 32 33 34 35
struct KoColorSpace::Private {
    QString id;
    QString name;
    QHash<QString, KoCompositeOp *> compositeOps;
    KoColorSpaceRegistry * parent;
    Q3ValueVector<KoChannelInfo *> channels;
    KoMixColorsOp* mixColorsOp;
    KoConvolutionOp* convolutionOp;
    mutable Q3MemArray<quint8> conversionCache; // XXX: This will be a bad problem when we have threading.
36 37
    DWORD cmType;                           // The colorspace type as defined by littlecms
    icColorSpaceSignature colorSpaceSignature; // The colorspace signature as defined in icm/icc files
38 39
};

Stefan Nikolaus's avatar
Stefan Nikolaus committed
40 41 42 43 44
KoColorSpace::KoColorSpace()
    : d (new Private())
{
}

45
KoColorSpace::KoColorSpace(const QString &id, const QString &name, KoColorSpaceRegistry * parent, KoMixColorsOp* mixColorsOp, KoConvolutionOp* convolutionOp, DWORD cmType, icColorSpaceSignature colorSpaceSignature )
46
    : d (new Private())
47
{
48 49 50 51 52 53 54
    d->id = id;
    d->name = name;
    d->parent = parent;
    d->mixColorsOp = mixColorsOp;
    d->convolutionOp = convolutionOp;
    d->cmType = cmType;
    d->colorSpaceSignature = colorSpaceSignature;
55 56
}

57
KoColorSpace::~KoColorSpace()
58
{
59 60 61 62 63 64 65 66 67
    delete d->mixColorsOp;
    delete d->convolutionOp;
    delete d;
}

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

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

68 69 70
quint32 KoColorSpace::colorSpaceType() const { return d->cmType; }
icColorSpaceSignature KoColorSpace::colorSpaceSignature() const { return d->colorSpaceSignature; }

71 72 73 74 75 76 77 78
Q3ValueVector<KoChannelInfo *> KoColorSpace::channels() const
{
    return d->channels;
}

void KoColorSpace::addChannel(KoChannelInfo * ci)
{
    d->channels.push_back(ci);
79 80
}

81
quint8 *KoColorSpace::allocPixelBuffer(quint32 numPixels) const
82 83 84 85
{
    return new quint8[pixelSize()*numPixels];
}

86
QList<KoCompositeOp*> KoColorSpace::userVisiblecompositeOps() const
87
{
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    return d->compositeOps.values();
}

void KoColorSpace::mixColors(const quint8 **colors, const quint8 *weights, quint32 nColors, quint8 *dst) const
{
    Q_ASSERT(d->mixColorsOp);
    d->mixColorsOp->mixColors(colors, weights, nColors, dst);
}

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

void KoColorSpace::convolveColors(quint8** colors, qint32* kernelValues, KoChannelInfo::enumChannelFlags channelFlags, quint8 *dst, qint32 factor, qint32 offset, qint32 nPixels) const
{
    Q_ASSERT(d->convolutionOp);
    d->convolutionOp->convolveColors(colors, kernelValues, channelFlags, dst, factor, offset, nPixels);
}

KoConvolutionOp* KoColorSpace::convolutionOp() const {
    return d->convolutionOp;
109 110
}

Cyrille Berger's avatar
Cyrille Berger committed
111
const KoCompositeOp * KoColorSpace::compositeOp(const QString & id) const
112
{
113 114
    if ( d->compositeOps.contains( id ) )
        return d->compositeOps.value( id );
115
    else
116
        return d->compositeOps.value( COMPOSITE_OVER );
117 118 119 120 121
}

void KoColorSpace::addCompositeOp(const KoCompositeOp * op)
{
    if ( op->colorSpace()->id() == id()) {
122
        d->compositeOps.insert( op->id(), const_cast<KoCompositeOp*>( op ) );
123 124 125 126
    }
}


127
bool KoColorSpace::convertPixelsTo(const quint8 * src,
128
                                   quint8 * dst,
Cyrille Berger's avatar
Cyrille Berger committed
129
                                   const KoColorSpace * dstColorSpace,
130
                                   quint32 numPixels,
Cyrille Berger's avatar
Cyrille Berger committed
131
                                   qint32 renderingIntent) const
132
{
133
    Q_UNUSED(renderingIntent);
134
    // 4 channels: labA, 2 bytes per lab channel
135
    quint8 *pixels = new quint8[sizeof(quint16)*4*numPixels];
136 137 138 139 140 141 142
    toLabA16(src, pixels,numPixels);
    dstColorSpace->fromLabA16(pixels, dst,numPixels);

    delete [] pixels;

    return true;
}
143 144 145 146 147 148 149 150 151 152 153 154

void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
                          KoColorSpace * srcSpace,
                          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
155
                          const QBitArray & channelFlags) const
156
{
157 158
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ), channelFlags);
159 160
    }
    else {
161
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ), channelFlags);
162 163 164 165 166 167 168 169 170 171 172 173 174 175
    }

}

void KoColorSpace::bitBlt(quint8 *dst,
                          qint32 dststride,
                          KoColorSpace * srcSpace,
                          const quint8 *src,
                          qint32 srcRowStride,
                          const quint8 *srcAlphaMask,
                          qint32 maskRowStride,
                          quint8 opacity,
                          qint32 rows,
                          qint32 cols,
Cyrille Berger's avatar
Cyrille Berger committed
176
                          const QString& op) const
177
{
178 179
    if ( d->compositeOps.contains( op ) ) {
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( op ));
180 181
    }
    else {
182
        bitBlt(dst, dststride, srcSpace, src, srcRowStride, srcAlphaMask, maskRowStride, opacity, rows, cols, d->compositeOps.value( COMPOSITE_OVER ) );
183 184
    }
}
185 186 187 188 189 190 191 192 193 194 195 196

void KoColorSpace::bitBlt(quint8 *dst,
                                   qint32 dststride,
                                   KoColorSpace * srcSpace,
                                   const quint8 *src,
                                   qint32 srcRowStride,
                                   const quint8 *srcAlphaMask,
                                   qint32 maskRowStride,
                                   quint8 opacity,
                                   qint32 rows,
                                   qint32 cols,
                                   const KoCompositeOp * op,
Cyrille Berger's avatar
Cyrille Berger committed
197
                                   const QBitArray & channelFlags) const
198 199 200 201 202 203 204 205
{
    if (rows <= 0 || cols <= 0)
        return;

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

        // If our conversion cache is too small, extend it.
206
        if (!d->conversionCache.resize( len, Q3GArray::SpeedOptim )) {
207 208 209 210 211 212 213
            kWarning() << "Could not allocate enough memory for the conversion!\n";
            // XXX: We should do a slow, pixel by pixel bitblt here...
            abort();
        }

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
214
                                      d->conversionCache.data() + row * cols * pixelSize(), this,
215 216 217 218 219 220 221
                                      cols);
        }

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

        op->composite( dst, dststride,
222
                       d->conversionCache.data(), srcRowStride,
223 224 225 226 227 228 229 230 231 232 233 234 235 236
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity, channelFlags );

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

237
// XXX: I don't want this code duplication, but also don't want an
238 239 240 241 242 243 244 245 246 247 248 249
//      extra function call in this critical section of code. What to
//      do?
void KoColorSpace::bitBlt(quint8 *dst,
                                   qint32 dststride,
                                   KoColorSpace * srcSpace,
                                   const quint8 *src,
                                   qint32 srcRowStride,
                                   const quint8 *srcAlphaMask,
                                   qint32 maskRowStride,
                                   quint8 opacity,
                                   qint32 rows,
                                   qint32 cols,
Cyrille Berger's avatar
Cyrille Berger committed
250
                                   const KoCompositeOp * op) const
251 252 253 254 255 256 257 258
{
    if (rows <= 0 || cols <= 0)
        return;

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

        // If our conversion cache is too small, extend it.
259
        if (!d->conversionCache.resize( len, Q3GArray::SpeedOptim )) {
260 261 262 263 264 265 266
            kWarning() << "Could not allocate enough memory for the conversion!\n";
            // XXX: We should do a slow, pixel by pixel bitblt here...
            abort();
        }

        for (qint32 row = 0; row < rows; row++) {
            srcSpace->convertPixelsTo(src + row * srcRowStride,
267
                                      d->conversionCache.data() + row * cols * pixelSize(), this,
268 269 270 271 272 273 274
                                      cols);
        }

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

        op->composite( dst, dststride,
275
                       d->conversionCache.data(), srcRowStride,
276 277 278 279 280 281 282 283 284 285 286 287 288
                       srcAlphaMask, maskRowStride,
                       rows,  cols,
                       opacity);

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