PixelData.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace BansheeEngine
  8. {
  9. // Do not modify, IDs must match C++ enum PixelFormat
  10. public enum PixelFormat
  11. {
  12. // 8-bit pixel format, all bits red.
  13. R8 = 1,
  14. // 2 byte pixel format, 1 byte red, 1 byte green.
  15. R8G8 = 2,
  16. // 24-bit pixel format, 8 bits for red, green and blue.
  17. R8G8B8 = 3,
  18. // 32-bit pixel format, 8 bits for red, green, blue and alpha.
  19. R8G8B8A8 = 8,
  20. // DXT1/BC1 format containing opaque RGB or 1-bit alpha RGB. 4 bits per pixel.
  21. BC1 = 13,
  22. // DXT3/BC2 format containing RGB with premultiplied alpha. 4 bits per pixel.
  23. BC1a = 14,
  24. // DXT3/BC2 format containing RGB with explicit alpha. 8 bits per pixel.
  25. BC2 = 15,
  26. // DXT5/BC2 format containing RGB with explicit alpha. 8 bits per pixel. Better alpha gradients than BC2.
  27. BC3 = 16,
  28. // One channel compressed format. 4 bits per pixel.
  29. BC4 = 17,
  30. // Two channel compressed format. 8 bits per pixel.
  31. BC5 = 18,
  32. // Format storing RGB in half (16-bit) floating point format usable for HDR. 8 bits per pixel.
  33. BC6H = 19,
  34. // Format storing RGB with optional alpha channel. Similar to BC1/BC2/BC3 formats but with higher quality and higher decompress overhead. 8 bits per pixel.
  35. BC7 = 20,
  36. // 16-bit pixel format, 16 bits (float) for red
  37. Float16_R = 21,
  38. // 32-bit, 2-channel s10e5 floating point pixel format, 16-bit red, 16-bit green
  39. Float16_RG = 22,
  40. // 48-bit pixel format, 16 bits (float) for red, 16 bits (float) for green, 16 bits (float) for blue
  41. Float16_RGB = 23,
  42. // 64-bit pixel format, 16 bits (float) for red, 16 bits (float) for green, 16 bits (float) for blue, 16 bits (float) for alpha
  43. Float16_RGBA = 24,
  44. // 32-bit pixel format, 32 bits (float) for red
  45. Float32_R = 25,
  46. // 64-bit, 2-channel floating point pixel format, 32-bit red, 32-bit green
  47. Float32_RG = 26,
  48. // 96-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue
  49. Float32_RGB = 27,
  50. // 128-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue, 32 bits (float) for alpha
  51. Float32_RGBA = 28,
  52. // Depth stencil format, 32bit depth, 8bit stencil + 24 unused
  53. D32_S8X24 = 29,
  54. // Depth stencil fomrat, 24bit depth + 8bit stencil
  55. D24S8 = 30,
  56. // Depth format, 32bits
  57. D32 = 31,
  58. // Depth format, 16bits
  59. D16 = 32
  60. };
  61. public sealed class PixelData : ScriptObject
  62. {
  63. public PixelVolume Extents
  64. {
  65. get
  66. {
  67. PixelVolume volume;
  68. Internal_GetExtents(mCachedPtr, out volume);
  69. return volume;
  70. }
  71. }
  72. public PixelFormat Format
  73. {
  74. get
  75. {
  76. PixelFormat format;
  77. Internal_GetFormat(mCachedPtr, out format);
  78. return format;
  79. }
  80. }
  81. public int RawRowPitch
  82. {
  83. get
  84. {
  85. int rowPitch;
  86. Internal_GetRowPitch(mCachedPtr, out rowPitch);
  87. return rowPitch;
  88. }
  89. }
  90. public int RawSlicePitch
  91. {
  92. get
  93. {
  94. int slicePitch;
  95. Internal_GetSlicePitch(mCachedPtr, out slicePitch);
  96. return slicePitch;
  97. }
  98. }
  99. public int RawSize
  100. {
  101. get
  102. {
  103. int size;
  104. Internal_GetSize(mCachedPtr, out size);
  105. return size;
  106. }
  107. }
  108. public bool RawIsConsecutive
  109. {
  110. get
  111. {
  112. bool isConsecutive;
  113. Internal_GetIsConsecutive(mCachedPtr, out isConsecutive);
  114. return isConsecutive;
  115. }
  116. }
  117. // Only for use by native code
  118. private PixelData()
  119. { }
  120. public PixelData(PixelVolume volume, PixelFormat format)
  121. {
  122. Internal_CreateInstance(this, volume, format);
  123. }
  124. public PixelData(int width, int height, PixelFormat format)
  125. {
  126. Internal_CreateInstance(this, new PixelVolume(0, 0, width, height), format);
  127. }
  128. public PixelData(int width, int height, int depth, PixelFormat format)
  129. {
  130. Internal_CreateInstance(this, new PixelVolume(0, 0, 0, width, height, depth), format);
  131. }
  132. public Color GetPixel(int x, int y, int z = 0)
  133. {
  134. Color pixel;
  135. Internal_GetPixel(mCachedPtr, x, y, z, out pixel);
  136. return pixel;
  137. }
  138. public void SetPixel(Color color, int x, int y, int z = 0)
  139. {
  140. Internal_SetPixel(mCachedPtr, x, y, z, color);
  141. }
  142. public Color[] GetPixels()
  143. {
  144. Color[] pixels;
  145. Internal_GetPixels(mCachedPtr, out pixels);
  146. return pixels;
  147. }
  148. public void SetPixels(Color[] pixels)
  149. {
  150. Internal_SetPixels(mCachedPtr, pixels);
  151. }
  152. public byte[] GetRawPixels()
  153. {
  154. byte[] pixels;
  155. Internal_GetRawPixels(mCachedPtr, out pixels);
  156. return pixels;
  157. }
  158. public void SetRawPixels(byte[] pixels)
  159. {
  160. Internal_SetRawPixels(mCachedPtr, pixels);
  161. }
  162. [MethodImpl(MethodImplOptions.InternalCall)]
  163. private static extern void Internal_CreateInstance(PixelData instance, PixelVolume volume, PixelFormat format);
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. private static extern void Internal_GetPixel(IntPtr thisPtr, int x, int y, int z, out Color value);
  166. [MethodImpl(MethodImplOptions.InternalCall)]
  167. private static extern void Internal_SetPixel(IntPtr thisPtr, int x, int y, int z, Color value);
  168. [MethodImpl(MethodImplOptions.InternalCall)]
  169. private static extern void Internal_GetPixels(IntPtr thisPtr, out Color[] value);
  170. [MethodImpl(MethodImplOptions.InternalCall)]
  171. private static extern void Internal_SetPixels(IntPtr thisPtr, Color[] value);
  172. [MethodImpl(MethodImplOptions.InternalCall)]
  173. private static extern void Internal_GetRawPixels(IntPtr thisPtr, out byte[] value);
  174. [MethodImpl(MethodImplOptions.InternalCall)]
  175. private static extern void Internal_SetRawPixels(IntPtr thisPtr, byte[] value);
  176. [MethodImpl(MethodImplOptions.InternalCall)]
  177. private static extern void Internal_GetExtents(IntPtr thisPtr, out PixelVolume value);
  178. [MethodImpl(MethodImplOptions.InternalCall)]
  179. private static extern void Internal_GetFormat(IntPtr thisPtr, out PixelFormat value);
  180. [MethodImpl(MethodImplOptions.InternalCall)]
  181. private static extern void Internal_GetRowPitch(IntPtr thisPtr, out int value);
  182. [MethodImpl(MethodImplOptions.InternalCall)]
  183. private static extern void Internal_GetSlicePitch(IntPtr thisPtr, out int value);
  184. [MethodImpl(MethodImplOptions.InternalCall)]
  185. private static extern void Internal_GetSize(IntPtr thisPtr, out int value);
  186. [MethodImpl(MethodImplOptions.InternalCall)]
  187. private static extern void Internal_GetIsConsecutive(IntPtr thisPtr, out bool value);
  188. }
  189. [StructLayout(LayoutKind.Sequential)]
  190. public struct PixelVolume
  191. {
  192. private int left, top, right, bottom, front, back;
  193. public int Left { get { return left; } }
  194. public int Right { get { return right; } }
  195. public int Top { get { return top; } }
  196. public int Bottom { get { return bottom; } }
  197. public int Front { get { return front; } }
  198. public int Back { get { return back; } }
  199. public int Width { get { return right - left; } }
  200. public int Height { get { return bottom - top; } }
  201. public int Depth { get { return back - front; } }
  202. public PixelVolume(int left, int top, int right, int bottom)
  203. {
  204. this.left = left;
  205. this.right = right;
  206. this.top = top;
  207. this.bottom = bottom;
  208. this.front = 0;
  209. this.back = 1;
  210. }
  211. public PixelVolume(int left, int top, int front, int right, int bottom, int back)
  212. {
  213. this.left = left;
  214. this.right = right;
  215. this.top = top;
  216. this.bottom = bottom;
  217. this.front = front;
  218. this.back = back;
  219. }
  220. };
  221. }