image.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #ifndef BGFX_IMAGE_H_HEADER_GUARD
  6. #define BGFX_IMAGE_H_HEADER_GUARD
  7. #include <stdint.h>
  8. namespace bgfx
  9. {
  10. struct ImageContainer
  11. {
  12. void* m_data;
  13. TextureFormat::Enum m_format;
  14. uint32_t m_size;
  15. uint32_t m_offset;
  16. uint32_t m_width;
  17. uint32_t m_height;
  18. uint32_t m_depth;
  19. uint8_t m_numMips;
  20. bool m_hasAlpha;
  21. bool m_cubeMap;
  22. bool m_ktx;
  23. bool m_ktxLE;
  24. bool m_srgb;
  25. };
  26. struct ImageMip
  27. {
  28. TextureFormat::Enum m_format;
  29. uint32_t m_width;
  30. uint32_t m_height;
  31. uint32_t m_blockSize;
  32. uint32_t m_size;
  33. uint8_t m_bpp;
  34. bool m_hasAlpha;
  35. const uint8_t* m_data;
  36. };
  37. struct EncodingType
  38. {
  39. enum Enum
  40. {
  41. Unorm,
  42. Int,
  43. Uint,
  44. Float,
  45. Snorm,
  46. Count
  47. };
  48. };
  49. struct ImageBlockInfo
  50. {
  51. uint8_t bitsPerPixel;
  52. uint8_t blockWidth;
  53. uint8_t blockHeight;
  54. uint8_t blockSize;
  55. uint8_t minBlockX;
  56. uint8_t minBlockY;
  57. uint8_t depthBits;
  58. uint8_t stencilBits;
  59. uint8_t encoding;
  60. };
  61. typedef void (*PackFn)(void*, const float*);
  62. typedef void (*UnpackFn)(float*, const void*);
  63. // R8
  64. void packR8(void* _dst, const float* _src);
  65. void unpackR8(float* _dst, const void* _src);
  66. // R8S
  67. void packR8S(void* _dst, const float* _src);
  68. void unpackR8S(float* _dst, const void* _src);
  69. // R8I
  70. void packR8I(void* _dst, const float* _src);
  71. void unpackR8I(float* _dst, const void* _src);
  72. // R8U
  73. void packR8U(void* _dst, const float* _src);
  74. void unpackR8U(float* _dst, const void* _src);
  75. // RG8
  76. void packRg8(void* _dst, const float* _src);
  77. void unpackRg8(float* _dst, const void* _src);
  78. // RG8S
  79. void packRg8S(void* _dst, const float* _src);
  80. void unpackRg8S(float* _dst, const void* _src);
  81. // RG8I
  82. void packRg8I(void* _dst, const float* _src);
  83. void unpackRg8I(float* _dst, const void* _src);
  84. // RG8U
  85. void packRg8U(void* _dst, const float* _src);
  86. void unpackRg8U(float* _dst, const void* _src);
  87. // RGB8
  88. void packRgb8(void* _dst, const float* _src);
  89. void unpackRgb8(float* _dst, const void* _src);
  90. // RGB8S
  91. void packRgb8S(void* _dst, const float* _src);
  92. void unpackRgb8S(float* _dst, const void* _src);
  93. // RGB8I
  94. void packRgb8I(void* _dst, const float* _src);
  95. void unpackRgb8I(float* _dst, const void* _src);
  96. // RGB8U
  97. void packRgb8U(void* _dst, const float* _src);
  98. void unpackRgb8U(float* _dst, const void* _src);
  99. // RGBA8
  100. void packRgba8(void* _dst, const float* _src);
  101. void unpackRgba8(float* _dst, const void* _src);
  102. // BGRA8
  103. void packBgra8(void* _dst, const float* _src);
  104. void unpackBgra8(float* _dst, const void* _src);
  105. // RGBA8S
  106. void packRgba8S(void* _dst, const float* _src);
  107. void unpackRgba8S(float* _dst, const void* _src);
  108. // RGBA8I
  109. void packRgba8I(void* _dst, const float* _src);
  110. void unpackRgba8I(float* _dst, const void* _src);
  111. // RGBA8U
  112. void packRgba8U(void* _dst, const float* _src);
  113. void unpackRgba8U(float* _dst, const void* _src);
  114. // R16
  115. void packR16(void* _dst, const float* _src);
  116. void unpackR16(float* _dst, const void* _src);
  117. // R16S
  118. void packR16S(void* _dst, const float* _src);
  119. void unpackR16S(float* _dst, const void* _src);
  120. // R16I
  121. void packR16I(void* _dst, const float* _src);
  122. void unpackR16I(float* _dst, const void* _src);
  123. // R16U
  124. void packR16U(void* _dst, const float* _src);
  125. void unpackR16U(float* _dst, const void* _src);
  126. // R16F
  127. void packR16F(void* _dst, const float* _src);
  128. void unpackR16F(float* _dst, const void* _src);
  129. // RG16
  130. void packRg16(void* _dst, const float* _src);
  131. void unpackRg16(float* _dst, const void* _src);
  132. // RG16S
  133. void packRg16S(void* _dst, const float* _src);
  134. void unpackRg16S(float* _dst, const void* _src);
  135. // RG16I
  136. void packRg16I(void* _dst, const float* _src);
  137. void unpackRg16I(float* _dst, const void* _src);
  138. // RG16U
  139. void packRg16U(void* _dst, const float* _src);
  140. void unpackRg16U(float* _dst, const void* _src);
  141. // RG16F
  142. void packRg16F(void* _dst, const float* _src);
  143. void unpackRg16F(float* _dst, const void* _src);
  144. // RGBA16
  145. void packRgba16(void* _dst, const float* _src);
  146. void unpackRgba16(float* _dst, const void* _src);
  147. // RGBA16S
  148. void packRgba16S(void* _dst, const float* _src);
  149. void unpackRgba16S(float* _dst, const void* _src);
  150. // RGBA16I
  151. void packRgba16I(void* _dst, const float* _src);
  152. void unpackRgba16I(float* _dst, const void* _src);
  153. // RGBA16U
  154. void packRgba16U(void* _dst, const float* _src);
  155. void unpackRgba16U(float* _dst, const void* _src);
  156. // RGBA16F
  157. void packRgba16F(void* _dst, const float* _src);
  158. void unpackRgba16F(float* _dst, const void* _src);
  159. // R32I
  160. void packR32I(void* _dst, const float* _src);
  161. void unpackR32I(float* _dst, const void* _src);
  162. // R32U
  163. void packR32U(void* _dst, const float* _src);
  164. void unpackR32U(float* _dst, const void* _src);
  165. // R32F
  166. void packR32F(void* _dst, const float* _src);
  167. void unpackR32F(float* _dst, const void* _src);
  168. // RG32I
  169. void packRg32I(void* _dst, const float* _src);
  170. void unpackRg32I(float* _dst, const void* _src);
  171. // RG32U
  172. void packRg32U(void* _dst, const float* _src);
  173. void unpackRg32U(float* _dst, const void* _src);
  174. // RGB9E5F
  175. void packRgb9E5F(void* _dst, const float* _src);
  176. void unpackRgb9E5F(float* _dst, const void* _src);
  177. // RGBA32I
  178. void packRgba32I(void* _dst, const float* _src);
  179. void unpackRgba32I(float* _dst, const void* _src);
  180. // RGBA32U
  181. void packRgba32U(void* _dst, const float* _src);
  182. void unpackRgba32U(float* _dst, const void* _src);
  183. // RGBA32F
  184. void packRgba32F(void* _dst, const float* _src);
  185. void unpackRgba32F(float* _dst, const void* _src);
  186. // R5G6B5
  187. void packR5G6B5(void* _dst, const float* _src);
  188. void unpackR5G6B5(float* _dst, const void* _src);
  189. // RGBA4
  190. void packRgba4(void* _dst, const float* _src);
  191. void unpackRgba4(float* _dst, const void* _src);
  192. // RGBA4
  193. void packBgra4(void* _dst, const float* _src);
  194. void unpackBgra4(float* _dst, const void* _src);
  195. // RGB5A1
  196. void packRgb5a1(void* _dst, const float* _src);
  197. void unpackRgb5a1(float* _dst, const void* _src);
  198. // BGR5A1
  199. void packBgr5a1(void* _dst, const float* _src);
  200. void unpackBgr5a1(float* _dst, const void* _src);
  201. // RGB10A2
  202. void packRgb10A2(void* _dst, const float* _src);
  203. void unpackRgb10A2(float* _dst, const void* _src);
  204. // R11G11B10F
  205. void packR11G11B10F(void* _dst, const float* _src);
  206. void unpackR11G11B10F(float* _dst, const void* _src);
  207. // RG32F
  208. void packRg32F(void* _dst, const float* _src);
  209. void unpackRg32F(float* _dst, const void* _src);
  210. /// Returns true if texture format is compressed.
  211. bool isCompressed(TextureFormat::Enum _format);
  212. /// Returns true if texture format is uncompressed.
  213. bool isColor(TextureFormat::Enum _format);
  214. /// Returns true if texture format is depth.
  215. bool isDepth(TextureFormat::Enum _format);
  216. /// Returns true if texture format is valid.
  217. bool isValid(TextureFormat::Enum _format);
  218. /// Returns bits per pixel.
  219. uint8_t getBitsPerPixel(TextureFormat::Enum _format);
  220. /// Returns texture block info.
  221. const ImageBlockInfo& getBlockInfo(TextureFormat::Enum _format);
  222. /// Converts format to string.
  223. const char* getName(TextureFormat::Enum _format);
  224. /// Converts string to format.
  225. TextureFormat::Enum getFormat(const char* _name);
  226. /// Returns number of mip-maps required for complete mip-map chain.
  227. uint8_t imageGetNumMips(TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth = 0);
  228. /// Returns image size.
  229. uint32_t imageGetSize(TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth = 0, bool _cubeMap = false, uint8_t _numMips = 0);
  230. ///
  231. void imageSolid(uint32_t _width, uint32_t _height, uint32_t _solid, void* _dst);
  232. ///
  233. void imageCheckerboard(uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1, void* _dst);
  234. ///
  235. void imageRgba8Downsample2x2(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
  236. ///
  237. void imageRgba32fDownsample2x2NormalMap(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
  238. ///
  239. void imageSwizzleBgra8(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
  240. ///
  241. void imageCopy(uint32_t _height, uint32_t _srcPitch, const void* _src, uint32_t _dstPitch, void* _dst);
  242. ///
  243. void imageCopy(uint32_t _width, uint32_t _height, uint32_t _bpp, uint32_t _pitch, const void* _src, void* _dst);
  244. ///
  245. bool imageConvert(TextureFormat::Enum _dstFormat, TextureFormat::Enum _srcFormat);
  246. ///
  247. void imageConvert(void* _dst, uint32_t _bpp, PackFn _pack, const void* _src, UnpackFn _unpack, uint32_t _size);
  248. ///
  249. void imageConvert(void* _dst, uint32_t _dstBpp, PackFn _pack, const void* _src, uint32_t _srcBpp, UnpackFn _unpack, uint32_t _width, uint32_t _height, uint32_t _srcPitch);
  250. ///
  251. bool imageConvert(void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height);
  252. ///
  253. const Memory* imageAlloc(ImageContainer& _imageContainer, TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth = 0, bool _cubeMap = false, bool _generateMips = false);
  254. ///
  255. void imageFree(const Memory* _memory);
  256. ///
  257. void imageWriteTga(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, bool _grayscale, bool _yflip);
  258. ///
  259. void imageWriteKtx(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, const void* _src);
  260. ///
  261. void imageWriteKtx(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size);
  262. ///
  263. bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader);
  264. ///
  265. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size);
  266. ///
  267. void imageDecodeToBgra8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format);
  268. ///
  269. void imageDecodeToRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format);
  270. ///
  271. void imageDecodeToRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format);
  272. ///
  273. bool imageGetRawData(const ImageContainer& _imageContainer, uint8_t _side, uint8_t _index, const void* _data, uint32_t _size, ImageMip& _mip);
  274. } // namespace bgfx
  275. #endif // BGFX_IMAGE_H_HEADER_GUARD