BsScriptPixelData.cpp 9.3 KB

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