| 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) { } |
| 11 | |
| 12 | template <typename T, int N> |
| 13 | Bitmap<T, N>::Bitmap(int width, int height) : w(width), h(height) { |
| 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) { |
| 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 Bitmap<T, N> &orig) : w(orig.w), h(orig.h) { |
| 25 | pixels = new T[N*w*h]; |
| 26 | memcpy(pixels, orig.pixels, sizeof(T)*N*w*h); |
| 27 | } |
| 28 | |
| 29 | #ifdef MSDFGEN_USE_CPP11 |
| 30 | template <typename T, int N> |
| 31 | Bitmap<T, N>::Bitmap(Bitmap<T, N> &&orig) : pixels(orig.pixels), w(orig.w), h(orig.h) { |
| 32 | orig.pixels = NULL; |
| 33 | orig.w = 0, orig.h = 0; |
| 34 | } |
| 35 | #endif |
| 36 | |
| 37 | template <typename T, int N> |
| 38 | Bitmap<T, N>::~Bitmap() { |
| 39 | delete [] pixels; |
| 40 | } |
| 41 | |
| 42 | template <typename T, int N> |
| 43 | Bitmap<T, N> & Bitmap<T, N>::operator=(const BitmapConstRef<T, N> &orig) { |
| 44 | if (pixels != orig.pixels) { |
| 45 | delete [] pixels; |
| 46 | w = orig.width, h = orig.height; |
| 47 | pixels = new T[N*w*h]; |
| 48 | memcpy(pixels, orig.pixels, sizeof(T)*N*w*h); |
| 49 | } |
| 50 | return *this; |
| 51 | } |
| 52 | |
| 53 | template <typename T, int N> |
| 54 | Bitmap<T, N> & Bitmap<T, N>::operator=(const Bitmap<T, N> &orig) { |
| 55 | if (this != &orig) { |
| 56 | delete [] pixels; |
| 57 | w = orig.w, h = orig.h; |
| 58 | pixels = new T[N*w*h]; |
| 59 | memcpy(pixels, orig.pixels, sizeof(T)*N*w*h); |
| 60 | } |
| 61 | return *this; |
| 62 | } |
| 63 | |
| 64 | #ifdef MSDFGEN_USE_CPP11 |
| 65 | template <typename T, int N> |
| 66 | Bitmap<T, N> & Bitmap<T, N>::operator=(Bitmap<T, N> &&orig) { |
| 67 | if (this != &orig) { |
| 68 | delete [] pixels; |
| 69 | pixels = orig.pixels; |
| 70 | w = orig.w, h = orig.h; |
| 71 | orig.pixels = NULL; |
| 72 | } |
| 73 | return *this; |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | template <typename T, int N> |
| 78 | int Bitmap<T, N>::width() const { |
| 79 | return w; |
| 80 | } |
| 81 | |
| 82 | template <typename T, int N> |
| 83 | int Bitmap<T, N>::height() const { |
| 84 | return h; |
| 85 | } |
| 86 | |
| 87 | template <typename T, int N> |
| 88 | T * Bitmap<T, N>::operator()(int x, int y) { |
| 89 | return pixels+N*(w*y+x); |
| 90 | } |
| 91 | |
| 92 | template <typename T, int N> |
| 93 | const T * Bitmap<T, N>::operator()(int x, int y) const { |
| 94 | return pixels+N*(w*y+x); |
| 95 | } |
| 96 | |
| 97 | template <typename T, int N> |
| 98 | Bitmap<T, N>::operator T *() { |
| 99 | return pixels; |
| 100 | } |
| 101 | |
| 102 | template <typename T, int N> |
| 103 | Bitmap<T, N>::operator const T *() const { |
| 104 | return pixels; |
| 105 | } |
| 106 | |
| 107 | template <typename T, int N> |
| 108 | Bitmap<T, N>::operator BitmapRef<T, N>() { |
| 109 | return BitmapRef<T, N>(pixels, w, h); |
| 110 | } |
| 111 | |
| 112 | template <typename T, int N> |
| 113 | Bitmap<T, N>::operator BitmapConstRef<T, N>() const { |
| 114 | return BitmapConstRef<T, N>(pixels, w, h); |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | |