BsPixelUtil.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsCorePrerequisites.h"
  6. #include "BsPixelData.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Available types of texture compression quality.
  11. */
  12. enum class CompressionQuality
  13. {
  14. Fastest,
  15. Normal,
  16. Production,
  17. Highest
  18. };
  19. /**
  20. * @brief Specifies what is alpha channel used for in the texture.
  21. */
  22. enum class AlphaMode
  23. {
  24. None,
  25. Transparency,
  26. Premultiplied
  27. };
  28. /**
  29. * @brief Wrap mode to use when generating mip maps.
  30. */
  31. enum class MipMapWrapMode
  32. {
  33. Mirror,
  34. Repeat,
  35. Clamp
  36. };
  37. /**
  38. * @brief Filter to use when generating mip maps.
  39. */
  40. enum class MipMapFilter
  41. {
  42. Box,
  43. Triangle,
  44. Kaiser
  45. };
  46. /**
  47. * @brief Options used to control texture compression.
  48. */
  49. struct CompressionOptions
  50. {
  51. PixelFormat format = PF_BC1;
  52. AlphaMode alphaMode = AlphaMode::None;
  53. bool isNormalMap = false;
  54. bool isSRGB = false;
  55. CompressionQuality quality = CompressionQuality::Normal;
  56. };
  57. /**
  58. * @brief Options used to control texture mip map generation.
  59. */
  60. struct MipMapGenOptions
  61. {
  62. MipMapFilter filter = MipMapFilter::Box;
  63. MipMapWrapMode wrapMode = MipMapWrapMode::Mirror;
  64. bool isNormalMap = false;
  65. bool normalizeMipmaps = false;
  66. };
  67. /**
  68. * @brief Utility methods for converting and managing pixel data and formats.
  69. */
  70. class BS_CORE_EXPORT PixelUtil
  71. {
  72. public:
  73. /**
  74. * @brief A list of filtering types to use when scaling images.
  75. */
  76. enum Filter
  77. {
  78. FILTER_NEAREST,
  79. FILTER_LINEAR
  80. };
  81. /**
  82. * @brief Returns the size of a single pixel of the provided pixel format,
  83. * in bytes.
  84. */
  85. static UINT32 getNumElemBytes(PixelFormat format);
  86. /**
  87. * @brief Returns the size of a single pixel of the provided pixel format,
  88. * in bits.
  89. */
  90. static UINT32 getNumElemBits( PixelFormat format );
  91. /**
  92. * @brief Returns the size of the memory region of the provided size and the pixel format.
  93. */
  94. static UINT32 getMemorySize(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format);
  95. /**
  96. * @brief Returns property flags for this pixel format.
  97. *
  98. * @see PixelFormatFlags
  99. */
  100. static UINT32 getFlags(PixelFormat format);
  101. /**
  102. * @brief Checks if the provided pixel format has an alpha channel.
  103. */
  104. static bool hasAlpha(PixelFormat format);
  105. /**
  106. * @brief Checks is the provided pixel format a floating point format.
  107. */
  108. static bool isFloatingPoint(PixelFormat format);
  109. /**
  110. * @brief Checks is the provided pixel format compressed.
  111. */
  112. static bool isCompressed(PixelFormat format);
  113. /**
  114. * @brief Checks is the provided pixel format a depth/stencil buffer format.
  115. */
  116. static bool isDepth(PixelFormat format);
  117. /**
  118. * @brief Checks is the provided format in native endian format.
  119. */
  120. static bool isNativeEndian(PixelFormat format);
  121. /**
  122. * @brief Checks are the provided dimensions valid for the specified pixel format.
  123. * Some formats (like DXT) require width/height to be multiples of four and some
  124. * formats dont allow depth larger than 1.
  125. */
  126. static bool isValidExtent(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format);
  127. /**
  128. * @brief Returns the number of bits per each element in the provided pixel format.
  129. * This will return all zero for compressed and depth/stencil formats.
  130. */
  131. static void getBitDepths(PixelFormat format, int rgba[4]);
  132. /**
  133. * @brief Returns bit masks that determine in what bit range is each channel stored.
  134. *
  135. * @note e.g. if your color is stored in an UINT32 and you want to extract the red channel
  136. * you should AND the color UINT32 with the bit-mask for the red channel and then
  137. * right shift it by the red channel bit shift amount.
  138. */
  139. static void getBitMasks(PixelFormat format, UINT32 rgba[4]);
  140. /**
  141. * @brief Returns number of bits you need to shift a pixel element in order
  142. * to move it to the start of the data type.
  143. *
  144. * @note e.g. if your color is stored in an UINT32 and you want to extract the red channel
  145. * you should AND the color UINT32 with the bit-mask for the red channel and then
  146. * right shift it by the red channel bit shift amount.
  147. */
  148. static void getBitShifts(PixelFormat format, UINT8 rgba[4]);
  149. /**
  150. * @brief Returns the name of the pixel format.
  151. */
  152. static String getFormatName(PixelFormat srcformat);
  153. /**
  154. * @brief Returns true if the pixel data in the format can be directly accessed and read.
  155. * This is generally not true for compressed formats.
  156. */
  157. static bool isAccessible(PixelFormat srcformat);
  158. /**
  159. * @brief Returns the type of an individual pixel element in the provided format.
  160. */
  161. static PixelComponentType getElementType(PixelFormat format);
  162. /**
  163. * @brief Returns the number of pixel elements in the provided format.
  164. */
  165. static UINT32 getNumElements(PixelFormat format);
  166. /**
  167. * @brief Returns the maximum number of mip maps that can be generated until we reach
  168. * the minimum size possible. This does not count the base level.
  169. */
  170. static UINT32 getMaxMipmaps(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format);
  171. /**
  172. * @brief Writes the color to the provided memory location.
  173. */
  174. static void packColor(const Color& color, PixelFormat format, void* dest);
  175. /**
  176. * @brief Writes the color to the provided memory location. If the destination
  177. * format is floating point, the byte values will be converted into [0.0, 1.0] range.
  178. */
  179. static void packColor(UINT8 r, UINT8 g, UINT8 b, UINT8 a, PixelFormat format, void* dest);
  180. /**
  181. * @brief Writes the color to the provided memory location. If the destination format
  182. * in non-floating point, the float values will be assumed to be in [0.0, 1.0] which
  183. * will be converted to integer range. ([0, 255] in the case of bytes)
  184. */
  185. static void packColor(float r, float g, float b, float a, const PixelFormat format, void* dest);
  186. /**
  187. * @brief Reads the color from the provided memory location and stores it
  188. * into the provided color object.
  189. */
  190. static void unpackColor(Color* color, PixelFormat format, const void* src);
  191. /**
  192. * @brief Reads the color from the provided memory location and stores it
  193. * into the provided color elements, as bytes clamped to [0, 255] range.
  194. */
  195. static void unpackColor(UINT8* r, UINT8* g, UINT8* b, UINT8* a, PixelFormat format, const void* src);
  196. /**
  197. * @brief Reads the color from the provided memory location and stores it
  198. * into the provided color elements. If the format is not natively floating
  199. * point a conversion is done in such a way that returned values range [0.0, 1.0].
  200. */
  201. static void unpackColor(float* r, float* g, float* b, float* a, PixelFormat format, const void* src);
  202. /**
  203. * @brief Converts pixels from one format to another. Provided pixel data objects
  204. * must have previously allocated buffers of adequate size and their sizes must match.
  205. */
  206. static void bulkPixelConversion(const PixelData& src, PixelData& dst);
  207. /**
  208. * @brief Compresses the provided data using the specified compression options.
  209. */
  210. static void compress(const PixelData& src, PixelData& dst, const CompressionOptions& options);
  211. /**
  212. * @brief Generates mip-maps from the provided source data using the specified compression options.
  213. * Returned list includes the base level.
  214. *
  215. * @returns A list of calculated mip-map data. First entry is the largest mip and other follow in
  216. * order from largest to smallest.
  217. */
  218. static Vector<PixelDataPtr> genMipmaps(const PixelData& src, const MipMapGenOptions& options);
  219. /**
  220. * @brief Scales pixel data in the source buffer and stores the scaled data in the destination buffer.
  221. * Provided pixel data objects must have previously allocated buffers of adequate size. You may
  222. * also provided a filtering method to use when scaling.
  223. */
  224. static void scale(const PixelData& src, PixelData& dst, Filter filter = FILTER_LINEAR);
  225. /**
  226. * @brief Applies gamma correction to the pixels in the provided buffer.
  227. *
  228. * @param buffer Pointer to the buffer containing the pixels.
  229. * @param gamma Gamma value to apply.
  230. * @param size Size of the buffer in bytes.
  231. * @param bpp Number of bits per pixel of the pixels in the buffer.
  232. */
  233. static void applyGamma(UINT8* buffer, float gamma, UINT32 size, UINT8 bpp);
  234. };
  235. }