2
0

BsPixelData.cpp 5.0 KB

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