BsPixelData.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. void PixelData::setColors(const Vector<Color>& colors)
  101. {
  102. UINT32 depth = mExtents.getDepth();
  103. UINT32 height = mExtents.getHeight();
  104. UINT32 width = mExtents.getWidth();
  105. UINT32 totalNumElements = width * height * depth;
  106. if (colors.size() != totalNumElements)
  107. {
  108. LOGERR("Unable to set colors, invalid array size.")
  109. return;
  110. }
  111. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  112. UINT8* data = getData();
  113. for (UINT32 z = 0; z < depth; z++)
  114. {
  115. UINT32 zArrayIdx = z * width * height;
  116. UINT32 zDataIdx = z * mSlicePitch * pixelSize;
  117. for (UINT32 y = 0; y < height; y++)
  118. {
  119. UINT32 yArrayIdx = y * width;
  120. UINT32 yDataIdx = y * mRowPitch * pixelSize;
  121. for (UINT32 x = 0; x < width; x++)
  122. {
  123. UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
  124. UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
  125. UINT8* dest = data + dataIdx;
  126. PixelUtil::packColor(colors[arrayIdx], mFormat, dest);
  127. }
  128. }
  129. }
  130. }
  131. UINT32 PixelData::getInternalBufferSize()
  132. {
  133. return getSize();
  134. }
  135. /************************************************************************/
  136. /* SERIALIZATION */
  137. /************************************************************************/
  138. RTTITypeBase* PixelData::getRTTIStatic()
  139. {
  140. return PixelDataRTTI::instance();
  141. }
  142. RTTITypeBase* PixelData::getRTTI() const
  143. {
  144. return PixelData::getRTTIStatic();
  145. }
  146. }