Repository navigation
Expand file tree
/
Copy pathBitmapRef.hpp
More file actions
169 lines (129 loc) · 7.94 KB
/
Copy pathBitmapRef.hpp
File metadata and controls
169 lines (129 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#pragma once
#include "YAxisOrientation.h"
namespace msdfgen {
/// Reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object.
template <typename T, int N = 1>
struct BitmapRef;
/// Constant reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object.
template <typename T, int N = 1>
struct BitmapConstRef;
/// Reference to a 2D image bitmap with non-contiguous rows of pixels. Pixel storage not owned or managed by the object. Can represent e.g. a section of a larger bitmap, bitmap with padded rows, or vertically flipped bitmap (rowStride can be negative).
template <typename T, int N = 1>
struct BitmapSection;
/// Constant reference to a 2D image bitmap with non-contiguous rows of pixels. Pixel storage not owned or managed by the object. Can represent e.g. a section of a larger bitmap, bitmap with padded rows, or vertically flipped bitmap (rowStride can be negative).
template <typename T, int N = 1>
struct BitmapConstSection;
template <typename T, int N>
struct BitmapConstSection {
const T *pixels;
int width, height;
/// Specifies the difference between the beginnings of adjacent pixel rows as the number of T elements, can be negative.
int rowStride;
YAxisOrientation yOrientation;
inline BitmapConstSection() : pixels(NULL), width(0), height(0), rowStride(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }
inline BitmapConstSection(const T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(N*width), yOrientation(yOrientation) { }
inline BitmapConstSection(const T *pixels, int width, int height, int rowStride, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(rowStride), yOrientation(yOrientation) { }
inline const T *operator()(int x, int y) const {
return pixels+rowStride*y+N*x;
}
/// Returns a constant reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax).
inline BitmapConstSection<T, N> getSection(int xMin, int yMin, int xMax, int yMax) const {
return BitmapConstSection<T, N>(pixels+rowStride*yMin+N*xMin, xMax-xMin, yMax-yMin, rowStride, yOrientation);
}
/// Returns a constant reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax).
inline BitmapConstSection<T, N> getConstSection(int xMin, int yMin, int xMax, int yMax) const {
return getSection(xMin, yMin, xMax, yMax);
}
/// Makes sure that the section's Y-axis orientation matches the argument by potentially reordering its rows.
inline void reorient(YAxisOrientation newYAxisOrientation) {
if (yOrientation != newYAxisOrientation) {
pixels += rowStride*(height-1);
rowStride = -rowStride;
yOrientation = newYAxisOrientation;
}
}
};
template <typename T, int N>
struct BitmapSection {
T *pixels;
int width, height;
/// Specifies the difference between the beginnings of adjacent pixel rows as the number of T elements, can be negative.
int rowStride;
YAxisOrientation yOrientation;
inline BitmapSection() : pixels(NULL), width(0), height(0), rowStride(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }
inline BitmapSection(T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(N*width), yOrientation(yOrientation) { }
inline BitmapSection(T *pixels, int width, int height, int rowStride, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(rowStride), yOrientation(yOrientation) { }
inline T *operator()(int x, int y) const {
return pixels+rowStride*y+N*x;
}
inline operator BitmapConstSection<T, N>() const {
return BitmapConstSection<T, N>(pixels, width, height, rowStride, yOrientation);
}
/// Returns a reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax).
inline BitmapSection<T, N> getSection(int xMin, int yMin, int xMax, int yMax) const {
return BitmapSection<T, N>(pixels+rowStride*yMin+N*xMin, xMax-xMin, yMax-yMin, rowStride, yOrientation);
}
/// Returns a constant reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax).
inline BitmapConstSection<T, N> getConstSection(int xMin, int yMin, int xMax, int yMax) const {
return BitmapConstSection<T, N>(pixels+rowStride*yMin+N*xMin, xMax-xMin, yMax-yMin, rowStride, yOrientation);
}
/// Makes sure that the section's Y-axis orientation matches the argument by potentially reordering its rows.
inline void reorient(YAxisOrientation newYAxisOrientation) {
if (yOrientation != newYAxisOrientation) {
pixels += rowStride*(height-1);
rowStride = -rowStride;
yOrientation = newYAxisOrientation;
}
}
};
template <typename T, int N>
struct BitmapConstRef {
const T *pixels;
int width, height;
YAxisOrientation yOrientation;
inline BitmapConstRef() : pixels(NULL), width(0), height(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }
inline BitmapConstRef(const T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), yOrientation(yOrientation) { }
inline const T *operator()(int x, int y) const {
return pixels+N*(width*y+x);
}
inline operator BitmapConstSection<T, N>() const {
return BitmapConstSection<T, N>(pixels, width, height, yOrientation);
}
/// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax).
inline BitmapConstSection<T, N> getSection(int xMin, int yMin, int xMax, int yMax) const {
return BitmapConstSection<T, N>(pixels+N*(width*yMin+xMin), xMax-xMin, yMax-yMin, N*width, yOrientation);
}
/// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax).
inline BitmapConstSection<T, N> getConstSection(int xMin, int yMin, int xMax, int yMax) const {
return getSection(xMin, yMin, xMax, yMax);
}
};
template <typename T, int N>
struct BitmapRef {
T *pixels;
int width, height;
YAxisOrientation yOrientation;
inline BitmapRef() : pixels(NULL), width(0), height(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }
inline BitmapRef(T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), yOrientation(yOrientation) { }
inline T *operator()(int x, int y) const {
return pixels+N*(width*y+x);
}
inline operator BitmapConstRef<T, N>() const {
return BitmapConstRef<T, N>(pixels, width, height, yOrientation);
}
inline operator BitmapSection<T, N>() const {
return BitmapSection<T, N>(pixels, width, height, yOrientation);
}
inline operator BitmapConstSection<T, N>() const {
return BitmapConstSection<T, N>(pixels, width, height, yOrientation);
}
/// Returns a reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax).
inline BitmapSection<T, N> getSection(int xMin, int yMin, int xMax, int yMax) const {
return BitmapSection<T, N>(pixels+N*(width*yMin+xMin), xMax-xMin, yMax-yMin, N*width, yOrientation);
}
/// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax).
inline BitmapConstSection<T, N> getConstSection(int xMin, int yMin, int xMax, int yMax) const {
return BitmapConstSection<T, N>(pixels+N*(width*yMin+xMin), xMax-xMin, yMax-yMin, N*width, yOrientation);
}
};
}