BitmapRef.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #pragma once
  2. #include "YAxisOrientation.h"
  3. namespace msdfgen {
  4. /// Reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object.
  5. template <typename T, int N = 1>
  6. struct BitmapRef;
  7. /// Constant reference to a 2D image bitmap or a buffer acting as one. Pixel storage not owned or managed by the object.
  8. template <typename T, int N = 1>
  9. struct BitmapConstRef;
  10. /// Reference to a 2D image bitmap with non-contiguous rows of pixels. Pixel storage not owned or managed by the object. Can represent e.g. a section of a larger bitmap, bitmap with padded rows, or vertically flipped bitmap (rowStride can be negative).
  11. template <typename T, int N = 1>
  12. struct BitmapSection;
  13. /// Constant reference to a 2D image bitmap with non-contiguous rows of pixels. Pixel storage not owned or managed by the object. Can represent e.g. a section of a larger bitmap, bitmap with padded rows, or vertically flipped bitmap (rowStride can be negative).
  14. template <typename T, int N = 1>
  15. struct BitmapConstSection;
  16. template <typename T, int N>
  17. struct BitmapRef {
  18. T *pixels;
  19. int width, height;
  20. YAxisOrientation yOrientation;
  21. inline BitmapRef() : pixels(NULL), width(0), height(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }
  22. inline BitmapRef(T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), yOrientation(yOrientation) { }
  23. inline T *operator()(int x, int y) const {
  24. return pixels+N*(width*y+x);
  25. }
  26. /// Returns a reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax).
  27. inline BitmapSection<T, N> getSection(int xMin, int yMin, int xMax, int yMax) const {
  28. return BitmapSection<T, N>(pixels+N*(width*yMin+xMin), xMax-xMin, yMax-yMin, N*width, yOrientation);
  29. }
  30. /// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax).
  31. inline BitmapConstSection<T, N> getConstSection(int xMin, int yMin, int xMax, int yMax) const {
  32. return BitmapConstSection<T, N>(pixels+N*(width*yMin+xMin), xMax-xMin, yMax-yMin, N*width, yOrientation);
  33. }
  34. };
  35. template <typename T, int N>
  36. struct BitmapConstRef {
  37. const T *pixels;
  38. int width, height;
  39. YAxisOrientation yOrientation;
  40. inline BitmapConstRef() : pixels(NULL), width(0), height(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }
  41. inline BitmapConstRef(const T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), yOrientation(yOrientation) { }
  42. inline BitmapConstRef(const BitmapRef<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), yOrientation(orig.yOrientation) { }
  43. inline const T *operator()(int x, int y) const {
  44. return pixels+N*(width*y+x);
  45. }
  46. /// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax).
  47. inline BitmapConstSection<T, N> getSection(int xMin, int yMin, int xMax, int yMax) const {
  48. return BitmapConstSection<T, N>(pixels+N*(width*yMin+xMin), xMax-xMin, yMax-yMin, N*width, yOrientation);
  49. }
  50. /// Returns a constant reference to a rectangular section of the bitmap specified by bounds (excluding xMax, yMax).
  51. inline BitmapConstSection<T, N> getConstSection(int xMin, int yMin, int xMax, int yMax) const {
  52. return getSection(xMin, yMin, xMax, yMax);
  53. }
  54. };
  55. template <typename T, int N>
  56. struct BitmapSection {
  57. T *pixels;
  58. int width, height;
  59. /// Specifies the difference between the beginnings of adjacent pixel rows as the number of T elements, can be negative.
  60. int rowStride;
  61. YAxisOrientation yOrientation;
  62. inline BitmapSection() : pixels(NULL), width(0), height(0), rowStride(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }
  63. inline BitmapSection(T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(N*width), yOrientation(yOrientation) { }
  64. inline BitmapSection(T *pixels, int width, int height, int rowStride, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(rowStride), yOrientation(yOrientation) { }
  65. inline BitmapSection(const BitmapRef<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), rowStride(N*orig.width), yOrientation(orig.yOrientation) { }
  66. inline T *operator()(int x, int y) const {
  67. return pixels+rowStride*y+N*x;
  68. }
  69. /// Returns a reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax).
  70. inline BitmapSection<T, N> getSection(int xMin, int yMin, int xMax, int yMax) const {
  71. return BitmapSection<T, N>(pixels+rowStride*yMin+N*xMin, xMax-xMin, yMax-yMin, rowStride, yOrientation);
  72. }
  73. /// Returns a constant reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax).
  74. inline BitmapConstSection<T, N> getConstSection(int xMin, int yMin, int xMax, int yMax) const {
  75. return BitmapConstSection<T, N>(pixels+rowStride*yMin+N*xMin, xMax-xMin, yMax-yMin, rowStride, yOrientation);
  76. }
  77. /// Makes sure that the section's Y-axis orientation matches the argument by potentially reordering its rows.
  78. inline void reorient(YAxisOrientation newYAxisOrientation) {
  79. if (yOrientation != newYAxisOrientation) {
  80. pixels += rowStride*(height-1);
  81. rowStride = -rowStride;
  82. yOrientation = newYAxisOrientation;
  83. }
  84. }
  85. };
  86. template <typename T, int N>
  87. struct BitmapConstSection {
  88. const T *pixels;
  89. int width, height;
  90. /// Specifies the difference between the beginnings of adjacent pixel rows as the number of T elements, can be negative.
  91. int rowStride;
  92. YAxisOrientation yOrientation;
  93. inline BitmapConstSection() : pixels(NULL), width(0), height(0), rowStride(0), yOrientation(MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) { }
  94. inline BitmapConstSection(const T *pixels, int width, int height, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(N*width), yOrientation(yOrientation) { }
  95. inline BitmapConstSection(const T *pixels, int width, int height, int rowStride, YAxisOrientation yOrientation = MSDFGEN_Y_AXIS_DEFAULT_ORIENTATION) : pixels(pixels), width(width), height(height), rowStride(rowStride), yOrientation(yOrientation) { }
  96. inline BitmapConstSection(const BitmapRef<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), rowStride(N*orig.width), yOrientation(orig.yOrientation) { }
  97. inline BitmapConstSection(const BitmapConstRef<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), rowStride(N*orig.width), yOrientation(orig.yOrientation) { }
  98. inline BitmapConstSection(const BitmapSection<T, N> &orig) : pixels(orig.pixels), width(orig.width), height(orig.height), rowStride(orig.rowStride), yOrientation(orig.yOrientation) { }
  99. inline const T *operator()(int x, int y) const {
  100. return pixels+rowStride*y+N*x;
  101. }
  102. /// Returns a constant reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax).
  103. inline BitmapConstSection<T, N> getSection(int xMin, int yMin, int xMax, int yMax) const {
  104. return BitmapConstSection<T, N>(pixels+rowStride*yMin+N*xMin, xMax-xMin, yMax-yMin, rowStride, yOrientation);
  105. }
  106. /// Returns a constant reference to a rectangular subsection of the bitmap specified by bounds (excluding xMax, yMax).
  107. inline BitmapConstSection<T, N> getConstSection(int xMin, int yMin, int xMax, int yMax) const {
  108. return getSection(xMin, yMin, xMax, yMax);
  109. }
  110. /// Makes sure that the section's Y-axis orientation matches the argument by potentially reordering its rows.
  111. inline void reorient(YAxisOrientation newYAxisOrientation) {
  112. if (yOrientation != newYAxisOrientation) {
  113. pixels += rowStride*(height-1);
  114. rowStride = -rowStride;
  115. yOrientation = newYAxisOrientation;
  116. }
  117. }
  118. };
  119. }