ColorSpace.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DeprecatedFlyString.h>
  8. #include <AK/Forward.h>
  9. #include <LibGfx/Color.h>
  10. #include <LibGfx/ICC/Profile.h>
  11. #include <LibGfx/PaintStyle.h>
  12. #include <LibPDF/Function.h>
  13. #include <LibPDF/Value.h>
  14. #define ENUMERATE_COLOR_SPACE_FAMILIES(V) \
  15. V(DeviceGray, true) \
  16. V(DeviceRGB, true) \
  17. V(DeviceCMYK, true) \
  18. V(CalGray, false) \
  19. V(CalRGB, false) \
  20. V(Lab, false) \
  21. V(ICCBased, false) \
  22. V(Indexed, false) \
  23. V(Pattern, true) \
  24. V(Separation, false) \
  25. V(DeviceN, false)
  26. namespace PDF {
  27. typedef Variant<Gfx::Color, NonnullRefPtr<Gfx::PaintStyle>> ColorOrStyle;
  28. class Renderer;
  29. class ColorSpaceFamily {
  30. public:
  31. ColorSpaceFamily(DeprecatedFlyString name, bool may_be_specified_directly)
  32. : m_name(move(name))
  33. , m_may_be_specified_directly(may_be_specified_directly)
  34. {
  35. }
  36. DeprecatedFlyString name() const { return m_name; }
  37. bool may_be_specified_directly() const { return m_may_be_specified_directly; }
  38. static PDFErrorOr<ColorSpaceFamily> get(DeprecatedFlyString const&);
  39. #define ENUMERATE(name, may_be_specified_directly) static ColorSpaceFamily name;
  40. ENUMERATE_COLOR_SPACE_FAMILIES(ENUMERATE)
  41. #undef ENUMERATE
  42. bool operator==(ColorSpaceFamily const& other) const
  43. {
  44. return m_name == other.m_name;
  45. }
  46. private:
  47. DeprecatedFlyString m_name;
  48. bool m_may_be_specified_directly;
  49. };
  50. class ColorSpace : public RefCounted<ColorSpace> {
  51. public:
  52. static PDFErrorOr<NonnullRefPtr<ColorSpace>> create(Document*, NonnullRefPtr<Object>, Renderer&);
  53. static PDFErrorOr<NonnullRefPtr<ColorSpace>> create(DeprecatedFlyString const&, Renderer&);
  54. static PDFErrorOr<NonnullRefPtr<ColorSpace>> create(Document*, NonnullRefPtr<ArrayObject>, Renderer&);
  55. virtual ~ColorSpace() = default;
  56. virtual PDFErrorOr<ColorOrStyle> style(ReadonlySpan<Value> arguments) const = 0;
  57. virtual int number_of_components() const = 0;
  58. virtual Vector<float> default_decode() const = 0; // "TABLE 4.40 Default Decode arrays"
  59. virtual ColorSpaceFamily const& family() const = 0;
  60. };
  61. class DeviceGrayColorSpace final : public ColorSpace {
  62. public:
  63. static NonnullRefPtr<DeviceGrayColorSpace> the();
  64. ~DeviceGrayColorSpace() override = default;
  65. PDFErrorOr<ColorOrStyle> style(ReadonlySpan<Value> arguments) const override;
  66. int number_of_components() const override { return 1; }
  67. Vector<float> default_decode() const override;
  68. ColorSpaceFamily const& family() const override { return ColorSpaceFamily::DeviceGray; }
  69. private:
  70. DeviceGrayColorSpace() = default;
  71. };
  72. class DeviceRGBColorSpace final : public ColorSpace {
  73. public:
  74. static NonnullRefPtr<DeviceRGBColorSpace> the();
  75. ~DeviceRGBColorSpace() override = default;
  76. PDFErrorOr<ColorOrStyle> style(ReadonlySpan<Value> arguments) const override;
  77. int number_of_components() const override { return 3; }
  78. Vector<float> default_decode() const override;
  79. ColorSpaceFamily const& family() const override { return ColorSpaceFamily::DeviceRGB; }
  80. private:
  81. DeviceRGBColorSpace() = default;
  82. };
  83. class DeviceCMYKColorSpace final : public ColorSpace {
  84. public:
  85. static NonnullRefPtr<DeviceCMYKColorSpace> the();
  86. ~DeviceCMYKColorSpace() override = default;
  87. PDFErrorOr<ColorOrStyle> style(ReadonlySpan<Value> arguments) const override;
  88. int number_of_components() const override { return 4; }
  89. Vector<float> default_decode() const override;
  90. ColorSpaceFamily const& family() const override { return ColorSpaceFamily::DeviceCMYK; }
  91. private:
  92. DeviceCMYKColorSpace() = default;
  93. };
  94. class DeviceNColorSpace final : public ColorSpace {
  95. public:
  96. static PDFErrorOr<NonnullRefPtr<DeviceNColorSpace>> create(Document*, Vector<Value>&& parameters, Renderer&);
  97. ~DeviceNColorSpace() override = default;
  98. PDFErrorOr<ColorOrStyle> style(ReadonlySpan<Value> arguments) const override;
  99. int number_of_components() const override;
  100. Vector<float> default_decode() const override;
  101. ColorSpaceFamily const& family() const override { return ColorSpaceFamily::DeviceN; }
  102. private:
  103. DeviceNColorSpace(NonnullRefPtr<ColorSpace>, NonnullRefPtr<Function>);
  104. Vector<ByteString> m_names;
  105. NonnullRefPtr<ColorSpace> m_alternate_space;
  106. NonnullRefPtr<Function> m_tint_transform;
  107. Vector<float> mutable m_tint_input_values;
  108. Vector<Value> mutable m_tint_output_values;
  109. };
  110. class CalGrayColorSpace final : public ColorSpace {
  111. public:
  112. static PDFErrorOr<NonnullRefPtr<CalGrayColorSpace>> create(Document*, Vector<Value>&& parameters);
  113. ~CalGrayColorSpace() override = default;
  114. PDFErrorOr<ColorOrStyle> style(ReadonlySpan<Value> arguments) const override;
  115. int number_of_components() const override { return 1; }
  116. Vector<float> default_decode() const override;
  117. ColorSpaceFamily const& family() const override { return ColorSpaceFamily::CalGray; }
  118. private:
  119. CalGrayColorSpace() = default;
  120. Array<float, 3> m_whitepoint { 0, 0, 0 };
  121. Array<float, 3> m_blackpoint { 0, 0, 0 };
  122. float m_gamma { 1 };
  123. };
  124. class CalRGBColorSpace final : public ColorSpace {
  125. public:
  126. static PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> create(Document*, Vector<Value>&& parameters);
  127. ~CalRGBColorSpace() override = default;
  128. PDFErrorOr<ColorOrStyle> style(ReadonlySpan<Value> arguments) const override;
  129. int number_of_components() const override { return 3; }
  130. Vector<float> default_decode() const override;
  131. ColorSpaceFamily const& family() const override { return ColorSpaceFamily::CalRGB; }
  132. private:
  133. CalRGBColorSpace() = default;
  134. Array<float, 3> m_whitepoint { 0, 0, 0 };
  135. Array<float, 3> m_blackpoint { 0, 0, 0 };
  136. Array<float, 3> m_gamma { 1, 1, 1 };
  137. Array<float, 9> m_matrix { 1, 0, 0, 0, 1, 0, 0, 0, 1 };
  138. };
  139. class ICCBasedColorSpace final : public ColorSpace {
  140. public:
  141. static PDFErrorOr<NonnullRefPtr<ColorSpace>> create(Document*, Vector<Value>&& parameters, Renderer&);
  142. ~ICCBasedColorSpace() override = default;
  143. PDFErrorOr<ColorOrStyle> style(ReadonlySpan<Value> arguments) const override;
  144. int number_of_components() const override;
  145. Vector<float> default_decode() const override;
  146. ColorSpaceFamily const& family() const override { return ColorSpaceFamily::ICCBased; }
  147. static NonnullRefPtr<Gfx::ICC::Profile> sRGB();
  148. private:
  149. ICCBasedColorSpace(NonnullRefPtr<Gfx::ICC::Profile>);
  150. static RefPtr<Gfx::ICC::Profile> s_srgb_profile;
  151. NonnullRefPtr<Gfx::ICC::Profile> m_profile;
  152. };
  153. class LabColorSpace final : public ColorSpace {
  154. public:
  155. static PDFErrorOr<NonnullRefPtr<LabColorSpace>> create(Document*, Vector<Value>&& parameters);
  156. ~LabColorSpace() override = default;
  157. PDFErrorOr<ColorOrStyle> style(ReadonlySpan<Value> arguments) const override;
  158. int number_of_components() const override { return 3; }
  159. Vector<float> default_decode() const override;
  160. ColorSpaceFamily const& family() const override { return ColorSpaceFamily::Lab; }
  161. private:
  162. LabColorSpace() = default;
  163. Array<float, 3> m_whitepoint { 0, 0, 0 };
  164. Array<float, 3> m_blackpoint { 0, 0, 0 };
  165. Array<float, 4> m_range { -100, 100, -100, 100 };
  166. };
  167. class IndexedColorSpace final : public ColorSpace {
  168. public:
  169. static PDFErrorOr<NonnullRefPtr<ColorSpace>> create(Document*, Vector<Value>&& parameters, Renderer&);
  170. ~IndexedColorSpace() override = default;
  171. PDFErrorOr<ColorOrStyle> style(ReadonlySpan<Value> arguments) const override;
  172. int number_of_components() const override { return 1; }
  173. Vector<float> default_decode() const override;
  174. ColorSpaceFamily const& family() const override { return ColorSpaceFamily::Indexed; }
  175. private:
  176. IndexedColorSpace(NonnullRefPtr<ColorSpace>);
  177. NonnullRefPtr<ColorSpace> m_base;
  178. int m_hival { 0 };
  179. Vector<u8> m_lookup;
  180. };
  181. class SeparationColorSpace final : public ColorSpace {
  182. public:
  183. static PDFErrorOr<NonnullRefPtr<SeparationColorSpace>> create(Document*, Vector<Value>&& parameters, Renderer&);
  184. ~SeparationColorSpace() override = default;
  185. PDFErrorOr<ColorOrStyle> style(ReadonlySpan<Value> arguments) const override;
  186. int number_of_components() const override { return 1; }
  187. Vector<float> default_decode() const override;
  188. ColorSpaceFamily const& family() const override { return ColorSpaceFamily::Separation; }
  189. private:
  190. SeparationColorSpace(NonnullRefPtr<ColorSpace>, NonnullRefPtr<Function>);
  191. ByteString m_name;
  192. NonnullRefPtr<ColorSpace> m_alternate_space;
  193. NonnullRefPtr<Function> m_tint_transform;
  194. Vector<Value> mutable m_tint_output_values;
  195. };
  196. }