BsPixelUtil.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsPixelData.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Utility-Core
  9. * @{
  10. */
  11. /** Types of texture compression quality. */
  12. enum class CompressionQuality
  13. {
  14. Fastest,
  15. Normal,
  16. Production,
  17. Highest
  18. };
  19. /** Mode of the alpha channel in a texture. */
  20. enum class AlphaMode
  21. {
  22. None, /*< Texture has no alpha values. */
  23. Transparency, /*< Alpha is in the separate transparency channel. */
  24. Premultiplied /*< Alpha values have been pre-multiplied with the color values. */
  25. };
  26. /** Wrap mode to use when generating mip maps. */
  27. enum class MipMapWrapMode
  28. {
  29. Mirror,
  30. Repeat,
  31. Clamp
  32. };
  33. /** Filter to use when generating mip maps. */
  34. enum class MipMapFilter
  35. {
  36. Box,
  37. Triangle,
  38. Kaiser
  39. };
  40. /** Options used to control texture compression. */
  41. struct CompressionOptions
  42. {
  43. PixelFormat format = PF_BC1; /*< Format to compress to. Must be a format containing compressed data. */
  44. AlphaMode alphaMode = AlphaMode::None; /*< Controls how to (and if) to compress the alpha channel. */
  45. bool isNormalMap = false; /*< Determines does the input data represent a normal map. */
  46. bool isSRGB = false; /*< Determines has the input data been gamma corrected. */
  47. CompressionQuality quality = CompressionQuality::Normal; /*< Compressed image quality. Better compression might take longer to execute but will generate better results. */
  48. };
  49. /** Options used to control texture mip map generation. */
  50. struct MipMapGenOptions
  51. {
  52. MipMapFilter filter = MipMapFilter::Box; /*< Filter to use when downsamping input data. */
  53. MipMapWrapMode wrapMode = MipMapWrapMode::Mirror; /*< Determines how to downsample pixels on borders. */
  54. bool isNormalMap = false; /*< Determines does the input data represent a normal map. */
  55. bool normalizeMipmaps = false; /*< Should the downsampled values be re-normalized. Only relevant for mip-maps representing normal maps. */
  56. };
  57. /** Utility methods for converting and managing pixel data and formats. */
  58. class BS_CORE_EXPORT PixelUtil
  59. {
  60. public:
  61. /** Filtering types to use when scaling images. */
  62. enum Filter
  63. {
  64. FILTER_NEAREST, /*< No filtering is performed and nearest existing value is used. */
  65. FILTER_LINEAR /*< Box filter is applied, averaging nearby pixels. */
  66. };
  67. /** Returns the size of a single pixel of the provided pixel format, in bytes. */
  68. static UINT32 getNumElemBytes(PixelFormat format);
  69. /** Returns the size of a single pixel of the provided pixel format, in bits. */
  70. static UINT32 getNumElemBits( PixelFormat format );
  71. /** Returns the size of the memory region required to hold pixels of the provided size ana format. */
  72. static UINT32 getMemorySize(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format);
  73. /** Calculates the size of a mip level of a texture with the provided size. */
  74. static void getSizeForMipLevel(UINT32 width, UINT32 height, UINT32 depth, UINT32 mipLevel,
  75. UINT32& mipWidth, UINT32& mipHeight, UINT32& mipDepth);
  76. /**
  77. * Returns property flags for this pixel format.
  78. *
  79. * @see PixelFormatFlags
  80. */
  81. static UINT32 getFlags(PixelFormat format);
  82. /** Checks if the provided pixel format has an alpha channel. */
  83. static bool hasAlpha(PixelFormat format);
  84. /** Checks is the provided pixel format a floating point format. */
  85. static bool isFloatingPoint(PixelFormat format);
  86. /** Checks is the provided pixel format compressed. */
  87. static bool isCompressed(PixelFormat format);
  88. /** Checks is the provided pixel format a depth/stencil buffer format. */
  89. static bool isDepth(PixelFormat format);
  90. /** Checks is the provided format in native endian format. */
  91. static bool isNativeEndian(PixelFormat format);
  92. /**
  93. * Checks are the provided dimensions valid for the specified pixel format. Some formats (like DXT) require
  94. * width/height to be multiples of four and some formats dont allow depth larger than 1.
  95. */
  96. static bool isValidExtent(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format);
  97. /**
  98. * Returns the number of bits per each element in the provided pixel format. This will return all zero for
  99. * compressed and depth/stencil formats.
  100. */
  101. static void getBitDepths(PixelFormat format, int rgba[4]);
  102. /**
  103. * Returns bit masks that determine in what bit range is each channel stored.
  104. *
  105. * @note
  106. * For example if your color is stored in an UINT32 and you want to extract the red channel you should AND the color
  107. * UINT32 with the bit-mask for the red channel and then right shift it by the red channel bit shift amount.
  108. */
  109. static void getBitMasks(PixelFormat format, UINT32 rgba[4]);
  110. /**
  111. * Returns number of bits you need to shift a pixel element in order to move it to the start of the data type.
  112. *
  113. * @note
  114. * For example if your color is stored in an UINT32 and you want to extract the red channel you should AND the color
  115. * UINT32 with the bit-mask for the red channel and then right shift it by the red channel bit shift amount.
  116. */
  117. static void getBitShifts(PixelFormat format, UINT8 rgba[4]);
  118. /** Returns the name of the pixel format. */
  119. static String getFormatName(PixelFormat srcformat);
  120. /**
  121. * Returns true if the pixel data in the format can be directly accessed and read. This is generally not true
  122. * for compressed formats.
  123. */
  124. static bool isAccessible(PixelFormat srcformat);
  125. /** Returns the type of an individual pixel element in the provided format. */
  126. static PixelComponentType getElementType(PixelFormat format);
  127. /** Returns the number of pixel elements in the provided format. */
  128. static UINT32 getNumElements(PixelFormat format);
  129. /**
  130. * Returns the maximum number of mip maps that can be generated until we reach the minimum size possible. This
  131. * does not count the base level.
  132. */
  133. static UINT32 getMaxMipmaps(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format);
  134. /** Writes the color to the provided memory location. */
  135. static void packColor(const Color& color, PixelFormat format, void* dest);
  136. /**
  137. * Writes the color to the provided memory location. If the destination format is floating point, the byte values
  138. * will be converted into [0.0, 1.0] range.
  139. */
  140. static void packColor(UINT8 r, UINT8 g, UINT8 b, UINT8 a, PixelFormat format, void* dest);
  141. /**
  142. * Writes the color to the provided memory location. If the destination format in non-floating point, the float
  143. * values will be assumed to be in [0.0, 1.0] which will be converted to integer range. ([0, 255] in the case of bytes)
  144. */
  145. static void packColor(float r, float g, float b, float a, const PixelFormat format, void* dest);
  146. /** Reads the color from the provided memory location and stores it into the provided color object. */
  147. static void unpackColor(Color* color, PixelFormat format, const void* src);
  148. /**
  149. * Reads the color from the provided memory location and stores it into the provided color elements, as bytes
  150. * clamped to [0, 255] range.
  151. */
  152. static void unpackColor(UINT8* r, UINT8* g, UINT8* b, UINT8* a, PixelFormat format, const void* src);
  153. /**
  154. * Reads the color from the provided memory location and stores it into the provided color elements. If the format
  155. * is not natively floating point a conversion is done in such a way that returned values range [0.0, 1.0].
  156. */
  157. static void unpackColor(float* r, float* g, float* b, float* a, PixelFormat format, const void* src);
  158. /**
  159. * Converts pixels from one format to another. Provided pixel data objects must have previously allocated buffers
  160. * of adequate size and their sizes must match.
  161. */
  162. static void bulkPixelConversion(const PixelData& src, PixelData& dst);
  163. /** Compresses the provided data using the specified compression options. */
  164. static void compress(const PixelData& src, PixelData& dst, const CompressionOptions& options);
  165. /**
  166. * Generates mip-maps from the provided source data using the specified compression options. Returned list includes
  167. * the base level.
  168. *
  169. * @return A list of calculated mip-map data. First entry is the largest mip and other follow in order from
  170. * largest to smallest.
  171. */
  172. static Vector<SPtr<PixelData>> genMipmaps(const PixelData& src, const MipMapGenOptions& options);
  173. /**
  174. * Scales pixel data in the source buffer and stores the scaled data in the destination buffer. Provided pixel data
  175. * objects must have previously allocated buffers of adequate size. You may also provided a filtering method to use
  176. * when scaling.
  177. */
  178. static void scale(const PixelData& src, PixelData& dst, Filter filter = FILTER_LINEAR);
  179. /**
  180. * Applies gamma correction to the pixels in the provided buffer.
  181. *
  182. * @param[in] buffer Pointer to the buffer containing the pixels.
  183. * @param[in] gamma Gamma value to apply.
  184. * @param[in] size Size of the buffer in bytes.
  185. * @param[in] bpp Number of bits per pixel of the pixels in the buffer.
  186. */
  187. static void applyGamma(UINT8* buffer, float gamma, UINT32 size, UINT8 bpp);
  188. };
  189. /** @} */
  190. }