BsPixelUtil.h 8.5 KB

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