PixelUtility.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Utility
  8. * @{
  9. */
  10. /// <summary>
  11. /// Utility methods for converting and managing pixel data and formats.
  12. /// </summary>
  13. public static class PixelUtility
  14. {
  15. /// <summary>
  16. /// Returns the size of the memory region required to hold pixels of the provided size ana format.
  17. /// </summary>
  18. /// <param name="width">Number of pixels in each row.</param>
  19. /// <param name="height">Number of pixels in each column.</param>
  20. /// <param name="depth">Number of 2D slices.</param>
  21. /// <param name="format">Format of individual pixels.</param>
  22. /// <returns>Size of the memory region in bytes.</returns>
  23. public static int GetMemorySize(int width, int height, int depth, PixelFormat format)
  24. {
  25. int value;
  26. Internal_GetMemorySize(width, height, depth, format, out value);
  27. return value;
  28. }
  29. /// <summary>
  30. /// Checks if the provided pixel format has an alpha channel.
  31. /// </summary>
  32. /// <param name="format">Format to check.</param>
  33. /// <returns>True if the format contains an alpha channel.</returns>
  34. public static bool HasAlpha(PixelFormat format)
  35. {
  36. bool value;
  37. Internal_HasAlpha(format, out value);
  38. return value;
  39. }
  40. /// <summary>
  41. /// Checks is the provided pixel format a floating point format.
  42. /// </summary>
  43. /// <param name="format">Format to check.</param>
  44. /// <returns>True if the format contains floating point values.</returns>
  45. public static bool IsFloatingPoint(PixelFormat format)
  46. {
  47. bool value;
  48. Internal_IsFloatingPoint(format, out value);
  49. return value;
  50. }
  51. /// <summary>
  52. /// Checks is the provided pixel format contains compressed data.
  53. /// </summary>
  54. /// <param name="format">Format to check.</param>
  55. /// <returns>True if the format contains compressed data.</returns>
  56. public static bool IsCompressed(PixelFormat format)
  57. {
  58. bool value;
  59. Internal_IsCompressed(format, out value);
  60. return value;
  61. }
  62. /// <summary>
  63. /// Checks is the provided pixel format a depth/stencil buffer format.
  64. /// </summary>
  65. /// <param name="format">Format to check.</param>
  66. /// <returns>True if the format is a depth/stencil buffer format.</returns>
  67. public static bool IsDepth(PixelFormat format)
  68. {
  69. bool value;
  70. Internal_IsDepth(format, out value);
  71. return value;
  72. }
  73. /// <summary>
  74. /// Returns the maximum number of mip maps that can be generated until we reachthe minimum size possible. This does
  75. /// not count the base level.
  76. /// </summary>
  77. /// <param name="width">Number of pixels in each row.</param>
  78. /// <param name="height">Number of pixels in each column.</param>
  79. /// <param name="depth">Number of 2D slices.</param>
  80. /// <param name="format">Format of individual pixels.</param>
  81. /// <returns>Possible number of mip-maps not counting the base level.</returns>
  82. public static int GetMaxMipmaps(int width, int height, int depth, PixelFormat format)
  83. {
  84. int value;
  85. Internal_GetMaxMipmaps(width, height, depth, format, out value);
  86. return value;
  87. }
  88. /// <summary>
  89. /// Converts a set of pixels from one format to another.
  90. /// </summary>
  91. /// <param name="source">Pixels to convert.</param>
  92. /// <param name="newFormat">New pixel format.</param>
  93. /// <returns>New pixel data object containing the converted pixels.</returns>
  94. public static PixelData ConvertFormat(PixelData source, PixelFormat newFormat)
  95. {
  96. return Internal_ConvertFormat(source, newFormat);
  97. }
  98. /// <summary>
  99. /// Compresses the provided pixels using the specified compression options.
  100. /// </summary>
  101. /// <param name="source">Pixels to compress.</param>
  102. /// <param name="options">Options to control the compression. Make sure the format contained within is a
  103. /// compressed format.</param>
  104. /// <returns>New pixel data object containing the compressed pixels.</returns>
  105. public static PixelData Compress(PixelData source, CompressionOptions options)
  106. {
  107. return Internal_Compress(source, ref options);
  108. }
  109. /// <summary>
  110. /// Generates mip-maps from the provided source data using the specified compression options. Returned list includes
  111. /// the base level.
  112. /// </summary>
  113. /// <param name="source">Pixels to generate mip-maps for.</param>
  114. /// <param name="options">Options controlling mip-map generation.</param>
  115. /// <returns>A list of calculated mip-map data. First entry is the largest mip and other follow in order from
  116. /// largest to smallest.</returns>
  117. public static PixelData[] GenerateMipmaps(PixelData source, MipMapGenOptions options)
  118. {
  119. return Internal_GenerateMipmaps(source, ref options);
  120. }
  121. /// <summary>
  122. /// Scales pixel data in the source buffer and stores the scaled data in the destination buffer.
  123. /// </summary>
  124. /// <param name="source">Source pixels to scale.</param>
  125. /// <param name="newSize">New dimensions to scale to.</param>
  126. /// <param name="filter">Filter to use when scaling.</param>
  127. /// <returns>New pixel data object containing the scaled pixels.</returns>
  128. public static PixelData Scale(PixelData source, PixelVolume newSize, ScaleFilter filter = ScaleFilter.Linear)
  129. {
  130. return Internal_Scale(source, ref newSize, filter);
  131. }
  132. /// <summary>
  133. /// Converts pixel data in linear space to one in sRGB space. Only converts the RGB components.
  134. /// </summary>
  135. /// <param name="source">Pixels to convert.</param>
  136. public static void LinearToSRGB(PixelData source)
  137. {
  138. Internal_LinearToSRGB(source);
  139. }
  140. /// <summary>
  141. /// Converts pixel data in sRGB space to one in linear space. Only converts the RGB components.
  142. /// </summary>
  143. /// <param name="source">Pixels to convert.</param>
  144. public static void SRGBToLinear(PixelData source)
  145. {
  146. Internal_SRGBToLinear(source);
  147. }
  148. [MethodImpl(MethodImplOptions.InternalCall)]
  149. private static extern void Internal_GetMemorySize(int width, int height, int depth, PixelFormat format, out int value);
  150. [MethodImpl(MethodImplOptions.InternalCall)]
  151. private static extern void Internal_HasAlpha(PixelFormat format, out bool value);
  152. [MethodImpl(MethodImplOptions.InternalCall)]
  153. private static extern void Internal_IsFloatingPoint(PixelFormat format, out bool value);
  154. [MethodImpl(MethodImplOptions.InternalCall)]
  155. private static extern void Internal_IsCompressed(PixelFormat format, out bool value);
  156. [MethodImpl(MethodImplOptions.InternalCall)]
  157. private static extern void Internal_IsDepth(PixelFormat format, out bool value);
  158. [MethodImpl(MethodImplOptions.InternalCall)]
  159. private static extern void Internal_GetMaxMipmaps(int width, int height, int depth, PixelFormat format, out int value);
  160. [MethodImpl(MethodImplOptions.InternalCall)]
  161. private static extern PixelData Internal_ConvertFormat(PixelData source, PixelFormat newFormat);
  162. [MethodImpl(MethodImplOptions.InternalCall)]
  163. private static extern PixelData Internal_Compress(PixelData source, ref CompressionOptions options);
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. private static extern PixelData[] Internal_GenerateMipmaps(PixelData source, ref MipMapGenOptions options);
  166. [MethodImpl(MethodImplOptions.InternalCall)]
  167. private static extern PixelData Internal_Scale(PixelData source, ref PixelVolume newSize, ScaleFilter filter);
  168. [MethodImpl(MethodImplOptions.InternalCall)]
  169. private static extern void Internal_LinearToSRGB(PixelData source);
  170. [MethodImpl(MethodImplOptions.InternalCall)]
  171. private static extern void Internal_SRGBToLinear(PixelData source);
  172. }
  173. /// <summary>
  174. /// Filtering types to use when scaling images.
  175. /// </summary>
  176. public enum ScaleFilter // Note: Must match the C++ enum PixelUtil::Filter
  177. {
  178. /// <summary>
  179. /// No filtering is performed and nearest existing value is used.
  180. /// </summary>
  181. Nearest,
  182. /// <summary>
  183. /// Box filter is applied, averaging nearby pixels.
  184. /// </summary>
  185. Linear
  186. };
  187. /// <summary>
  188. /// Types of texture compression quality.
  189. /// </summary>
  190. public enum CompressionQuality // Note: Must match the C++ enum CompressionQuality
  191. {
  192. Fastest,
  193. Normal,
  194. Production,
  195. Highest
  196. };
  197. /// <summary>
  198. /// Mode of the alpha channel in a texture.
  199. /// </summary>
  200. public enum AlphaMode // Note: Must match the C++ enum AlphaMode
  201. {
  202. /// <summary>
  203. /// Texture has no alpha values.
  204. /// </summary>
  205. None,
  206. /// <summary>
  207. /// Alpha is in the separate transparency channel.
  208. /// </summary>
  209. Transparency,
  210. /// <summary>
  211. /// Alpha values have been pre-multiplied with the color values.
  212. /// </summary>
  213. Premultiplied
  214. };
  215. /// <summary>
  216. /// Wrap mode to use when generating mip maps.
  217. /// </summary>
  218. public enum MipMapWrapMode // Note: Must match the C++ enum MipMapWrapMode
  219. {
  220. Mirror,
  221. Repeat,
  222. Clamp
  223. };
  224. /// <summary>
  225. /// Filter to use when generating mip maps.
  226. /// </summary>
  227. public enum MipMapFilter // Note: Must match the C++ enum MipMapFilter
  228. {
  229. Box,
  230. Triangle,
  231. Kaiser
  232. };
  233. /// <summary>
  234. /// Options used to control texture compression.
  235. /// </summary>
  236. [StructLayout(LayoutKind.Sequential)]
  237. public struct CompressionOptions // Note: Must match the C++ struct CompressionOptions
  238. {
  239. /// <summary>
  240. /// Format to compress to. Must be a format containing compressed data.
  241. /// </summary>
  242. public PixelFormat format;
  243. /// <summary>
  244. /// Controls how to (and if) to compress the alpha channel.
  245. /// </summary>
  246. public AlphaMode alphaMode;
  247. /// <summary>
  248. /// Determines does the input data represent a normal map.
  249. /// </summary>
  250. public bool isNormalMap;
  251. /// <summary>
  252. /// Determines has the input data been gamma corrected.
  253. /// </summary>
  254. public bool isSRGB;
  255. /// <summary>
  256. /// Compressed image quality. Better compression might take longer to execute but will generate better results.
  257. /// </summary>
  258. public CompressionQuality quality;
  259. };
  260. /// <summary>
  261. /// Options used to control texture mip map generation.
  262. /// </summary>
  263. [StructLayout(LayoutKind.Sequential)]
  264. public struct MipMapGenOptions // Note: Must match the C++ struct MipMapGenOptions
  265. {
  266. /// <summary>
  267. /// Filter to use when downsamping input data.
  268. /// </summary>
  269. public MipMapFilter filter;
  270. /// <summary>
  271. /// Determines how to downsample pixels on borders.
  272. /// </summary>
  273. public MipMapWrapMode wrapMode;
  274. /// <summary>
  275. /// Determines does the input data represent a normal map.
  276. /// </summary>
  277. public bool isNormalMap;
  278. /// <summary>
  279. /// Should the downsampled values be re-normalized. Only relevant for mip-maps representing normal maps.
  280. /// </summary>
  281. public bool normalizeMipmaps;
  282. /// <summary>
  283. /// Determines has the input data been gamma corrected.
  284. /// </summary>
  285. bool isSRGB;
  286. };
  287. /** @} */
  288. }