Changeset View
Changeset View
Standalone View
Standalone View
libs/pigment/KoColorSpaceTraits.h
Context not available. | |||||
26 | #include "KoColorSpaceMaths.h" | 26 | #include "KoColorSpaceMaths.h" | ||
---|---|---|---|---|---|
27 | #include "DebugPigment.h" | 27 | #include "DebugPigment.h" | ||
28 | 28 | | |||
29 | const int MAX_CHANNELS_TYPE_SIZE = sizeof(double); | ||||
30 | const int MAX_CHANNELS_NB = 5; | ||||
31 | const int MAX_PIXEL_SIZE = MAX_CHANNELS_NB * MAX_CHANNELS_TYPE_SIZE; | ||||
32 | | ||||
29 | /** | 33 | /** | ||
30 | * This class is the base class to define the main characteristics of a colorspace | 34 | * This class is the base class to define the main characteristics of a colorspace | ||
31 | * which inherits KoColorSpaceAbstract. | 35 | * which inherits KoColorSpaceAbstract. | ||
Context not available. | |||||
36 | * - _channels_nb_ is the total number of channels in an image (for example RGB is 3 but RGBA is four) | 40 | * - _channels_nb_ is the total number of channels in an image (for example RGB is 3 but RGBA is four) | ||
37 | * - _alpha_pos_ is the position of the alpha channel among the channels, if there is no alpha channel, | 41 | * - _alpha_pos_ is the position of the alpha channel among the channels, if there is no alpha channel, | ||
38 | * then _alpha_pos_ is set to -1 | 42 | * then _alpha_pos_ is set to -1 | ||
39 | * | 43 | * | ||
40 | * For instance a colorspace of three color channels and alpha channel in 16bits, | 44 | * For instance a colorspace of three color channels and alpha channel in 16bits, | ||
41 | * will be defined as KoColorSpaceTrait\<quint16, 4, 3\>. The same without the alpha | 45 | * will be defined as KoColorSpaceTrait\<quint16, 4, 3\>. The same without the alpha | ||
42 | * channel is KoColorSpaceTrait\<quint16,3,-1\> | 46 | * channel is KoColorSpaceTrait\<quint16,3,-1\> | ||
Context not available. | |||||
44 | */ | 48 | */ | ||
45 | template<typename _channels_type_, int _channels_nb_, int _alpha_pos_> | 49 | template<typename _channels_type_, int _channels_nb_, int _alpha_pos_> | ||
46 | struct KoColorSpaceTrait { | 50 | struct KoColorSpaceTrait { | ||
47 | 51 | | |||
52 | static_assert(sizeof(_channels_type_) <= MAX_CHANNELS_TYPE_SIZE, "MAX_CHANNELS_TYPE_SIZE too small"); | ||||
53 | static_assert(_channels_nb_ <= MAX_CHANNELS_NB, "MAX_CHANNELS_NB too small"); | ||||
54 | | ||||
48 | /// the type of the value of the channels of this color space | 55 | /// the type of the value of the channels of this color space | ||
49 | typedef _channels_type_ channels_type; | 56 | typedef _channels_type_ channels_type; | ||
50 | 57 | | |||
51 | /// the number of channels in this color space | 58 | /// the number of channels in this color space | ||
52 | static const quint32 channels_nb = _channels_nb_; | 59 | static const quint32 channels_nb = _channels_nb_; | ||
53 | 60 | | |||
54 | /// the position of the alpha channel in the channels of the pixel (or -1 if no alpha | 61 | /// the position of the alpha channel in the channels of the pixel (or -1 if no alpha | ||
55 | /// channel. | 62 | /// channel. | ||
56 | static const qint32 alpha_pos = _alpha_pos_; | 63 | static const qint32 alpha_pos = _alpha_pos_; | ||
57 | 64 | | |||
58 | /// the number of bit for each channel | 65 | /// the number of bit for each channel | ||
59 | static const int depth = KoColorSpaceMathsTraits<_channels_type_>::bits; | 66 | static const int depth = KoColorSpaceMathsTraits<_channels_type_>::bits; | ||
60 | 67 | | |||
61 | /** | 68 | /** | ||
62 | * @return the size in byte of one pixel | 69 | * @return the size in byte of one pixel | ||
63 | */ | 70 | */ | ||
64 | static const quint32 pixelSize = channels_nb * sizeof(channels_type); | 71 | static const quint32 pixelSize = channels_nb * sizeof(channels_type); | ||
65 | 72 | | |||
66 | /** | 73 | /** | ||
67 | * @return the value of the alpha channel for this pixel in the 0..255 range | 74 | * @return the value of the alpha channel for this pixel in the 0..255 range | ||
68 | */ | 75 | */ | ||
Context not available. | |||||
71 | channels_type c = nativeArray(U8_pixel)[alpha_pos]; | 78 | channels_type c = nativeArray(U8_pixel)[alpha_pos]; | ||
72 | return KoColorSpaceMaths<channels_type, quint8>::scaleToA(c); | 79 | return KoColorSpaceMaths<channels_type, quint8>::scaleToA(c); | ||
73 | } | 80 | } | ||
74 | 81 | | |||
75 | inline static qreal opacityF(const quint8 * U8_pixel) { | 82 | inline static qreal opacityF(const quint8 * U8_pixel) { | ||
76 | if (alpha_pos < 0) return OPACITY_OPAQUE_F; | 83 | if (alpha_pos < 0) return OPACITY_OPAQUE_F; | ||
77 | channels_type c = nativeArray(U8_pixel)[alpha_pos]; | 84 | channels_type c = nativeArray(U8_pixel)[alpha_pos]; | ||
78 | return KoColorSpaceMaths<channels_type, qreal>::scaleToA(c); | 85 | return KoColorSpaceMaths<channels_type, qreal>::scaleToA(c); | ||
79 | } | 86 | } | ||
80 | 87 | | |||
81 | /** | 88 | /** | ||
82 | * Set the alpha channel for this pixel from a value in the 0..255 range | 89 | * Set the alpha channel for this pixel from a value in the 0..255 range | ||
83 | */ | 90 | */ | ||
Context not available. | |||||
89 | nativeArray(pixels)[alpha_pos] = valpha; | 96 | nativeArray(pixels)[alpha_pos] = valpha; | ||
90 | } | 97 | } | ||
91 | } | 98 | } | ||
92 | 99 | | |||
93 | inline static void setOpacity(quint8 * pixels, qreal alpha, qint32 nPixels) { | 100 | inline static void setOpacity(quint8 * pixels, qreal alpha, qint32 nPixels) { | ||
94 | if (alpha_pos < 0) return; | 101 | if (alpha_pos < 0) return; | ||
95 | qint32 psize = pixelSize; | 102 | qint32 psize = pixelSize; | ||
Context not available. | |||||
98 | nativeArray(pixels)[alpha_pos] = valpha; | 105 | nativeArray(pixels)[alpha_pos] = valpha; | ||
99 | } | 106 | } | ||
100 | } | 107 | } | ||
101 | 108 | | |||
102 | /** | 109 | /** | ||
103 | * Convenient function for transforming a quint8* array in a pointer of the native channels type | 110 | * Convenient function for transforming a quint8* array in a pointer of the native channels type | ||
104 | */ | 111 | */ | ||
105 | inline static const channels_type* nativeArray(const quint8 * a) { | 112 | inline static const channels_type* nativeArray(const quint8 * a) { | ||
106 | return reinterpret_cast<const channels_type*>(a); | 113 | return reinterpret_cast<const channels_type*>(a); | ||
107 | } | 114 | } | ||
108 | 115 | | |||
109 | /** | 116 | /** | ||
110 | * Convenient function for transforming a quint8* array in a pointer of the native channels type | 117 | * Convenient function for transforming a quint8* array in a pointer of the native channels type | ||
111 | */ | 118 | */ | ||
112 | inline static channels_type* nativeArray(quint8 * a) { | 119 | inline static channels_type* nativeArray(quint8 * a) { | ||
113 | return reinterpret_cast< channels_type*>(a); | 120 | return reinterpret_cast< channels_type*>(a); | ||
114 | } | 121 | } | ||
115 | 122 | | |||
116 | /** | 123 | /** | ||
117 | * Allocate nPixels pixels for this colorspace. | 124 | * Allocate nPixels pixels for this colorspace. | ||
118 | */ | 125 | */ | ||
119 | inline static quint8* allocate(quint32 nPixels) { | 126 | inline static quint8* allocate(quint32 nPixels) { | ||
120 | return new quint8[ nPixels * pixelSize ]; | 127 | return new quint8[ nPixels * pixelSize ]; | ||
121 | } | 128 | } | ||
122 | 129 | | |||
123 | inline static void singleChannelPixel(quint8 *dstPixel, const quint8 *srcPixel, quint32 channelIndex) { | 130 | inline static void singleChannelPixel(quint8 *dstPixel, const quint8 *srcPixel, quint32 channelIndex) { | ||
124 | const channels_type* src = nativeArray(srcPixel); | 131 | const channels_type* src = nativeArray(srcPixel); | ||
125 | channels_type* dst = nativeArray(dstPixel); | 132 | channels_type* dst = nativeArray(dstPixel); | ||
Context not available. | |||||
131 | } | 138 | } | ||
132 | } | 139 | } | ||
133 | } | 140 | } | ||
134 | 141 | | |||
135 | inline static QString channelValueText(const quint8 *pixel, quint32 channelIndex) { | 142 | inline static QString channelValueText(const quint8 *pixel, quint32 channelIndex) { | ||
136 | if (channelIndex > channels_nb) return QString("Error"); | 143 | if (channelIndex > channels_nb) return QString("Error"); | ||
137 | channels_type c = nativeArray(pixel)[channelIndex]; | 144 | channels_type c = nativeArray(pixel)[channelIndex]; | ||
Context not available. |