CmPixelData.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "CmPixelData.h"
  2. #include "CmPixelUtil.h"
  3. #include "CmPixelDataRTTI.h"
  4. #include "CmColor.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. PixelData PixelData::getSubVolume(const PixelVolume &def) const
  29. {
  30. if (PixelUtil::isCompressed(mFormat))
  31. {
  32. if (def.left == getLeft() && def.top == getTop() && def.front == getFront() &&
  33. def.right == getRight() && def.bottom == getBottom() && def.back == getBack())
  34. {
  35. // Entire buffer is being queried
  36. return *this;
  37. }
  38. CM_EXCEPT(InvalidParametersException, "Cannot return subvolume of compressed PixelBuffer");
  39. }
  40. if (!mExtents.contains(def))
  41. {
  42. CM_EXCEPT(InvalidParametersException, "Bounds out of range");
  43. }
  44. const size_t elemSize = PixelUtil::getNumElemBytes(mFormat);
  45. PixelData rval(def.getWidth(), def.getHeight(), def.getDepth(), mFormat);
  46. rval.setExternalBuffer(((UINT8*)getData()) + ((def.left - getLeft())*elemSize)
  47. + ((def.top - getTop())*mRowPitch*elemSize)
  48. + ((def.front - getFront())*mSlicePitch*elemSize));
  49. rval.mRowPitch = mRowPitch;
  50. rval.mSlicePitch = mSlicePitch;
  51. rval.mFormat = mFormat;
  52. return rval;
  53. }
  54. Color PixelData::getColorAt(UINT32 x, UINT32 y, UINT32 z)
  55. {
  56. Color cv;
  57. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  58. UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
  59. PixelUtil::unpackColour(&cv, mFormat, (unsigned char *)getData() + pixelOffset);
  60. return cv;
  61. }
  62. void PixelData::setColorAt(Color const &cv, UINT32 x, UINT32 y, UINT32 z)
  63. {
  64. UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
  65. UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
  66. PixelUtil::packColour(cv, mFormat, (unsigned char *)getData() + pixelOffset);
  67. }
  68. UINT32 PixelData::getInternalBufferSize()
  69. {
  70. return PixelUtil::getMemorySize(getWidth(), getHeight(), getDepth(), getFormat());
  71. }
  72. /************************************************************************/
  73. /* SERIALIZATION */
  74. /************************************************************************/
  75. RTTITypeBase* PixelData::getRTTIStatic()
  76. {
  77. return PixelDataRTTI::instance();
  78. }
  79. RTTITypeBase* PixelData::getRTTI() const
  80. {
  81. return PixelData::getRTTIStatic();
  82. }
  83. }