BsPixelData.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "BsPixelData.h"
  2. #include "BsPixelUtil.h"
  3. #include "BsPixelDataRTTI.h"
  4. #include "BsColor.h"
  5. namespace BansheeEngine
  6. {
  7. PixelData::PixelData(const PixelData& copy)
  8. :GpuResourceData(copy)
  9. {
  10. mFormat = copy.mFormat;
  11. mRowPitch = copy.mRowPitch;
  12. mSlicePitch = copy.mSlicePitch;
  13. mExtents = copy.mExtents;
  14. }
  15. PixelData& PixelData::operator=(const PixelData& rhs)
  16. {
  17. GpuResourceData::operator= (rhs);
  18. mFormat = rhs.mFormat;
  19. mRowPitch = rhs.mRowPitch;
  20. mSlicePitch = rhs.mSlicePitch;
  21. mExtents = rhs.mExtents;
  22. return *this;
  23. }
  24. UINT32 PixelData::getConsecutiveSize() const
  25. {
  26. return PixelUtil::getMemorySize(getWidth(), getHeight(), getDepth(), mFormat);
  27. }
  28. UINT32 PixelData::getSize() const
  29. {
  30. return PixelUtil::getMemorySize(mRowPitch, mSlicePitch / mRowPitch, getDepth(), getFormat());
  31. }
  32. PixelData PixelData::getSubVolume(const PixelVolume &def) const
  33. {
  34. if (PixelUtil::isCompressed(mFormat))
  35. {
  36. if (def.left == getLeft() && def.top == getTop() && def.front == getFront() &&
  37. def.right == getRight() && def.bottom == getBottom() && def.back == getBack())
  38. {
  39. // Entire buffer is being queried
  40. return *this;
  41. }
  42. BS_EXCEPT(InvalidParametersException, "Cannot return subvolume of compressed PixelBuffer");
  43. }
  44. if (!mExtents.contains(def))
  45. {
  46. BS_EXCEPT(InvalidParametersException, "Bounds out of range");
  47. }
  48. const size_t elemSize = PixelUtil::getNumElemBytes(mFormat);
  49. PixelData rval(def.getWidth(), def.getHeight(), def.getDepth(), mFormat);
  50. rval.setExternalBuffer(((UINT8*)getData()) + ((def.left - getLeft())*elemSize)
  51. + ((def.top - getTop())*mRowPitch*elemSize)
  52. + ((def.front - getFront())*mSlicePitch*elemSize));
  53. rval.mRowPitch = mRowPitch;
  54. rval.mSlicePitch = mSlicePitch;
  55. rval.mFormat = mFormat;
  56. return rval;
  57. }
  58. Color PixelData::getColorAt(UINT32 x, UINT32 y, UINT32 z)
  59. {
  60. Color cv;
  61. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  62. UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
  63. PixelUtil::unpackColor(&cv, mFormat, (unsigned char *)getData() + pixelOffset);
  64. return cv;
  65. }
  66. void PixelData::setColorAt(Color const &cv, UINT32 x, UINT32 y, UINT32 z)
  67. {
  68. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  69. UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
  70. PixelUtil::packColor(cv, mFormat, (unsigned char *)getData() + pixelOffset);
  71. }
  72. UINT32 PixelData::getInternalBufferSize()
  73. {
  74. return getSize();
  75. }
  76. /************************************************************************/
  77. /* SERIALIZATION */
  78. /************************************************************************/
  79. RTTITypeBase* PixelData::getRTTIStatic()
  80. {
  81. return PixelDataRTTI::instance();
  82. }
  83. RTTITypeBase* PixelData::getRTTI() const
  84. {
  85. return PixelData::getRTTIStatic();
  86. }
  87. }