~ubuntu-branches/ubuntu/oneiric/koffice/oneiric-updates

« back to all changes in this revision

Viewing changes to libs/pigment/KoColorSpaceMaths.h

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (c) 2006-2007 Cyrille Berger <cberger@cberger.bet
 
2
 *  Copyright (c) 2006,2007,2010 Cyrille Berger <cberger@cberger.bet
3
3
 *
4
4
 * This library is free software; you can redistribute it and/or
5
5
 * modify it under the terms of the GNU Lesser General Public
23
23
#include "pigment_export.h"
24
24
#include <KoIntegerMaths.h>
25
25
#include "KoChannelInfo.h"
 
26
#include "KoLut.h"
26
27
 
27
28
#undef _T
28
29
 
104
105
    static const KoChannelInfo::enumChannelValueType channelValueType;
105
106
};
106
107
 
107
 
#include <config-openexr.h>
 
108
#include <KoConfig.h>
108
109
#ifdef HAVE_OPENEXR
109
110
#include <half.h>
110
111
 
151
152
    static const KoChannelInfo::enumChannelValueType channelValueType;
152
153
};
153
154
 
 
155
#ifdef Q_CC_MSVC
 
156
// MSVC do not have lrint
 
157
 
 
158
const double _double2fixmagic = 68719476736.0*1.5;
 
159
const qint32 _shiftamt        = 16;                    //16.16 fixed point representation,
 
160
 
 
161
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
 
162
        #define iexp_                           0
 
163
        #define iman_                           1
 
164
#else
 
165
        #define iexp_                           1
 
166
        #define iman_                           0
 
167
#endif //BigEndian_
 
168
 
 
169
inline int float2int(double val)
 
170
{
 
171
    val = val + _double2fixmagic;
 
172
    return ((int*)&val)[iman_] >> _shiftamt; 
 
173
}
 
174
 
 
175
inline int float2int(float val)
 
176
{
 
177
    return float2int((double)val);
 
178
}
 
179
 
 
180
#else
 
181
#include <cmath>
 
182
 
 
183
inline int float2int(float x)
 
184
{
 
185
    return lrintf(x);
 
186
}
 
187
 
 
188
inline int float2int(double x)
 
189
{
 
190
    return lrint(x);
 
191
}
 
192
 
 
193
#endif
 
194
 
 
195
#include <KoLut.h>
 
196
 
 
197
template<typename _T_>
 
198
struct KoIntegerToFloat {
 
199
  inline float operator()(_T_ f) const
 
200
  {
 
201
    return f / float(KoColorSpaceMathsTraits<_T_>::max);
 
202
  }
 
203
};
 
204
 
 
205
 
 
206
extern PIGMENTCMS_EXPORT const Ko::FullLut< KoIntegerToFloat<quint16>, float, quint16> KoUint16ToFloatLut;
 
207
extern PIGMENTCMS_EXPORT const Ko::FullLut< KoIntegerToFloat<quint8>, float, quint8> KoUint8ToFloatLut;
 
208
 
 
209
 
154
210
/**
155
211
 * This class defines some elementary operations used by various color
156
212
 * space. It's intended to be generic, but some specialization exists
170
226
            typename  KoColorSpaceMathsTraits<_Tdst>::compositetype b) {
171
227
        return ((traits_compositetype)a * b) /  KoColorSpaceMathsTraits<_Tdst>::unitValue;
172
228
    }
 
229
    
 
230
    inline static traits_compositetype multiply(traits_compositetype a,
 
231
            typename  KoColorSpaceMathsTraits<_Tdst>::compositetype b,
 
232
            typename  KoColorSpaceMathsTraits<_Tdst>::compositetype c) {
 
233
        return ((traits_compositetype)a * b * c) / (KoColorSpaceMathsTraits<_Tdst>::unitValue * KoColorSpaceMathsTraits<_Tdst>::unitValue);
 
234
    }
173
235
 
174
236
    /**
175
237
     * Division : (a * MAX ) / b
187
249
     * @param alpha
188
250
     */
189
251
    inline static _T blend(_T a, _T b, _T alpha) {
190
 
        traits_compositetype c = (((traits_compositetype)a - (traits_compositetype)b) * alpha) >> traits::bits;
 
252
        traits_compositetype c = (((traits_compositetype)a - (traits_compositetype)b) * alpha) / traits::unitValue;
191
253
        return c + b;
192
254
    }
