ColorSpace.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibPDF/ColorSpace.h>
  7. #include <LibPDF/CommonNames.h>
  8. #include <LibPDF/ObjectDerivatives.h>
  9. namespace PDF {
  10. RefPtr<DeviceGrayColorSpace> DeviceGrayColorSpace::the()
  11. {
  12. static auto instance = adopt_ref(*new DeviceGrayColorSpace());
  13. return instance;
  14. }
  15. Color DeviceGrayColorSpace::color(Vector<Value> const& arguments) const
  16. {
  17. VERIFY(arguments.size() == 1);
  18. auto gray = static_cast<u8>(arguments[0].to_float() * 255.0f);
  19. return Color(gray, gray, gray);
  20. }
  21. RefPtr<DeviceRGBColorSpace> DeviceRGBColorSpace::the()
  22. {
  23. static auto instance = adopt_ref(*new DeviceRGBColorSpace());
  24. return instance;
  25. }
  26. Color DeviceRGBColorSpace::color(Vector<Value> const& arguments) const
  27. {
  28. VERIFY(arguments.size() == 3);
  29. auto r = static_cast<u8>(arguments[0].to_float() * 255.0f);
  30. auto g = static_cast<u8>(arguments[1].to_float() * 255.0f);
  31. auto b = static_cast<u8>(arguments[2].to_float() * 255.0f);
  32. return Color(r, g, b);
  33. }
  34. RefPtr<DeviceCMYKColorSpace> DeviceCMYKColorSpace::the()
  35. {
  36. static auto instance = adopt_ref(*new DeviceCMYKColorSpace());
  37. return instance;
  38. }
  39. Color DeviceCMYKColorSpace::color(Vector<Value> const& arguments) const
  40. {
  41. VERIFY(arguments.size() == 4);
  42. auto c = arguments[0].to_float();
  43. auto m = arguments[1].to_float();
  44. auto y = arguments[2].to_float();
  45. auto k = arguments[3].to_float();
  46. return Color::from_cmyk(c, m, y, k);
  47. }
  48. RefPtr<CalRGBColorSpace> CalRGBColorSpace::create(RefPtr<Document> document, Vector<Value>&& parameters)
  49. {
  50. if (parameters.size() != 1)
  51. return {};
  52. auto param = parameters[0];
  53. if (!param.has<NonnullRefPtr<Object>>() || !param.get<NonnullRefPtr<Object>>()->is_dict())
  54. return {};
  55. auto dict = object_cast<DictObject>(param.get<NonnullRefPtr<Object>>());
  56. if (!dict->contains(CommonNames::WhitePoint))
  57. return {};
  58. auto white_point_array = dict->get_array(document, CommonNames::WhitePoint);
  59. if (white_point_array->size() != 3)
  60. return {};
  61. auto color_space = adopt_ref(*new CalRGBColorSpace());
  62. color_space->m_whitepoint[0] = white_point_array->at(0).to_float();
  63. color_space->m_whitepoint[1] = white_point_array->at(1).to_float();
  64. color_space->m_whitepoint[2] = white_point_array->at(2).to_float();
  65. if (color_space->m_whitepoint[1] != 1.0f)
  66. return {};
  67. if (dict->contains(CommonNames::BlackPoint)) {
  68. auto black_point_array = dict->get_array(document, CommonNames::BlackPoint);
  69. if (black_point_array->size() == 3) {
  70. color_space->m_blackpoint[0] = black_point_array->at(0).to_float();
  71. color_space->m_blackpoint[1] = black_point_array->at(1).to_float();
  72. color_space->m_blackpoint[2] = black_point_array->at(2).to_float();
  73. }
  74. }
  75. if (dict->contains(CommonNames::Gamma)) {
  76. auto gamma_array = dict->get_array(document, CommonNames::Gamma);
  77. if (gamma_array->size() == 3) {
  78. color_space->m_gamma[0] = gamma_array->at(0).to_float();
  79. color_space->m_gamma[1] = gamma_array->at(1).to_float();
  80. color_space->m_gamma[2] = gamma_array->at(2).to_float();
  81. }
  82. }
  83. if (dict->contains(CommonNames::Matrix)) {
  84. auto matrix_array = dict->get_array(document, CommonNames::Matrix);
  85. if (matrix_array->size() == 3) {
  86. color_space->m_matrix[0] = matrix_array->at(0).to_float();
  87. color_space->m_matrix[1] = matrix_array->at(1).to_float();
  88. color_space->m_matrix[2] = matrix_array->at(2).to_float();
  89. color_space->m_matrix[3] = matrix_array->at(3).to_float();
  90. color_space->m_matrix[4] = matrix_array->at(4).to_float();
  91. color_space->m_matrix[5] = matrix_array->at(5).to_float();
  92. color_space->m_matrix[6] = matrix_array->at(6).to_float();
  93. color_space->m_matrix[7] = matrix_array->at(7).to_float();
  94. color_space->m_matrix[8] = matrix_array->at(8).to_float();
  95. }
  96. }
  97. return color_space;
  98. }
  99. constexpr Array<float, 3> matrix_multiply(Array<float, 9> a, Array<float, 3> b)
  100. {
  101. return Array<float, 3> {
  102. a[0] * b[0] + a[1] * b[1] + a[2] * b[2],
  103. a[3] * b[0] + a[4] * b[1] + a[5] * b[2],
  104. a[6] * b[0] + a[7] * b[1] + a[8] * b[2]
  105. };
  106. }
  107. // Converts to a flat XYZ space with white point = (1, 1, 1)
  108. // Step 2 of https://www.adobe.com/content/dam/acom/en/devnet/photoshop/sdk/AdobeBPC.pdf
  109. constexpr Array<float, 3> flatten_and_normalize_whitepoint(Array<float, 3> whitepoint, Array<float, 3> xyz)
  110. {
  111. VERIFY(whitepoint[1] == 1.0f);
  112. return {
  113. (1.0f / whitepoint[0]) * xyz[0],
  114. xyz[1],
  115. (1.0f / whitepoint[2]) * xyz[2],
  116. };
  117. }
  118. constexpr float decode_l(float input)
  119. {
  120. constexpr float decode_l_scaling_constant = 0.00110705646f; // (((8 + 16) / 116) ^ 3) / 8
  121. if (input < 0.0f)
  122. return -decode_l(-input);
  123. if (input >= 0.0f && input <= 8.0f)
  124. return input * decode_l_scaling_constant;
  125. return powf(((input + 16.0f) / 116.0f), 3.0f);
  126. }
  127. constexpr Array<float, 3> scale_black_point(Array<float, 3> blackpoint, Array<float, 3> xyz)
  128. {
  129. auto y_dst = decode_l(0); // DestinationBlackPoint is just [0, 0, 0]
  130. auto y_src = decode_l(blackpoint[0]);
  131. auto scale = (1 - y_dst) / (1 - y_src);
  132. auto offset = 1 - scale;
  133. return {
  134. xyz[0] * scale + offset,
  135. xyz[1] * scale + offset,
  136. xyz[2] * scale + offset,
  137. };
  138. }
  139. // https://en.wikipedia.org/wiki/Illuminant_D65
  140. constexpr Array<float, 3> convert_to_d65(Array<float, 3> whitepoint, Array<float, 3> xyz)
  141. {
  142. constexpr float d65x = 0.95047f;
  143. constexpr float d65y = 1.0f;
  144. constexpr float d65z = 1.08883f;
  145. return {
  146. (xyz[0] * d65x) / whitepoint[0],
  147. (xyz[1] * d65y) / whitepoint[1],
  148. (xyz[2] * d65z) / whitepoint[2],
  149. };
  150. }
  151. // https://en.wikipedia.org/wiki/SRGB
  152. constexpr Array<float, 3> convert_to_srgb(Array<float, 3> xyz)
  153. {
  154. // See the sRGB D65 [M]^-1 matrix in the following page
  155. // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
  156. constexpr Array<float, 9> conversion_matrix = {
  157. 3.2404542,
  158. -1.5371385,
  159. -0.4985314,
  160. -0.969266,
  161. 1.8760108,
  162. 0.0415560,
  163. 0.0556434,
  164. -0.2040259,
  165. 1.0572252,
  166. };
  167. return matrix_multiply(conversion_matrix, xyz);
  168. }
  169. Color CalRGBColorSpace::color(Vector<Value> const& arguments) const
  170. {
  171. VERIFY(arguments.size() == 3);
  172. auto a = clamp(arguments[0].to_float(), 0.0f, 1.0f);
  173. auto b = clamp(arguments[1].to_float(), 0.0f, 1.0f);
  174. auto c = clamp(arguments[2].to_float(), 0.0f, 1.0f);
  175. auto agr = powf(a, m_gamma[0]);
  176. auto bgg = powf(b, m_gamma[1]);
  177. auto cgb = powf(c, m_gamma[2]);
  178. auto x = m_matrix[0] * agr + m_matrix[3] * bgg + m_matrix[6] * cgb;
  179. auto y = m_matrix[1] * agr + m_matrix[4] * bgg + m_matrix[7] * cgb;
  180. auto z = m_matrix[2] * agr + m_matrix[5] * bgg + m_matrix[8] * cgb;
  181. auto flattened_xyz = flatten_and_normalize_whitepoint(m_whitepoint, { x, y, z });
  182. auto scaled_black_point_xyz = scale_black_point(m_blackpoint, flattened_xyz);
  183. auto d65_normalized = convert_to_d65(m_whitepoint, scaled_black_point_xyz);
  184. auto srgb = convert_to_srgb(d65_normalized);
  185. auto red = static_cast<u8>(srgb[0] * 255.0f);
  186. auto green = static_cast<u8>(srgb[1] * 255.0f);
  187. auto blue = static_cast<u8>(srgb[2] * 255.0f);
  188. return Color(red, green, blue);
  189. }
  190. }