Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_heif_export_tools.h
Go to the documentation of this file.
1
8#ifndef KIS_HEIF_EXPORT_TOOLS_H
9#define KIS_HEIF_EXPORT_TOOLS_H
10
11#include <cstdint>
12
14#include <KoColorProfile.h>
15#include <KoColorSpace.h>
16#include <KoColorSpaceTraits.h>
18#include <kis_iterator_ng.h>
19
20namespace Gray
21{
22template<int endValue0, int endValue1, int luma>
23inline void
24applyValue(const quint8 *data, uint8_t *ptrG, int strideG, int x, int y)
25{
26 if (luma == 8) {
27 ptrG[y * strideG + x] = KoGrayU8Traits::gray(data);
28 } else {
29 uint16_t v = qBound<uint16_t>(
30 0,
31 static_cast<uint16_t>(float(KoGrayU16Traits::gray(data))
33 max12bit);
34 ptrG[(x * 2) + y * strideG + endValue0] = static_cast<uint8_t>(v >> 8);
35 ptrG[(x * 2) + y * strideG + endValue1] =
36 static_cast<uint8_t>(v & 0xFF);
37 }
38}
39
40template<int endValue0, int endValue1, int luma, bool hasAlpha>
41inline void
42applyAlpha(const quint8 *data, uint8_t *ptrA, const int strideA, int x, int y)
43{
44 if (hasAlpha) {
45 if (luma == 8) {
46 ptrA[y * strideA + x] = KoGrayU8Traits::opacityU8(data);
47 } else {
48 uint16_t vA = qBound<uint16_t>(
49 0,
50 static_cast<uint16_t>(KoGrayU16Traits::opacityF(data)
51 * max12bit),
52 max12bit);
53 ptrA[(x * 2) + y * strideA + endValue0] = (uint8_t)(vA >> 8);
54 ptrA[(x * 2) + y * strideA + endValue1] = (uint8_t)(vA & 0xFF);
55 }
56 }
57}
58
59template<int endValue0, int endValue1, int luma, bool hasAlpha>
60inline void writeLayer(const int width,
61 const int height,
62 uint8_t *ptrG,
63 const int strideG,
64 uint8_t *ptrA,
65 const int strideA,
67{
68 for (int y = 0; y < height; y++) {
69 for (int x = 0; x < width; x++) {
70 const uint8_t *data = it->rawDataConst();
71
72 applyValue<endValue0, endValue1, luma>(data, ptrG, strideG, x, y);
73
74 applyAlpha<endValue0, endValue1, luma, hasAlpha>(data,
75 ptrA,
76 strideA,
77 x,
78 y);
79
80 it->nextPixel();
81 }
82
83 it->nextRow();
84 }
85}
86
87template<int endValue0, int endValue1, int luma, typename... Args>
88inline auto writePlanarWithAlpha(bool hasAlpha, Args &&...args)
89{
90 if (hasAlpha) {
91 return Gray::writeLayer<endValue0, endValue1, luma, true>(
92 std::forward<Args>(args)...);
93 } else {
94 return Gray::writeLayer<endValue0, endValue1, luma, false>(
95 std::forward<Args>(args)...);
96 }
97}
98
99template<int endValue0, int endValue1, typename... Args>
100inline auto writePlanarWithLuma(const int luma, Args &&...args)
101{
102 if (luma == 8) {
103 return writePlanarWithAlpha<endValue0, endValue1, 8>(
104 std::forward<Args>(args)...);
105 } else {
106 return writePlanarWithAlpha<endValue0, endValue1, 12>(
107 std::forward<Args>(args)...);
108 }
109}
110
111template<typename... Args>
112inline auto writePlanarLayer(QSysInfo::Endian endian, Args &&...args)
113{
114 if (endian == QSysInfo::LittleEndian) {
115 return Gray::writePlanarWithLuma<1, 0>(std::forward<Args>(args)...);
116 } else {
117 return Gray::writePlanarWithLuma<0, 1>(std::forward<Args>(args)...);
118 }
119}
120} // namespace Gray
121
122namespace Planar
123{
124template<bool hasAlpha>
125inline void writeLayerImpl(const int width,
126 const int height,
127 uint8_t *ptrR,
128 const int strideR,
129 uint8_t *ptrG,
130 const int strideG,
131 uint8_t *ptrB,
132 const int strideB,
133 uint8_t *ptrA,
134 const int strideA,
136{
137 for (int y = 0; y < height; y++) {
138 for (int x = 0; x < width; x++) {
139 const quint8 *data = it->rawDataConst();
140 ptrR[y * strideR + x] = KoBgrU8Traits::red(data);
141 ptrG[y * strideG + x] = KoBgrU8Traits::green(data);
142 ptrB[y * strideB + x] = KoBgrU8Traits::blue(data);
143
144 // If we already employ KoBgrU8Traits to access the iterator,
145 // we can use it for the opacity too. -amyspark
146 if (hasAlpha) {
147 ptrA[y * strideA + x] = KoBgrU8Traits::opacityU8(data);
148 }
149
150 it->nextPixel();
151 }
152
153 it->nextRow();
154 }
155}
156
157template<typename... Args>
158inline auto writeLayer(bool hasAlpha, Args &&...args)
159{
160 if (hasAlpha) {
161 return Planar::writeLayerImpl<true>(std::forward<Args>(args)...);
162 } else {
163 return Planar::writeLayerImpl<false>(std::forward<Args>(args)...);
164 }
165}
166} // namespace Planar
167
168namespace HDRInt
169{
170template<int endValue0, int endValue1, int channels>
171inline void writeLayerImpl(const int width,
172 const int height,
173 uint8_t *ptr,
174 const int stride,
176{
177 std::array<KoBgrU16Traits::channels_type, channels> pixelValues{};
178
179 for (int y = 0; y < height; y++) {
180 for (int x = 0; x < width; x++) {
181 const quint8 *data = it->rawDataConst();
182 pixelValues[0] = KoBgrU16Traits::red(data);
183 pixelValues[1] = KoBgrU16Traits::green(data);
184 pixelValues[2] = KoBgrU16Traits::blue(data);
185 if (channels == 4) {
186 pixelValues[3] = static_cast<KoBgrU16Traits::channels_type>(
188 }
189
190 for (int ch = 0; ch < channels; ch++) {
191 uint16_t v = qBound<uint16_t>(
192 0,
193 static_cast<uint16_t>(float(pixelValues[ch])
195 max12bit);
196 ptr[2 * (x * channels) + y * stride + endValue0 + (ch * 2)] =
197 static_cast<uint8_t>(v >> 8);
198 ptr[2 * (x * channels) + y * stride + endValue1 + (ch * 2)] =
199 static_cast<uint8_t>(v & 0xFF);
200 }
201
202 it->nextPixel();
203 }
204
205 it->nextRow();
206 }
207}
208
209template<int endValue0, int endValue1, typename... Args>
210inline auto writeInterleavedWithAlpha(bool hasAlpha, Args &&...args)
211{
212 if (hasAlpha) {
213 return HDRInt::writeLayerImpl<endValue0, endValue1, 4>(
214 std::forward<Args>(args)...);
215 } else {
216 return HDRInt::writeLayerImpl<endValue0, endValue1, 3>(
217 std::forward<Args>(args)...);
218 }
219}
220
221template<typename... Args>
222inline auto writeInterleavedLayer(QSysInfo::Endian endian, Args &&...args)
223{
224 if (endian == QSysInfo::LittleEndian) {
225 return writeInterleavedWithAlpha<1, 0>(std::forward<Args>(args)...);
226 } else {
227 return writeInterleavedWithAlpha<0, 1>(std::forward<Args>(args)...);
228 }
229}
230} // namespace HDRInt
231
232namespace HDRFloat
233{
234
235template<ConversionPolicy policy>
236inline float applyCurveAsNeeded(float value)
237{
238 if (policy == ConversionPolicy::ApplyPQ) {
240 } else if (policy == ConversionPolicy::ApplyHLG) {
241 return applyHLGCurve(value);
242 } else if (policy == ConversionPolicy::ApplySMPTE428) {
244 }
245 return value;
246}
247
248template<typename CSTrait,
249 QSysInfo::Endian endianness,
250 int channels,
251 bool convertToRec2020,
252 bool isLinear,
253 ConversionPolicy conversionPolicy,
254 bool removeOOTF>
255inline void writeFloatLayerImpl(const int width,
256 const int height,
257 uint8_t *ptr,
258 const int stride,
260 float hlgGamma,
261 const KoColorSpace *cs)
262{
263 const int endValue0 = endianness == QSysInfo::LittleEndian ? 1 : 0;
264 const int endValue1 = endianness == QSysInfo::LittleEndian ? 0 : 1;
265 QVector<float> pixelValues(4);
266 QVector<qreal> pixelValuesLinear(4);
267 const KoColorProfile *profile = cs->profile();
268 const QVector<qreal> lCoef{cs->lumaCoefficients()};
269 double *src = pixelValuesLinear.data();
270 float *dst = pixelValues.data();
271
272 for (int y = 0; y < height; y++) {
273 for (int x = 0; x < width; x++) {
274 CSTrait::normalisedChannelsValue(it->rawDataConst(), pixelValues);
275 if (!convertToRec2020 && !isLinear) {
276 for (int i = 0; i < 4; i++) {
277 src[i] = static_cast<double>(dst[i]);
278 }
279 profile->linearizeFloatValue(pixelValuesLinear);
280 for (int i = 0; i < 4; i++) {
281 dst[i] = static_cast<float>(src[i]);
282 }
283 }
284
285 if (conversionPolicy == ConversionPolicy::ApplyHLG && removeOOTF) {
286 removeHLGOOTF(dst, lCoef.constData(), hlgGamma);
287 }
288
289 for (int ch = 0; ch < channels; ch++) {
290 uint16_t v = 0;
291 if (ch == CSTrait::alpha_pos) {
292 v = qBound<uint16_t>(
293 0,
294 static_cast<uint16_t>(
295 applyCurveAsNeeded<ConversionPolicy::KeepTheSame>(
296 dst[ch])
297 * max12bit),
298 max12bit);
299 } else {
300 v = qBound<uint16_t>(
301 0,
302 static_cast<uint16_t>(
303 applyCurveAsNeeded<conversionPolicy>(dst[ch])
304 * max12bit),
305 max12bit);
306 }
307 ptr[2 * (x * channels) + y * stride + endValue0 + (ch * 2)] =
308 (uint8_t)(v >> 8);
309 ptr[2 * (x * channels) + y * stride + endValue1 + (ch * 2)] =
310 (uint8_t)(v & 0xFF);
311 }
312
313 it->nextPixel();
314 }
315
316 it->nextRow();
317 }
318}
319template<typename CSTrait,
320 QSysInfo::Endian endianness,
321 int channels,
322 bool convertToRec2020,
323 bool isLinear,
324 ConversionPolicy linearizePolicy,
325 typename... Args>
326inline auto writeInterleavedWithPolicy(bool removeOOTF, Args &&...args)
327{
328 if (removeOOTF) {
329 return writeFloatLayerImpl<CSTrait,
330 endianness,
331 channels,
332 convertToRec2020,
333 isLinear,
334 linearizePolicy,
335 true>(std::forward<Args>(args)...);
336 } else {
337 return writeFloatLayerImpl<CSTrait,
338 endianness,
339 channels,
340 convertToRec2020,
341 isLinear,
342 linearizePolicy,
343 false>(std::forward<Args>(args)...);
344 }
345}
346
347template<typename CSTrait,
348 QSysInfo::Endian endianness,
349 int channels,
350 bool convertToRec2020,
351 bool isLinear,
352 typename... Args>
354 Args &&...args)
355{
356 if (linearizePolicy == ConversionPolicy::ApplyHLG) {
357 return writeInterleavedWithPolicy<CSTrait,
358 endianness,
359 channels,
360 convertToRec2020,
361 isLinear,
362 ConversionPolicy::ApplyHLG>(std::forward<Args>(args)...);
363 } else if (linearizePolicy == ConversionPolicy::ApplyPQ) {
364 return writeInterleavedWithPolicy<CSTrait,
365 endianness,
366 channels,
367 convertToRec2020,
368 isLinear,
369 ConversionPolicy::ApplyPQ>(std::forward<Args>(args)...);
370 } else if (linearizePolicy == ConversionPolicy::ApplySMPTE428) {
371 return writeInterleavedWithPolicy<CSTrait,
372 endianness,
373 channels,
374 convertToRec2020,
375 isLinear,
376 ConversionPolicy::ApplySMPTE428>(std::forward<Args>(args)...);
377 } else {
378 return writeInterleavedWithPolicy<CSTrait,
379 endianness,
380 channels,
381 convertToRec2020,
382 isLinear,
383 ConversionPolicy::KeepTheSame>(std::forward<Args>(args)...);
384 }
385}
386
387template<typename CSTrait,
388 QSysInfo::Endian endianness,
389 int channels,
390 bool convertToRec2020,
391 typename... Args>
392inline auto writeInterleavedWithRec2020(bool isLinear, Args &&...args)
393{
394 if (isLinear) {
395 return writeInterleavedWithLinear<CSTrait,
396 endianness,
397 channels,
398 convertToRec2020,
399 true>(std::forward<Args>(args)...);
400 } else {
401 return writeInterleavedWithLinear<CSTrait,
402 endianness,
403 channels,
404 convertToRec2020,
405 false>(std::forward<Args>(args)...);
406 }
407}
408
409template<typename CSTrait,
410 QSysInfo::Endian endianness,
411 int channels,
412 typename... Args>
413inline auto writeInterleavedWithAlpha(bool convertToRec2020, Args &&...args)
414{
415 if (convertToRec2020) {
416 return writeInterleavedWithRec2020<CSTrait, endianness, channels, true>(
417 std::forward<Args>(args)...);
418 } else {
419 return writeInterleavedWithRec2020<CSTrait,
420 endianness,
421 channels,
422 false>(std::forward<Args>(args)...);
423 }
424}
425
426template<typename CSTrait, QSysInfo::Endian endianness, typename... Args>
427inline auto writeInterleavedWithEndian(bool hasAlpha, Args &&...args)
428{
429 if (hasAlpha) {
430 return writeInterleavedWithAlpha<CSTrait, endianness, 4>(
431 std::forward<Args>(args)...);
432 } else {
433 return writeInterleavedWithAlpha<CSTrait, endianness, 3>(
434 std::forward<Args>(args)...);
435 }
436}
437
438template<typename CSTrait, typename... Args>
439inline auto writeInterleavedWithDepth(QSysInfo::Endian endian, Args &&...args)
440{
441 if (endian == QSysInfo::LittleEndian) {
442 return writeInterleavedWithEndian<CSTrait, QSysInfo::LittleEndian>(
443 std::forward<Args>(args)...);
444 } else {
445 return writeInterleavedWithEndian<CSTrait, QSysInfo::BigEndian>(
446 std::forward<Args>(args)...);
447 }
448}
449
450template<typename... Args>
451inline auto writeInterleavedLayer(const KoID &id, Args &&...args)
452{
453#ifdef HAVE_OPENEXR
454 if (id == Float16BitsColorDepthID) {
455 return writeInterleavedWithDepth<KoBgrF16Traits>(
456 std::forward<Args>(args)...);
457 } else
458#else
459 Q_UNUSED(id);
460#endif
461 {
462 return writeInterleavedWithDepth<KoBgrF32Traits>(
463 std::forward<Args>(args)...);
464 }
465}
466} // namespace HDRFloat
467
468#endif // KIS_HEIF_EXPORT_TOOLS_H
float value(const T *src, size_t ch)
qreal v
const KoID Float16BitsColorDepthID("F16", ki18n("16-bit float/channel"))
ALWAYS_INLINE void removeHLGOOTF(float *rgb, const double *lumaCoefficients, float gamma=1.2f) noexcept
static constexpr float max16bit
static constexpr uint16_t max12bit
ALWAYS_INLINE float applySmpte2048Curve(float x, float refWhite=203.0) noexcept
static constexpr float multiplier16bit
ALWAYS_INLINE float applySMPTE_ST_428Curve(float x) noexcept
ALWAYS_INLINE float applyHLGCurve(float x) noexcept
virtual const quint8 * rawDataConst() const =0
virtual bool nextPixel()=0
virtual void nextRow()=0
QVector< qreal > lumaCoefficients
virtual const KoColorProfile * profile() const =0
Definition KoID.h:30
void applyValue(const quint8 *data, uint8_t *ptrG, int strideG, int x, int y)
auto writePlanarWithAlpha(bool hasAlpha, Args &&...args)
void applyAlpha(const quint8 *data, uint8_t *ptrA, const int strideA, int x, int y)
void writeLayer(const int width, const int height, uint8_t *ptrG, const int strideG, uint8_t *ptrA, const int strideA, KisHLineConstIteratorSP it)
auto writePlanarLayer(QSysInfo::Endian endian, Args &&...args)
auto writePlanarWithLuma(const int luma, Args &&...args)
auto writeInterleavedWithEndian(bool hasAlpha, Args &&...args)
auto writeInterleavedWithLinear(ConversionPolicy linearizePolicy, Args &&...args)
float applyCurveAsNeeded(float value)
auto writeInterleavedWithPolicy(bool removeOOTF, Args &&...args)
auto writeInterleavedWithRec2020(bool isLinear, Args &&...args)
void writeFloatLayerImpl(const int width, const int height, uint8_t *ptr, const int stride, KisHLineConstIteratorSP it, float hlgGamma, const KoColorSpace *cs)
auto writeInterleavedLayer(const KoID &id, Args &&...args)
auto writeInterleavedWithAlpha(bool convertToRec2020, Args &&...args)
auto writeInterleavedWithDepth(QSysInfo::Endian endian, Args &&...args)
auto writeInterleavedLayer(QSysInfo::Endian endian, Args &&...args)
void writeLayerImpl(const int width, const int height, uint8_t *ptr, const int stride, KisHLineConstIteratorSP it)
auto writeInterleavedWithAlpha(bool hasAlpha, Args &&...args)
auto writeLayer(bool hasAlpha, Args &&...args)
void writeLayerImpl(const int width, const int height, uint8_t *ptrR, const int strideR, uint8_t *ptrG, const int strideG, uint8_t *ptrB, const int strideB, uint8_t *ptrA, const int strideA, KisHLineConstIteratorSP it)
static channels_type blue(const quint8 *data)
static channels_type green(const quint8 *data)
static channels_type red(const quint8 *data)
virtual void linearizeFloatValue(QVector< qreal > &Value) const =0
static quint8 opacityU8(const quint8 *U8_pixel)
_channels_type_ channels_type
the type of the value of the channels of this color space
static qreal opacityF(const quint8 *U8_pixel)
static channels_type gray(const quint8 *data)