image.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 rBits;
  60. uint8_t gBits;
  61. uint8_t bBits;
  62. uint8_t aBits;
  63. uint8_t encoding;
  64. };
  65. typedef void (*PackFn)(void*, const float*);
  66. typedef void (*UnpackFn)(float*, const void*);
  67. // R8
  68. void packR8(void* _dst, const float* _src);
  69. void unpackR8(float* _dst, const void* _src);
  70. // R8S
  71. void packR8S(void* _dst, const float* _src);
  72. void unpackR8S(float* _dst, const void* _src);
  73. // R8I
  74. void packR8I(void* _dst, const float* _src);
  75. void unpackR8I(float* _dst, const void* _src);
  76. // R8U
  77. void packR8U(void* _dst, const float* _src);
  78. void unpackR8U(float* _dst, const void* _src);
  79. // RG8
  80. void packRg8(void* _dst, const float* _src);
  81. void unpackRg8(float* _dst, const void* _src);
  82. // RG8S
  83. void packRg8S(void* _dst, const float* _src);
  84. void unpackRg8S(float* _dst, const void* _src);
  85. // RG8I
  86. void packRg8I(void* _dst, const float* _src);
  87. void unpackRg8I(float* _dst, const void* _src);
  88. // RG8U
  89. void packRg8U(void* _dst, const float* _src);
  90. void unpackRg8U(float* _dst, const void* _src);
  91. // RGB8
  92. void packRgb8(void* _dst, const float* _src);
  93. void unpackRgb8(float* _dst, const void* _src);
  94. // RGB8S
  95. void packRgb8S(void* _dst, const float* _src);
  96. void unpackRgb8S(float* _dst, const void* _src);
  97. // RGB8I
  98. void packRgb8I(void* _dst, const float* _src);
  99. void unpackRgb8I(float* _dst, const void* _src);
  100. // RGB8U
  101. void packRgb8U(void* _dst, const float* _src);
  102. void unpackRgb8U(float* _dst, const void* _src);
  103. // RGBA8
  104. void packRgba8(void* _dst, const float* _src);
  105. void unpackRgba8(float* _dst, const void* _src);
  106. // BGRA8
  107. void packBgra8(void* _dst, const float* _src);
  108. void unpackBgra8(float* _dst, const void* _src);
  109. // RGBA8S
  110. void packRgba8S(void* _dst, const float* _src);
  111. void unpackRgba8S(float* _dst, const void* _src);
  112. // RGBA8I
  113. void packRgba8I(void* _dst, const float* _src);
  114. void unpackRgba8I(float* _dst, const void* _src);
  115. // RGBA8U
  116. void packRgba8U(void* _dst, const float* _src);
  117. void unpackRgba8U(float* _dst, const void* _src);
  118. // R16
  119. void packR16(void* _dst, const float* _src);
  120. void unpackR16(float* _dst, const void* _src);
  121. // R16S
  122. void packR16S(void* _dst, const float* _src);
  123. void unpackR16S(float* _dst, const void* _src);
  124. // R16I
  125. void packR16I(void* _dst, const float* _src);
  126. void unpackR16I(float* _dst, const void* _src);
  127. // R16U
  128. void packR16U(void* _dst, const float* _src);
  129. void unpackR16U(float* _dst, const void* _src);
  130. // R16F
  131. void packR16F(void* _dst, const float* _src);
  132. void unpackR16F(float* _dst, const void* _src);
  133. // RG16
  134. void packRg16(void* _dst, const float* _src);
  135. void unpackRg16(float* _dst, const void* _src);
  136. // RG16S
  137. void packRg16S(void* _dst, const float* _src);
  138. void unpackRg16S(float* _dst, const void* _src);
  139. // RG16I
  140. void packRg16I(void* _dst, const float* _src);
  141. void unpackRg16I(float* _dst, const void* _src);
  142. // RG16U
  143. void packRg16U(void* _dst, const float* _src);
  144. void unpackRg16U(float* _dst, const void* _src);
  145. // RG16F
  146. void packRg16F(void* _dst, const float* _src);
  147. void unpackRg16F(float* _dst, const void* _src);
  148. // RGBA16
  149. void packRgba16(void* _dst, const float* _src);
  150. void unpackRgba16(float* _dst, const void* _src);
  151. // RGBA16S
  152. void packRgba16S(void* _dst, const float* _src);
  153. void unpackRgba16S(float* _dst, const void* _src);
  154. // RGBA16I
  155. void packRgba16I(void* _dst, const float* _src);
  156. void unpackRgba16I(float* _dst, const void* _src);
  157. // RGBA16U
  158. void packRgba16U(void* _dst, const float* _src);
  159. void unpackRgba16U(float* _dst, const void* _src);
  160. // RGBA16F
  161. void packRgba16F(void* _dst, const float* _src);
  162. void unpackRgba16F(float* _dst, const void* _src);
  163. // R32I
  164. void packR32I(void* _dst, const float* _src);
  165. void unpackR32I(float* _dst, const void* _src);
  166. // R32U
  167. void packR32U(void* _dst, const float* _src);
  168. void unpackR32U(float* _dst, const void* _src);
  169. // R32F
  170. void packR32F(void* _dst, const float* _src);
  171. void unpackR32F(float* _dst, const void* _src);
  172. // RG32I
  173. void packRg32I(void* _dst, const float* _src);
  174. void unpackRg32I(float* _dst, const void* _src);
  175. // RG32U
  176. void packRg32U(void* _dst, const float* _src);
  177. void unpackRg32U(float* _dst, const void* _src);
  178. // RGB9E5F
  179. void packRgb9E5F(void* _dst, const float* _src);
  180. void unpackRgb9E5F(float* _dst, const void* _src);
  181. // RGBA32I
  182. void packRgba32I(void* _dst, const float* _src);
  183. void unpackRgba32I(float* _dst, const void* _src);
  184. // RGBA32U
  185. void packRgba32U(void* _dst, const float* _src);
  186. void unpackRgba32U(float* _dst, const void* _src);
  187. // RGBA32F
  188. void packRgba32F(void* _dst, const float* _src);
  189. void unpackRgba32F(float* _dst, const void* _src);
  190. // R5G6B5
  191. void packR5G6B5(void* _dst, const float* _src);
  192. void unpackR5G6B5(float* _dst, const void* _src);
  193. // RGBA4
  194. void packRgba4(void* _dst, const float* _src);
  195. void unpackRgba4(float* _dst, const void* _src);
  196. // RGBA4
  197. void packBgra4(void* _dst, const float* _src);
  198. void unpackBgra4(float* _dst, const void* _src);
  199. // RGB5A1
  200. void packRgb5a1(void* _dst, const float* _src);
  201. void unpackRgb5a1(float* _dst, const void* _src);
  202. // BGR5A1
  203. void packBgr5a1(void* _dst, const float* _src);
  204. void unpackBgr5a1(float* _dst, const void* _src);
  205. // RGB10A2
  206. void packRgb10A2(void* _dst, const float* _src);
  207. void unpackRgb10A2(float* _dst, const void* _src);
  208. // R11G11B10F
  209. void packR11G11B10F(void* _dst, const float* _src);
  210. void unpackR11G11B10F(float* _dst, const void* _src);
  211. // RG32F
  212. void packRg32F(void* _dst, const float* _src);
  213. void unpackRg32F(float* _dst, const void* _src);
  214. /// Returns true if texture format is compressed.
  215. bool isCompressed(TextureFormat::Enum _format);
  216. /// Returns true if texture format is uncompressed.
  217. bool isColor(TextureFormat::Enum _format);
  218. /// Returns true if texture format is depth.
  219. bool isDepth(TextureFormat::Enum _format);
  220. /// Returns true if texture format is valid.
  221. bool isValid(TextureFormat::Enum _format);
  222. /// Returns bits per pixel.
  223. uint8_t getBitsPerPixel(TextureFormat::Enum _format);
  224. /// Returns texture block info.
  225. const ImageBlockInfo& getBlockInfo(TextureFormat::Enum _format);
  226. /// Converts format to string.
  227. const char* getName(TextureFormat::Enum _format);
  228. /// Converts string to format.
  229. TextureFormat::Enum getFormat(const char* _name);
  230. /// Returns number of mip-maps required for complete mip-map chain.
  231. uint8_t imageGetNumMips(TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth = 0);
  232. /// Returns image size.
  233. uint32_t imageGetSize(TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth = 0, bool _cubeMap = false, uint8_t _numMips = 0);
  234. ///
  235. void imageSolid(uint32_t _width, uint32_t _height, uint32_t _solid, void* _dst);
  236. ///
  237. void imageCheckerboard(uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1, void* _dst);
  238. ///
  239. void imageRgba8Downsample2x2(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
  240. ///
  241. void imageRgba32fToLinear(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src);
  242. ///
  243. void imageRgba32fToGamma(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src);
  244. ///
  245. void imageRgba32fLinearDownsample2x2(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
  246. ///
  247. void imageRgba32fDownsample2x2NormalMap(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
  248. ///
  249. void imageSwizzleBgra8(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst);
  250. ///
  251. void imageCopy(uint32_t _height, uint32_t _srcPitch, const void* _src, uint32_t _dstPitch, void* _dst);
  252. ///
  253. void imageCopy(uint32_t _width, uint32_t _height, uint32_t _bpp, uint32_t _pitch, const void* _src, void* _dst);
  254. ///
  255. bool imageConvert(TextureFormat::Enum _dstFormat, TextureFormat::Enum _srcFormat);
  256. ///
  257. void imageConvert(void* _dst, uint32_t _bpp, PackFn _pack, const void* _src, UnpackFn _unpack, uint32_t _size);
  258. ///
  259. 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);
  260. ///
  261. bool imageConvert(void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height);
  262. ///
  263. const Memory* imageAlloc(ImageContainer& _imageContainer, TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth = 0, bool _cubeMap = false, bool _generateMips = false);
  264. ///
  265. void imageFree(const Memory* _memory);
  266. ///
  267. void imageWriteTga(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, bool _grayscale, bool _yflip, bx::Error* _err = NULL);
  268. ///
  269. 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, bx::Error* _err = NULL);
  270. ///
  271. void imageWriteKtx(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err = NULL);
  272. ///
  273. bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader);
  274. ///
  275. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size);
  276. ///
  277. void imageDecodeToBgra8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format);
  278. ///
  279. void imageDecodeToRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format);
  280. ///
  281. void imageDecodeToRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format);
  282. ///
  283. bool imageGetRawData(const ImageContainer& _imageContainer, uint8_t _side, uint8_t _index, const void* _data, uint32_t _size, ImageMip& _mip);
  284. } // namespace bgfx
  285. #endif // BGFX_IMAGE_H_HEADER_GUARD