Bitmap.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "Bitmap.h"
  2. #include <cstdlib>
  3. #include <cstring>
  4. namespace msdfgen {
  5. template <typename T, int N>
  6. Bitmap<T, N>::Bitmap() : pixels(NULL), w(0), h(0) { }
  7. template <typename T, int N>
  8. Bitmap<T, N>::Bitmap(int width, int height) : w(width), h(height) {
  9. pixels = new T[N*w*h];
  10. }
  11. template <typename T, int N>
  12. Bitmap<T, N>::Bitmap(const BitmapConstRef<T, N> &orig) : w(orig.width), h(orig.height) {
  13. pixels = new T[N*w*h];
  14. memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
  15. }
  16. template <typename T, int N>
  17. Bitmap<T, N>::Bitmap(const Bitmap<T, N> &orig) : w(orig.w), h(orig.h) {
  18. pixels = new T[N*w*h];
  19. memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
  20. }
  21. #ifdef MSDFGEN_USE_CPP11
  22. template <typename T, int N>
  23. Bitmap<T, N>::Bitmap(Bitmap<T, N> &&orig) : pixels(orig.pixels), w(orig.w), h(orig.h) {
  24. orig.pixels = NULL;
  25. orig.w = 0, orig.h = 0;
  26. }
  27. #endif
  28. template <typename T, int N>
  29. Bitmap<T, N>::~Bitmap() {
  30. delete [] pixels;
  31. }
  32. template <typename T, int N>
  33. Bitmap<T, N> & Bitmap<T, N>::operator=(const BitmapConstRef<T, N> &orig) {
  34. if (pixels != orig.pixels) {
  35. delete [] pixels;
  36. w = orig.width, h = orig.height;
  37. pixels = new T[N*w*h];
  38. memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
  39. }
  40. return *this;
  41. }
  42. template <typename T, int N>
  43. Bitmap<T, N> & Bitmap<T, N>::operator=(const Bitmap<T, N> &orig) {
  44. if (this != &orig) {
  45. delete [] pixels;
  46. w = orig.w, h = orig.h;
  47. pixels = new T[N*w*h];
  48. memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
  49. }
  50. return *this;
  51. }
  52. #ifdef MSDFGEN_USE_CPP11
  53. template <typename T, int N>
  54. Bitmap<T, N> & Bitmap<T, N>::operator=(Bitmap<T, N> &&orig) {
  55. if (this != &orig) {
  56. delete [] pixels;
  57. pixels = orig.pixels;
  58. w = orig.w, h = orig.h;
  59. orig.pixels = NULL;
  60. }
  61. return *this;
  62. }
  63. #endif
  64. template <typename T, int N>
  65. int Bitmap<T, N>::width() const {
  66. return w;
  67. }
  68. template <typename T, int N>
  69. int Bitmap<T, N>::height() const {
  70. return h;
  71. }
  72. template <typename T, int N>
  73. T * Bitmap<T, N>::operator()(int x, int y) {
  74. return pixels+N*(w*y+x);
  75. }
  76. template <typename T, int N>
  77. const T * Bitmap<T, N>::operator()(int x, int y) const {
  78. return pixels+N*(w*y+x);
  79. }
  80. template <typename T, int N>
  81. Bitmap<T, N>::operator T *() {
  82. return pixels;
  83. }
  84. template <typename T, int N>
  85. Bitmap<T, N>::operator const T *() const {
  86. return pixels;
  87. }
  88. template <typename T, int N>
  89. Bitmap<T, N>::operator BitmapRef<T, N>() {
  90. return BitmapRef<T, N>(pixels, w, h);
  91. }
  92. template <typename T, int N>
  93. Bitmap<T, N>::operator BitmapConstRef<T, N>() const {
  94. return BitmapConstRef<T, N>(pixels, w, h);
  95. }
  96. }