ColorSpace.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/ICC/WellKnownProfiles.h>
  7. #include <LibPDF/ColorSpace.h>
  8. #include <LibPDF/CommonNames.h>
  9. #include <LibPDF/Document.h>
  10. #include <LibPDF/ObjectDerivatives.h>
  11. namespace PDF {
  12. RefPtr<Gfx::ICC::Profile> ICCBasedColorSpace::s_srgb_profile;
  13. #define ENUMERATE(name, ever_needs_parameters) \
  14. ColorSpaceFamily ColorSpaceFamily::name { #name, ever_needs_parameters };
  15. ENUMERATE_COLOR_SPACE_FAMILIES(ENUMERATE);
  16. #undef ENUMERATE
  17. PDFErrorOr<ColorSpaceFamily> ColorSpaceFamily::get(DeprecatedFlyString const& family_name)
  18. {
  19. #define ENUMERATE(f_name, ever_needs_parameters) \
  20. if (family_name == f_name.name()) { \
  21. return ColorSpaceFamily::f_name; \
  22. }
  23. ENUMERATE_COLOR_SPACE_FAMILIES(ENUMERATE)
  24. #undef ENUMERATE
  25. return Error(Error::Type::MalformedPDF, DeprecatedString::formatted("Unknown ColorSpace family {}", family_name));
  26. }
  27. PDFErrorOr<NonnullRefPtr<ColorSpace>> ColorSpace::create(DeprecatedFlyString const& name)
  28. {
  29. // Simple color spaces with no parameters, which can be specified directly
  30. if (name == CommonNames::DeviceGray)
  31. return DeviceGrayColorSpace::the();
  32. if (name == CommonNames::DeviceRGB)
  33. return DeviceRGBColorSpace::the();
  34. if (name == CommonNames::DeviceCMYK)
  35. return DeviceCMYKColorSpace::the();
  36. if (name == CommonNames::Pattern)
  37. return Error::rendering_unsupported_error("Pattern color spaces not yet implemented");
  38. VERIFY_NOT_REACHED();
  39. }
  40. PDFErrorOr<NonnullRefPtr<ColorSpace>> ColorSpace::create(Document* document, NonnullRefPtr<ArrayObject> color_space_array)
  41. {
  42. auto color_space_name = TRY(color_space_array->get_name_at(document, 0))->name();
  43. Vector<Value> parameters;
  44. parameters.ensure_capacity(color_space_array->size() - 1);
  45. for (size_t i = 1; i < color_space_array->size(); i++)
  46. parameters.unchecked_append(color_space_array->at(i));
  47. if (color_space_name == CommonNames::CalRGB)
  48. return TRY(CalRGBColorSpace::create(document, move(parameters)));
  49. if (color_space_name == CommonNames::ICCBased)
  50. return TRY(ICCBasedColorSpace::create(document, move(parameters)));
  51. dbgln("Unknown color space: {}", color_space_name);
  52. return Error::rendering_unsupported_error("unknown color space");
  53. }
  54. NonnullRefPtr<DeviceGrayColorSpace> DeviceGrayColorSpace::the()
  55. {
  56. static auto instance = adopt_ref(*new DeviceGrayColorSpace());
  57. return instance;
  58. }
  59. PDFErrorOr<Color> DeviceGrayColorSpace::color(Vector<Value> const& arguments) const
  60. {
  61. VERIFY(arguments.size() == 1);
  62. auto gray = static_cast<u8>(arguments[0].to_float() * 255.0f);
  63. return Color(gray, gray, gray);
  64. }
  65. Vector<float> DeviceGrayColorSpace::default_decode() const
  66. {
  67. return { 0.0f, 1.0f };
  68. }
  69. NonnullRefPtr<DeviceRGBColorSpace> DeviceRGBColorSpace::the()
  70. {
  71. static auto instance = adopt_ref(*new DeviceRGBColorSpace());
  72. return instance;
  73. }
  74. PDFErrorOr<Color> DeviceRGBColorSpace::color(Vector<Value> const& arguments) const
  75. {
  76. VERIFY(arguments.size() == 3);
  77. auto r = static_cast<u8>(arguments[0].to_float() * 255.0f);
  78. auto g = static_cast<u8>(arguments[1].to_float() * 255.0f);
  79. auto b = static_cast<u8>(arguments[2].to_float() * 255.0f);
  80. return Color(r, g, b);
  81. }
  82. Vector<float> DeviceRGBColorSpace::default_decode() const
  83. {
  84. return { 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f };
  85. }
  86. NonnullRefPtr<DeviceCMYKColorSpace> DeviceCMYKColorSpace::the()
  87. {
  88. static auto instance = adopt_ref(*new DeviceCMYKColorSpace());
  89. return instance;
  90. }
  91. PDFErrorOr<Color> DeviceCMYKColorSpace::color(Vector<Value> const& arguments) const
  92. {
  93. VERIFY(arguments.size() == 4);
  94. auto c = arguments[0].to_float();
  95. auto m = arguments[1].to_float();
  96. auto y = arguments[2].to_float();
  97. auto k = arguments[3].to_float();
  98. return Color::from_cmyk(c, m, y, k);
  99. }
  100. Vector<float> DeviceCMYKColorSpace::default_decode() const
  101. {
  102. return { 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f };
  103. }
  104. PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> CalRGBColorSpace::create(Document* document, Vector<Value>&& parameters)
  105. {
  106. if (parameters.size() != 1)
  107. return Error { Error::Type::MalformedPDF, "RGB color space expects one parameter" };
  108. auto param = parameters[0];
  109. if (!param.has<NonnullRefPtr<Object>>() || !param.get<NonnullRefPtr<Object>>()->is<DictObject>())
  110. return Error { Error::Type::MalformedPDF, "RGB color space expects a dict parameter" };
  111. auto dict = param.get<NonnullRefPtr<Object>>()->cast<DictObject>();
  112. if (!dict->contains(CommonNames::WhitePoint))
  113. return Error { Error::Type::MalformedPDF, "RGB color space expects a Whitepoint key" };
  114. auto white_point_array = TRY(dict->get_array(document, CommonNames::WhitePoint));
  115. if (white_point_array->size() != 3)
  116. return Error { Error::Type::MalformedPDF, "RGB color space expects 3 Whitepoint parameters" };
  117. auto color_space = adopt_ref(*new CalRGBColorSpace());
  118. color_space->m_whitepoint[0] = white_point_array->at(0).to_float();
  119. color_space->m_whitepoint[1] = white_point_array->at(1).to_float();
  120. color_space->m_whitepoint[2] = white_point_array->at(2).to_float();
  121. if (color_space->m_whitepoint[1] != 1.0f)
  122. return Error { Error::Type::MalformedPDF, "RGB color space expects 2nd Whitepoint to be 1.0" };
  123. if (dict->contains(CommonNames::BlackPoint)) {
  124. auto black_point_array = TRY(dict->get_array(document, CommonNames::BlackPoint));
  125. if (black_point_array->size() == 3) {
  126. color_space->m_blackpoint[0] = black_point_array->at(0).to_float();
  127. color_space->m_blackpoint[1] = black_point_array->at(1).to_float();
  128. color_space->m_blackpoint[2] = black_point_array->at(2).to_float();
  129. }
  130. }
  131. if (dict->contains(CommonNames::Gamma)) {
  132. auto gamma_array = TRY(dict->get_array(document, CommonNames::Gamma));
  133. if (gamma_array->size() == 3) {
  134. color_space->m_gamma[0] = gamma_array->at(0).to_float();
  135. color_space->m_gamma[1] = gamma_array->at(1).to_float();
  136. color_space->m_gamma[2] = gamma_array->at(2).to_float();
  137. }
  138. }
  139. if (dict->contains(CommonNames::Matrix)) {
  140. auto matrix_array = TRY(dict->get_array(document, CommonNames::Matrix));
  141. if (matrix_array->size() == 3) {
  142. color_space->m_matrix[0] = matrix_array->at(0).to_float();
  143. color_space->m_matrix[1] = matrix_array->at(1).to_float();
  144. color_space->m_matrix[2] = matrix_array->at(2).to_float();
  145. color_space->m_matrix[3] = matrix_array->at(3).to_float();
  146. color_space->m_matrix[4] = matrix_array->at(4).to_float();
  147. color_space->m_matrix[5] = matrix_array->at(5).to_float();
  148. color_space->m_matrix[6] = matrix_array->at(6).to_float();
  149. color_space->m_matrix[7] = matrix_array->at(7).to_float();
  150. color_space->m_matrix[8] = matrix_array->at(8).to_float();
  151. }
  152. }
  153. return color_space;
  154. }
  155. constexpr Array<float, 3> matrix_multiply(Array<float, 9> a, Array<float, 3> b)
  156. {
  157. return Array<float, 3> {
  158. a[0] * b[0] + a[1] * b[1] + a[2] * b[2],
  159. a[3] * b[0] + a[4] * b[1] + a[5] * b[2],
  160. a[6] * b[0] + a[7] * b[1] + a[8] * b[2]
  161. };
  162. }
  163. // Converts to a flat XYZ space with white point = (1, 1, 1)
  164. // Step 2 of https://www.adobe.com/content/dam/acom/en/devnet/photoshop/sdk/AdobeBPC.pdf
  165. constexpr Array<float, 3> flatten_and_normalize_whitepoint(Array<float, 3> whitepoint, Array<float, 3> xyz)
  166. {
  167. VERIFY(whitepoint[1] == 1.0f);
  168. return {
  169. (1.0f / whitepoint[0]) * xyz[0],
  170. xyz[1],
  171. (1.0f / whitepoint[2]) * xyz[2],
  172. };
  173. }
  174. constexpr float decode_l(float input)
  175. {
  176. constexpr float decode_l_scaling_constant = 0.00110705646f; // (((8 + 16) / 116) ^ 3) / 8
  177. if (input < 0.0f)
  178. return -decode_l(-input);
  179. if (input >= 0.0f && input <= 8.0f)
  180. return input * decode_l_scaling_constant;
  181. return powf(((input + 16.0f) / 116.0f), 3.0f);
  182. }
  183. constexpr Array<float, 3> scale_black_point(Array<float, 3> blackpoint, Array<float, 3> xyz)
  184. {
  185. auto y_dst = decode_l(0); // DestinationBlackPoint is just [0, 0, 0]
  186. auto y_src = decode_l(blackpoint[0]);
  187. auto scale = (1 - y_dst) / (1 - y_src);
  188. auto offset = 1 - scale;
  189. return {
  190. xyz[0] * scale + offset,
  191. xyz[1] * scale + offset,
  192. xyz[2] * scale + offset,
  193. };
  194. }
  195. // https://en.wikipedia.org/wiki/Illuminant_D65
  196. constexpr Array<float, 3> convert_to_d65(Array<float, 3> whitepoint, Array<float, 3> xyz)
  197. {
  198. constexpr float d65x = 0.95047f;
  199. constexpr float d65y = 1.0f;
  200. constexpr float d65z = 1.08883f;
  201. return {
  202. (xyz[0] * d65x) / whitepoint[0],
  203. (xyz[1] * d65y) / whitepoint[1],
  204. (xyz[2] * d65z) / whitepoint[2],
  205. };
  206. }
  207. // https://en.wikipedia.org/wiki/SRGB
  208. constexpr Array<float, 3> convert_to_srgb(Array<float, 3> xyz)
  209. {
  210. // See the sRGB D65 [M]^-1 matrix in the following page
  211. // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
  212. constexpr Array<float, 9> conversion_matrix = {
  213. 3.2404542,
  214. -1.5371385,
  215. -0.4985314,
  216. -0.969266,
  217. 1.8760108,
  218. 0.0415560,
  219. 0.0556434,
  220. -0.2040259,
  221. 1.0572252,
  222. };
  223. return matrix_multiply(conversion_matrix, xyz);
  224. }
  225. PDFErrorOr<Color> CalRGBColorSpace::color(Vector<Value> const& arguments) const
  226. {
  227. VERIFY(arguments.size() == 3);
  228. auto a = clamp(arguments[0].to_float(), 0.0f, 1.0f);
  229. auto b = clamp(arguments[1].to_float(), 0.0f, 1.0f);
  230. auto c = clamp(arguments[2].to_float(), 0.0f, 1.0f);
  231. auto agr = powf(a, m_gamma[0]);
  232. auto bgg = powf(b, m_gamma[1]);
  233. auto cgb = powf(c, m_gamma[2]);
  234. auto x = m_matrix[0] * agr + m_matrix[3] * bgg + m_matrix[6] * cgb;
  235. auto y = m_matrix[1] * agr + m_matrix[4] * bgg + m_matrix[7] * cgb;
  236. auto z = m_matrix[2] * agr + m_matrix[5] * bgg + m_matrix[8] * cgb;
  237. auto flattened_xyz = flatten_and_normalize_whitepoint(m_whitepoint, { x, y, z });
  238. auto scaled_black_point_xyz = scale_black_point(m_blackpoint, flattened_xyz);
  239. auto d65_normalized = convert_to_d65(m_whitepoint, scaled_black_point_xyz);
  240. auto srgb = convert_to_srgb(d65_normalized);
  241. auto red = static_cast<u8>(srgb[0] * 255.0f);
  242. auto green = static_cast<u8>(srgb[1] * 255.0f);
  243. auto blue = static_cast<u8>(srgb[2] * 255.0f);
  244. return Color(red, green, blue);
  245. }
  246. Vector<float> CalRGBColorSpace::default_decode() const
  247. {
  248. return { 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f };
  249. }
  250. PDFErrorOr<NonnullRefPtr<ColorSpace>> ICCBasedColorSpace::create(Document* document, Vector<Value>&& parameters)
  251. {
  252. if (parameters.is_empty())
  253. return Error { Error::Type::MalformedPDF, "ICCBased color space expected one parameter" };
  254. auto param = TRY(document->resolve(parameters[0]));
  255. if (!param.has<NonnullRefPtr<Object>>() || !param.get<NonnullRefPtr<Object>>()->is<StreamObject>())
  256. return Error { Error::Type::MalformedPDF, "ICCBased color space expects a stream parameter" };
  257. auto stream = param.get<NonnullRefPtr<Object>>()->cast<StreamObject>();
  258. auto dict = stream->dict();
  259. auto maybe_profile = Gfx::ICC::Profile::try_load_from_externally_owned_memory(stream->bytes());
  260. if (!maybe_profile.is_error())
  261. return adopt_ref(*new ICCBasedColorSpace(maybe_profile.release_value()));
  262. if (dict->contains(CommonNames::Alternate)) {
  263. auto alternate_color_space_object = MUST(dict->get_object(document, CommonNames::Alternate));
  264. if (alternate_color_space_object->is<NameObject>())
  265. return ColorSpace::create(alternate_color_space_object->cast<NameObject>()->name());
  266. return Error { Error::Type::Internal, "Alternate color spaces in array format are not supported" };
  267. }
  268. return Error { Error::Type::MalformedPDF, "Failed to load ICC color space with malformed profile and no alternate" };
  269. }
  270. ICCBasedColorSpace::ICCBasedColorSpace(NonnullRefPtr<Gfx::ICC::Profile> profile)
  271. : m_profile(profile)
  272. {
  273. }
  274. PDFErrorOr<Color> ICCBasedColorSpace::color(Vector<Value> const& arguments) const
  275. {
  276. if (!s_srgb_profile)
  277. s_srgb_profile = TRY(Gfx::ICC::sRGB());
  278. Vector<u8> bytes;
  279. for (auto const& arg : arguments) {
  280. VERIFY(arg.has_number());
  281. bytes.append(static_cast<u8>(arg.to_float() * 255.0f));
  282. }
  283. auto pcs = TRY(m_profile->to_pcs(bytes));
  284. Array<u8, 3> output;
  285. TRY(s_srgb_profile->from_pcs(pcs, output.span()));
  286. return Color(output[0], output[1], output[2]);
  287. }
  288. Vector<float> ICCBasedColorSpace::default_decode() const
  289. {
  290. auto color_space = m_profile->data_color_space();
  291. switch (color_space) {
  292. case Gfx::ICC::ColorSpace::Gray:
  293. return { 0.0, 1.0 };
  294. case Gfx::ICC::ColorSpace::RGB:
  295. return { 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 };
  296. case Gfx::ICC::ColorSpace::CMYK:
  297. return { 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 };
  298. default:
  299. warnln("PDF: Unknown default_decode params for color space {}", Gfx::ICC::data_color_space_name(color_space));
  300. Vector<float> decoding_ranges;
  301. for (u8 i = 0; i < Gfx::ICC::number_of_components_in_color_space(color_space); i++) {
  302. decoding_ranges.append(0.0);
  303. decoding_ranges.append(1.0);
  304. }
  305. return decoding_ranges;
  306. }
  307. }
  308. }