Changeset View
Changeset View
Standalone View
Standalone View
plugins/color/lcms2engine/LcmsRGBP2020PQColorSpace.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2019 Dmitry Kazakov <dimula73@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program 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 | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #ifndef LCMSRGBP2020PQCOLORSPACE_H | ||||
20 | #define LCMSRGBP2020PQCOLORSPACE_H | ||||
21 | | ||||
22 | #include <colorspaces/rgb_u8/RgbU8ColorSpace.h> | ||||
23 | #include <colorspaces/rgb_u16/RgbU16ColorSpace.h> | ||||
24 | #include <colorspaces/rgb_f16/RgbF16ColorSpace.h> | ||||
25 | #include <colorspaces/rgb_f32/RgbF32ColorSpace.h> | ||||
26 | | ||||
27 | #include "KoColorConversionTransformationFactory.h" | ||||
28 | | ||||
29 | #include <LcmsRGBP2020PQColorSpaceTransformation.h> | ||||
30 | | ||||
31 | template <class T> | ||||
32 | struct ColorSpaceFromFactory { | ||||
33 | }; | ||||
34 | | ||||
35 | template<> | ||||
36 | struct ColorSpaceFromFactory<RgbU8ColorSpaceFactory> { | ||||
37 | typedef RgbU8ColorSpace type; | ||||
38 | }; | ||||
39 | | ||||
40 | template<> | ||||
41 | struct ColorSpaceFromFactory<RgbU16ColorSpaceFactory> { | ||||
42 | typedef RgbU16ColorSpace type; | ||||
43 | }; | ||||
44 | | ||||
45 | template<> | ||||
46 | struct ColorSpaceFromFactory<RgbF16ColorSpaceFactory> { | ||||
47 | typedef RgbF16ColorSpace type; | ||||
48 | }; | ||||
49 | | ||||
50 | template<> | ||||
51 | struct ColorSpaceFromFactory<RgbF32ColorSpaceFactory> { | ||||
52 | typedef RgbF32ColorSpace type; | ||||
53 | }; | ||||
54 | | ||||
55 | /** | ||||
56 | * Define a singly linked list of supported bit depth traits | ||||
57 | */ | ||||
58 | template<class T> struct NextTrait { using type = void; }; | ||||
59 | template<> struct NextTrait<KoBgrU8Traits> { using type = KoBgrU16Traits; }; | ||||
60 | template<> struct NextTrait<KoBgrU16Traits> { using type = KoRgbF16Traits; }; | ||||
61 | template<> struct NextTrait<KoRgbF16Traits> { using type = KoRgbF32Traits; }; | ||||
62 | | ||||
63 | /** | ||||
64 | * Recursively add bit-depths conversions to the color space. We add only | ||||
65 | * **outgoing** conversions for every RGB color space. That is, every color | ||||
66 | * space has exactly three outgoing edges for color conversion. | ||||
67 | */ | ||||
68 | template<typename ParentColorSpace, typename CurrentTraits> | ||||
69 | void addInternalConversion(QList<KoColorConversionTransformationFactory*> &list, CurrentTraits*) | ||||
70 | { | ||||
71 | // general case: add a converter and recurse for the next traits | ||||
72 | list << new LcmsScaleRGBP2020PQTransformationFactory<ParentColorSpace, CurrentTraits>(); | ||||
73 | | ||||
74 | using NextTraits = typename NextTrait<CurrentTraits>::type; | ||||
75 | addInternalConversion<ParentColorSpace>(list, static_cast<NextTraits*>(0)); | ||||
76 | } | ||||
77 | | ||||
78 | template<typename ParentColorSpace> | ||||
79 | void addInternalConversion(QList<KoColorConversionTransformationFactory*> &list, typename ParentColorSpace::ColorSpaceTraits*) | ||||
80 | { | ||||
81 | // exception: skip adding an edge to the same bit depth | ||||
82 | | ||||
83 | using CurrentTraits = typename ParentColorSpace::ColorSpaceTraits; | ||||
84 | using NextTraits = typename NextTrait<CurrentTraits>::type; | ||||
85 | addInternalConversion<ParentColorSpace>(list, static_cast<NextTraits*>(0)); | ||||
86 | } | ||||
87 | | ||||
88 | template<typename ParentColorSpace> | ||||
89 | void addInternalConversion(QList<KoColorConversionTransformationFactory*> &, void*) | ||||
90 | { | ||||
91 | // stop recursion | ||||
92 | } | ||||
93 | | ||||
94 | template <class BaseColorSpaceFactory> | ||||
95 | class LcmsRGBP2020PQColorSpaceFactoryWrapper : public BaseColorSpaceFactory | ||||
96 | { | ||||
97 | typedef typename ColorSpaceFromFactory<BaseColorSpaceFactory>::type RelatedColorSpaceType; | ||||
98 | | ||||
99 | KoColorSpace *createColorSpace(const KoColorProfile *p) const override | ||||
100 | { | ||||
101 | return new RelatedColorSpaceType(this->name(), p->clone()); | ||||
102 | } | ||||
103 | | ||||
104 | QList<KoColorConversionTransformationFactory *> colorConversionLinks() const override | ||||
105 | { | ||||
106 | QList<KoColorConversionTransformationFactory *> list; | ||||
107 | | ||||
108 | /** | ||||
109 | * We explicitly disable direct conversions to/from integer color spaces, because | ||||
110 | * they may cause the the conversion system to choose them as an intermediate | ||||
111 | * color space for the conversion chain, e.g. | ||||
112 | * p709-g10 F32 -> p2020-g10 U16 -> Rec2020-pq U16, which is incorrect and loses | ||||
113 | * all the HDR data | ||||
114 | */ | ||||
115 | list << new LcmsFromRGBP2020PQTransformationFactory<RelatedColorSpaceType, KoRgbF16Traits>(); | ||||
116 | list << new LcmsFromRGBP2020PQTransformationFactory<RelatedColorSpaceType, KoRgbF32Traits>(); | ||||
117 | list << new LcmsToRGBP2020PQTransformationFactory<RelatedColorSpaceType, KoRgbF16Traits>(); | ||||
118 | list << new LcmsToRGBP2020PQTransformationFactory<RelatedColorSpaceType, KoRgbF32Traits>(); | ||||
119 | | ||||
120 | // internally, we can convert to RGB U8 if needed | ||||
121 | addInternalConversion<RelatedColorSpaceType>(list, static_cast<KoBgrU8Traits*>(0)); | ||||
122 | | ||||
123 | return list; | ||||
124 | } | ||||
125 | }; | ||||
126 | | ||||
127 | #endif // LCMSRGBP2020PQCOLORSPACE_H |