PixelUtility.cs 11 KB

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