BsPixelData.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 BansheeEngine
  9. {
  10. PixelData::PixelData(const PixelData& copy)
  11. :GpuResourceData(copy)
  12. {
  13. mFormat = copy.mFormat;
  14. mRowPitch = copy.mRowPitch;
  15. mSlicePitch = copy.mSlicePitch;
  16. mExtents = copy.mExtents;
  17. }
  18. PixelData& PixelData::operator=(const PixelData& rhs)
  19. {
  20. GpuResourceData::operator= (rhs);
  21. mFormat = rhs.mFormat;
  22. mRowPitch = rhs.mRowPitch;
  23. mSlicePitch = rhs.mSlicePitch;
  24. mExtents = rhs.mExtents;
  25. return *this;
  26. }
  27. UINT32 PixelData::getConsecutiveSize() const
  28. {
  29. return PixelUtil::getMemorySize(getWidth(), getHeight(), getDepth(), mFormat);
  30. }
  31. UINT32 PixelData::getSize() const
  32. {
  33. return PixelUtil::getMemorySize(mRowPitch, mSlicePitch / mRowPitch, getDepth(), getFormat());
  34. }
  35. PixelData PixelData::getSubVolume(const PixelVolume &def) const
  36. {
  37. if (PixelUtil::isCompressed(mFormat))
  38. {
  39. if (def.left == getLeft() && def.top == getTop() && def.front == getFront() &&
  40. def.right == getRight() && def.bottom == getBottom() && def.back == getBack())
  41. {
  42. // Entire buffer is being queried
  43. return *this;
  44. }
  45. BS_EXCEPT(InvalidParametersException, "Cannot return subvolume of compressed PixelBuffer");
  46. }
  47. if (!mExtents.contains(def))
  48. {
  49. BS_EXCEPT(InvalidParametersException, "Bounds out of range");
  50. }
  51. const size_t elemSize = PixelUtil::getNumElemBytes(mFormat);
  52. PixelData rval(def.getWidth(), def.getHeight(), def.getDepth(), mFormat);
  53. rval.setExternalBuffer(((UINT8*)getData()) + ((def.left - getLeft())*elemSize)
  54. + ((def.top - getTop())*mRowPitch*elemSize)
  55. + ((def.front - getFront())*mSlicePitch*elemSize));
  56. rval.mRowPitch = mRowPitch;
  57. rval.mSlicePitch = mSlicePitch;
  58. rval.mFormat = mFormat;
  59. return rval;
  60. }
  61. Color PixelData::getColorAt(UINT32 x, UINT32 y, UINT32 z) const
  62. {
  63. Color cv;
  64. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  65. UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
  66. PixelUtil::unpackColor(&cv, mFormat, (unsigned char *)getData() + pixelOffset);
  67. return cv;
  68. }
  69. void PixelData::setColorAt(Color const &cv, UINT32 x, UINT32 y, UINT32 z)
  70. {
  71. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  72. UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
  73. PixelUtil::packColor(cv, mFormat, (unsigned char *)getData() + pixelOffset);
  74. }
  75. Vector<Color> PixelData::getColors() const
  76. {
  77. UINT32 depth = mExtents.getDepth();
  78. UINT32 height = mExtents.getHeight();
  79. UINT32 width = mExtents.getWidth();
  80. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  81. UINT8* data = getData();
  82. Vector<Color> colors(width * height * depth);
  83. for (UINT32 z = 0; z < depth; z++)
  84. {
  85. UINT32 zArrayIdx = z * width * height;
  86. UINT32 zDataIdx = z * mSlicePitch * pixelSize;
  87. for (UINT32 y = 0; y < height; y++)
  88. {
  89. UINT32 yArrayIdx = y * width;
  90. UINT32 yDataIdx = y * mRowPitch * pixelSize;
  91. for (UINT32 x = 0; x < width; x++)
  92. {
  93. UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
  94. UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
  95. UINT8* dest = data + dataIdx;
  96. PixelUtil::unpackColor(&colors[arrayIdx], mFormat, dest);
  97. }
  98. }
  99. }
  100. return colors;
  101. }
  102. template<class T>
  103. void PixelData::setColorsInternal(const T& colors, UINT32 numElements)
  104. {
  105. UINT32 depth = mExtents.getDepth();
  106. UINT32 height = mExtents.getHeight();
  107. UINT32 width = mExtents.getWidth();
  108. UINT32 totalNumElements = width * height * depth;
  109. if (numElements != totalNumElements)
  110. {
  111. LOGERR("Unable to set colors, invalid array size.");
  112. return;
  113. }
  114. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  115. UINT8* data = getData();
  116. for (UINT32 z = 0; z < depth; z++)
  117. {
  118. UINT32 zArrayIdx = z * width * height;
  119. UINT32 zDataIdx = z * mSlicePitch * pixelSize;
  120. for (UINT32 y = 0; y < height; y++)
  121. {
  122. UINT32 yArrayIdx = y * width;
  123. UINT32 yDataIdx = y * mRowPitch * pixelSize;
  124. for (UINT32 x = 0; x < width; x++)
  125. {
  126. UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
  127. UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
  128. UINT8* dest = data + dataIdx;
  129. PixelUtil::packColor(colors[arrayIdx], mFormat, dest);
  130. }
  131. }
  132. }
  133. }
  134. template BS_CORE_EXPORT void PixelData::setColorsInternal(Color* const &, UINT32);
  135. template BS_CORE_EXPORT void PixelData::setColorsInternal(const Vector<Color>&, UINT32);
  136. void PixelData::setColors(const Vector<Color>& colors)
  137. {
  138. setColorsInternal(colors, (UINT32)colors.size());
  139. }
  140. void PixelData::setColors(Color* colors, UINT32 numElements)
  141. {
  142. setColorsInternal(colors, numElements);
  143. }
  144. PixelDataPtr PixelData::create(const PixelVolume &extents, PixelFormat pixelFormat)
  145. {
  146. PixelDataPtr pixelData = bs_shared_ptr_new<PixelData>(extents, pixelFormat);
  147. pixelData->allocateInternalBuffer();
  148. return pixelData;
  149. }
  150. PixelDataPtr PixelData::create(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat)
  151. {
  152. PixelDataPtr pixelData = bs_shared_ptr_new<PixelData>(width, height, depth, pixelFormat);
  153. pixelData->allocateInternalBuffer();
  154. return pixelData;
  155. }
  156. UINT32 PixelData::getInternalBufferSize() const
  157. {
  158. return getSize();
  159. }
  160. /************************************************************************/
  161. /* SERIALIZATION */
  162. /************************************************************************/
  163. RTTITypeBase* PixelData::getRTTIStatic()
  164. {
  165. return PixelDataRTTI::instance();
  166. }
  167. RTTITypeBase* PixelData::getRTTI() const
  168. {
  169. return PixelData::getRTTIStatic();
  170. }
  171. }