1
2#include "Bitmap.h"
3
4#include <cstdlib>
5#include <cstring>
6
7namespace msdfgen {
8
9template <typename T, int N>
10Bitmap<T, N>::Bitmap() : pixels(NULL), w(0), h(0) { }
11
12template <typename T, int N>
13Bitmap<T, N>::Bitmap(int width, int height) : w(width), h(height) {
14 pixels = new T[N*w*h];
15}
16
17template <typename T, int N>
18Bitmap<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
23template <typename T, int N>
24Bitmap<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
30template <typename T, int N>
31Bitmap<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
37template <typename T, int N>
38Bitmap<T, N>::~Bitmap() {
39 delete [] pixels;
40}
41
42template <typename T, int N>
43Bitmap<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
53template <typename T, int N>
54Bitmap<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
65template <typename T, int N>
66Bitmap<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
77template <typename T, int N>
78int Bitmap<T, N>::width() const {
79 return w;
80}
81
82template <typename T, int N>
83int Bitmap<T, N>::height() const {
84 return h;
85}
86
87template <typename T, int N>
88T * Bitmap<T, N>::operator()(int x, int y) {
89 return pixels+N*(w*y+x);
90}
91
92template <typename T, int N>
93const T * Bitmap<T, N>::operator()(int x, int y) const {
94 return pixels+N*(w*y+x);
95}
96
97template <typename T, int N>
98Bitmap<T, N>::operator T *() {
99 return pixels;
100}
101
102template <typename T, int N>
103Bitmap<T, N>::operator const T *() const {
104 return pixels;
105}
106
107template <typename T, int N>
108Bitmap<T, N>::operator BitmapRef<T, N>() {
109 return BitmapRef<T, N>(pixels, w, h);
110}
111
112template <typename T, int N>
113Bitmap<T, N>::operator BitmapConstRef<T, N>() const {
114 return BitmapConstRef<T, N>(pixels, w, h);
115}
116
117}
118