| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #include "BsPixelData.h"
- #include "BsPixelUtil.h"
- #include "BsPixelDataRTTI.h"
- #include "BsColor.h"
- #include "BsDebug.h"
- namespace BansheeEngine
- {
- PixelData::PixelData(const PixelData& copy)
- :GpuResourceData(copy)
- {
- mFormat = copy.mFormat;
- mRowPitch = copy.mRowPitch;
- mSlicePitch = copy.mSlicePitch;
- mExtents = copy.mExtents;
- }
- PixelData& PixelData::operator=(const PixelData& rhs)
- {
- GpuResourceData::operator= (rhs);
- mFormat = rhs.mFormat;
- mRowPitch = rhs.mRowPitch;
- mSlicePitch = rhs.mSlicePitch;
- mExtents = rhs.mExtents;
- return *this;
- }
- UINT32 PixelData::getConsecutiveSize() const
- {
- return PixelUtil::getMemorySize(getWidth(), getHeight(), getDepth(), mFormat);
- }
- UINT32 PixelData::getSize() const
- {
- return PixelUtil::getMemorySize(mRowPitch, mSlicePitch / mRowPitch, getDepth(), getFormat());
- }
- PixelData PixelData::getSubVolume(const PixelVolume &def) const
- {
- if (PixelUtil::isCompressed(mFormat))
- {
- if (def.left == getLeft() && def.top == getTop() && def.front == getFront() &&
- def.right == getRight() && def.bottom == getBottom() && def.back == getBack())
- {
- // Entire buffer is being queried
- return *this;
- }
- BS_EXCEPT(InvalidParametersException, "Cannot return subvolume of compressed PixelBuffer");
- }
- if (!mExtents.contains(def))
- {
- BS_EXCEPT(InvalidParametersException, "Bounds out of range");
- }
- const size_t elemSize = PixelUtil::getNumElemBytes(mFormat);
- PixelData rval(def.getWidth(), def.getHeight(), def.getDepth(), mFormat);
- rval.setExternalBuffer(((UINT8*)getData()) + ((def.left - getLeft())*elemSize)
- + ((def.top - getTop())*mRowPitch*elemSize)
- + ((def.front - getFront())*mSlicePitch*elemSize));
- rval.mRowPitch = mRowPitch;
- rval.mSlicePitch = mSlicePitch;
- rval.mFormat = mFormat;
- return rval;
- }
- Color PixelData::getColorAt(UINT32 x, UINT32 y, UINT32 z)
- {
- Color cv;
- UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
- UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
- PixelUtil::unpackColor(&cv, mFormat, (unsigned char *)getData() + pixelOffset);
- return cv;
- }
- void PixelData::setColorAt(Color const &cv, UINT32 x, UINT32 y, UINT32 z)
- {
- UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
- UINT32 pixelOffset = pixelSize * (z * mSlicePitch + y * mRowPitch + x);
- PixelUtil::packColor(cv, mFormat, (unsigned char *)getData() + pixelOffset);
- }
- Vector<Color> PixelData::getColors() const
- {
- UINT32 depth = mExtents.getDepth();
- UINT32 height = mExtents.getHeight();
- UINT32 width = mExtents.getWidth();
- UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
- UINT8* data = getData();
- Vector<Color> colors(width * height * depth);
- for (UINT32 z = 0; z < depth; z++)
- {
- UINT32 zArrayIdx = z * width * height;
- UINT32 zDataIdx = z * mSlicePitch * pixelSize;
- for (UINT32 y = 0; y < height; y++)
- {
- UINT32 yArrayIdx = y * width;
- UINT32 yDataIdx = y * mRowPitch * pixelSize;
- for (UINT32 x = 0; x < width; x++)
- {
- UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
- UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
- UINT8* dest = data + dataIdx;
- PixelUtil::unpackColor(&colors[arrayIdx], mFormat, dest);
- }
- }
- }
- return colors;
- }
- template<class T>
- void PixelData::setColorsInternal(const T& colors, UINT32 numElements)
- {
- UINT32 depth = mExtents.getDepth();
- UINT32 height = mExtents.getHeight();
- UINT32 width = mExtents.getWidth();
- UINT32 totalNumElements = width * height * depth;
- if (numElements != totalNumElements)
- {
- LOGERR("Unable to set colors, invalid array size.");
- return;
- }
- UINT32 pixelSize = PixelUtil::getNumElemBytes(mFormat);
- UINT8* data = getData();
- for (UINT32 z = 0; z < depth; z++)
- {
- UINT32 zArrayIdx = z * width * height;
- UINT32 zDataIdx = z * mSlicePitch * pixelSize;
- for (UINT32 y = 0; y < height; y++)
- {
- UINT32 yArrayIdx = y * width;
- UINT32 yDataIdx = y * mRowPitch * pixelSize;
- for (UINT32 x = 0; x < width; x++)
- {
- UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
- UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
- UINT8* dest = data + dataIdx;
- PixelUtil::packColor(colors[arrayIdx], mFormat, dest);
- }
- }
- }
- }
- template BS_CORE_EXPORT void PixelData::setColorsInternal(Color* const &, UINT32);
- template BS_CORE_EXPORT void PixelData::setColorsInternal(const Vector<Color>&, UINT32);
- void PixelData::setColors(const Vector<Color>& colors)
- {
- setColorsInternal(colors, (UINT32)colors.size());
- }
- void PixelData::setColors(Color* colors, UINT32 numElements)
- {
- setColorsInternal(colors, numElements);
- }
- UINT32 PixelData::getInternalBufferSize() const
- {
- return getSize();
- }
- /************************************************************************/
- /* SERIALIZATION */
- /************************************************************************/
- RTTITypeBase* PixelData::getRTTIStatic()
- {
- return PixelDataRTTI::instance();
- }
- RTTITypeBase* PixelData::getRTTI() const
- {
- return PixelData::getRTTIStatic();
- }
- }
|