2
* Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
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
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with this library; see the file COPYING.LIB. If not, write to
16
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
20
#ifndef _KO_CTL_CHANNEL_H_
21
#define _KO_CTL_CHANNEL_H_
25
#include <KoColorSpaceMaths.h>
26
#include <KoColorSpaceConstants.h>
31
virtual ~KoCtlChannel();
32
virtual QString channelValueText(const quint8 *pixel) const = 0;
33
virtual QString normalisedChannelValueText(const quint8 *pixel) const = 0;
34
virtual quint8 scaleToU8(const quint8 * srcPixel) const = 0;
35
virtual void scaleFromU8(quint8 * dstPixel, quint8 value) const = 0;
36
virtual quint16 scaleToU16(const quint8 * srcPixel) const = 0;
37
virtual float scaleToF32(const quint8 * srcPixel) const = 0;
38
virtual void scaleFromF32(quint8 * dstPixel, float value) const = 0;
39
virtual void singleChannelPixel(quint8 *dstPixel, const quint8 *srcPixel) const = 0;
40
virtual void multiplyU8(quint8 * pixels, quint8 alpha, qint32 nPixels) const = 0;
41
virtual void applyU8Mask(quint8 * pixels, const quint8 * alpha, qint32 nPixels) const = 0;
42
virtual void applyInverseU8Mask(quint8 * pixels, const quint8 * alpha, qint32 nPixels) const = 0;
48
template<typename _ChannelType_>
49
class KoCtlChannelImpl : public KoCtlChannel
52
KoCtlChannelImpl(quint32 _pos, quint32 _pixelSize) : m_pos(_pos), m_pixelSize(_pixelSize) { }
53
virtual ~KoCtlChannelImpl() {}
54
virtual QString channelValueText(const quint8* pixel) const {
55
return QString::number(*channel(pixel));
57
virtual QString normalisedChannelValueText(const quint8* pixel) const {
58
return QString::number(scaleToF32(pixel));
60
virtual quint8 scaleToU8(const quint8* srcPixel) const {
61
return KoColorSpaceMaths<_ChannelType_, quint8>::scaleToA(*channel(srcPixel));
63
virtual void scaleFromU8(quint8 * dstPixel, quint8 value) const {
64
*channel(dstPixel) = KoColorSpaceMaths<quint8, _ChannelType_ >::scaleToA(value);
66
virtual quint16 scaleToU16(const quint8* srcPixel) const {
67
return KoColorSpaceMaths<_ChannelType_, quint16>::scaleToA(*channel(srcPixel));
69
virtual float scaleToF32(const quint8* srcPixel) const {
70
return KoColorSpaceMaths<_ChannelType_, float>::scaleToA(*channel(srcPixel));
72
virtual void singleChannelPixel(quint8* dstPixel, const quint8 *srcPixel) const {
73
*channel(dstPixel) = *channel(srcPixel);
75
virtual void scaleFromF32(quint8* dstPixel, float value) const {
76
*channel(dstPixel) = KoColorSpaceMaths<float, _ChannelType_ >::scaleToA(value);
78
virtual void multiplyU8(quint8 * pixels, quint8 alpha, qint32 nPixels) const {
79
_ChannelType_ valpha = KoColorSpaceMaths<quint8, _ChannelType_>::scaleToA(alpha);
81
for (; nPixels > 0; --nPixels, pixels += m_pixelSize) {
82
_ChannelType_* alphapixel = channel(pixels);
83
*alphapixel = KoColorSpaceMaths<_ChannelType_>::multiply(*alphapixel, valpha);
87
virtual void applyU8Mask(quint8 * pixels, const quint8 * alpha, qint32 nPixels) const {
88
for (; nPixels > 0; --nPixels, pixels += m_pixelSize, ++alpha) {
89
_ChannelType_ valpha = KoColorSpaceMaths<quint8, _ChannelType_>::scaleToA(*alpha);
90
_ChannelType_* alphapixel = channel(pixels);
91
*alphapixel = KoColorSpaceMaths<_ChannelType_>::multiply(*alphapixel, valpha);
95
virtual void applyInverseU8Mask(quint8 * pixels, const quint8 * alpha, qint32 nPixels) const {
96
for (; nPixels > 0; --nPixels, pixels += m_pixelSize, ++alpha) {
97
_ChannelType_ valpha = KoColorSpaceMaths<quint8, _ChannelType_>::scaleToA(OPACITY_OPAQUE_U8 - *alpha);
98
_ChannelType_* alphapixel = channel(pixels);
99
*alphapixel = KoColorSpaceMaths<_ChannelType_>::multiply(*alphapixel, valpha);
103
inline const _ChannelType_* channel(const quint8* pixel) const {
104
return reinterpret_cast<const _ChannelType_*>(pixel + m_pos);
106
inline _ChannelType_* channel(quint8* pixel) const {
107
return reinterpret_cast<_ChannelType_*>(pixel + m_pos);