Bitmap.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }
  7. template <typename T, int N>
  8. Bitmap<T, N>::Bitmap(int width, int height, YAxisOrientation yOrientation) : w(width), h(height), yOrientation(yOrientation) {
  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), yOrientation(orig.yOrientation) {
  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 BitmapConstSection<T, N> &orig) : w(orig.width), h(orig.height), yOrientation(orig.yOrientation) {
  18. pixels = new T[N*w*h];
  19. T *dst = pixels;
  20. const T *src = orig.pixels;
  21. int rowLength = N*w;
  22. for (int y = 0; y < h; ++y) {
  23. memcpy(dst, src, sizeof(T)*rowLength);
  24. dst += rowLength;
  25. src += orig.rowStride;
  26. }
  27. }
  28. template <typename T, int N>
  29. Bitmap<T, N>::Bitmap(const Bitmap<T, N> &orig) : w(orig.w), h(orig.h), yOrientation(orig.yOrientation) {
  30. pixels = new T[N*w*h];
  31. memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
  32. }
  33. #ifdef MSDFGEN_USE_CPP11
  34. template <typename T, int N>
  35. Bitmap<T, N>::Bitmap(Bitmap<T, N> &&orig) : pixels(orig.pixels), w(orig.w), h(orig.h), yOrientation(orig.yOrientation) {
  36. orig.pixels = NULL;
  37. orig.w = 0, orig.h = 0;
  38. }
  39. #endif
  40. template <typename T, int N>
  41. Bitmap<T, N>::~Bitmap() {
  42. delete[] pixels;
  43. }
  44. template <typename T, int N>
  45. Bitmap<T, N> &Bitmap<T, N>::operator=(const BitmapConstRef<T, N> &orig) {
  46. if (pixels != orig.pixels) {
  47. delete[] pixels;
  48. w = orig.width, h = orig.height;
  49. yOrientation = orig.yOrientation;
  50. pixels = new T[N*w*h];
  51. memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
  52. }
  53. return *this;
  54. }
  55. template <typename T, int N>
  56. Bitmap<T, N> &Bitmap<T, N>::operator=(const BitmapConstSection<T, N> &orig) {
  57. if (orig.pixels && orig.pixels >= pixels && orig.pixels < pixels+N*w*h)
  58. return *this = Bitmap<T, N>(orig);
  59. delete[] pixels;
  60. w = orig.width, h = orig.height;
  61. yOrientation = orig.yOrientation;
  62. pixels = new T[N*w*h];
  63. T *dst = pixels;
  64. const T *src = orig.pixels;
  65. int rowLength = N*w;
  66. for (int y = 0; y < h; ++y) {
  67. memcpy(dst, src, sizeof(T)*rowLength);
  68. dst += rowLength;
  69. src += orig.rowStride;
  70. }
  71. return *this;
  72. }
  73. template <typename T, int N>
  74. Bitmap<T, N> &Bitmap<T, N>::operator=(const Bitmap<T, N> &orig) {
  75. if (this != &orig) {
  76. delete[] pixels;
  77. w = orig.w, h = orig.h;
  78. yOrientation = orig.yOrientation;
  79. pixels = new T[N*w*h];
  80. memcpy(pixels, orig.pixels, sizeof(T)*N*w*h);
  81. }
  82. return *this;
  83. }
  84. #ifdef MSDFGEN_USE_CPP11
  85. template <typename T, int N>
  86. Bitmap<T, N> &Bitmap<T, N>::operator=(Bitmap<T, N> &&orig) {
  87. if (this != &orig) {
  88. delete[] pixels;
  89. pixels = orig.pixels;
  90. w = orig.w, h = orig.h;
  91. yOrientation = orig.yOrientation;
  92. orig.pixels = NULL;
  93. }
  94. return *this;
  95. }
  96. #endif
  97. template <typename T, int N>
  98. int Bitmap<T, N>::width() const {
  99. return w;
  100. }
  101. template <typename T, int N>
  102. int Bitmap<T, N>::height() const {
  103. return h;
  104. }
  105. template <typename T, int N>
  106. T *Bitmap<T, N>::operator()(int x, int y) {
  107. return pixels+N*(w*y+x);
  108. }
  109. template <typename T, int N>
  110. const T *Bitmap<T, N>::operator()(int x, int y) const {
  111. return pixels+N*(w*y+x);
  112. }
  113. template <typename T, int N>
  114. Bitmap<T, N>::operator T *() {
  115. return pixels;
  116. }
  117. template <typename T, int N>
  118. Bitmap<T, N>::operator const T *() const {
  119. return pixels;
  120. }
  121. template <typename T, int N>
  122. Bitmap<T, N>::operator BitmapRef<T, N>() {
  123. return BitmapRef<T, N>(pixels, w, h, yOrientation);
  124. }
  125. template <typename T, int N>
  126. Bitmap<T, N>::operator BitmapConstRef<T, N>() const {
  127. return BitmapConstRef<T, N>(pixels, w, h, yOrientation);
  128. }
  129. template <typename T, int N>
  130. Bitmap<T, N>::operator BitmapSection<T, N>() {
  131. return BitmapSection<T, N>(pixels, w, h, yOrientation);
  132. }
  133. template <typename T, int N>
  134. Bitmap<T, N>::operator BitmapConstSection<T, N>() const {
  135. return BitmapConstSection<T, N>(pixels, w, h, yOrientation);
  136. }
  137. template <typename T, int N>
  138. BitmapSection<T, N> Bitmap<T, N>::getSection(int xMin, int yMin, int xMax, int yMax) {
  139. return BitmapSection<T, N>(pixels+N*(w*yMin+xMin), xMax-xMin, yMax-yMin, N*w, yOrientation);
  140. }
  141. template <typename T, int N>
  142. BitmapConstSection<T, N> Bitmap<T, N>::getConstSection(int xMin, int yMin, int xMax, int yMax) const {
  143. return BitmapConstSection<T, N>(pixels+N*(w*yMin+xMin), xMax-xMin, yMax-yMin, N*w, yOrientation);
  144. }
  145. }