BsPixelData.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPixelData.h"
  4. #include "BsPixelUtil.h"
  5. #include "BsPixelDataRTTI.h"
  6. #include "BsColor.h"
  7. #include "BsDebug.h"
  8. namespace bs
  9. {
  10. PixelData::PixelData()
  11. :mExtents(0, 0, 0, 0), mFormat(PF_UNKNOWN), mRowPitch(0), mSlicePitch(0)
  12. { }
  13. PixelData::PixelData(const PixelVolume& extents, PixelFormat pixelFormat)
  14. :mExtents(extents), mFormat(pixelFormat)
  15. {
  16. PixelUtil::getPitch(extents.getWidth(), extents.getHeight(), extents.getDepth(), pixelFormat, mRowPitch,
  17. mSlicePitch);
  18. }
  19. PixelData::PixelData(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat)
  20. : mExtents(0, 0, 0, width, height, depth), mFormat(pixelFormat)
  21. {
  22. PixelUtil::getPitch(width, height, depth, pixelFormat, mRowPitch, mSlicePitch);
  23. }
  24. PixelData::PixelData(const PixelData& copy)
  25. :GpuResourceData(copy)
  26. {
  27. mFormat = copy.mFormat;
  28. mRowPitch = copy.mRowPitch;
  29. mSlicePitch = copy.mSlicePitch;
  30. mExtents = copy.mExtents;
  31. }
  32. PixelData& PixelData::operator=(const PixelData& rhs)
  33. {
  34. GpuResourceData::operator= (rhs);
  35. mFormat = rhs.mFormat;
  36. mRowPitch = rhs.mRowPitch;
  37. mSlicePitch = rhs.mSlicePitch;
  38. mExtents = rhs.mExtents;
  39. return *this;
  40. }
  41. UINT32 PixelData::getConsecutiveSize() const
  42. {
  43. return PixelUtil::getMemorySize(getWidth(), getHeight(), getDepth(), mFormat);
  44. }
  45. UINT32 PixelData::getSize() const
  46. {
  47. return PixelUtil::getMemorySize(mRowPitch, mSlicePitch / mRowPitch, getDepth(), getFormat());
  48. }
  49. PixelData PixelData::getSubVolume(const PixelVolume &def) const
  50. {
  51. if (PixelUtil::isCompressed(mFormat))
  52. {
  53. if (def.left == getLeft() && def.top == getTop() && def.front == getFront() &&
  54. def.right == getRight() && def.bottom == getBottom() && def.back == getBack())
  55. {
  56. // Entire buffer is being queried
  57. return *this;
  58. }
  59. BS_EXCEPT(InvalidParametersException, "Cannot return subvolume of compressed PixelBuffer");
  60. }
  61. if (!mExtents.contains(def))
  62. {
  63. BS_EXCEPT(InvalidParametersException, "Bounds out of range");
  64. }
  65. const size_t elemSize = PixelUtil::getNumElemBytes(mFormat);
  66. PixelData rval(def.getWidth(), def.getHeight(), def.getDepth(), mFormat);
  67. rval.setExternalBuffer(((UINT8*)getData()) + ((def.left - getLeft())*elemSize)
  68. + ((def.top - getTop())*mRowPitch*elemSize)
  69. + ((def.front - getFront())*mSlicePitch*elemSize));
  70. rval.mRowPitch = mRowPitch;
  71. rval.mSlicePitch = mSlicePitch;
  72. rval.mFormat = mFormat;
  73. return rval;
  74. }
  75. Color PixelData::getColorAt(UINT32 x, UINT32 y, UINT32 z) const
  76. {
  77. Color cv;
  78. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  79. UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
  80. PixelUtil::unpackColor(&cv, mFormat, (unsigned char *)getData() + pixelOffset);
  81. return cv;
  82. }
  83. void PixelData::setColorAt(const Color& color, UINT32 x, UINT32 y, UINT32 z)
  84. {
  85. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  86. UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
  87. PixelUtil::packColor(color, mFormat, (unsigned char *)getData() + pixelOffset);
  88. }
  89. Vector<Color> PixelData::getColors() const
  90. {
  91. UINT32 depth = mExtents.getDepth();
  92. UINT32 height = mExtents.getHeight();
  93. UINT32 width = mExtents.getWidth();
  94. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  95. UINT8* data = getData();
  96. Vector<Color> colors(width * height * depth);
  97. for (UINT32 z = 0; z < depth; z++)
  98. {
  99. UINT32 zArrayIdx = z * width * height;
  100. UINT32 zDataIdx = z * mSlicePitch * pixelSize;
  101. for (UINT32 y = 0; y < height; y++)
  102. {
  103. UINT32 yArrayIdx = y * width;
  104. UINT32 yDataIdx = y * mRowPitch * pixelSize;
  105. for (UINT32 x = 0; x < width; x++)
  106. {
  107. UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
  108. UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
  109. UINT8* dest = data + dataIdx;
  110. PixelUtil::unpackColor(&colors[arrayIdx], mFormat, dest);
  111. }
  112. }
  113. }
  114. return colors;
  115. }
  116. template<class T>
  117. void PixelData::setColorsInternal(const T& colors, UINT32 numElements)
  118. {
  119. UINT32 depth = mExtents.getDepth();
  120. UINT32 height = mExtents.getHeight();
  121. UINT32 width = mExtents.getWidth();
  122. UINT32 totalNumElements = width * height * depth;
  123. if (numElements != totalNumElements)
  124. {
  125. LOGERR("Unable to set colors, invalid array size.");
  126. return;
  127. }
  128. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  129. UINT8* data = getData();
  130. for (UINT32 z = 0; z < depth; z++)
  131. {
  132. UINT32 zArrayIdx = z * width * height;
  133. UINT32 zDataIdx = z * mSlicePitch * pixelSize;
  134. for (UINT32 y = 0; y < height; y++)
  135. {
  136. UINT32 yArrayIdx = y * width;
  137. UINT32 yDataIdx = y * mRowPitch * pixelSize;
  138. for (UINT32 x = 0; x < width; x++)
  139. {
  140. UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
  141. UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
  142. UINT8* dest = data + dataIdx;
  143. PixelUtil::packColor(colors[arrayIdx], mFormat, dest);
  144. }
  145. }
  146. }
  147. }
  148. template BS_CORE_EXPORT void PixelData::setColorsInternal(Color* const &, UINT32);
  149. template BS_CORE_EXPORT void PixelData::setColorsInternal(const Vector<Color>&, UINT32);
  150. void PixelData::setColors(const Vector<Color>& colors)
  151. {
  152. setColorsInternal(colors, (UINT32)colors.size());
  153. }
  154. void PixelData::setColors(Color* colors, UINT32 numElements)
  155. {
  156. setColorsInternal(colors, numElements);
  157. }
  158. float PixelData::getDepthAt(UINT32 x, UINT32 y, UINT32 z) const
  159. {
  160. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  161. UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
  162. return PixelUtil::unpackDepth(mFormat, (unsigned char *)getData() + pixelOffset);;
  163. }
  164. void PixelData::setDepthAt(float depth, UINT32 x, UINT32 y, UINT32 z)
  165. {
  166. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  167. UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
  168. PixelUtil::packDepth(depth, mFormat, (unsigned char *)getData() + pixelOffset);
  169. }
  170. Vector<float> PixelData::getDepths() const
  171. {
  172. UINT32 depth = mExtents.getDepth();
  173. UINT32 height = mExtents.getHeight();
  174. UINT32 width = mExtents.getWidth();
  175. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  176. UINT8* data = getData();
  177. Vector<float> depths(width * height * depth);
  178. for (UINT32 z = 0; z < depth; z++)
  179. {
  180. UINT32 zArrayIdx = z * width * height;
  181. UINT32 zDataIdx = z * mSlicePitch * pixelSize;
  182. for (UINT32 y = 0; y < height; y++)
  183. {
  184. UINT32 yArrayIdx = y * width;
  185. UINT32 yDataIdx = y * mRowPitch * pixelSize;
  186. for (UINT32 x = 0; x < width; x++)
  187. {
  188. UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
  189. UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
  190. UINT8* dest = data + dataIdx;
  191. depths[arrayIdx] = PixelUtil::unpackDepth(mFormat, dest);
  192. }
  193. }
  194. }
  195. return depths;
  196. }
  197. SPtr<PixelData> PixelData::create(const PixelVolume &extents, PixelFormat pixelFormat)
  198. {
  199. SPtr<PixelData> pixelData = bs_shared_ptr_new<PixelData>(extents, pixelFormat);
  200. pixelData->allocateInternalBuffer();
  201. return pixelData;
  202. }
  203. SPtr<PixelData> PixelData::create(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat)
  204. {
  205. SPtr<PixelData> pixelData = bs_shared_ptr_new<PixelData>(width, height, depth, pixelFormat);
  206. pixelData->allocateInternalBuffer();
  207. return pixelData;
  208. }
  209. UINT32 PixelData::getInternalBufferSize() const
  210. {
  211. return getSize();
  212. }
  213. /************************************************************************/
  214. /* SERIALIZATION */
  215. /************************************************************************/
  216. RTTITypeBase* PixelData::getRTTIStatic()
  217. {
  218. return PixelDataRTTI::instance();
  219. }
  220. RTTITypeBase* PixelData::getRTTI() const
  221. {
  222. return PixelData::getRTTIStatic();
  223. }
  224. }