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