BsPixelData.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsPixelData.h"
  5. #include "BsPixelUtil.h"
  6. #include "BsPixelDataRTTI.h"
  7. #include "BsColor.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)
  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. UINT32 PixelData::getInternalBufferSize()
  76. {
  77. return getSize();
  78. }
  79. /************************************************************************/
  80. /* SERIALIZATION */
  81. /************************************************************************/
  82. RTTITypeBase* PixelData::getRTTIStatic()
  83. {
  84. return PixelDataRTTI::instance();
  85. }
  86. RTTITypeBase* PixelData::getRTTI() const
  87. {
  88. return PixelData::getRTTIStatic();
  89. }
  90. }