Coverage for colour/io/luts/tests/test__init__.py: 100%

50 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-15 19:01 +1300

1"""Define the unit tests for the :mod:`colour.io.luts.__init__` module.""" 

2 

3from __future__ import annotations 

4 

5import os 

6import shutil 

7import tempfile 

8 

9import numpy as np 

10import pytest 

11 

12from colour.constants import TOLERANCE_ABSOLUTE_TESTS 

13from colour.hints import cast 

14from colour.io import LUT1D, LUTSequence, read_LUT, write_LUT 

15 

16__author__ = "Colour Developers" 

17__copyright__ = "Copyright 2013 Colour Developers" 

18__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause" 

19__maintainer__ = "Colour Developers" 

20__email__ = "colour-developers@colour-science.org" 

21__status__ = "Production" 

22 

23__all__ = [ 

24 "ROOT_LUTS", 

25 "TestReadLUT", 

26 "TestWriteLUT", 

27] 

28 

29ROOT_LUTS: str = os.path.join(os.path.dirname(__file__), "resources") 

30 

31 

32class TestReadLUT: 

33 """ 

34 Define :func:`colour.io.luts.__init__.read_LUT` definition unit tests 

35 methods. 

36 """ 

37 

38 def test_read_LUT(self) -> None: 

39 """Test :func:`colour.io.luts.__init__.read_LUT` definition.""" 

40 

41 LUT_1 = cast( 

42 "LUT1D", 

43 read_LUT(os.path.join(ROOT_LUTS, "sony_spi1d", "eotf_sRGB_1D.spi1d")), 

44 ) 

45 

46 np.testing.assert_allclose( 

47 LUT_1.table, 

48 np.array( 

49 [ 

50 -7.73990000e-03, 

51 5.16000000e-04, 

52 1.22181000e-02, 

53 3.96819000e-02, 

54 8.71438000e-02, 

55 1.57439400e-01, 

56 2.52950100e-01, 

57 3.75757900e-01, 

58 5.27729400e-01, 

59 7.10566500e-01, 

60 9.25840600e-01, 

61 1.17501630e00, 

62 1.45946870e00, 

63 1.78049680e00, 

64 2.13933380e00, 

65 2.53715520e00, 

66 ] 

67 ), 

68 atol=TOLERANCE_ABSOLUTE_TESTS, 

69 ) 

70 assert LUT_1.name == "eotf sRGB 1D" 

71 assert LUT_1.dimensions == 1 

72 np.testing.assert_array_equal(LUT_1.domain, np.array([-0.1, 1.5])) 

73 assert LUT_1.size == 16 

74 assert LUT_1.comments == [ 

75 'Generated by "Colour 0.3.11".', 

76 '"colour.models.eotf_sRGB".', 

77 ] 

78 

79 LUT_2 = cast( 

80 "LUTSequence", 

81 read_LUT(os.path.join(ROOT_LUTS, "resolve_cube", "LogC_Video.cube")), 

82 ) 

83 np.testing.assert_allclose( 

84 LUT_2[0].table, 

85 np.array( 

86 [ 

87 [0.00000000, 0.00000000, 0.00000000], 

88 [0.02708500, 0.02708500, 0.02708500], 

89 [0.06304900, 0.06304900, 0.06304900], 

90 [0.11314900, 0.11314900, 0.11314900], 

91 [0.18304900, 0.18304900, 0.18304900], 

92 [0.28981100, 0.28981100, 0.28981100], 

93 [0.41735300, 0.41735300, 0.41735300], 

94 [0.54523100, 0.54523100, 0.54523100], 

95 [0.67020500, 0.67020500, 0.67020500], 

96 [0.78963000, 0.78963000, 0.78963000], 

97 [0.88646800, 0.88646800, 0.88646800], 

98 [0.94549100, 0.94549100, 0.94549100], 

99 [0.97644900, 0.97644900, 0.97644900], 

100 [0.98924800, 0.98924800, 0.98924800], 

101 [0.99379700, 0.99379700, 0.99379700], 

102 [1.00000000, 1.00000000, 1.00000000], 

103 ] 

104 ), 

105 atol=TOLERANCE_ABSOLUTE_TESTS, 

106 ) 

107 assert LUT_2[1].size == 4 

108 

109 assert read_LUT( 

110 os.path.join(ROOT_LUTS, "sony_spi1d", "eotf_sRGB_1D.spi1d") 

111 ) == read_LUT( 

112 os.path.join(ROOT_LUTS, "sony_spi1d", "eotf_sRGB_1D.spi1d"), 

113 method="Sony SPI1D", 

114 ) 

115 

116 def test_raise_exception_read_LUT(self) -> None: 

117 """ 

118 Test :func:`colour.io.luts.__init__.read_LUT` definition raised 

119 exception. 

120 """ 

121 

122 pytest.raises( 

123 ValueError, 

124 read_LUT, 

125 os.path.join(ROOT_LUTS, "sony_spi1d", "Exception_Raising.spi1d"), 

126 ) 

127 

128 

129class TestWriteLUT: 

130 """ 

131 Define :func:`colour.io.luts.__init__.write_LUT` definition unit tests 

132 methods. 

133 """ 

134 

135 def setup_method(self) -> None: 

136 """Initialise the common tests attributes.""" 

137 

138 self._temporary_directory = tempfile.mkdtemp() 

139 

140 def teardown_method(self) -> None: 

141 """After tests actions.""" 

142 

143 shutil.rmtree(self._temporary_directory) 

144 

145 def test_write_LUT(self) -> None: 

146 """Test :func:`colour.io.luts.__init__.write_LUT` definition.""" 

147 

148 LUT_1_r = read_LUT(os.path.join(ROOT_LUTS, "sony_spi1d", "eotf_sRGB_1D.spi1d")) 

149 

150 write_LUT( 

151 LUT_1_r, 

152 os.path.join(self._temporary_directory, "eotf_sRGB_1D.spi1d"), 

153 ) 

154 

155 LUT_1_t = read_LUT( 

156 os.path.join(self._temporary_directory, "eotf_sRGB_1D.spi1d") 

157 ) 

158 

159 assert LUT_1_r == LUT_1_t 

160 

161 write_LUT( 

162 LUTSequence(LUT_1_r), 

163 os.path.join(self._temporary_directory, "eotf_sRGB_1D.spi1d"), 

164 ) 

165 

166 assert LUT_1_r == LUT_1_t 

167 

168 LUT_2_r = read_LUT( 

169 os.path.join( 

170 ROOT_LUTS, 

171 "resolve_cube", 

172 "Three_Dimensional_Table_With_Shaper.cube", 

173 ) 

174 ) 

175 

176 write_LUT( 

177 LUT_2_r, 

178 os.path.join( 

179 self._temporary_directory, 

180 "Three_Dimensional_Table_With_Shaper.cube", 

181 ), 

182 ) 

183 

184 LUT_2_t = read_LUT( 

185 os.path.join( 

186 self._temporary_directory, 

187 "Three_Dimensional_Table_With_Shaper.cube", 

188 ) 

189 ) 

190 

191 assert LUT_2_r == LUT_2_t 

192 

193 write_LUT( 

194 LUT_1_r, 

195 os.path.join(self._temporary_directory, "eotf_sRGB_1D"), 

196 method="Sony SPI1D", 

197 ) 

198 

199 assert read_LUT( 

200 os.path.join(self._temporary_directory, "eotf_sRGB_1D.spi1d") 

201 ) == read_LUT( 

202 os.path.join(self._temporary_directory, "eotf_sRGB_1D"), 

203 method="Sony SPI1D", 

204 )