ColorSpace.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. #include <LibPDF/Renderer.h>
  12. namespace PDF {
  13. RefPtr<Gfx::ICC::Profile> ICCBasedColorSpace::s_srgb_profile;
  14. #define ENUMERATE(name, may_be_specified_directly) \
  15. ColorSpaceFamily ColorSpaceFamily::name { #name, may_be_specified_directly };
  16. ENUMERATE_COLOR_SPACE_FAMILIES(ENUMERATE);
  17. #undef ENUMERATE
  18. PDFErrorOr<ColorSpaceFamily> ColorSpaceFamily::get(DeprecatedFlyString const& family_name)
  19. {
  20. #define ENUMERATE(f_name, may_be_specified_directly) \
  21. if (family_name == f_name.name()) { \
  22. return ColorSpaceFamily::f_name; \
  23. }
  24. ENUMERATE_COLOR_SPACE_FAMILIES(ENUMERATE)
  25. #undef ENUMERATE
  26. dbgln_if(PDF_DEBUG, "Unknown ColorSpace family: {}", family_name);
  27. return Error(Error::Type::MalformedPDF, "Unknown ColorSpace family"_string);
  28. }
  29. PDFErrorOr<NonnullRefPtr<ColorSpace>> ColorSpace::create(Document* document, NonnullRefPtr<Object> color_space_object, Renderer& renderer)
  30. {
  31. // "A color space is defined by an array object whose first element is a name object identifying the color space family.
  32. // The remaining array elements, if any, are parameters that further characterize the color space;
  33. // their number and types vary according to the particular family.
  34. // For families that do not require parameters, the color space can be specified simply by the family name itself instead of an array."
  35. if (color_space_object->is<NameObject>())
  36. return ColorSpace::create(color_space_object->cast<NameObject>()->name(), renderer);
  37. if (color_space_object->is<ArrayObject>())
  38. return ColorSpace::create(document, color_space_object->cast<ArrayObject>(), renderer);
  39. return Error { Error::Type::MalformedPDF, "Color space must be name or array" };
  40. }
  41. PDFErrorOr<NonnullRefPtr<ColorSpace>> ColorSpace::create(DeprecatedFlyString const& name, Renderer& renderer)
  42. {
  43. // Simple color spaces with no parameters, which can be specified directly
  44. if (name == CommonNames::DeviceGray)
  45. return DeviceGrayColorSpace::the();
  46. if (name == CommonNames::DeviceRGB)
  47. return DeviceRGBColorSpace::the();
  48. if (name == CommonNames::DeviceCMYK)
  49. return DeviceCMYKColorSpace::the();
  50. if (name == CommonNames::Pattern)
  51. return PatternColorSpace::create(renderer);
  52. VERIFY_NOT_REACHED();
  53. }
  54. PDFErrorOr<NonnullRefPtr<ColorSpace>> ColorSpace::create(Document* document, NonnullRefPtr<ArrayObject> color_space_array, Renderer& renderer)
  55. {
  56. auto color_space_name = TRY(color_space_array->get_name_at(document, 0))->name();
  57. Vector<Value> parameters;
  58. parameters.ensure_capacity(color_space_array->size() - 1);
  59. for (size_t i = 1; i < color_space_array->size(); i++)
  60. parameters.unchecked_append(color_space_array->at(i));
  61. if (color_space_name == CommonNames::CalGray)
  62. return TRY(CalGrayColorSpace::create(document, move(parameters)));
  63. if (color_space_name == CommonNames::CalRGB)
  64. return TRY(CalRGBColorSpace::create(document, move(parameters)));
  65. if (color_space_name == CommonNames::DeviceN)
  66. return TRY(DeviceNColorSpace::create(document, move(parameters), renderer));
  67. if (color_space_name == CommonNames::ICCBased)
  68. return TRY(ICCBasedColorSpace::create(document, move(parameters), renderer));
  69. if (color_space_name == CommonNames::Indexed)
  70. return TRY(IndexedColorSpace::create(document, move(parameters), renderer));
  71. if (color_space_name == CommonNames::Lab)
  72. return TRY(LabColorSpace::create(document, move(parameters)));
  73. if (color_space_name == CommonNames::Separation)
  74. return TRY(SeparationColorSpace::create(document, move(parameters), renderer));
  75. dbgln("Unknown color space: {}", color_space_name);
  76. return Error::rendering_unsupported_error("unknown color space");
  77. }
  78. NonnullRefPtr<DeviceGrayColorSpace> DeviceGrayColorSpace::the()
  79. {
  80. static auto instance = adopt_ref(*new DeviceGrayColorSpace());
  81. return instance;
  82. }
  83. PDFErrorOr<ColorOrStyle> DeviceGrayColorSpace::style(ReadonlySpan<Value> arguments) const
  84. {
  85. VERIFY(arguments.size() == 1);
  86. auto gray = static_cast<u8>(arguments[0].to_float() * 255.0f);
  87. return Color(gray, gray, gray);
  88. }
  89. Vector<float> DeviceGrayColorSpace::default_decode() const
  90. {
  91. return { 0.0f, 1.0f };
  92. }
  93. NonnullRefPtr<DeviceRGBColorSpace> DeviceRGBColorSpace::the()
  94. {
  95. static auto instance = adopt_ref(*new DeviceRGBColorSpace());
  96. return instance;
  97. }
  98. PDFErrorOr<ColorOrStyle> DeviceRGBColorSpace::style(ReadonlySpan<Value> arguments) const
  99. {
  100. VERIFY(arguments.size() == 3);
  101. auto r = static_cast<u8>(arguments[0].to_float() * 255.0f);
  102. auto g = static_cast<u8>(arguments[1].to_float() * 255.0f);
  103. auto b = static_cast<u8>(arguments[2].to_float() * 255.0f);
  104. return Color(r, g, b);
  105. }
  106. Vector<float> DeviceRGBColorSpace::default_decode() const
  107. {
  108. return { 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f };
  109. }
  110. NonnullRefPtr<DeviceCMYKColorSpace> DeviceCMYKColorSpace::the()
  111. {
  112. static auto instance = adopt_ref(*new DeviceCMYKColorSpace());
  113. return instance;
  114. }
  115. PDFErrorOr<ColorOrStyle> DeviceCMYKColorSpace::style(ReadonlySpan<Value> arguments) const
  116. {
  117. VERIFY(arguments.size() == 4);
  118. auto c = arguments[0].to_float();
  119. auto m = arguments[1].to_float();
  120. auto y = arguments[2].to_float();
  121. auto k = arguments[3].to_float();
  122. return Color::from_cmyk(c, m, y, k);
  123. }
  124. Vector<float> DeviceCMYKColorSpace::default_decode() const
  125. {
  126. return { 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f };
  127. }
  128. PDFErrorOr<NonnullRefPtr<DeviceNColorSpace>> DeviceNColorSpace::create(Document* document, Vector<Value>&& parameters, Renderer& renderer)
  129. {
  130. // "[ /DeviceN names alternateSpace tintTransform ]
  131. // or
  132. // [ /DeviceN names alternateSpace tintTransform attributes ]"
  133. // (`/DeviceN` is already stripped from the array by the time we get here.)
  134. if (parameters.size() != 3 && parameters.size() != 4)
  135. return Error { Error::Type::MalformedPDF, "DeviceN color space expects 4 or 5 parameters" };
  136. // "The names parameter is an array of name objects specifying the individual color components.
  137. // The length of the array determines the number of components in the DeviceN color space"
  138. auto names_array = TRY(document->resolve_to<ArrayObject>(parameters[0]));
  139. Vector<DeprecatedString> names;
  140. for (size_t i = 0; i < names_array->size(); ++i)
  141. names.append(names_array->get_name_at(i)->name());
  142. // "The alternateSpace parameter is an array or name object that can be any device or CIE-based color space
  143. // but not another special color space (Pattern, Indexed, Separation, or DeviceN)."
  144. auto alternate_space_object = TRY(document->resolve_to<Object>(parameters[1]));
  145. auto alternate_space = TRY(ColorSpace::create(document, alternate_space_object, renderer));
  146. auto family = alternate_space->family();
  147. if (family == ColorSpaceFamily::Pattern || family == ColorSpaceFamily::Indexed || family == ColorSpaceFamily::Separation || family == ColorSpaceFamily::DeviceN)
  148. return Error { Error::Type::MalformedPDF, "DeviceN color space has invalid alternate color space" };
  149. // "The tintTransform parameter specifies a function"
  150. auto tint_transform_object = TRY(document->resolve_to<Object>(parameters[2]));
  151. auto tint_transform = TRY(Function::create(document, tint_transform_object));
  152. // FIXME: If `attributes` is present and has /Subtype set to /NChannel, possibly
  153. // do slightly different processing.
  154. auto color_space = adopt_ref(*new DeviceNColorSpace(move(alternate_space), move(tint_transform)));
  155. color_space->m_names = move(names);
  156. return color_space;
  157. }
  158. DeviceNColorSpace::DeviceNColorSpace(NonnullRefPtr<ColorSpace> alternate_space, NonnullRefPtr<Function> tint_transform)
  159. : m_alternate_space(move(alternate_space))
  160. , m_tint_transform(move(tint_transform))
  161. {
  162. }
  163. PDFErrorOr<ColorOrStyle> DeviceNColorSpace::style(ReadonlySpan<Value> arguments) const
  164. {
  165. // FIXME: Does this need handling for the special colorant name "None"?
  166. // FIXME: When drawing to a printer, do something else.
  167. m_tint_input_values.resize(arguments.size());
  168. for (size_t i = 0; i < arguments.size(); ++i)
  169. m_tint_input_values[i] = arguments[i].to_float();
  170. auto tint_output = TRY(m_tint_transform->evaluate(m_tint_input_values.span()));
  171. m_tint_output_values.resize(tint_output.size());
  172. for (size_t i = 0; i < tint_output.size(); ++i)
  173. m_tint_output_values[i] = tint_output[i];
  174. return m_alternate_space->style(m_tint_output_values);
  175. }
  176. int DeviceNColorSpace::number_of_components() const
  177. {
  178. return m_names.size();
  179. }
  180. Vector<float> DeviceNColorSpace::default_decode() const
  181. {
  182. Vector<float> decoding_ranges;
  183. for (u8 i = 0; i < number_of_components(); i++) {
  184. decoding_ranges.append(0.0);
  185. decoding_ranges.append(1.0);
  186. }
  187. return decoding_ranges;
  188. }
  189. constexpr Array<float, 3> matrix_multiply(Array<float, 9> a, Array<float, 3> b)
  190. {
  191. return Array<float, 3> {
  192. a[0] * b[0] + a[1] * b[1] + a[2] * b[2],
  193. a[3] * b[0] + a[4] * b[1] + a[5] * b[2],
  194. a[6] * b[0] + a[7] * b[1] + a[8] * b[2]
  195. };
  196. }
  197. // Converts to a flat XYZ space with white point = (1, 1, 1)
  198. // Step 2 of https://www.color.org/adobebpc.pdf
  199. constexpr Array<float, 3> flatten_and_normalize_whitepoint(Array<float, 3> whitepoint, Array<float, 3> xyz)
  200. {
  201. VERIFY(whitepoint[1] == 1.0f);
  202. return {
  203. (1.0f / whitepoint[0]) * xyz[0],
  204. xyz[1],
  205. (1.0f / whitepoint[2]) * xyz[2],
  206. };
  207. }
  208. constexpr float decode_l(float input)
  209. {
  210. constexpr float decode_l_scaling_constant = 0.00110705646f; // (((8 + 16) / 116) ^ 3) / 8
  211. if (input < 0.0f)
  212. return -decode_l(-input);
  213. if (input >= 0.0f && input <= 8.0f)
  214. return input * decode_l_scaling_constant;
  215. return powf(((input + 16.0f) / 116.0f), 3.0f);
  216. }
  217. constexpr Array<float, 3> scale_black_point(Array<float, 3> blackpoint, Array<float, 3> xyz)
  218. {
  219. auto y_dst = decode_l(0); // DestinationBlackPoint is just [0, 0, 0]
  220. auto y_src = decode_l(blackpoint[0]);
  221. auto scale = (1 - y_dst) / (1 - y_src);
  222. auto offset = 1 - scale;
  223. return {
  224. xyz[0] * scale + offset,
  225. xyz[1] * scale + offset,
  226. xyz[2] * scale + offset,
  227. };
  228. }
  229. // https://en.wikipedia.org/wiki/Illuminant_D65
  230. constexpr Array<float, 3> convert_to_d65(Array<float, 3> xyz)
  231. {
  232. constexpr float d65x = 0.95047f;
  233. constexpr float d65y = 1.0f;
  234. constexpr float d65z = 1.08883f;
  235. return { xyz[0] * d65x, xyz[1] * d65y, xyz[2] * d65z };
  236. }
  237. // https://en.wikipedia.org/wiki/SRGB
  238. constexpr Array<float, 3> convert_to_srgb(Array<float, 3> xyz)
  239. {
  240. // See the sRGB D65 [M]^-1 matrix in the following page
  241. // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
  242. constexpr Array<float, 9> conversion_matrix = {
  243. 3.2404542,
  244. -1.5371385,
  245. -0.4985314,
  246. -0.969266,
  247. 1.8760108,
  248. 0.0415560,
  249. 0.0556434,
  250. -0.2040259,
  251. 1.0572252,
  252. };
  253. auto linear_srgb = matrix_multiply(conversion_matrix, xyz);
  254. // FIXME: Use the real sRGB curve by replacing this function with Gfx::ICC::sRGB().from_pcs().
  255. return { pow(linear_srgb[0], 1.0f / 2.2f), pow(linear_srgb[1], 1.0f / 2.2f), pow(linear_srgb[2], 1.0f / 2.2f) };
  256. }
  257. PDFErrorOr<NonnullRefPtr<CalGrayColorSpace>> CalGrayColorSpace::create(Document* document, Vector<Value>&& parameters)
  258. {
  259. if (parameters.size() != 1)
  260. return Error { Error::Type::MalformedPDF, "Gray color space expects one parameter" };
  261. auto dict = TRY(document->resolve_to<DictObject>(parameters[0]));
  262. if (!dict->contains(CommonNames::WhitePoint))
  263. return Error { Error::Type::MalformedPDF, "Gray color space expects a Whitepoint key" };
  264. auto white_point_array = TRY(dict->get_array(document, CommonNames::WhitePoint));
  265. if (white_point_array->size() != 3)
  266. return Error { Error::Type::MalformedPDF, "Gray color space expects 3 Whitepoint parameters" };
  267. auto color_space = adopt_ref(*new CalGrayColorSpace());
  268. color_space->m_whitepoint[0] = white_point_array->at(0).to_float();
  269. color_space->m_whitepoint[1] = white_point_array->at(1).to_float();
  270. color_space->m_whitepoint[2] = white_point_array->at(2).to_float();
  271. if (color_space->m_whitepoint[1] != 1.0f)
  272. return Error { Error::Type::MalformedPDF, "Gray color space expects 2nd Whitepoint to be 1.0" };
  273. if (dict->contains(CommonNames::BlackPoint)) {
  274. auto black_point_array = TRY(dict->get_array(document, CommonNames::BlackPoint));
  275. if (black_point_array->size() == 3) {
  276. color_space->m_blackpoint[0] = black_point_array->at(0).to_float();
  277. color_space->m_blackpoint[1] = black_point_array->at(1).to_float();
  278. color_space->m_blackpoint[2] = black_point_array->at(2).to_float();
  279. }
  280. }
  281. if (dict->contains(CommonNames::Gamma)) {
  282. color_space->m_gamma = TRY(document->resolve(dict->get_value(CommonNames::Gamma))).to_float();
  283. }
  284. return color_space;
  285. }
  286. PDFErrorOr<ColorOrStyle> CalGrayColorSpace::style(ReadonlySpan<Value> arguments) const
  287. {
  288. VERIFY(arguments.size() == 1);
  289. auto a = clamp(arguments[0].to_float(), 0.0f, 1.0f);
  290. auto ag = powf(a, m_gamma);
  291. auto x = m_whitepoint[0] * ag;
  292. auto y = m_whitepoint[1] * ag;
  293. auto z = m_whitepoint[2] * ag;
  294. auto flattened_xyz = flatten_and_normalize_whitepoint(m_whitepoint, { x, y, z });
  295. auto scaled_black_point_xyz = scale_black_point(m_blackpoint, flattened_xyz);
  296. auto d65_normalized = convert_to_d65(scaled_black_point_xyz);
  297. auto srgb = convert_to_srgb(d65_normalized);
  298. auto red = static_cast<u8>(clamp(srgb[0], 0.0f, 1.0f) * 255.0f);
  299. auto green = static_cast<u8>(clamp(srgb[1], 0.0f, 1.0f) * 255.0f);
  300. auto blue = static_cast<u8>(clamp(srgb[2], 0.0f, 1.0f) * 255.0f);
  301. return Color(red, green, blue);
  302. }
  303. Vector<float> CalGrayColorSpace::default_decode() const
  304. {
  305. return { 0.0f, 1.0f };
  306. }
  307. PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> CalRGBColorSpace::create(Document* document, Vector<Value>&& parameters)
  308. {
  309. if (parameters.size() != 1)
  310. return Error { Error::Type::MalformedPDF, "RGB color space expects one parameter" };
  311. auto dict = TRY(document->resolve_to<DictObject>(parameters[0]));
  312. if (!dict->contains(CommonNames::WhitePoint))
  313. return Error { Error::Type::MalformedPDF, "RGB color space expects a Whitepoint key" };
  314. auto white_point_array = TRY(dict->get_array(document, CommonNames::WhitePoint));
  315. if (white_point_array->size() != 3)
  316. return Error { Error::Type::MalformedPDF, "RGB color space expects 3 Whitepoint parameters" };
  317. auto color_space = adopt_ref(*new CalRGBColorSpace());
  318. color_space->m_whitepoint[0] = white_point_array->at(0).to_float();
  319. color_space->m_whitepoint[1] = white_point_array->at(1).to_float();
  320. color_space->m_whitepoint[2] = white_point_array->at(2).to_float();
  321. if (color_space->m_whitepoint[1] != 1.0f)
  322. return Error { Error::Type::MalformedPDF, "RGB color space expects 2nd Whitepoint to be 1.0" };
  323. if (dict->contains(CommonNames::BlackPoint)) {
  324. auto black_point_array = TRY(dict->get_array(document, CommonNames::BlackPoint));
  325. if (black_point_array->size() == 3) {
  326. color_space->m_blackpoint[0] = black_point_array->at(0).to_float();
  327. color_space->m_blackpoint[1] = black_point_array->at(1).to_float();
  328. color_space->m_blackpoint[2] = black_point_array->at(2).to_float();
  329. }
  330. }
  331. if (dict->contains(CommonNames::Gamma)) {
  332. auto gamma_array = TRY(dict->get_array(document, CommonNames::Gamma));
  333. if (gamma_array->size() == 3) {
  334. color_space->m_gamma[0] = gamma_array->at(0).to_float();
  335. color_space->m_gamma[1] = gamma_array->at(1).to_float();
  336. color_space->m_gamma[2] = gamma_array->at(2).to_float();
  337. }
  338. }
  339. if (dict->contains(CommonNames::Matrix)) {
  340. auto matrix_array = TRY(dict->get_array(document, CommonNames::Matrix));
  341. if (matrix_array->size() == 9) {
  342. color_space->m_matrix[0] = matrix_array->at(0).to_float();
  343. color_space->m_matrix[1] = matrix_array->at(1).to_float();
  344. color_space->m_matrix[2] = matrix_array->at(2).to_float();
  345. color_space->m_matrix[3] = matrix_array->at(3).to_float();
  346. color_space->m_matrix[4] = matrix_array->at(4).to_float();
  347. color_space->m_matrix[5] = matrix_array->at(5).to_float();
  348. color_space->m_matrix[6] = matrix_array->at(6).to_float();
  349. color_space->m_matrix[7] = matrix_array->at(7).to_float();
  350. color_space->m_matrix[8] = matrix_array->at(8).to_float();
  351. }
  352. }
  353. return color_space;
  354. }
  355. PDFErrorOr<ColorOrStyle> CalRGBColorSpace::style(ReadonlySpan<Value> arguments) const
  356. {
  357. VERIFY(arguments.size() == 3);
  358. auto a = clamp(arguments[0].to_float(), 0.0f, 1.0f);
  359. auto b = clamp(arguments[1].to_float(), 0.0f, 1.0f);
  360. auto c = clamp(arguments[2].to_float(), 0.0f, 1.0f);
  361. auto agr = powf(a, m_gamma[0]);
  362. auto bgg = powf(b, m_gamma[1]);
  363. auto cgb = powf(c, m_gamma[2]);
  364. auto x = m_matrix[0] * agr + m_matrix[3] * bgg + m_matrix[6] * cgb;
  365. auto y = m_matrix[1] * agr + m_matrix[4] * bgg + m_matrix[7] * cgb;
  366. auto z = m_matrix[2] * agr + m_matrix[5] * bgg + m_matrix[8] * cgb;
  367. auto flattened_xyz = flatten_and_normalize_whitepoint(m_whitepoint, { x, y, z });
  368. auto scaled_black_point_xyz = scale_black_point(m_blackpoint, flattened_xyz);
  369. auto d65_normalized = convert_to_d65(scaled_black_point_xyz);
  370. auto srgb = convert_to_srgb(d65_normalized);
  371. auto red = static_cast<u8>(clamp(srgb[0], 0.0f, 1.0f) * 255.0f);
  372. auto green = static_cast<u8>(clamp(srgb[1], 0.0f, 1.0f) * 255.0f);
  373. auto blue = static_cast<u8>(clamp(srgb[2], 0.0f, 1.0f) * 255.0f);
  374. return Color(red, green, blue);
  375. }
  376. Vector<float> CalRGBColorSpace::default_decode() const
  377. {
  378. return { 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f };
  379. }
  380. PDFErrorOr<NonnullRefPtr<ColorSpace>> ICCBasedColorSpace::create(Document* document, Vector<Value>&& parameters, Renderer& renderer)
  381. {
  382. if (parameters.is_empty())
  383. return Error { Error::Type::MalformedPDF, "ICCBased color space expected one parameter" };
  384. auto stream = TRY(document->resolve_to<StreamObject>(parameters[0]));
  385. auto dict = stream->dict();
  386. auto maybe_profile = Gfx::ICC::Profile::try_load_from_externally_owned_memory(stream->bytes());
  387. if (!maybe_profile.is_error())
  388. return adopt_ref(*new ICCBasedColorSpace(maybe_profile.release_value()));
  389. if (dict->contains(CommonNames::Alternate)) {
  390. auto alternate_color_space_object = MUST(dict->get_object(document, CommonNames::Alternate));
  391. if (alternate_color_space_object->is<NameObject>())
  392. return ColorSpace::create(alternate_color_space_object->cast<NameObject>()->name(), renderer);
  393. return Error { Error::Type::Internal, "Alternate color spaces in array format are not supported" };
  394. }
  395. return Error { Error::Type::MalformedPDF, "Failed to load ICC color space with malformed profile and no alternate" };
  396. }
  397. ICCBasedColorSpace::ICCBasedColorSpace(NonnullRefPtr<Gfx::ICC::Profile> profile)
  398. : m_profile(profile)
  399. {
  400. }
  401. PDFErrorOr<ColorOrStyle> ICCBasedColorSpace::style(ReadonlySpan<Value> arguments) const
  402. {
  403. if (!s_srgb_profile)
  404. s_srgb_profile = TRY(Gfx::ICC::sRGB());
  405. Vector<u8> bytes;
  406. for (size_t i = 0; i < arguments.size(); ++i) {
  407. auto const& arg = arguments[i];
  408. VERIFY(arg.has_number());
  409. float number = arg.to_float();
  410. if (m_profile->data_color_space() == Gfx::ICC::ColorSpace::CIELAB) {
  411. // CIELAB channels go from 0..100 and -128..127 instead of from 0..1.
  412. // FIXME: We should probably have an API on Gfx::ICC::Profile that takes floats instead of bytes and that does this internally instead.
  413. if (i == 0)
  414. number /= 100.0f;
  415. else
  416. number = (number + 128.0f) / 255.0f;
  417. }
  418. bytes.append(static_cast<u8>(number * 255.0f));
  419. }
  420. auto pcs = TRY(m_profile->to_pcs(bytes));
  421. Array<u8, 3> output;
  422. TRY(s_srgb_profile->from_pcs(m_profile, pcs, output.span()));
  423. return Color(output[0], output[1], output[2]);
  424. }
  425. int ICCBasedColorSpace::number_of_components() const
  426. {
  427. return Gfx::ICC::number_of_components_in_color_space(m_profile->data_color_space());
  428. }
  429. Vector<float> ICCBasedColorSpace::default_decode() const
  430. {
  431. auto color_space = m_profile->data_color_space();
  432. switch (color_space) {
  433. case Gfx::ICC::ColorSpace::Gray:
  434. return { 0.0, 1.0 };
  435. case Gfx::ICC::ColorSpace::RGB:
  436. return { 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 };
  437. case Gfx::ICC::ColorSpace::CMYK:
  438. return { 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 };
  439. default:
  440. warnln("PDF: Unknown default_decode params for color space {}", Gfx::ICC::data_color_space_name(color_space));
  441. Vector<float> decoding_ranges;
  442. for (u8 i = 0; i < Gfx::ICC::number_of_components_in_color_space(color_space); i++) {
  443. decoding_ranges.append(0.0);
  444. decoding_ranges.append(1.0);
  445. }
  446. return decoding_ranges;
  447. }
  448. }
  449. PDFErrorOr<NonnullRefPtr<LabColorSpace>> LabColorSpace::create(Document* document, Vector<Value>&& parameters)
  450. {
  451. if (parameters.size() != 1)
  452. return Error { Error::Type::MalformedPDF, "Lab color space expects one parameter" };
  453. auto dict = TRY(document->resolve_to<DictObject>(parameters[0]));
  454. if (!dict->contains(CommonNames::WhitePoint))
  455. return Error { Error::Type::MalformedPDF, "Lab color space expects a Whitepoint key" };
  456. auto white_point_array = TRY(dict->get_array(document, CommonNames::WhitePoint));
  457. if (white_point_array->size() != 3)
  458. return Error { Error::Type::MalformedPDF, "Lab color space expects 3 Whitepoint parameters" };
  459. auto color_space = adopt_ref(*new LabColorSpace());
  460. color_space->m_whitepoint[0] = white_point_array->at(0).to_float();
  461. color_space->m_whitepoint[1] = white_point_array->at(1).to_float();
  462. color_space->m_whitepoint[2] = white_point_array->at(2).to_float();
  463. if (color_space->m_whitepoint[1] != 1.0f)
  464. return Error { Error::Type::MalformedPDF, "Lab color space expects 2nd Whitepoint to be 1.0" };
  465. if (dict->contains(CommonNames::BlackPoint)) {
  466. auto black_point_array = TRY(dict->get_array(document, CommonNames::BlackPoint));
  467. if (black_point_array->size() == 3) {
  468. color_space->m_blackpoint[0] = black_point_array->at(0).to_float();
  469. color_space->m_blackpoint[1] = black_point_array->at(1).to_float();
  470. color_space->m_blackpoint[2] = black_point_array->at(2).to_float();
  471. }
  472. }
  473. if (dict->contains(CommonNames::Range)) {
  474. auto range_array = TRY(dict->get_array(document, CommonNames::Range));
  475. if (range_array->size() == 4) {
  476. color_space->m_range[0] = range_array->at(0).to_float();
  477. color_space->m_range[1] = range_array->at(1).to_float();
  478. color_space->m_range[2] = range_array->at(2).to_float();
  479. color_space->m_range[3] = range_array->at(3).to_float();
  480. }
  481. }
  482. return color_space;
  483. }
  484. PDFErrorOr<ColorOrStyle> LabColorSpace::style(ReadonlySpan<Value> arguments) const
  485. {
  486. VERIFY(arguments.size() == 3);
  487. auto L_star = clamp(arguments[0].to_float(), 0.0f, 100.0f);
  488. auto a_star = clamp(arguments[1].to_float(), m_range[0], m_range[1]);
  489. auto b_star = clamp(arguments[2].to_float(), m_range[2], m_range[3]);
  490. auto L = (L_star + 16) / 116 + a_star / 500;
  491. auto M = (L_star + 16) / 116;
  492. auto N = (L_star + 16) / 116 - b_star / 200;
  493. auto g = [](float x) {
  494. if (x >= 6.0f / 29.0f)
  495. return powf(x, 3);
  496. return 108.0f / 841.0f * (x - 4.0f / 29.0f);
  497. };
  498. auto x = m_whitepoint[0] * g(L);
  499. auto y = m_whitepoint[1] * g(M);
  500. auto z = m_whitepoint[2] * g(N);
  501. auto flattened_xyz = flatten_and_normalize_whitepoint(m_whitepoint, { x, y, z });
  502. auto scaled_black_point_xyz = scale_black_point(m_blackpoint, flattened_xyz);
  503. auto d65_normalized = convert_to_d65(scaled_black_point_xyz);
  504. auto srgb = convert_to_srgb(d65_normalized);
  505. auto red = static_cast<u8>(clamp(srgb[0], 0.0f, 1.0f) * 255.0f);
  506. auto green = static_cast<u8>(clamp(srgb[1], 0.0f, 1.0f) * 255.0f);
  507. auto blue = static_cast<u8>(clamp(srgb[2], 0.0f, 1.0f) * 255.0f);
  508. return Color(red, green, blue);
  509. }
  510. Vector<float> LabColorSpace::default_decode() const
  511. {
  512. return { 0.0f, 100.0f, m_range[0], m_range[1], m_range[2], m_range[3] };
  513. }
  514. PDFErrorOr<NonnullRefPtr<ColorSpace>> IndexedColorSpace::create(Document* document, Vector<Value>&& parameters, Renderer& renderer)
  515. {
  516. if (parameters.size() != 3)
  517. return Error { Error::Type::MalformedPDF, "Indexed color space expected three parameters" };
  518. // "The base parameter is an array or name that identifies the base color space in which the values
  519. // in the color table are to be interpreted. It can be any device or CIE-based color space or (in PDF 1.3)
  520. // a Separation or DeviceN space, but not a Pattern space or another Indexed space."
  521. auto base_object = TRY(document->resolve_to<Object>(parameters[0]));
  522. auto base = TRY(ColorSpace::create(document, base_object, renderer));
  523. if (base->family() == ColorSpaceFamily::Pattern || base->family() == ColorSpaceFamily::Indexed)
  524. return Error { Error::Type::MalformedPDF, "Indexed color space has invalid base color space" };
  525. // "The hival parameter is an integer that specifies the maximum valid index value. In other words,
  526. // the color table is to be indexed by integers in the range 0 to hival. hival can be no greater than 255"
  527. auto hival = TRY(document->resolve_to<int>(parameters[1]));
  528. if (hival < 0 || hival > 255)
  529. return Error { Error::Type::MalformedPDF, "Indexed color space hival out of range" };
  530. // "The color table is defined by the lookup parameter, which can be either a stream or (in PDF 1.2) a byte string.
  531. // It provides the mapping between index values and the corresponding colors in the base color space.
  532. // The color table data must be m × (hival + 1) bytes long, where m is the number of color components in the
  533. // base color space. Each byte is an unsigned integer in the range 0 to 255 that is scaled to the range of
  534. // the corresponding color component in the base color space; that is, 0 corresponds to the minimum value
  535. // in the range for that component, and 255 corresponds to the maximum."
  536. auto lookup_object = TRY(document->resolve_to<Object>(parameters[2]));
  537. Vector<u8> lookup;
  538. if (lookup_object->is<StreamObject>()) {
  539. lookup = Vector<u8> { lookup_object->cast<StreamObject>()->bytes() };
  540. } else if (lookup_object->is<StringObject>()) {
  541. // FIXME: Check if it's a hex string.
  542. auto const& string = lookup_object->cast<StringObject>()->string();
  543. lookup = Vector<u8> { ReadonlyBytes { string.characters(), string.length() } };
  544. } else {
  545. return Error { Error::Type::MalformedPDF, "Indexed color space expects stream or string for third arg" };
  546. }
  547. size_t needed_size = (hival + 1) * base->number_of_components();
  548. if (lookup.size() - 1 == needed_size) {
  549. // FIXME: Could do this if lookup.size() > needed_size generally, but so far I've only seen files that had one byte too much.
  550. lookup.resize(needed_size);
  551. }
  552. if (lookup.size() != needed_size) {
  553. dbgln("lookup size {} doesn't match hival {} and base components {}", lookup.size(), hival, base->number_of_components());
  554. return Error { Error::Type::MalformedPDF, "Indexed color space lookup table doesn't match size" };
  555. }
  556. auto color_space = adopt_ref(*new IndexedColorSpace(move(base)));
  557. color_space->m_hival = hival;
  558. color_space->m_lookup = move(lookup);
  559. return color_space;
  560. }
  561. IndexedColorSpace::IndexedColorSpace(NonnullRefPtr<ColorSpace> base)
  562. : m_base(move(base))
  563. {
  564. }
  565. PDFErrorOr<ColorOrStyle> IndexedColorSpace::style(ReadonlySpan<Value> arguments) const
  566. {
  567. VERIFY(arguments.size() == 1);
  568. auto index = arguments[0].to_int();
  569. if (index < 0 || index > m_hival)
  570. return Error { Error::Type::MalformedPDF, "Indexed color space index out of range" };
  571. Vector<Value, 4> components;
  572. size_t const n = m_base->number_of_components();
  573. for (size_t i = 0; i < n; ++i)
  574. TRY(components.try_append(Value(m_lookup[index * n + i] / 255.0f)));
  575. return m_base->style(components);
  576. }
  577. Vector<float> IndexedColorSpace::default_decode() const
  578. {
  579. return { 0.0, 255.0 };
  580. }
  581. PDFErrorOr<NonnullRefPtr<SeparationColorSpace>> SeparationColorSpace::create(Document* document, Vector<Value>&& parameters, Renderer& renderer)
  582. {
  583. if (parameters.size() != 3)
  584. return Error { Error::Type::MalformedPDF, "Separation color space expected three parameters" };
  585. // "The name parameter is a name object specifying the name of the colorant that this Separation color space
  586. // is intended to represent (or one of the special names All or None; see below)"
  587. auto name_object = TRY(document->resolve_to<NameObject>(parameters[0]));
  588. auto name = name_object->cast<NameObject>()->name();
  589. // "The alternateSpace parameter must be an array or name object that identifies the alternate color space,
  590. // which can be any device or CIE-based color space but not another special color space
  591. // (Pattern, Indexed, Separation, or DeviceN)."
  592. auto alternate_space_object = TRY(document->resolve_to<Object>(parameters[1]));
  593. auto alternate_space = TRY(ColorSpace::create(document, alternate_space_object, renderer));
  594. auto family = alternate_space->family();
  595. if (family == ColorSpaceFamily::Pattern || family == ColorSpaceFamily::Indexed || family == ColorSpaceFamily::Separation || family == ColorSpaceFamily::DeviceN)
  596. return Error { Error::Type::MalformedPDF, "Separation color space has invalid alternate color space" };
  597. // "The tintTransform parameter must be a function"
  598. auto tint_transform_object = TRY(document->resolve_to<Object>(parameters[2]));
  599. auto tint_transform = TRY(Function::create(document, tint_transform_object));
  600. auto color_space = adopt_ref(*new SeparationColorSpace(move(alternate_space), move(tint_transform)));
  601. color_space->m_name = move(name);
  602. return color_space;
  603. }
  604. SeparationColorSpace::SeparationColorSpace(NonnullRefPtr<ColorSpace> alternate_space, NonnullRefPtr<Function> tint_transform)
  605. : m_alternate_space(move(alternate_space))
  606. , m_tint_transform(move(tint_transform))
  607. {
  608. }
  609. PDFErrorOr<ColorOrStyle> SeparationColorSpace::style(ReadonlySpan<Value> arguments) const
  610. {
  611. // "For an additive device such as a computer display, a Separation color space never applies a process colorant directly;
  612. // it always reverts to the alternate color space as described below."
  613. // "During subsequent painting operations, an application calls [the tint] function to transform a tint value into
  614. // color component values in the alternate color space."
  615. // FIXME: Does this need handling for the special colorant names "All" and "None"?
  616. // FIXME: When drawing to a printer, do something else.
  617. VERIFY(arguments.size() == 1);
  618. auto a = arguments[0].to_float();
  619. auto tint_output = TRY(m_tint_transform->evaluate(ReadonlySpan<float> { &a, 1 }));
  620. m_tint_output_values.resize(tint_output.size());
  621. for (size_t i = 0; i < tint_output.size(); ++i)
  622. m_tint_output_values[i] = tint_output[i];
  623. return m_alternate_space->style(m_tint_output_values);
  624. }
  625. Vector<float> SeparationColorSpace::default_decode() const
  626. {
  627. return { 0.0f, 1.0f };
  628. }
  629. NonnullRefPtr<PatternColorSpace> PatternColorSpace::create(Renderer& renderer)
  630. {
  631. return adopt_ref(*new PatternColorSpace(renderer));
  632. }
  633. PDFErrorOr<ColorOrStyle> PatternColorSpace::style(ReadonlySpan<Value> arguments) const
  634. {
  635. VERIFY(arguments.size() >= 1);
  636. auto resources = m_renderer.m_page.resources;
  637. auto pattern_resource = resources->get_value(CommonNames::Pattern);
  638. auto doc_pattern_dict = pattern_resource.get<NonnullRefPtr<Object>>()->cast<DictObject>();
  639. auto const& pattern_name = arguments.last().get<NonnullRefPtr<Object>>()->cast<NameObject>()->name();
  640. if (!doc_pattern_dict->contains(pattern_name))
  641. return Error::malformed_error("Pattern dictionary does not contain pattern {}", pattern_name);
  642. auto const pattern = TRY(m_renderer.m_document->resolve_to<StreamObject>(doc_pattern_dict->get_value(pattern_name)));
  643. auto const type = pattern->dict()->get(CommonNames::Type)->get<NonnullRefPtr<Object>>()->cast<NameObject>();
  644. if (type->name() != CommonNames::Pattern)
  645. return Error::rendering_unsupported_error("Unsupported pattern type {}", type->name());
  646. auto const pattern_type = pattern->dict()->get(CommonNames::PatternType)->get_u16();
  647. if (pattern_type != 1)
  648. return Error::rendering_unsupported_error("Unsupported pattern type {}", pattern_type);
  649. auto const pattern_paint_type = pattern->dict()->get("PaintType")->get_u16();
  650. if (pattern_paint_type != 1)
  651. return Error::rendering_unsupported_error("Unsupported pattern paint type {}", pattern_paint_type);
  652. Vector<Value> pattern_matrix;
  653. if (pattern->dict()->contains(CommonNames::Matrix)) {
  654. pattern_matrix = pattern->dict()->get_array(m_renderer.m_document, CommonNames::Matrix).value()->elements();
  655. } else {
  656. pattern_matrix = Vector { Value { 1 }, Value { 0 }, Value { 0 }, Value { 1 }, Value { 0 }, Value { 0 } };
  657. }
  658. auto pattern_bounding_box = pattern->dict()->get_array(m_renderer.m_document, "BBox").value()->elements();
  659. auto pattern_transform = Gfx::AffineTransform(
  660. pattern_matrix[0].to_float(),
  661. pattern_matrix[1].to_float(),
  662. pattern_matrix[2].to_float(),
  663. pattern_matrix[3].to_float(),
  664. pattern_matrix[4].to_float(),
  665. pattern_matrix[5].to_float());
  666. // To get the device space size for the bitmap, apply the pattern transform to the pattern space bounding box, and then apply the initial ctm.
  667. // NB: the pattern pattern_matrix maps pattern space to the default (initial) coordinate space of the page. (i.e cannot be updated via cm).
  668. auto initial_ctm = Gfx::AffineTransform(m_renderer.m_graphics_state_stack.first().ctm);
  669. initial_ctm.set_translation(0, 0);
  670. initial_ctm.set_scale(initial_ctm.x_scale(), initial_ctm.y_scale());
  671. auto pattern_space_lower_left = Gfx::FloatPoint { pattern_bounding_box[0].to_int(), pattern_bounding_box[1].to_int() };
  672. auto pattern_space_upper_right = Gfx::FloatPoint { pattern_bounding_box[2].to_int(), pattern_bounding_box[3].to_int() };
  673. auto device_space_lower_left = initial_ctm.map(pattern_transform.map(pattern_space_lower_left));
  674. auto device_space_upper_right = initial_ctm.map(pattern_transform.map(pattern_space_upper_right));
  675. auto bitmap_width = (int)device_space_upper_right.x() - (int)device_space_lower_left.x();
  676. auto bitmap_height = (int)device_space_upper_right.y() - (int)device_space_lower_left.y();
  677. auto pattern_cell = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { bitmap_width, bitmap_height }));
  678. auto page = Page(m_renderer.m_page);
  679. page.media_box = Rectangle {
  680. pattern_space_lower_left.x(), pattern_space_lower_left.y(),
  681. pattern_space_upper_right.x(), pattern_space_upper_right.y()
  682. };
  683. page.crop_box = page.media_box;
  684. auto pattern_renderer = Renderer(m_renderer.m_document, page, pattern_cell, {}, m_renderer.m_rendering_preferences);
  685. auto operators = TRY(Parser::parse_operators(m_renderer.m_document, pattern->bytes()));
  686. for (auto& op : operators)
  687. TRY(pattern_renderer.handle_operator(op, resources));
  688. auto x_steps = pattern->dict()->get("XStep").value_or(bitmap_width).to_int();
  689. auto y_steps = pattern->dict()->get("YStep").value_or(bitmap_height).to_int();
  690. auto device_space_steps = initial_ctm.map(pattern_transform.map(Gfx::IntPoint { x_steps, y_steps }));
  691. NonnullRefPtr<Gfx::PaintStyle> style = MUST(Gfx::RepeatingBitmapPaintStyle::create(
  692. *pattern_cell,
  693. device_space_steps,
  694. {}));
  695. return style;
  696. }
  697. int PatternColorSpace::number_of_components() const
  698. {
  699. // Not permitted
  700. VERIFY_NOT_REACHED();
  701. }
  702. Vector<float> PatternColorSpace::default_decode() const
  703. {
  704. // Not permitted
  705. VERIFY_NOT_REACHED();
  706. }
  707. }