1 #pragma once
2 
3 #ifndef ZIMG_COLORSPACE_COLORSPACE_H_
4 #define ZIMG_COLORSPACE_COLORSPACE_H_
5 
6 #include <memory>
7 
8 namespace zimg {
9 
10 enum class CPUClass;
11 
12 namespace graph {
13 
14 class ImageFilter;
15 
16 } // namespace graph
17 
18 
19 namespace colorspace {
20 
21 enum class MatrixCoefficients {
22 	UNSPECIFIED,
23 	RGB,
24 	REC_601,
25 	REC_709,
26 	FCC,
27 	SMPTE_240M,
28 	YCGCO,
29 	REC_2020_NCL,
30 	REC_2020_CL,
31 	CHROMATICITY_DERIVED_NCL,
32 	CHROMATICITY_DERIVED_CL,
33 	REC_2100_LMS,
34 	REC_2100_ICTCP,
35 };
36 
37 enum class TransferCharacteristics {
38 	UNSPECIFIED,
39 	LINEAR,
40 	LOG_100,
41 	LOG_316,
42 	REC_709,
43 	REC_470_M,
44 	REC_470_BG,
45 	SMPTE_240M,
46 	XVYCC,
47 	SRGB,
48 	ST_2084,
49 	ARIB_B67,
50 };
51 
52 enum class ColorPrimaries {
53 	UNSPECIFIED,
54 	REC_470_M,
55 	REC_470_BG,
56 	SMPTE_C,
57 	REC_709,
58 	FILM,
59 	REC_2020,
60 	XYZ,
61 	DCI_P3,
62 	DCI_P3_D65,
63 	JEDEC_P22,
64 };
65 
66 /**
67  * Definition of a working colorspace.
68  */
69 struct ColorspaceDefinition {
70 	MatrixCoefficients matrix;
71 	TransferCharacteristics transfer;
72 	ColorPrimaries primaries;
73 
74 	// Helper functions to create modified colorspaces.
toColorspaceDefinition75 	constexpr ColorspaceDefinition to(MatrixCoefficients matrix_) const noexcept
76 	{
77 		return{ matrix_, transfer, primaries };
78 	}
79 
toColorspaceDefinition80 	constexpr ColorspaceDefinition to(TransferCharacteristics transfer_) const noexcept
81 	{
82 		return{ matrix, transfer_, primaries };
83 	}
84 
toColorspaceDefinition85 	constexpr ColorspaceDefinition to(ColorPrimaries primaries_) const noexcept
86 	{
87 		return{ matrix, transfer, primaries_ };
88 	}
89 
to_rgbColorspaceDefinition90 	constexpr ColorspaceDefinition to_rgb() const noexcept
91 	{
92 		return to(MatrixCoefficients::RGB);
93 	}
94 
to_linearColorspaceDefinition95 	constexpr ColorspaceDefinition to_linear() const noexcept
96 	{
97 		return to(TransferCharacteristics::LINEAR);
98 	}
99 };
100 
101 // Compare colorspaces by comparing each component.
102 constexpr bool operator==(const ColorspaceDefinition &a, const ColorspaceDefinition &b) noexcept
103 {
104 	return a.matrix == b.matrix && a.transfer == b.transfer && a.primaries == b.primaries;
105 }
106 
107 constexpr bool operator!=(const ColorspaceDefinition &a, const ColorspaceDefinition &b) noexcept
108 {
109 	return !(a == b);
110 }
111 
112 
113 struct ColorspaceConversion {
114 	unsigned width;
115 	unsigned height;
116 
117 #include "common/builder.h"
118 	BUILDER_MEMBER(ColorspaceDefinition, csp_in)
119 	BUILDER_MEMBER(ColorspaceDefinition, csp_out)
120 	BUILDER_MEMBER(double, peak_luminance)
121 	BUILDER_MEMBER(bool, approximate_gamma)
122 	BUILDER_MEMBER(bool, scene_referred)
123 	BUILDER_MEMBER(CPUClass, cpu)
124 #undef BUILDER_MEMBER
125 
126 	ColorspaceConversion(unsigned width, unsigned height);
127 
128 	std::unique_ptr<graph::ImageFilter> create() const;
129 };
130 
131 } // namespace colorspace
132 } // namespace zimg
133 
134 #endif // ZIMG_COLORSPACE_COLORSPACE2_H_
135