tiny-skia-cpp
A C++20 2D rendering library (port of tiny-skia)
Loading...
Searching...
No Matches
Mask.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <cstdint>
7#include <optional>
8#include <span>
9#include <utility>
10#include <vector>
11
12#include "tiny_skia/Geom.h"
13#include "tiny_skia/Pixmap.h"
14
15namespace tiny_skia {
16
17// Forward declarations for fillPath/intersectPath.
18class Path;
19class Transform;
20enum class FillRule : std::uint8_t;
21
23enum class MaskType : std::uint8_t {
24 Alpha = 0,
25 Luminance = 1,
26};
27
30struct SubMaskView {
31 IntSize size{};
32 std::uint32_t realWidth = 0;
33 const std::uint8_t* data = nullptr;
34};
35
38struct MutableSubMaskView {
39 IntSize size{};
40 std::uint32_t realWidth = 0;
41 std::uint8_t* data = nullptr;
42};
43
49class Mask {
50 public:
51 Mask() = default;
52
54 static std::optional<Mask> fromSize(std::uint32_t width, std::uint32_t height);
55
57 static std::optional<Mask> fromVec(std::vector<std::uint8_t> data, IntSize size);
58
60 static Mask fromPixmap(const PixmapView& pixmap, MaskType maskType);
61
62 [[nodiscard]] std::uint32_t width() const { return size_.width(); }
63 [[nodiscard]] std::uint32_t height() const { return size_.height(); }
64 [[nodiscard]] IntSize size() const { return size_; }
65
66 [[nodiscard]] std::span<const std::uint8_t> data() const {
67 return std::span<const std::uint8_t>(data_.data(), data_.size());
68 }
69
70 [[nodiscard]] std::span<std::uint8_t> data() {
71 return std::span<std::uint8_t>(data_.data(), data_.size());
72 }
73
75 [[nodiscard]] SubMaskView submask() const;
77 [[nodiscard]] std::optional<SubMaskView> submask(IntRect rect) const;
79 [[nodiscard]] MutableSubMaskView subpixmap();
81 [[nodiscard]] std::optional<MutableSubMaskView> subpixmap(IntRect rect);
82
84 [[nodiscard]] std::vector<std::uint8_t> release();
85
87 void fillPath(const Path& path, FillRule fillRule, bool antiAlias, Transform transform);
88
90 void intersectPath(const Path& path, FillRule fillRule, bool antiAlias, Transform transform);
91
93 void invert();
94
96 void clear();
97
98 private:
99 explicit Mask(std::vector<std::uint8_t> data, IntSize size)
100 : data_(std::move(data)), size_(size) {}
101
102 std::vector<std::uint8_t> data_;
103 IntSize size_;
104};
105
106} // namespace tiny_skia
Geometric primitives: Rect, IntRect, ScreenIntRect, IntSize.
MaskType
How to extract mask data from a pixmap.
Definition Mask.h:23
@ Luminance
Use luminance (weighted RGB).
@ Alpha
Use the alpha channel.
FillRule
Fill rule for path filling.
Definition Path.h:220
Pixel buffers and views for RGBA image data.
Non-zero integer dimensions (width x height).
Definition Geom.h:18
8-bit alpha mask for clipping.
Definition Mask.h:49
static Mask fromPixmap(const PixmapView &pixmap, MaskType maskType)
Extracts a mask from a pixmap's alpha or luminance channel.
void intersectPath(const Path &path, FillRule fillRule, bool antiAlias, Transform transform)
Intersects (AND) a path with the current mask contents.
std::vector< std::uint8_t > release()
Releases the underlying byte buffer.
static std::optional< Mask > fromVec(std::vector< std::uint8_t > data, IntSize size)
Creates a mask from existing data. Returns nullopt if size mismatches.
static std::optional< Mask > fromSize(std::uint32_t width, std::uint32_t height)
Creates a zero-filled mask. Returns nullopt for zero dimensions.
void clear()
Clears the mask to zero.
void invert()
Inverts all mask values (255 - x).
void fillPath(const Path &path, FillRule fillRule, bool antiAlias, Transform transform)
Draws a filled path onto the mask (white=255 where filled).
Immutable vector path — a sequence of lines, quadratics, and cubics.
Definition Path.h:56
Immutable view into RGBA pixel data. Does not own memory.
Definition Pixmap.h:26
2D affine transformation: [sx kx tx; ky sy ty; 0 0 1].
Definition Transform.h:17