tiny-skia-cpp
A C++20 2D rendering library (port of tiny-skia)
Loading...
Searching...
No Matches
Pixmap.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <cstddef>
7#include <cstdint>
8#include <limits>
9#include <optional>
10#include <span>
11#include <utility>
12#include <vector>
13
14#include "tiny_skia/Color.h"
15#include "tiny_skia/Geom.h"
16
17namespace tiny_skia {
18
20inline constexpr std::size_t kBytesPerPixel = 4;
21
22class Pixmap;
23struct MutableSubPixmapView;
24
27 public:
28 PixmapView() = default;
29
31 static std::optional<PixmapView> fromBytes(std::span<const std::uint8_t> data, std::uint32_t width,
32 std::uint32_t height);
33
34 [[nodiscard]] std::uint32_t width() const { return size_.width(); }
35 [[nodiscard]] std::uint32_t height() const { return size_.height(); }
36 [[nodiscard]] IntSize size() const { return size_; }
37
39 [[nodiscard]] std::span<const std::uint8_t> data() const {
40 return std::span<const std::uint8_t>(data_, len_);
41 }
42
44 [[nodiscard]] std::span<const PremultipliedColorU8> pixels() const;
45
47 [[nodiscard]] std::optional<PremultipliedColorU8> pixel(std::uint32_t x, std::uint32_t y) const;
48
50 [[nodiscard]] std::optional<Pixmap> cloneRect(const IntRect& rect) const;
51
52 private:
53 friend class Pixmap;
54
55 explicit PixmapView(const std::uint8_t* data, std::size_t len, IntSize size)
56 : data_(data), len_(len), size_(size) {}
57
58 const std::uint8_t* data_ = nullptr;
59 std::size_t len_ = 0;
60 IntSize size_;
61};
62
66 public:
67 MutablePixmapView() = default;
68 explicit MutablePixmapView(std::uint8_t* data, std::size_t len, IntSize size)
69 : data_(data), len_(len), size_(size) {}
70
72 static std::optional<MutablePixmapView> fromBytes(std::span<std::uint8_t> data, std::uint32_t width,
73 std::uint32_t height);
74
75 [[nodiscard]] std::uint32_t width() const { return size_.width(); }
76 [[nodiscard]] std::uint32_t height() const { return size_.height(); }
77 [[nodiscard]] IntSize size() const { return size_; }
78
80 [[nodiscard]] std::span<std::uint8_t> data() const {
81 return std::span<std::uint8_t>(data_, len_);
82 }
83
85 [[nodiscard]] std::span<PremultipliedColorU8> pixels() const;
86
88 [[nodiscard]] MutableSubPixmapView subpixmap() const;
89
91 [[nodiscard]] std::optional<MutableSubPixmapView> subpixmap(const IntRect& rect) const;
92
93 private:
94 std::uint8_t* data_ = nullptr;
95 std::size_t len_ = 0;
96 IntSize size_;
97};
98
101struct MutableSubPixmapView {
102 IntSize size{};
103 std::size_t realWidth = 0;
104 std::uint8_t* data = nullptr;
105
106 [[nodiscard]] std::size_t width() const { return size.width(); }
107 [[nodiscard]] std::size_t height() const { return size.height(); }
108 [[nodiscard]] std::span<std::uint8_t> dataSpan() const;
109};
110
116class Pixmap {
117 public:
118 Pixmap() = default;
119
121 static std::optional<Pixmap> fromSize(std::uint32_t width, std::uint32_t height);
122
124 static std::optional<Pixmap> fromVec(std::vector<std::uint8_t> data, IntSize size);
125
127 [[nodiscard]] PixmapView view() const { return PixmapView(data_.data(), data_.size(), size_); }
128
130 [[nodiscard]] MutablePixmapView mutableView() { return MutablePixmapView(data_.data(), data_.size(), size_); }
131
132 [[nodiscard]] std::uint32_t width() const { return size_.width(); }
133 [[nodiscard]] std::uint32_t height() const { return size_.height(); }
134 [[nodiscard]] IntSize size() const { return size_; }
135
137 [[nodiscard]] std::span<const std::uint8_t> data() const {
138 return std::span<const std::uint8_t>(data_.data(), data_.size());
139 }
140
142 [[nodiscard]] std::span<std::uint8_t> data() {
143 return std::span<std::uint8_t>(data_.data(), data_.size());
144 }
145
146 [[nodiscard]] std::span<const PremultipliedColorU8> pixels() const;
147 [[nodiscard]] std::span<PremultipliedColorU8> pixels();
148
150 [[nodiscard]] std::optional<PremultipliedColorU8> pixel(std::uint32_t x, std::uint32_t y) const;
151
153 [[nodiscard]] std::optional<Pixmap> cloneRect(const IntRect& rect) const;
154
156 void fill(const Color& color);
157
159 [[nodiscard]] std::vector<std::uint8_t> release();
160
163 [[nodiscard]] std::vector<std::uint8_t> releaseDemultiplied();
164
165 private:
166 explicit Pixmap(std::vector<std::uint8_t> data, IntSize size)
167 : data_(std::move(data)), size_(size) {}
168
169 std::vector<std::uint8_t> data_;
170 IntSize size_;
171};
172
173} // namespace tiny_skia
Color types (8-bit and floating-point, straight and premultiplied).
Geometric primitives: Rect, IntRect, ScreenIntRect, IntSize.
constexpr std::size_t kBytesPerPixel
Bytes per pixel (always 4: RGBA premultiplied).
Definition Pixmap.h:20
Floating-point RGBA color [0,1] (straight alpha).
Definition Color.h:88
Signed integer rectangle (x, y, width, height).
Definition Geom.h:85
Non-zero integer dimensions (width x height).
Definition Geom.h:18
Mutable view into RGBA pixel data. Does not own memory. Primary drawing target for all rendering oper...
Definition Pixmap.h:65
std::span< PremultipliedColorU8 > pixels() const
Pixel data as mutable PremultipliedColorU8 span.
std::optional< MutableSubPixmapView > subpixmap(const IntRect &rect) const
Returns a sub-view for the given rectangle, or nullopt if out of bounds.
std::span< std::uint8_t > data() const
Raw mutable byte data (premultiplied RGBA).
Definition Pixmap.h:80
MutableSubPixmapView subpixmap() const
Returns a sub-view covering the full pixmap.
static std::optional< MutablePixmapView > fromBytes(std::span< std::uint8_t > data, std::uint32_t width, std::uint32_t height)
Creates a mutable view from raw byte data. Returns nullopt if size mismatches.
Immutable view into RGBA pixel data. Does not own memory.
Definition Pixmap.h:26
static std::optional< PixmapView > fromBytes(std::span< const std::uint8_t > data, std::uint32_t width, std::uint32_t height)
Creates a view from raw byte data. Returns nullopt if size mismatches.
std::span< const std::uint8_t > data() const
Raw byte data (premultiplied RGBA).
Definition Pixmap.h:39
std::span< const PremultipliedColorU8 > pixels() const
Pixel data as PremultipliedColorU8 span.
std::optional< Pixmap > cloneRect(const IntRect &rect) const
Clones a rectangular region into a new Pixmap.
std::optional< PremultipliedColorU8 > pixel(std::uint32_t x, std::uint32_t y) const
Returns the pixel at (x, y), or nullopt if out of bounds.
Owned RGBA pixel buffer. Always premultiplied alpha internally.
Definition Pixmap.h:116
MutablePixmapView mutableView()
Mutable view — the primary drawing target.
Definition Pixmap.h:130
std::vector< std::uint8_t > releaseDemultiplied()
Releases the byte buffer after converting to straight (non-premultiplied) alpha. Use this for PNG enc...
void fill(const Color &color)
Fills the entire pixmap with a color (premultiplied internally).
PixmapView view() const
Immutable view.
Definition Pixmap.h:127
static std::optional< Pixmap > fromVec(std::vector< std::uint8_t > data, IntSize size)
Creates a pixmap from existing data. Returns nullopt if size mismatches.
std::optional< Pixmap > cloneRect(const IntRect &rect) const
Clones a rectangular region into a new Pixmap.
std::optional< PremultipliedColorU8 > pixel(std::uint32_t x, std::uint32_t y) const
Returns the pixel at (x, y), or nullopt if out of bounds.
std::vector< std::uint8_t > release()
Releases the raw byte buffer (premultiplied alpha).
std::span< const std::uint8_t > data() const
Raw byte data (premultiplied RGBA), const.
Definition Pixmap.h:137
std::span< std::uint8_t > data()
Raw byte data (premultiplied RGBA), mutable.
Definition Pixmap.h:142
static std::optional< Pixmap > fromSize(std::uint32_t width, std::uint32_t height)
Creates a zero-filled pixmap. Returns nullopt for zero dimensions.