ColorSpace.cpp 7.3 KB

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