Base Layer Structure#
Note
This struct is not to be used directly but holds all the virtual base functions which all the other layers may use. It additionally defines the
Layer<T>::Params struct which is expected to be passed into layer initialization for its subclassed types
Base Layer Type#
This is the base definition for all layer types which holds generic data such as name, mask data, opacity etc. This class is not supposed to be used directly but instead through any of its subclassed layers as this layer type doesn’t exist in Photoshop itself.
-
template<typename T>
struct Layer : public MaskMixin<T># Layer types.
Base Struct for Layers of all types (Group, Image, [Adjustment], etc.) which includes the minimum to parse a generic layer type.
Subclassed by AdjustmentLayer< T >, ArtboardLayer< T >, GroupLayer< T >, ImageLayer< T >, SectionDividerLayer< T >, ShapeLayer< T >, SmartObjectLayer< T >, TextLayer< T >
Masks
-
inline bool has_mask() const noexcept#
Checks whether the layer has a pixel mask.
- Returns:
trueif a mask is present, otherwisefalse.
-
inline std::vector<T> get_mask() const#
Retrieves the mask channel data, if present.
If the layer does not have a mask, this function returns an empty vector.
The returned vector contains
mask_width() * mask_height()elements.
-
inline void get_mask(std::span<T> buffer) const#
Fills a preallocated buffer with the mask channel data, if present.
If no mask is present, the buffer remains unchanged.
- Parameters:
buffer – A preallocated buffer expected to have exactly
mask_width() * mask_height()elements.
-
inline compressed::channel<T> extract_mask()#
Extract the compressed mask channel used internally.
- Throws:
std::runtime_error – if no mask is present (as checked by
has_mask).
-
inline void set_mask(std::span<const T> buffer, size_t width, size_t height)#
Sets the layer’s mask to the given buffer.
If no mask was previously held the inserted mask will be at the top-left of the canvas. Use
mask_positionto adjust this.- Parameters:
buffer – The image data for the mask, provided in scanline order.
width – The width of the new mask.
height – The height of the new mask.
- Throws:
std::invalid_argument – If the buffer size does not match the expected dimensions.
-
inline void set_mask(const LayeredFile<T> &document, std::span<const T> buffer, std::optional<size_t> width = std::nullopt, std::optional<size_t> height = std::nullopt)#
Sets the layer’s mask to the given buffer.
If the layer previously had a mask, the new mask will be centered at the same position.
If no mask was previously present, the new mask will be centered on the document.
- Parameters:
document – The file to which this mask layer belongs.
buffer – The image data for the mask, provided in scanline order. If
widthandheightare unspecified, the buffer must have exactlymask_width() * mask_height()elements. If no dimensions are specified and the layer previously had no mask, an exception is thrown.width – (Optional) The width of the new mask. Required if replacing with a differently sized mask.
height – (Optional) The height of the new mask. Required if replacing with a differently sized mask.
- Throws:
std::invalid_argument – If the buffer size does not match the expected dimensions.
std::runtime_error – If no mask existed and no explicit dimensions were provided.
-
inline void set_mask_compression(Enum::Compression _compcode) noexcept#
Set the masks write compression.
If
has_mask()evaluates to false this is a no-op.- Parameters:
_compcode – The compression codec to apply on-write.
-
inline Geometry::BoundingBox<double> mask_bbox() const#
Retrieves the bounding box of the mask, if present.
If no mask exists, this function returns a zero-sized bounding box.
- Returns:
The mask’s bounding box, or an empty bounding box if no mask is present.
-
inline size_t mask_width() const#
Retrieves the width of the mask, if present.
If no mask exists, this function returns zero
- Returns:
The mask’s width or zero if no mask exists
-
inline size_t mask_height() const#
Retrieves the height of the mask, if present.
If no mask exists, this function returns zero
- Returns:
The mask’s height or zero if no mask exists
-
inline Geometry::Point2D<double> mask_position() const#
Retrieves the position of the mask, defined as its center.
If no mask is present, this function returns
{-1.0, -1.0}.- Returns:
The center position of the mask, or
{-1.0, -1.0}if no mask exists.
-
inline void mask_position(Geometry::Point2D<double> position)#
Sets the center position of the mask.
If no mask is present, this function does nothing.
- Parameters:
position – The new center position of the mask.
-
inline bool mask_relative_to_layer()#
Checks whether the mask is relative to the layer.
- Returns:
trueif the mask is relative to the layer, otherwisefalse.
-
inline void mask_relative_to_layer(bool value)#
Sets whether the mask should be relative to the layer.
- Parameters:
value –
trueto make the mask relative to the layer, otherwisefalse.
-
inline bool mask_disabled()#
Checks whether the mask is disabled.
- Returns:
trueif the mask is disabled, otherwisefalse.
-
inline void mask_disabled(bool value)#
Enables or disables the mask.
- Parameters:
value –
trueto disable the mask, otherwisefalse.
-
inline uint8_t mask_default_color()#
Retrieves the mask’s default fill color.
- Returns:
The default mask color.
-
inline void mask_default_color(uint8_t value)#
Sets the mask’s default fill color.
- Parameters:
value – The new default color value.
-
inline std::optional<uint8_t> mask_density()#
Retrieves the mask density, if specified.
- Returns:
The mask density value, or an empty optional if unspecified.
-
inline void mask_density(uint8_t value)#
Sets the mask density.
- Parameters:
value – The new mask density.
-
inline void mask_density(std::optional<uint8_t> value)#
Sets the mask density, allowing for removal.
- Parameters:
value – An optional new mask density.
-
inline std::optional<float64_t> mask_feather()#
Retrieves the mask feathering amount, if specified.
- Returns:
The mask feather value, or an empty optional if unspecified.
-
inline void mask_feather(float64_t value)#
Sets the mask feathering amount.
- Parameters:
value – The new mask feather value.
-
inline void mask_feather(std::optional<float64_t> value)#
Public Types
Public Functions
-
inline const std::string &name() const noexcept#
The layers’ name. Stored as a utf-8 string.
-
inline std::string &name() noexcept#
The layers’ name. Stored as a utf-8 string.
-
inline void name(const std::string &layer_name) noexcept#
The layers’ name. Stored as a utf-8 string.
-
inline Enum::BlendMode &blendmode() noexcept#
The blendmode of the layer, the
Passthroughblendmode is only valid for groups.
-
inline Enum::BlendMode blendmode() const noexcept#
The blendmode of the layer, the
Passthroughblendmode is only valid for groups.
-
inline void blendmode(Enum::BlendMode blend_mode) noexcept#
The blendmode of the layer, the
Passthroughblendmode is only valid for groups.
-
inline bool &locked() noexcept#
Whether the layers’ pixel values are locked. This is currently an all or nothing setting.
-
inline bool locked() const noexcept#
Whether the layers’ pixel values are locked. This is currently an all or nothing setting.
-
inline void locked(bool is_locked) noexcept#
Whether the layers’ pixel values are locked. This is currently an all or nothing setting.
-
inline bool &visible() noexcept#
Visibility toggle of the layer.
-
inline bool visible() const noexcept#
Visibility toggle of the layer.
-
inline void visible(bool is_visible) noexcept#
Visibility toggle of the layer.
-
inline bool &clipping_mask() noexcept#
Clipping mask toggle of the layer, clips it to the layer below.
-
inline bool clipping_mask() const noexcept#
Clipping mask toggle of the layer, clips it to the layer below.
-
inline void clipping_mask(bool is_clipped) noexcept#
Clipping mask toggle of the layer, clips it to the layer below.
-
inline float opacity() const noexcept#
The layers’ opacity.
In photoshop this is stored as a
uint8_tfrom 0-255 but access and write is in terms of a float for better consistency.
-
inline void opacity(float value) noexcept#
The layers’ opacity.
In photoshop this is stored as a
uint8_tfrom 0-255 but access and write is in terms of a float for better consistency.
-
inline virtual uint32_t width() const noexcept#
The layers’ width from 0 - 300,000.
-
inline virtual void width(uint32_t layer_width)#
The layers’ width from 0 - 300,000.
-
inline virtual uint32_t height() const noexcept#
The layers’ height from 0 - 300,000.
-
inline virtual void height(uint32_t layer_height)#
The layers’ height from 0 - 300,000.
-
inline virtual float center_x() const noexcept#
The layers’ x center coordinate
I.e. if the layer has the bounds { 200, 200 } - { 1000, 1000 } The center would be at { 600, 600 }
-
inline virtual void center_x(float x_coord) noexcept#
-
inline virtual float center_y() const noexcept#
The layers’ y center coordinate
I.e. if the layer has the bounds { 200, 200 } - { 1000, 1000 } The center would be at { 600, 600 }
-
inline virtual void center_y(float y_coord) noexcept#
-
inline float top_left_x() const noexcept#
Convenience function for accessing the top left x coordinate of a layer.
-
inline float top_left_y() const noexcept#
Convenience function for accessing the top left y coordinate of a layer.
-
inline Enum::ColorMode color_mode() const noexcept#
The color mode with which the file was created, only stored to allow better detection during channel access for e.g. image layers
-
inline Enum::LayerColor display_color() const noexcept#
The layers’ display color in the GUI.
-
inline void display_color(const Enum::LayerColor color) noexcept#
-
inline virtual void set_write_compression(Enum::Compression _compcode)#
Set the write compression for all channels.
This has no effect on the in-memory compression of these channels but only on write. Setting this therefore has a near-zero runtime cost.
- Parameters:
_compcode – The new compression setting.
-
inline Layer()#
-
inline Layer(const LayerRecord &layerRecord, ChannelImageData &channelImageData, const FileHeader &header)#
Initialize a Layer instance from the internal Photoshop File Format structures.
This is part of the internal API and as a user you will likely never have to use this function
This constructor is responsible for creating a Layer object based on the information stored in the provided Photoshop File Format structures. It extracts relevant data from the LayerRecord, ChannelImageData, and FileHeader to set up the Layer.
- Parameters:
layerRecord – The LayerRecord containing information about the layer.
channelImageData – The ChannelImageData holding the image data.
header – The FileHeader providing overall file information.
-
virtual ~Layer() = default#
-
inline virtual std::tuple<LayerRecord, ChannelImageData> to_photoshop()#
Function for creating a PhotoshopFile compatible types from the layer.
This is part of the internal API and as a user you will likely never have to use this function.
In the future, the intention is to make this a pure virtual function. However, due to the presence of multiple miscellaneous layers not yet implemented for the initial release, this function is provided. It generates a tuple containing LayerRecord and ChannelImageData based on the specified ColorMode, and using the provided FileHeader.
- Returns:
A tuple containing LayerRecord and ChannelImageData representing the layer in the PhotoshopFile.
Public Static Attributes
-
static constexpr auto s_mask_index = Enum::ChannelIDInfo{Enum::ChannelID::UserSuppliedLayerMask, -2}#
Colormode independent mask index as Enum::ChannelIDInfo that may be used.
-
struct Params#
Layer Parameters for initialization of a generic layer type. It provides sensible defaults so only what is needed needs to be overridden
Public Members
-
std::optional<std::variant<std::vector<T>, compressed::channel<T>>> mask = std::nullopt#
Optional Layer Mask parameter, if none is specified there is no mask. This image data must have the same size as the layer itself
-
Enum::BlendMode blendmode = Enum::BlendMode::Normal#
The Layers Blend Mode, all available blend modes are valid except for ‘Passthrough’ on non-group layers.
-
int32_t center_x = 0#
The X Center coordinate in respect to the canvas’ top left. So a value of 32 would mean the layer is centered 32 pixels from the left of the canvas.
-
int32_t center_y = 0#
The Y Center coordinate in respect to the canvas’ top left. So a value of 32 would mean the layer is centered 32 pixels from the top of the canvas.
-
uint32_t width = 0u#
The width of the layer, this value must be passed explicitly as we do not deduce this from the Image Data itself.
-
uint32_t height = 0u#
The height of the layer, this value must be passed explicitly as we do not deduce this from the Image Data itself.
-
uint8_t opacity = 255u#
The Layer opacity, the value displayed by Photoshop will be this value / 255 so 255 corresponds to 100% while 128 would correspond to ~50%
-
Enum::Compression compression = Enum::Compression::ZipPrediction#
The compression codec of the layer, it is perfectly valid for each layer (and channel) to be compressed differently.
-
bool visible = true#
Whether the layer is visible.
-
bool locked = false#
Whether the layer is locked.
-
bool clipping_mask = false#
Whether the layer is clipped to the one below.
-
Enum::LayerColor display_color = Enum::LayerColor::none#
The layer display color.
-
std::optional<std::variant<std::vector<T>, compressed::channel<T>>> mask = std::nullopt#
-
inline bool has_mask() const noexcept#