ColorSpace.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. if (color_space_name == CommonNames::ICCBased)
  36. return TRY(ICCBasedColorSpace::create(document, page, move(parameters)));
  37. dbgln("Unknown color space: {}", color_space_name);
  38. TODO();
  39. }
  40. NonnullRefPtr<DeviceGrayColorSpace> DeviceGrayColorSpace::the()
  41. {
  42. static auto instance = adopt_ref(*new DeviceGrayColorSpace());
  43. return instance;
  44. }
  45. Color DeviceGrayColorSpace::color(Vector<Value> const& arguments) const
  46. {
  47. VERIFY(arguments.size() == 1);
  48. auto gray = static_cast<u8>(arguments[0].to_float() * 255.0f);
  49. return Color(gray, gray, gray);
  50. }
  51. NonnullRefPtr<DeviceRGBColorSpace> DeviceRGBColorSpace::the()
  52. {
  53. static auto instance = adopt_ref(*new DeviceRGBColorSpace());
  54. return instance;
  55. }
  56. Color DeviceRGBColorSpace::color(Vector<Value> const& arguments) const
  57. {
  58. VERIFY(arguments.size() == 3);
  59. auto r = static_cast<u8>(arguments[0].to_float() * 255.0f);
  60. auto g = static_cast<u8>(arguments[1].to_float() * 255.0f);
  61. auto b = static_cast<u8>(arguments[2].to_float() * 255.0f);
  62. return Color(r, g, b);
  63. }
  64. NonnullRefPtr<DeviceCMYKColorSpace> DeviceCMYKColorSpace::the()
  65. {
  66. static auto instance = adopt_ref(*new DeviceCMYKColorSpace());
  67. return instance;
  68. }
  69. Color DeviceCMYKColorSpace::color(Vector<Value> const& arguments) const
  70. {
  71. VERIFY(arguments.size() == 4);
  72. auto c = arguments[0].to_float();
  73. auto m = arguments[1].to_float();
  74. auto y = arguments[2].to_float();
  75. auto k = arguments[3].to_float();
  76. return Color::from_cmyk(c, m, y, k);
  77. }
  78. PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> CalRGBColorSpace::create(Document* document, Vector<Value>&& parameters)
  79. {
  80. if (parameters.size() != 1)
  81. return Error { Error::Type::MalformedPDF, "RGB color space expects one parameter" };
  82. auto param = parameters[0];
  83. if (!param.has<NonnullRefPtr<Object>>() || !param.get<NonnullRefPtr<Object>>()->is<DictObject>())
  84. return Error { Error::Type::MalformedPDF, "RGB color space expects a dict parameter" };
  85. auto dict = param.get<NonnullRefPtr<Object>>()->cast<DictObject>();
  86. if (!dict->contains(CommonNames::WhitePoint))
  87. return Error { Error::Type::MalformedPDF, "RGB color space expects a Whitepoint key" };
  88. auto white_point_array = TRY(dict->get_array(document, CommonNames::WhitePoint));
  89. if (white_point_array->size() != 3)
  90. return Error { Error::Type::MalformedPDF, "RGB color space expects 3 Whitepoint parameters" };
  91. auto color_space = adopt_ref(*new CalRGBColorSpace());
  92. color_space->m_whitepoint[0] = white_point_array->at(0).to_float();
  93. color_space->m_whitepoint[1] = white_point_array->at(1).to_float();
  94. color_space->m_whitepoint[2] = white_point_array->at(2).to_float();
  95. if (color_space->m_whitepoint[1] != 1.0f)
  96. return Error { Error::Type::MalformedPDF, "RGB color space expects 2nd Whitepoint to be 1.0" };
  97. if (dict->contains(CommonNames::BlackPoint)) {
  98. auto black_point_array = TRY(dict->get_array(document, CommonNames::BlackPoint));
  99. if (black_point_array->size() == 3) {
  100. color_space->m_blackpoint[0] = black_point_array->at(0).to_float();
  101. color_space->m_blackpoint[1] = black_point_array->at(1).to_float();
  102. color_space->m_blackpoint[2] = black_point_array->at(2).to_float();
  103. }
  104. }
  105. if (dict->contains(CommonNames::Gamma)) {
  106. auto gamma_array = TRY(dict->get_array(document, CommonNames::Gamma));
  107. if (gamma_array->size() == 3) {
  108. color_space->m_gamma[0] = gamma_array->at(0).to_float();
  109. color_space->m_gamma[1] = gamma_array->at(1).to_float();
  110. color_space->m_gamma[2] = gamma_array->at(2).to_float();
  111. }
  112. }
  113. if (dict->contains(CommonNames::Matrix)) {
  114. auto matrix_array = TRY(dict->get_array(document, CommonNames::Matrix));
  115. if (matrix_array->size() == 3) {
  116. color_space->m_matrix[0] = matrix_array->at(0).to_float();
  117. color_space->m_matrix[1] = matrix_array->at(1).to_float();
  118. color_space->m_matrix[2] = matrix_array->at(2).to_float();
  119. color_space->m_matrix[3] = matrix_array->at(3).to_float();
  120. color_space->m_matrix[4] = matrix_array->at(4).to_float();
  121. color_space->m_matrix[5] = matrix_array->at(5).to_float();
  122. color_space->m_matrix[6] = matrix_array->at(6).to_float();
  123. color_space->m_matrix[7] = matrix_array->at(7).to_float();
  124. color_space->m_matrix[8] = matrix_array->at(8).to_float();
  125. }
  126. }
  127. return color_space;
  128. }
  129. constexpr Array<float, 3> matrix_multiply(Array<float, 9> a, Array<float, 3> b)
  130. {
  131. return Array<float, 3> {
  132. a[0] * b[0] + a[1] * b[1] + a[2] * b[2],
  133. a[3] * b[0] + a[4] * b[1] + a[5] * b[2],
  134. a[6] * b[0] + a[7] * b[1] + a[8] * b[2]
  135. };
  136. }
  137. // Converts to a flat XYZ space with white point = (1, 1, 1)
  138. // Step 2 of https://www.adobe.com/content/dam/acom/en/devnet/photoshop/sdk/AdobeBPC.pdf
  139. constexpr Array<float, 3> flatten_and_normalize_whitepoint(Array<float, 3> whitepoint, Array<float, 3> xyz)
  140. {
  141. VERIFY(whitepoint[1] == 1.0f);
  142. return {
  143. (1.0f / whitepoint[0]) * xyz[0],
  144. xyz[1],
  145. (1.0f / whitepoint[2]) * xyz[2],
  146. };
  147. }
  148. constexpr float decode_l(float input)
  149. {
  150. constexpr float decode_l_scaling_constant = 0.00110705646f; // (((8 + 16) / 116) ^ 3) / 8
  151. if (input < 0.0f)
  152. return -decode_l(-input);
  153. if (input >= 0.0f && input <= 8.0f)
  154. return input * decode_l_scaling_constant;
  155. return powf(((input + 16.0f) / 116.0f), 3.0f);
  156. }
  157. constexpr Array<float, 3> scale_black_point(Array<float, 3> blackpoint, Array<float, 3> xyz)
  158. {
  159. auto y_dst = decode_l(0); // DestinationBlackPoint is just [0, 0, 0]
  160. auto y_src = decode_l(blackpoint[0]);
  161. auto scale = (1 - y_dst) / (1 - y_src);
  162. auto offset = 1 - scale;
  163. return {
  164. xyz[0] * scale + offset,
  165. xyz[1] * scale + offset,
  166. xyz[2] * scale + offset,
  167. };
  168. }
  169. // https://en.wikipedia.org/wiki/Illuminant_D65
  170. constexpr Array<float, 3> convert_to_d65(Array<float, 3> whitepoint, Array<float, 3> xyz)
  171. {
  172. constexpr float d65x = 0.95047f;
  173. constexpr float d65y = 1.0f;
  174. constexpr float d65z = 1.08883f;
  175. return {
  176. (xyz[0] * d65x) / whitepoint[0],
  177. (xyz[1] * d65y) / whitepoint[1],
  178. (xyz[2] * d65z) / whitepoint[2],
  179. };
  180. }
  181. // https://en.wikipedia.org/wiki/SRGB
  182. constexpr Array<float, 3> convert_to_srgb(Array<float, 3> xyz)
  183. {
  184. // See the sRGB D65 [M]^-1 matrix in the following page
  185. // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
  186. constexpr Array<float, 9> conversion_matrix = {
  187. 3.2404542,
  188. -1.5371385,
  189. -0.4985314,
  190. -0.969266,
  191. 1.8760108,
  192. 0.0415560,
  193. 0.0556434,
  194. -0.2040259,
  195. 1.0572252,
  196. };
  197. return matrix_multiply(conversion_matrix, xyz);
  198. }
  199. Color CalRGBColorSpace::color(Vector<Value> const& arguments) const
  200. {
  201. VERIFY(arguments.size() == 3);
  202. auto a = clamp(arguments[0].to_float(), 0.0f, 1.0f);
  203. auto b = clamp(arguments[1].to_float(), 0.0f, 1.0f);
  204. auto c = clamp(arguments[2].to_float(), 0.0f, 1.0f);
  205. auto agr = powf(a, m_gamma[0]);
  206. auto bgg = powf(b, m_gamma[1]);
  207. auto cgb = powf(c, m_gamma[2]);
  208. auto x = m_matrix[0] * agr + m_matrix[3] * bgg + m_matrix[6] * cgb;
  209. auto y = m_matrix[1] * agr + m_matrix[4] * bgg + m_matrix[7] * cgb;
  210. auto z = m_matrix[2] * agr + m_matrix[5] * bgg + m_matrix[8] * cgb;
  211. auto flattened_xyz = flatten_and_normalize_whitepoint(m_whitepoint, { x, y, z });
  212. auto scaled_black_point_xyz = scale_black_point(m_blackpoint, flattened_xyz);
  213. auto d65_normalized = convert_to_d65(m_whitepoint, scaled_black_point_xyz);
  214. auto srgb = convert_to_srgb(d65_normalized);
  215. auto red = static_cast<u8>(srgb[0] * 255.0f);
  216. auto green = static_cast<u8>(srgb[1] * 255.0f);
  217. auto blue = static_cast<u8>(srgb[2] * 255.0f);
  218. return Color(red, green, blue);
  219. }
  220. PDFErrorOr<NonnullRefPtr<ColorSpace>> ICCBasedColorSpace::create(Document* document, Page const& page, Vector<Value>&& parameters)
  221. {
  222. if (parameters.is_empty())
  223. return Error { Error::Type::MalformedPDF, "ICCBased color space expected one parameter" };
  224. auto param = TRY(document->resolve(parameters[0]));
  225. if (!param.has<NonnullRefPtr<Object>>() || !param.get<NonnullRefPtr<Object>>()->is<StreamObject>())
  226. return Error { Error::Type::MalformedPDF, "ICCBased color space expects a stream parameter" };
  227. auto dict = param.get<NonnullRefPtr<Object>>()->cast<StreamObject>()->dict();
  228. FlyString name;
  229. if (!dict->contains(CommonNames::Alternate)) {
  230. auto number_of_components = dict->get_value(CommonNames::N).to_int();
  231. if (number_of_components == 1)
  232. name = CommonNames::DeviceGray;
  233. else if (number_of_components == 3)
  234. name = CommonNames::DeviceRGB;
  235. else if (number_of_components == 4)
  236. name = CommonNames::DeviceCMYK;
  237. else
  238. VERIFY_NOT_REACHED();
  239. } else {
  240. name = TRY(dict->get_name(document, CommonNames::Alternate))->name();
  241. }
  242. return TRY(ColorSpace::create(document, name, page));
  243. }
  244. Color ICCBasedColorSpace::color(Vector<Value> const&) const
  245. {
  246. VERIFY_NOT_REACHED();
  247. }
  248. }