image.h 11 KB

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