BsScriptPixelData.cpp 8.9 KB

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