PixelUtility.cs 12 KB

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