193
255
 
210
272
inline quint8 KoColorSpaceMaths<double, quint8>::scaleToA(double a)
211
273
{
212
274
    double v = a * 255;
213
 
    return (quint8)(CLAMP(v, 0, 255));
 
275
    return float2int(CLAMP(v, 0, 255));
214
276
}
215
277
 
216
278
template<>
217
279
inline double KoColorSpaceMaths<quint8, double>::scaleToA(quint8 a)
218
280
{
219
 
    return a *(1.0 / 255.0);
 
281
    return KoUint8ToFloatLut(a);
220
282
}
221
283
 
222
284
template<>
223
285
inline quint16 KoColorSpaceMaths<double, quint16>::scaleToA(double a)
224
286
{
225
287
    double v = a * 0xFFFF;
226
 
    return (quint16)(CLAMP(v, 0, 0xFFFF));
 
288
    return float2int(CLAMP(v, 0, 0xFFFF));
227
289
}
228
290
 
229
291
template<>
230
292
inline double KoColorSpaceMaths<quint16, double>::scaleToA(quint16 a)
231
293
{
232
 
    return a *(1.0 / 0xFFFF);
 
294
    return KoUint16ToFloatLut(a);
233
295
}
234
296
 
235
297
template<>
256
318
inline quint16 KoColorSpaceMaths<float, quint16>::scaleToA(float a)
257
319
{
258
320
    float v = a * 0xFFFF;
259
 
    return (quint16)(CLAMP(v, 0, 0xFFFF));
 
321
    return (quint16)float2int(CLAMP(v, 0, 0xFFFF));
260
322
}
261
323
 
262
324
template<>
263
325
inline float KoColorSpaceMaths<quint16, float>::scaleToA(quint16 a)
264
326
{
265
 
    return a *(1.0 / 0xFFFF);
 
327
    return KoUint16ToFloatLut(a);
266
328
}
267
329
 
268
330
template<>
269
331
inline quint8 KoColorSpaceMaths<float, quint8>::scaleToA(float a)
270
332
{
271
333
    float v = a * 255;
272
 
    return (quint8)(CLAMP(v, 0, 255));
 
334
    return (quint8)float2int(CLAMP(v, 0, 255));
273
335
}
274
336
 
275
337
template<>
276
338
inline float KoColorSpaceMaths<quint8, float>::scaleToA(quint8 a)
277
339
{
278
 
    return a *(1.0 / 255.0);
 
340
    return KoUint8ToFloatLut(a);
279
341
}
280
342
 
281
343
template<>
364
426
 
365
427
#endif
366
428
 
 
429
//------------------------------ quint8 specialization ------------------------------//
 
430
 
 
431
template<>
 
432
inline qint32 KoColorSpaceMaths<quint8>::multiply(qint32 a, qint32 b)
 
433
{
 
434
    return UINT8_MULT(a, b);
 
435
}
 
436
 
 
437
 
 
438
template<>
 
439
inline qint32 KoColorSpaceMaths<quint8>::multiply(qint32 a, qint32 b, qint32 c)
 
440
{
 
441
    return UINT8_MULT3(a, b, c);
 
442
}
 
443
 
 
444
template<>
 
445
inline quint8 KoColorSpaceMaths<quint8>::divide(quint8 a, quint8 b)
 
446
{
 
447
    return UINT8_DIVIDE(a, b);
 
448
}
 
449
 
 
450
template<>
 
451
inline quint8 KoColorSpaceMaths<quint8>::blend(quint8 a, quint8 b, quint8 c)
 
452
{
 
453
    return UINT8_BLEND(a, b, c);
 
454
}
 
455
 
 
456
//------------------------------ quint16 specialization ------------------------------//
 
457
 
 
458
template<>
 
459
inline qint64 KoColorSpaceMaths<quint16>::multiply(qint64 a, qint64 b)
 
460
{
 
461
    return UINT16_MULT(a, b);
 
462
}
 
463
 
 
464
template<>
 
465
inline quint16 KoColorSpaceMaths<quint16>::divide(quint16 a, quint16 b)
 
466
{
 
467
    return UINT16_DIVIDE(a, b);
 
468
}
 
469
 
367
470
//------------------------------ various specialization ------------------------------//
368
471
 
369
472