ColorSpace.cpp 14 KB

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