| 1 | |
| 2 | #include "Bitmap.h" |
| 3 | |
| 4 | #include <cstdlib> |
| 5 | #include <cstring> |
| 6 | |
| 7 | namespace msdfgen { |
| 8 | |
| 9 | template <typename T, int N> |
| 10 | Bitmap<T, N>::Bitmap() : pixels(NULL), w(0), h(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { } |
| 11 | |
| 12 | template <typename T, int N> |
| 13 | Bitmap<T, N>::Bitmap(int width, int height, YAxisOrientation yOrientation) : w(width), h(height), yOrientation(yOrientation) { |
| 14 | pixels = new T[N*w*h]; |
| 15 | } |
| 16 | |
| 17 | template <typename T, int N> |
| 18 | Bitmap<T, N>::Bitmap(const BitmapConstRef<T, N> &orig) : w(orig.width), h(orig.height), yOrientation(orig.yOrientation) { |
| 19 | pixels = new T[N*w*h]; |
| 20 | memcpy(pixels, orig.pixels, sizeof(T)*N*w*h); |
| 21 | } |
| 22 | |
| 23 | template <typename T, int N> |
| 24 | Bitmap<T, N>::Bitmap(const BitmapConstSection<T, N> &orig) : w(orig.width), h(orig.height), yOrientation(orig.yOrientation) { |
| 25 | pixels = new T[N*w*h]; |
| 26 | T *dst = pixels; |
| 27 | const T *src = orig.pixels; |
| 28 | int rowLength = N*w; |
| 29 | for (int y = 0; y < h; ++y) { |
| 30 | memcpy(dst, src, sizeof(T)*rowLength); |
| 31 | dst += rowLength; |
| 32 | src += orig.rowStride; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | template <typename T, int N> |
| 37 | Bitmap<T, N>::Bitmap(const Bitmap<T, N> &orig) : w(orig.w), h(orig.h), yOrientation(orig.yOrientation) { |
| 38 | pixels = new T[N*w*h]; |
| 39 | memcpy(pixels, orig.pixels, sizeof(T)*N*w*h); |
| 40 | } |
| 41 | |
| 42 | #ifdef MSDFGEN_USE_CPP11 |
| 43 | template <typename T, int N> |
| 44 | Bitmap<T, N>::Bitmap(Bitmap<T, N> &&orig) : pixels(orig.pixels), w(orig.w), h(orig.h), yOrientation(orig.yOrientation) { |
| 45 | orig.pixels = NULL; |
| 46 | orig.w = 0, orig.h = 0; |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | template <typename T, int N> |
| 51 | Bitmap<T, N>::~Bitmap() { |
| 52 | delete[] pixels; |
| 53 | } |
| 54 | |
| 55 | template <typename T, int N> |
| 56 | Bitmap<T, N> &Bitmap<T, N>::operator=(const BitmapConstRef<T, N> &orig) { |
| 57 | if (pixels != orig.pixels) { |
| 58 | delete[] pixels; |
| 59 | w = orig.width, h = orig.height; |
| 60 | yOrientation = orig.yOrientation; |
| 61 | pixels = new T[N*w*h]; |
| 62 | memcpy(pixels, orig.pixels, sizeof(T)*N*w*h); |
| 63 | } |
| 64 | return *this; |
| 65 | } |
| 66 | |
| 67 | template <typename T, int N> |
| 68 | Bitmap<T, N> &Bitmap<T, N>::operator=(const BitmapConstSection<T, N> &orig) { |
| 69 | if (orig.pixels && orig.pixels >= pixels && orig.pixels < pixels+N*w*h) |
| 70 | return *this = Bitmap<T, N>(orig); |
| 71 | delete[] pixels; |
| 72 | w = orig.width, h = orig.height; |
| 73 | yOrientation = orig.yOrientation; |
| 74 | pixels = new T[N*w*h]; |
| 75 | T *dst = pixels; |
| 76 | const T *src = orig.pixels; |
| 77 | int rowLength = N*w; |
| 78 | for (int y = 0; y < h; ++y) { |
| 79 | memcpy(dst, src, sizeof(T)*rowLength); |
| 80 | dst += rowLength; |
| 81 | src += orig.rowStride; |
| 82 | } |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | template <typename T, int N> |
| 87 | Bitmap<T, N> &Bitmap<T, N>::operator=(const Bitmap<T, N> &orig) { |
| 88 | if (this != &orig) { |
| 89 | delete[] pixels; |
| 90 | w = orig.w, h = orig.h; |
| 91 | yOrientation = orig.yOrientation; |
| 92 | pixels = new T[N*w*h]; |
| 93 | memcpy(pixels, orig.pixels, sizeof(T)*N*w*h); |
| 94 | } |
| 95 | return *this; |
| 96 | } |
| 97 | |
| 98 | #ifdef MSDFGEN_USE_CPP11 |
| 99 | template <typename T, int N> |
| 100 | Bitmap<T, N> &Bitmap<T, N>::operator=(Bitmap<T, N> &&orig) { |
| 101 | if (this != &orig) { |
| 102 | delete[] pixels; |
| 103 | pixels = orig.pixels; |
| 104 | w = orig.w, h = orig.h; |
| 105 | yOrientation = orig.yOrientation; |
| 106 | orig.pixels = NULL; |
| 107 | } |
| 108 | return *this; |
| 109 | } |
| 110 | #endif |
| 111 | |
| 112 | template <typename T, int N> |
| 113 | int Bitmap<T, N>::width() const { |
| 114 | return w; |
| 115 | } |
| 116 | |
| 117 | template <typename T, int N> |
| 118 | int Bitmap<T, N>::height() const { |
| 119 | return h; |
| 120 | } |
| 121 | |
| 122 | template <typename T, int N> |
| 123 | T *Bitmap<T, N>::operator()(int x, int y) { |
| 124 | return pixels+N*(w*y+x); |
| 125 | } |
| 126 | |
| 127 | template <typename T, int N> |
| 128 | const T *Bitmap<T, N>::operator()(int x, int y) const { |
| 129 | return pixels+N*(w*y+x); |
| 130 | } |
| 131 | |
| 132 | template <typename T, int N> |
| 133 | Bitmap<T, N>::operator T *() { |
| 134 | return pixels; |
| 135 | } |
| 136 | |
| 137 | template <typename T, int N> |
| 138 | Bitmap<T, N>::operator const T *() const { |
| 139 | return pixels; |
| 140 | } |
| 141 | |
| 142 | template <typename T, int N> |
| 143 | Bitmap<T, N>::operator BitmapRef<T, N>() { |
| 144 | return BitmapRef<T, N>(pixels, w, h, yOrientation); |
| 145 | } |
| 146 | |
| 147 | template <typename T, int N> |
| 148 | Bitmap<T, N>::operator BitmapConstRef<T, N>() const { |
| 149 | return BitmapConstRef<T, N>(pixels, w, h, yOrientation); |
| 150 | } |
| 151 | |
| 152 | template <typename T, int N> |
| 153 | Bitmap<T, N>::operator BitmapSection<T, N>() { |
| 154 | return BitmapSection<T, N>(pixels, w, h, yOrientation); |
| 155 | } |
| 156 | |
| 157 | template <typename T, int N> |
| 158 | Bitmap<T, N>::operator BitmapConstSection<T, N>() const { |
| 159 | return BitmapConstSection<T, N>(pixels, w, h, yOrientation); |
| 160 | } |
| 161 | |
| 162 | template <typename T, int N> |
| 163 | BitmapSection<T, N> Bitmap<T, N>::getSection(int xMin, int yMin, int xMax, int yMax) { |
| 164 | return BitmapSection<T, N>(pixels+N*(w*yMin+xMin), xMax-xMin, yMax-yMin, N*w, yOrientation); |
| 165 | } |
| 166 | |
| 167 | template <typename T, int N> |
| 168 | BitmapConstSection<T, N> Bitmap<T, N>::getConstSection(int xMin, int yMin, int xMax, int yMax) const { |
| 169 | return BitmapConstSection<T, N>(pixels+N*(w*yMin+xMin), xMax-xMin, yMax-yMin, N*w, yOrientation); |
| 170 | } |
| 171 | |
| 172 | } |