| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "BsScriptPixelData.h"
- #include "BsScriptMeta.h"
- #include "BsMonoField.h"
- #include "BsMonoClass.h"
- #include "BsMonoManager.h"
- #include "BsMonoUtil.h"
- #include "BsPixelUtil.h"
- #include "BsScriptColor.h"
- namespace bs
- {
- ScriptPixelData::ScriptPixelData(MonoObject* managedInstance)
- :ScriptObject(managedInstance)
- {
- }
- ScriptPixelData::~ScriptPixelData()
- {
- }
- void ScriptPixelData::initRuntimeData()
- {
- metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptPixelData::internal_createInstance);
- metaData.scriptClass->addInternalCall("Internal_GetPixel", &ScriptPixelData::internal_getPixel);
- metaData.scriptClass->addInternalCall("Internal_SetPixel", &ScriptPixelData::internal_setPixel);
- metaData.scriptClass->addInternalCall("Internal_GetPixels", &ScriptPixelData::internal_getPixels);
- metaData.scriptClass->addInternalCall("Internal_SetPixels", &ScriptPixelData::internal_setPixels);
- metaData.scriptClass->addInternalCall("Internal_GetRawPixels", &ScriptPixelData::internal_getRawPixels);
- metaData.scriptClass->addInternalCall("Internal_SetRawPixels", &ScriptPixelData::internal_setRawPixels);
- metaData.scriptClass->addInternalCall("Internal_GetExtents", &ScriptPixelData::internal_getExtents);
- metaData.scriptClass->addInternalCall("Internal_GetFormat", &ScriptPixelData::internal_getFormat);
- metaData.scriptClass->addInternalCall("Internal_GetRowPitch", &ScriptPixelData::internal_getRowPitch);
- metaData.scriptClass->addInternalCall("Internal_GetSlicePitch", &ScriptPixelData::internal_getSlicePitch);
- metaData.scriptClass->addInternalCall("Internal_GetSize", &ScriptPixelData::internal_getSize);
- metaData.scriptClass->addInternalCall("Internal_GetIsConsecutive", &ScriptPixelData::internal_getIsConsecutive);
- }
- void ScriptPixelData::initialize(const SPtr<PixelData>& pixelData)
- {
- mPixelData = pixelData;
- }
- MonoObject* ScriptPixelData::create(const SPtr<PixelData>& pixelData)
- {
- MonoObject* pixelDataObj = metaData.scriptClass->createInstance();
- ScriptPixelData* scriptPixelData = ScriptPixelData::toNative(pixelDataObj);
- scriptPixelData->initialize(pixelData);
- return pixelDataObj;
- }
- void ScriptPixelData::internal_createInstance(MonoObject* instance, PixelVolume* volume, PixelFormat format)
- {
- SPtr<PixelData> pixelData = bs_shared_ptr_new<PixelData>(*volume, format);
- pixelData->allocateInternalBuffer();
- ScriptPixelData* scriptPixelData = new (bs_alloc<ScriptPixelData>()) ScriptPixelData(instance);
- scriptPixelData->initialize(pixelData);
- }
- void ScriptPixelData::internal_getPixel(ScriptPixelData* thisPtr, int x, int y, int z, Color* value)
- {
- if (!checkIsLocked(thisPtr))
- *value = thisPtr->mPixelData->getColorAt(x, y, z);
- else
- *value = Color();
- }
- void ScriptPixelData::internal_setPixel(ScriptPixelData* thisPtr, int x, int y, int z, Color* value)
- {
- if (!checkIsLocked(thisPtr))
- thisPtr->mPixelData->setColorAt(*value, x, y, z);
- }
- void ScriptPixelData::internal_getPixels(ScriptPixelData* thisPtr, MonoArray** value)
- {
- if (!checkIsLocked(thisPtr))
- return;
- SPtr<PixelData> pixelData = thisPtr->mPixelData;
- PixelVolume pixelVolume = pixelData->getExtents();
- UINT32 depth = pixelVolume.getDepth();
- UINT32 height = pixelVolume.getHeight();
- UINT32 width = pixelVolume.getWidth();
- ::MonoClass* colorClass = ScriptColor::getMetaData()->scriptClass->_getInternalClass();
- UINT32 totalNumElements = width * height * depth;
- ScriptArray scriptArray(colorClass, totalNumElements);
- PixelFormat format = pixelData->getFormat();
- UINT32 pixelSize = PixelUtil::getNumElemBytes(format);
- UINT8* data = pixelData->getData();
- UINT32 rowPitch = pixelData->getRowPitch();
- UINT32 slicePitch = pixelData->getSlicePitch();
- // Note: Can I copy bytes more directly?
- for (UINT32 z = 0; z < depth; z++)
- {
- UINT32 zArrayIdx = z * width * height;
- UINT32 zDataIdx = z * slicePitch * pixelSize;
- for (UINT32 y = 0; y < height; y++)
- {
- UINT32 yArrayIdx = y * width;
- UINT32 yDataIdx = y * rowPitch * pixelSize;
- for (UINT32 x = 0; x < width; x++)
- {
- UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
- UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
- UINT8* dest = data + dataIdx;
- scriptArray.set(arrayIdx, *(Color*)dest);
- }
- }
- }
- *value = scriptArray.getInternal();
- }
- void ScriptPixelData::internal_setPixels(ScriptPixelData* thisPtr, MonoArray* value)
- {
- if (!checkIsLocked(thisPtr))
- return;
- SPtr<PixelData> pixelData = thisPtr->mPixelData;
- PixelVolume pixelVolume = pixelData->getExtents();
- UINT32 depth = pixelVolume.getDepth();
- UINT32 height = pixelVolume.getHeight();
- UINT32 width = pixelVolume.getWidth();
- ScriptArray scriptArray(value);
- UINT32 arrayLen = scriptArray.size();
- UINT32 totalNumElements = width * height * depth;
- if (arrayLen != totalNumElements)
- {
- LOGERR("Unable to set colors, invalid array size.")
- return;
- }
- PixelFormat format = pixelData->getFormat();
- UINT32 pixelSize = PixelUtil::getNumElemBytes(format);
- UINT8* data = pixelData->getData();
- UINT32 rowPitch = pixelData->getRowPitch();
- UINT32 slicePitch = pixelData->getSlicePitch();
- for (UINT32 z = 0; z < depth; z++)
- {
- UINT32 zArrayIdx = z * width * height;
- UINT32 zDataIdx = z * slicePitch * pixelSize;
- for (UINT32 y = 0; y < height; y++)
- {
- UINT32 yArrayIdx = y * width;
- UINT32 yDataIdx = y * rowPitch * pixelSize;
- for (UINT32 x = 0; x < width; x++)
- {
- UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
- UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
- UINT8* dest = data + dataIdx;
- Color color = scriptArray.get<Color>(arrayIdx);
- PixelUtil::packColor(color, format, dest);
- }
- }
- }
- }
- void ScriptPixelData::internal_getRawPixels(ScriptPixelData* thisPtr, MonoArray** value)
- {
- if (!checkIsLocked(thisPtr))
- return;
- SPtr<PixelData> pixelData = thisPtr->mPixelData;
- PixelVolume pixelVolume = pixelData->getExtents();
- UINT32 depth = pixelVolume.getDepth();
- UINT32 height = pixelVolume.getHeight();
- UINT32 width = pixelVolume.getWidth();
- ScriptArray scriptArray(MonoUtil::getByteClass(), pixelData->getSize());
- PixelFormat format = pixelData->getFormat();
- UINT32 pixelSize = PixelUtil::getNumElemBytes(format);
- UINT8* data = pixelData->getData();
- UINT32 rowPitch = pixelData->getRowPitch();
- UINT32 slicePitch = pixelData->getSlicePitch();
- // Note: Can I copy bytes more directly?
- for (UINT32 z = 0; z < depth; z++)
- {
- UINT32 zArrayIdx = z * width * height;
- UINT32 zDataIdx = z * slicePitch * pixelSize;
- for (UINT32 y = 0; y < height; y++)
- {
- UINT32 yArrayIdx = y * width;
- UINT32 yDataIdx = y * rowPitch * pixelSize;
- for (UINT32 x = 0; x < width; x++)
- {
- UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
- UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
- UINT8* dest = data + dataIdx;
- scriptArray.set(arrayIdx, *dest);
- }
- }
- }
- *value = scriptArray.getInternal();
- }
- void ScriptPixelData::internal_setRawPixels(ScriptPixelData* thisPtr, MonoArray* value)
- {
- if (!checkIsLocked(thisPtr))
- return;
- SPtr<PixelData> pixelData = thisPtr->mPixelData;
- PixelVolume pixelVolume = pixelData->getExtents();
- UINT32 depth = pixelVolume.getDepth();
- UINT32 height = pixelVolume.getHeight();
- UINT32 width = pixelVolume.getWidth();
- ScriptArray scriptArray(value);
- UINT32 arrayLen = scriptArray.size();
- if (pixelData->getSize() != arrayLen)
- {
- LOGERR("Unable to set colors, invalid array size.")
- return;
- }
- PixelFormat format = pixelData->getFormat();
- UINT32 pixelSize = PixelUtil::getNumElemBytes(format);
- UINT8* data = pixelData->getData();
- UINT32 rowPitch = pixelData->getRowPitch();
- UINT32 slicePitch = pixelData->getSlicePitch();
- // Note: Can I copy bytes more directly?
- for (UINT32 z = 0; z < depth; z++)
- {
- UINT32 zArrayIdx = z * width * height;
- UINT32 zDataIdx = z * slicePitch * pixelSize;
- for (UINT32 y = 0; y < height; y++)
- {
- UINT32 yArrayIdx = y * width;
- UINT32 yDataIdx = y * rowPitch * pixelSize;
- for (UINT32 x = 0; x < width; x++)
- {
- UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
- UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
- UINT8* dest = data + dataIdx;
- *dest = scriptArray.get<char>(arrayIdx);
- }
- }
- }
- }
- void ScriptPixelData::internal_getExtents(ScriptPixelData* thisPtr, PixelVolume* value)
- {
- *value = thisPtr->mPixelData->getExtents();
- }
- void ScriptPixelData::internal_getFormat(ScriptPixelData* thisPtr, PixelFormat* value)
- {
- *value = thisPtr->mPixelData->getFormat();
- }
- void ScriptPixelData::internal_getRowPitch(ScriptPixelData* thisPtr, int* value)
- {
- *value = thisPtr->mPixelData->getRowPitch();
- }
- void ScriptPixelData::internal_getSlicePitch(ScriptPixelData* thisPtr, int* value)
- {
- *value = thisPtr->mPixelData->getSlicePitch();
- }
- void ScriptPixelData::internal_getSize(ScriptPixelData* thisPtr, int* value)
- {
- *value = thisPtr->mPixelData->getSize();
- }
- void ScriptPixelData::internal_getIsConsecutive(ScriptPixelData* thisPtr, bool* value)
- {
- *value = thisPtr->mPixelData->isConsecutive();
- }
- bool ScriptPixelData::checkIsLocked(ScriptPixelData* thisPtr)
- {
- if (thisPtr->mPixelData->isLocked())
- {
- LOGWRN("Attempting to access a locked pixel data buffer.");
- return true;
- }
- return false;
- }
- }
|