ColorSpace.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2021-2022, 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/Document.h>
  9. #include <LibPDF/ObjectDerivatives.h>
  10. namespace PDF {
  11. PDFErrorOr<NonnullRefPtr<ColorSpace>> ColorSpace::create(Document* document, FlyString const& name, Page const& page)
  12. {
  13. // Simple color spaces with no parameters, which can be specified directly
  14. if (name == CommonNames::DeviceGray)
  15. return DeviceGrayColorSpace::the();
  16. if (name == CommonNames::DeviceRGB)
  17. return DeviceRGBColorSpace::the();
  18. if (name == CommonNames::DeviceCMYK)
  19. return DeviceCMYKColorSpace::the();
  20. if (name == CommonNames::Pattern)
  21. TODO();
  22. // The color space is a complex color space with parameters that resides in
  23. // the resource dictionary
  24. auto color_space_resource_dict = TRY(page.resources->get_dict(document, CommonNames::ColorSpace));
  25. if (!color_space_resource_dict->contains(name))
  26. TODO();
  27. auto color_space_array = TRY(color_space_resource_dict->get_array(document, name));
  28. auto color_space_name = TRY(color_space_array->get_name_at(document, 0))->name();
  29. Vector<Value> parameters;
  30. parameters.ensure_capacity(color_space_array->size() - 1);
  31. for (size_t i = 1; i < color_space_array->size(); i++)
  32. parameters.unchecked_append(color_space_array->at(i));
  33. if (color_space_name == CommonNames::CalRGB)
  34. return TRY(CalRGBColorSpace::create(document, move(parameters)));
  35. dbgln("Unknown color space: {}", color_space_name);
  36. TODO();
  37. }
  38. NonnullRefPtr<DeviceGrayColorSpace> DeviceGrayColorSpace::the()
  39. {
  40. static auto instance = adopt_ref(*new DeviceGrayColorSpace());
  41. return instance;
  42. }
  43. Color DeviceGrayColorSpace::color(Vector<Value> const& arguments) const
  44. {
  45. VERIFY(arguments.size() == 1);
  46. auto gray = static_cast<u8>(arguments[0].to_float() * 255.0f);
  47. return Color(gray, gray, gray);
  48. }
  49. NonnullRefPtr<DeviceRGBColorSpace> DeviceRGBColorSpace::the()
  50. {
  51. static auto instance = adopt_ref(*new DeviceRGBColorSpace());
  52. return instance;
  53. }
  54. Color DeviceRGBColorSpace::color(Vector<Value> const& arguments) const
  55. {
  56. VERIFY(arguments.size() == 3);
  57. auto r = static_cast<u8>(arguments[0].to_float() * 255.0f);
  58. auto g = static_cast<u8>(arguments[1].to_float() * 255.0f);
  59. auto b = static_cast<u8>(arguments[2].to_float() * 255.0f);
  60. return Color(r, g, b);
  61. }
  62. NonnullRefPtr<DeviceCMYKColorSpace> DeviceCMYKColorSpace::the()
  63. {
  64. static auto instance = adopt_ref(*new DeviceCMYKColorSpace());
  65. return instance;
  66. }
  67. Color DeviceCMYKColorSpace::color(Vector<Value> const& arguments) const
  68. {
  69. VERIFY(arguments.size() == 4);
  70. auto c = arguments[0].to_float();
  71. auto m = arguments[1].to_float();
  72. auto y = arguments[2].to_float();
  73. auto k = arguments[3].to_float();
  74. return Color::from_cmyk(c, m, y, k);
  75. }
  76. PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> CalRGBColorSpace::create(Document* document, Vector<Value>&& parameters)
  77. {
  78. if (parameters.size() != 1)
  79. return Error { Error::Type::MalformedPDF, "RGB color space expects one parameter" };
  80. auto param = parameters[0];
  81. if (!param.has<NonnullRefPtr<Object>>() || !param.get<NonnullRefPtr<Object>>()->is<DictObject>())
  82. return Error { Error::Type::MalformedPDF, "RGB color space expects a dict parameter" };
  83. auto dict = param.get<NonnullRefPtr<Object>>()->cast<DictObject>();
  84. if (!dict->contains(CommonNames::WhitePoint))
  85. return Error { Error::Type::MalformedPDF, "RGB color space expects a Whitepoint key" };
  86. auto white_point_array = TRY(dict->get_array(document, CommonNames::WhitePoint));
  87. if (white_point_array->size() != 3)
  88. return Error { Error::Type::MalformedPDF, "RGB color space expects 3 Whitepoint parameters" };
  89. auto color_space = adopt_ref(*new CalRGBColorSpace());
  90. color_space->m_whitepoint[0] = white_point_array->at(0).to_float();
  91. color_space->m_whitepoint[1] = white_point_array->at(1).to_float();
  92. color_space->m_whitepoint[2] = white_point_array->at(2).to_float();
  93. if (color_space->m_whitepoint[1] != 1.0f)
  94. return Error { Error::Type::MalformedPDF, "RGB color space expects 2nd Whitepoint to be 1.0" };
  95. if (dict->contains(CommonNames::BlackPoint)) {
  96. auto black_point_array = TRY(dict->get_array(document, CommonNames::BlackPoint));
  97. if (black_point_array->size() == 3) {
  98. color_space->m_blackpoint[0] = black_point_array->at(0).to_float();
  99. color_space->m_blackpoint[1] = black_point_array->at(1).to_float();
  100. color_space->m_blackpoint[2] = black_point_array->at(2).to_float();
  101. }
  102. }
  103. if (dict->contains(CommonNames::Gamma)) {
  104. auto gamma_array = TRY(dict->get_array(document, CommonNames::Gamma));
  105. if (gamma_array->size() == 3) {
  106. color_space->m_gamma[0] = gamma_array->at(0).to_float();
  107. color_space->m_gamma[1] = gamma_array->at(1).to_float();
  108. color_space->m_gamma[2] = gamma_array->at(2).to_float();
  109. }
  110. }
  111. if (dict->contains(CommonNames::Matrix)) {
  112. auto matrix_array = TRY(dict->get_array(document, CommonNames::Matrix));
  113. if (matrix_array->size() == 3) {
  114. color_space->m_matrix[0] = matrix_array->at(0).to_float();
  115. color_space->m_matrix[1] = matrix_array->at(1).to_float();
  116. color_space->m_matrix[2] = matrix_array->at(2).to_float();
  117. color_space->m_matrix[3] = matrix_array->at(3).to_float();
  118. color_space->m_matrix[4] = matrix_array->at(4).to_float();
  119. color_space->m_matrix[5] = matrix_array->at(5).to_float();
  120. color_space->m_matrix[6] = matrix_array->at(6).to_float();
  121. color_space->m_matrix[7] = matrix_array->at(7).to_float();
  122. color_space->m_matrix[8] = matrix_array->at(8).to_float();
  123. }
  124. }
  125. return color_space;
  126. }
  127. constexpr Array<float, 3> matrix_multiply(Array<float, 9> a, Array<float, 3> b)
  128. {
  129. return Array<float, 3> {
  130. a[0] * b[0] + a[1] * b[1] + a[2] * b[2],
  131. a[3] * b[0] + a[4] * b[1] + a[5] * b[2],
  132. a[6] * b[0] + a[7] * b[1] + a[8] * b[2]
  133. };
  134. }
  135. // Converts to a flat XYZ space with white point = (1, 1, 1)
  136. // Step 2 of https://www.adobe.com/content/dam/acom/en/devnet/photoshop/sdk/AdobeBPC.pdf
  137. constexpr Array<float, 3> flatten_and_normalize_whitepoint(Array<float, 3> whitepoint, Array<float, 3> xyz)
  138. {
  139. VERIFY(whitepoint[1] == 1.0f);
  140. return {
  141. (1.0f / whitepoint[0]) * xyz[0],
  142. xyz[1],
  143. (1.0f / whitepoint[2]) * xyz[2],
  144. };
  145. }
  146. constexpr float decode_l(float input)
  147. {
  148. constexpr float decode_l_scaling_constant = 0.00110705646f; // (((8 + 16) / 116) ^ 3) / 8
  149. if (input < 0.0f)
  150. return -decode_l(-input);
  151. if (input >= 0.0f && input <= 8.0f)
  152. return input * decode_l_scaling_constant;
  153. return powf(((input + 16.0f) / 116.0f), 3.0f);
  154. }
  155. constexpr Array<float, 3> scale_black_point(Array<float, 3> blackpoint, Array<float, 3> xyz)
  156. {
  157. auto y_dst = decode_l(0); // DestinationBlackPoint is just [0, 0, 0]
  158. auto y_src = decode_l(blackpoint[0]);
  159. auto scale = (1 - y_dst) / (1 - y_src);
  160. auto offset = 1 - scale;
  161. return {
  162. xyz[0] * scale + offset,
  163. xyz[1] * scale + offset,
  164. xyz[2] * scale + offset,
  165. };
  166. }
  167. // https://en.wikipedia.org/wiki/Illuminant_D65
  168. constexpr Array<float, 3> convert_to_d65(Array<float, 3> whitepoint, Array<float, 3> xyz)
  169. {
  170. constexpr float d65x = 0.95047f;
  171. constexpr float d65y = 1.0f;
  172. constexpr float d65z = 1.08883f;
  173. return {
  174. (xyz[0] * d65x) / whitepoint[0],
  175. (xyz[1] * d65y) / whitepoint[1],
  176. (xyz[2] * d65z) / whitepoint[2],
  177. };
  178. }
  179. // https://en.wikipedia.org/wiki/SRGB
  180. constexpr Array<float, 3> convert_to_srgb(Array<float, 3> xyz)
  181. {
  182. // See the sRGB D65 [M]^-1 matrix in the following page
  183. // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
  184. constexpr Array<float, 9> conversion_matrix = {
  185. 3.2404542,
  186. -1.5371385,
  187. -0.4985314,
  188. -0.969266,
  189. 1.8760108,
  190. 0.0415560,
  191. 0.0556434,
  192. -0.2040259,
  193. 1.0572252,
  194. };
  195. return matrix_multiply(conversion_matrix, xyz);
  196. }
  197. Color CalRGBColorSpace::color(Vector<Value> const& arguments) const
  198. {
  199. VERIFY(arguments.size() == 3);
  200. auto a = clamp(arguments[0].to_float(), 0.0f, 1.0f);
  201. auto b = clamp(arguments[1].to_float(), 0.0f, 1.0f);
  202. auto c = clamp(arguments[2].to_float(), 0.0f, 1.0f);
  203. auto agr = powf(a, m_gamma[0]);
  204. auto bgg = powf(b, m_gamma[1]);
  205. auto cgb = powf(c, m_gamma[2]);
  206. auto x = m_matrix[0] * agr + m_matrix[3] * bgg + m_matrix[6] * cgb;
  207. auto y = m_matrix[1] * agr + m_matrix[4] * bgg + m_matrix[7] * cgb;
  208. auto z = m_matrix[2] * agr + m_matrix[5] * bgg + m_matrix[8] * cgb;
  209. auto flattened_xyz = flatten_and_normalize_whitepoint(m_whitepoint, { x, y, z });
  210. auto scaled_black_point_xyz = scale_black_point(m_blackpoint, flattened_xyz);
  211. auto d65_normalized = convert_to_d65(m_whitepoint, scaled_black_point_xyz);
  212. auto srgb = convert_to_srgb(d65_normalized);
  213. auto red = static_cast<u8>(srgb[0] * 255.0f);
  214. auto green = static_cast<u8>(srgb[1] * 255.0f);
  215. auto blue = static_cast<u8>(srgb[2] * 255.0f);
  216. return Color(red, green, blue);
  217. }
  218. }