ladybird/Userland/Libraries/LibPDF/ColorSpace.h
Rodrigo Tobar ba16310739 LibPDF: Refactor parsing of ColorSpaces
ColorSpaces can be specified in two ways: with a stream as operands of
the color space operations (CS/cs), or as a separate PDF object, which
is then referred to by other means (e.g., from Image XObjects and other
entities). These two modes of addressing a ColorSpace are slightly
different and need to be addressed separately. However, the current
implementation embedded the full logic of the first case in the routine
that created ColorSpace objects.

This commit refactors the creation of ColorSpace to support both cases.
First, a new ColorSpaceFamily class encapsulates the static aspects of a
family, like its name or whether color space construction never requires
parameters. Then we define the supported ColorSpaceFamily objects.

On top of this also sit a breakage on how ColorSpaces are created. Two
methods are now offered: one only providing construction of no-argument
color spaces (and thus taking a simple name), and another taking an
ArrayObject, hence used to create ColorSpaces requiring arguments.

Finally, on top of *that* two ways to get a color space in the Renderer
are made available: the first creates a ColorSpace with a name and a
Resources dictionary, and another takes an Object. These model the two
addressing modes described above.
2022-12-10 10:49:03 +01:00

131 lines
3.8 KiB
C++

/*
* Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
#include <LibGfx/Color.h>
#include <LibPDF/Value.h>
#define ENUMERATE_COLOR_SPACE_FAMILIES(V) \
V(DeviceGray, true) \
V(DeviceRGB, true) \
V(DeviceCMYK, true) \
V(CalGray, false) \
V(CalRGB, false) \
V(Lab, false) \
V(ICCBased, false) \
V(Indexed, false) \
V(Pattern, false) \
V(Separation, false) \
V(DeviceN, false)
namespace PDF {
class ColorSpaceFamily {
public:
ColorSpaceFamily(FlyString name, bool never_needs_paramaters_p)
: m_name(move(name))
, m_never_needs_parameters(never_needs_paramaters_p)
{
}
FlyString name() const { return m_name; };
bool never_needs_parameters() const { return m_never_needs_parameters; };
static PDFErrorOr<ColorSpaceFamily> get(FlyString const&);
#define ENUMERATE(name, ever_needs_parameters) static ColorSpaceFamily name;
ENUMERATE_COLOR_SPACE_FAMILIES(ENUMERATE)
#undef ENUMERATE
private:
FlyString m_name;
bool m_never_needs_parameters;
};
class ColorSpace : public RefCounted<ColorSpace> {
public:
static PDFErrorOr<NonnullRefPtr<ColorSpace>> create(FlyString const&);
static PDFErrorOr<NonnullRefPtr<ColorSpace>> create(Document*, NonnullRefPtr<ArrayObject>);
virtual ~ColorSpace() = default;
virtual Color color(Vector<Value> const& arguments) const = 0;
virtual ColorSpaceFamily const& family() const = 0;
};
class DeviceGrayColorSpace final : public ColorSpace {
public:
static NonnullRefPtr<DeviceGrayColorSpace> the();
~DeviceGrayColorSpace() override = default;
Color color(Vector<Value> const& arguments) const override;
ColorSpaceFamily const& family() const override { return ColorSpaceFamily::DeviceGray; }
private:
DeviceGrayColorSpace() = default;
};
class DeviceRGBColorSpace final : public ColorSpace {
public:
static NonnullRefPtr<DeviceRGBColorSpace> the();
~DeviceRGBColorSpace() override = default;
Color color(Vector<Value> const& arguments) const override;
ColorSpaceFamily const& family() const override { return ColorSpaceFamily::DeviceRGB; }
private:
DeviceRGBColorSpace() = default;
};
class DeviceCMYKColorSpace final : public ColorSpace {
public:
static NonnullRefPtr<DeviceCMYKColorSpace> the();
~DeviceCMYKColorSpace() override = default;
Color color(Vector<Value> const& arguments) const override;
ColorSpaceFamily const& family() const override { return ColorSpaceFamily::DeviceCMYK; }
private:
DeviceCMYKColorSpace() = default;
};
class CalRGBColorSpace final : public ColorSpace {
public:
static PDFErrorOr<NonnullRefPtr<CalRGBColorSpace>> create(Document*, Vector<Value>&& parameters);
~CalRGBColorSpace() override = default;
Color color(Vector<Value> const& arguments) const override;
ColorSpaceFamily const& family() const override { return ColorSpaceFamily::CalRGB; }
private:
CalRGBColorSpace() = default;
Array<float, 3> m_whitepoint { 0, 0, 0 };
Array<float, 3> m_blackpoint { 0, 0, 0 };
Array<float, 3> m_gamma { 1, 1, 1 };
Array<float, 9> m_matrix { 1, 0, 0, 0, 1, 0, 0, 0, 1 };
};
class ICCBasedColorSpace final : public ColorSpace {
public:
static PDFErrorOr<NonnullRefPtr<ColorSpace>> create(Document*, Vector<Value>&& parameters);
~ICCBasedColorSpace() override = default;
Color color(Vector<Value> const& arguments) const override;
ColorSpaceFamily const& family() const override { return ColorSpaceFamily::ICCBased; }
private:
ICCBasedColorSpace() = delete;
};
}