BsScriptPixelData.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsScriptPixelData.h"
  4. #include "BsScriptMeta.h"
  5. #include "BsMonoField.h"
  6. #include "BsMonoClass.h"
  7. #include "BsMonoManager.h"
  8. #include "BsMonoUtil.h"
  9. #include "BsPixelUtil.h"
  10. #include "BsScriptColor.h"
  11. namespace bs
  12. {
  13. ScriptPixelData::ScriptPixelData(MonoObject* managedInstance)
  14. :ScriptObject(managedInstance)
  15. {
  16. }
  17. ScriptPixelData::~ScriptPixelData()
  18. {
  19. }
  20. void ScriptPixelData::initRuntimeData()
  21. {
  22. metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptPixelData::internal_createInstance);
  23. metaData.scriptClass->addInternalCall("Internal_GetPixel", &ScriptPixelData::internal_getPixel);
  24. metaData.scriptClass->addInternalCall("Internal_SetPixel", &ScriptPixelData::internal_setPixel);
  25. metaData.scriptClass->addInternalCall("Internal_GetPixels", &ScriptPixelData::internal_getPixels);
  26. metaData.scriptClass->addInternalCall("Internal_SetPixels", &ScriptPixelData::internal_setPixels);
  27. metaData.scriptClass->addInternalCall("Internal_GetRawPixels", &ScriptPixelData::internal_getRawPixels);
  28. metaData.scriptClass->addInternalCall("Internal_SetRawPixels", &ScriptPixelData::internal_setRawPixels);
  29. metaData.scriptClass->addInternalCall("Internal_GetExtents", &ScriptPixelData::internal_getExtents);
  30. metaData.scriptClass->addInternalCall("Internal_GetFormat", &ScriptPixelData::internal_getFormat);
  31. metaData.scriptClass->addInternalCall("Internal_GetRowPitch", &ScriptPixelData::internal_getRowPitch);
  32. metaData.scriptClass->addInternalCall("Internal_GetSlicePitch", &ScriptPixelData::internal_getSlicePitch);
  33. metaData.scriptClass->addInternalCall("Internal_GetSize", &ScriptPixelData::internal_getSize);
  34. metaData.scriptClass->addInternalCall("Internal_GetIsConsecutive", &ScriptPixelData::internal_getIsConsecutive);
  35. }
  36. void ScriptPixelData::initialize(const SPtr<PixelData>& pixelData)
  37. {
  38. mPixelData = pixelData;
  39. }
  40. MonoObject* ScriptPixelData::create(const SPtr<PixelData>& pixelData)
  41. {
  42. MonoObject* pixelDataObj = metaData.scriptClass->createInstance();
  43. ScriptPixelData* scriptPixelData = ScriptPixelData::toNative(pixelDataObj);
  44. scriptPixelData->initialize(pixelData);
  45. return pixelDataObj;
  46. }
  47. void ScriptPixelData::internal_createInstance(MonoObject* instance, PixelVolume* volume, PixelFormat format)
  48. {
  49. SPtr<PixelData> pixelData = bs_shared_ptr_new<PixelData>(*volume, format);
  50. pixelData->allocateInternalBuffer();
  51. ScriptPixelData* scriptPixelData = new (bs_alloc<ScriptPixelData>()) ScriptPixelData(instance);
  52. scriptPixelData->initialize(pixelData);
  53. }
  54. void ScriptPixelData::internal_getPixel(ScriptPixelData* thisPtr, int x, int y, int z, Color* value)
  55. {
  56. if (!checkIsLocked(thisPtr))
  57. *value = thisPtr->mPixelData->getColorAt(x, y, z);
  58. else
  59. *value = Color();
  60. }
  61. void ScriptPixelData::internal_setPixel(ScriptPixelData* thisPtr, int x, int y, int z, Color* value)
  62. {
  63. if (!checkIsLocked(thisPtr))
  64. thisPtr->mPixelData->setColorAt(*value, x, y, z);
  65. }
  66. void ScriptPixelData::internal_getPixels(ScriptPixelData* thisPtr, MonoArray** value)
  67. {
  68. if (!checkIsLocked(thisPtr))
  69. return;
  70. SPtr<PixelData> pixelData = thisPtr->mPixelData;
  71. PixelVolume pixelVolume = pixelData->getExtents();
  72. UINT32 depth = pixelVolume.getDepth();
  73. UINT32 height = pixelVolume.getHeight();
  74. UINT32 width = pixelVolume.getWidth();
  75. ::MonoClass* colorClass = ScriptColor::getMetaData()->scriptClass->_getInternalClass();
  76. UINT32 totalNumElements = width * height * depth;
  77. ScriptArray scriptArray(colorClass, totalNumElements);
  78. PixelFormat format = pixelData->getFormat();
  79. UINT32 pixelSize = PixelUtil::getNumElemBytes(format);
  80. UINT8* data = pixelData->getData();
  81. UINT32 rowPitch = pixelData->getRowPitch();
  82. UINT32 slicePitch = pixelData->getSlicePitch();
  83. // Note: Can I copy bytes more directly?
  84. for (UINT32 z = 0; z < depth; z++)
  85. {
  86. UINT32 zArrayIdx = z * width * height;
  87. UINT32 zDataIdx = z * slicePitch * pixelSize;
  88. for (UINT32 y = 0; y < height; y++)
  89. {
  90. UINT32 yArrayIdx = y * width;
  91. UINT32 yDataIdx = y * rowPitch * pixelSize;
  92. for (UINT32 x = 0; x < width; x++)
  93. {
  94. UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
  95. UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
  96. UINT8* dest = data + dataIdx;
  97. scriptArray.set(arrayIdx, *(Color*)dest);
  98. }
  99. }
  100. }
  101. *value = scriptArray.getInternal();
  102. }
  103. void ScriptPixelData::internal_setPixels(ScriptPixelData* thisPtr, MonoArray* value)
  104. {
  105. if (!checkIsLocked(thisPtr))
  106. return;
  107. SPtr<PixelData> pixelData = thisPtr->mPixelData;
  108. PixelVolume pixelVolume = pixelData->getExtents();
  109. UINT32 depth = pixelVolume.getDepth();
  110. UINT32 height = pixelVolume.getHeight();
  111. UINT32 width = pixelVolume.getWidth();
  112. ScriptArray scriptArray(value);
  113. UINT32 arrayLen = scriptArray.size();
  114. UINT32 totalNumElements = width * height * depth;
  115. if (arrayLen != totalNumElements)
  116. {
  117. LOGERR("Unable to set colors, invalid array size.")
  118. return;
  119. }
  120. PixelFormat format = pixelData->getFormat();
  121. UINT32 pixelSize = PixelUtil::getNumElemBytes(format);
  122. UINT8* data = pixelData->getData();
  123. UINT32 rowPitch = pixelData->getRowPitch();
  124. UINT32 slicePitch = pixelData->getSlicePitch();
  125. for (UINT32 z = 0; z < depth; z++)
  126. {
  127. UINT32 zArrayIdx = z * width * height;
  128. UINT32 zDataIdx = z * slicePitch * pixelSize;
  129. for (UINT32 y = 0; y < height; y++)
  130. {
  131. UINT32 yArrayIdx = y * width;
  132. UINT32 yDataIdx = y * rowPitch * pixelSize;
  133. for (UINT32 x = 0; x < width; x++)
  134. {
  135. UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
  136. UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
  137. UINT8* dest = data + dataIdx;
  138. Color color = scriptArray.get<Color>(arrayIdx);
  139. PixelUtil::packColor(color, format, dest);
  140. }
  141. }
  142. }
  143. }
  144. void ScriptPixelData::internal_getRawPixels(ScriptPixelData* thisPtr, MonoArray** value)
  145. {
  146. if (!checkIsLocked(thisPtr))
  147. return;
  148. SPtr<PixelData> pixelData = thisPtr->mPixelData;
  149. PixelVolume pixelVolume = pixelData->getExtents();
  150. UINT32 depth = pixelVolume.getDepth();
  151. UINT32 height = pixelVolume.getHeight();
  152. UINT32 width = pixelVolume.getWidth();
  153. ScriptArray scriptArray(MonoUtil::getByteClass(), pixelData->getSize());
  154. PixelFormat format = pixelData->getFormat();
  155. UINT32 pixelSize = PixelUtil::getNumElemBytes(format);
  156. UINT8* data = pixelData->getData();
  157. UINT32 rowPitch = pixelData->getRowPitch();
  158. UINT32 slicePitch = pixelData->getSlicePitch();
  159. // Note: Can I copy bytes more directly?
  160. for (UINT32 z = 0; z < depth; z++)
  161. {
  162. UINT32 zArrayIdx = z * width * height;
  163. UINT32 zDataIdx = z * slicePitch * pixelSize;
  164. for (UINT32 y = 0; y < height; y++)
  165. {
  166. UINT32 yArrayIdx = y * width;
  167. UINT32 yDataIdx = y * rowPitch * pixelSize;
  168. for (UINT32 x = 0; x < width; x++)
  169. {
  170. UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
  171. UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
  172. UINT8* dest = data + dataIdx;
  173. scriptArray.set(arrayIdx, *dest);
  174. }
  175. }
  176. }
  177. *value = scriptArray.getInternal();
  178. }
  179. void ScriptPixelData::internal_setRawPixels(ScriptPixelData* thisPtr, MonoArray* value)
  180. {
  181. if (!checkIsLocked(thisPtr))
  182. return;
  183. SPtr<PixelData> pixelData = thisPtr->mPixelData;
  184. PixelVolume pixelVolume = pixelData->getExtents();
  185. UINT32 depth = pixelVolume.getDepth();
  186. UINT32 height = pixelVolume.getHeight();
  187. UINT32 width = pixelVolume.getWidth();
  188. ScriptArray scriptArray(value);
  189. UINT32 arrayLen = scriptArray.size();
  190. if (pixelData->getSize() != arrayLen)
  191. {
  192. LOGERR("Unable to set colors, invalid array size.")
  193. return;
  194. }
  195. PixelFormat format = pixelData->getFormat();
  196. UINT32 pixelSize = PixelUtil::getNumElemBytes(format);
  197. UINT8* data = pixelData->getData();
  198. UINT32 rowPitch = pixelData->getRowPitch();
  199. UINT32 slicePitch = pixelData->getSlicePitch();
  200. // Note: Can I copy bytes more directly?
  201. for (UINT32 z = 0; z < depth; z++)
  202. {
  203. UINT32 zArrayIdx = z * width * height;
  204. UINT32 zDataIdx = z * slicePitch * pixelSize;
  205. for (UINT32 y = 0; y < height; y++)
  206. {
  207. UINT32 yArrayIdx = y * width;
  208. UINT32 yDataIdx = y * rowPitch * pixelSize;
  209. for (UINT32 x = 0; x < width; x++)
  210. {
  211. UINT32 arrayIdx = x + yArrayIdx + zArrayIdx;
  212. UINT32 dataIdx = x * pixelSize + yDataIdx + zDataIdx;
  213. UINT8* dest = data + dataIdx;
  214. *dest = scriptArray.get<char>(arrayIdx);
  215. }
  216. }
  217. }
  218. }
  219. void ScriptPixelData::internal_getExtents(ScriptPixelData* thisPtr, PixelVolume* value)
  220. {
  221. *value = thisPtr->mPixelData->getExtents();
  222. }
  223. void ScriptPixelData::internal_getFormat(ScriptPixelData* thisPtr, PixelFormat* value)
  224. {
  225. *value = thisPtr->mPixelData->getFormat();
  226. }
  227. void ScriptPixelData::internal_getRowPitch(ScriptPixelData* thisPtr, int* value)
  228. {
  229. *value = thisPtr->mPixelData->getRowPitch();
  230. }
  231. void ScriptPixelData::internal_getSlicePitch(ScriptPixelData* thisPtr, int* value)
  232. {
  233. *value = thisPtr->mPixelData->getSlicePitch();
  234. }
  235. void ScriptPixelData::internal_getSize(ScriptPixelData* thisPtr, int* value)
  236. {
  237. *value = thisPtr->mPixelData->getSize();
  238. }
  239. void ScriptPixelData::internal_getIsConsecutive(ScriptPixelData* thisPtr, bool* value)
  240. {
  241. *value = thisPtr->mPixelData->isConsecutive();
  242. }
  243. bool ScriptPixelData::checkIsLocked(ScriptPixelData* thisPtr)
  244. {
  245. if (thisPtr->mPixelData->isLocked())
  246. {
  247. LOGWRN("Attempting to access a locked pixel data buffer.");
  248. return true;
  249. }
  250. return false;
  251. }
  252. }