image.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "entry/dbg.h"
  6. #include <bgfx/bgfx.h>
  7. #include <bx/allocator.h>
  8. #include <bx/endian.h>
  9. #include <bx/readerwriter.h>
  10. #include "bgfx_utils.h"
  11. BX_PRAGMA_DIAGNOSTIC_PUSH()
  12. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wtype-limits")
  13. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-parameter")
  14. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-value")
  15. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4100) // error C4100: '' : unreferenced formal parameter
  16. #if BX_PLATFORM_EMSCRIPTEN
  17. # include <compat/ctype.h>
  18. #endif // BX_PLATFORM_EMSCRIPTEN
  19. #define MINIZ_NO_STDIO
  20. #define TINYEXR_IMPLEMENTATION
  21. #include <tinyexr/tinyexr.h>
  22. BX_PRAGMA_DIAGNOSTIC_POP()
  23. #define LODEPNG_NO_COMPILE_ENCODER
  24. #define LODEPNG_NO_COMPILE_DISK
  25. #define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
  26. #define LODEPNG_NO_COMPILE_ERROR_TEXT
  27. #define LODEPNG_NO_COMPILE_ALLOCATORS
  28. #define LODEPNG_NO_COMPILE_CPP
  29. #include <lodepng/lodepng.h>
  30. typedef unsigned char stbi_uc;
  31. extern "C" int stbi_is_hdr_from_memory(stbi_uc const* _buffer, int _len);
  32. extern "C" stbi_uc* stbi_load_from_memory(stbi_uc const* _buffer, int _len, int* _x, int* _y, int* _comp, int _req_comp);
  33. extern "C" float* stbi_loadf_from_memory(stbi_uc const* _buffer, int _len, int* _x, int* _y, int* _comp, int _req_comp);
  34. extern "C" void stbi_image_free(void* _ptr);
  35. extern void lodepng_free(void* _ptr);
  36. namespace bgfx
  37. {
  38. struct ImageMip
  39. {
  40. TextureFormat::Enum m_format;
  41. uint32_t m_width;
  42. uint32_t m_height;
  43. uint32_t m_blockSize;
  44. uint32_t m_size;
  45. uint8_t m_bpp;
  46. bool m_hasAlpha;
  47. const uint8_t* m_data;
  48. };
  49. uint32_t imageGetSize(
  50. TextureInfo* _info
  51. , uint16_t _width
  52. , uint16_t _height
  53. , uint16_t _depth
  54. , bool _cubeMap
  55. , bool _hasMips
  56. , uint16_t _numLayers
  57. , TextureFormat::Enum _format
  58. );
  59. ///
  60. ImageContainer* imageParseBgfx(bx::AllocatorI* _allocator, const void* _src, uint32_t _size);
  61. ///
  62. bool imageConvert(
  63. void* _dst
  64. , TextureFormat::Enum _dstFormat
  65. , const void* _src
  66. , TextureFormat::Enum _srcFormat
  67. , uint32_t _width
  68. , uint32_t _height
  69. );
  70. ///
  71. ImageContainer* imageConvert(
  72. bx::AllocatorI* _allocator
  73. , TextureFormat::Enum _dstFormat
  74. , const ImageContainer& _input
  75. );
  76. } // namespace bgfx
  77. namespace bgfx
  78. {
  79. static ImageContainer* imageParseLodePng(bx::AllocatorI* _allocator, const void* _data, uint32_t _size)
  80. {
  81. static uint8_t pngMagic[] = { 0x89, 0x50, 0x4E, 0x47, 0x0d, 0x0a };
  82. if (0 != bx::memCmp(_data, pngMagic, sizeof(pngMagic) ) )
  83. {
  84. return NULL;
  85. }
  86. bgfx::TextureFormat::Enum format = bgfx::TextureFormat::RGBA8;
  87. uint32_t width = 0;
  88. uint32_t height = 0;
  89. unsigned error;
  90. LodePNGState state;
  91. lodepng_state_init(&state);
  92. state.decoder.color_convert = 0;
  93. uint8_t* data = NULL;
  94. error = lodepng_decode(&data, &width, &height, &state, (uint8_t*)_data, _size);
  95. if (0 == error)
  96. {
  97. switch (state.info_raw.bitdepth)
  98. {
  99. case 8:
  100. switch (state.info_raw.colortype)
  101. {
  102. case LCT_GREY:
  103. format = bgfx::TextureFormat::R8;
  104. break;
  105. case LCT_GREY_ALPHA:
  106. format = bgfx::TextureFormat::RG8;
  107. break;
  108. case LCT_RGB:
  109. format = bgfx::TextureFormat::RGB8;
  110. break;
  111. case LCT_RGBA:
  112. format = bgfx::TextureFormat::RGBA8;
  113. break;
  114. case LCT_PALETTE:
  115. break;
  116. }
  117. break;
  118. case 16:
  119. switch (state.info_raw.colortype)
  120. {
  121. case LCT_GREY:
  122. for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
  123. {
  124. uint16_t* rgba = (uint16_t*)data + ii;
  125. rgba[0] = bx::toHostEndian(rgba[0], false);
  126. }
  127. format = bgfx::TextureFormat::R16;
  128. break;
  129. case LCT_GREY_ALPHA:
  130. for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
  131. {
  132. uint16_t* rgba = (uint16_t*)data + ii*2;
  133. rgba[0] = bx::toHostEndian(rgba[0], false);
  134. rgba[1] = bx::toHostEndian(rgba[1], false);
  135. }
  136. format = bgfx::TextureFormat::RG16;
  137. break;
  138. case LCT_RGBA:
  139. for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
  140. {
  141. uint16_t* rgba = (uint16_t*)data + ii*4;
  142. rgba[0] = bx::toHostEndian(rgba[0], false);
  143. rgba[1] = bx::toHostEndian(rgba[1], false);
  144. rgba[2] = bx::toHostEndian(rgba[2], false);
  145. rgba[3] = bx::toHostEndian(rgba[3], false);
  146. }
  147. format = bgfx::TextureFormat::RGBA16;
  148. break;
  149. case LCT_RGB:
  150. case LCT_PALETTE:
  151. break;
  152. }
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. lodepng_state_cleanup(&state);
  159. ImageContainer* output = imageAlloc(_allocator
  160. , format
  161. , uint16_t(width)
  162. , uint16_t(height)
  163. , 0
  164. , 1
  165. , false
  166. , false
  167. , data
  168. );
  169. lodepng_free(data);
  170. return output;
  171. }
  172. static ImageContainer* imageParseTinyExr(bx::AllocatorI* _allocator, const void* _data, uint32_t _size)
  173. {
  174. EXRVersion exrVersion;
  175. int result = ParseEXRVersionFromMemory(&exrVersion, (uint8_t*)_data, _size);
  176. if (TINYEXR_SUCCESS != result)
  177. {
  178. return NULL;
  179. }
  180. bgfx::TextureFormat::Enum format = bgfx::TextureFormat::RGBA8;
  181. uint32_t width = 0;
  182. uint32_t height = 0;
  183. uint8_t* data = NULL;
  184. const char* err = NULL;
  185. EXRHeader exrHeader;
  186. result = ParseEXRHeaderFromMemory(&exrHeader, &exrVersion, (uint8_t*)_data, _size, &err);
  187. if (TINYEXR_SUCCESS == result)
  188. {
  189. EXRImage exrImage;
  190. InitEXRImage(&exrImage);
  191. result = LoadEXRImageFromMemory(&exrImage, &exrHeader, (uint8_t*)_data, _size, &err);
  192. if (TINYEXR_SUCCESS == result)
  193. {
  194. uint8_t idxR = UINT8_MAX;
  195. uint8_t idxG = UINT8_MAX;
  196. uint8_t idxB = UINT8_MAX;
  197. uint8_t idxA = UINT8_MAX;
  198. for (uint8_t ii = 0, num = uint8_t(exrHeader.num_channels); ii < num; ++ii)
  199. {
  200. const EXRChannelInfo& channel = exrHeader.channels[ii];
  201. if (UINT8_MAX == idxR
  202. && 0 == bx::strncmp(channel.name, "R") )
  203. {
  204. idxR = ii;
  205. }
  206. else if (UINT8_MAX == idxG
  207. && 0 == bx::strncmp(channel.name, "G") )
  208. {
  209. idxG = ii;
  210. }
  211. else if (UINT8_MAX == idxB
  212. && 0 == bx::strncmp(channel.name, "B") )
  213. {
  214. idxB = ii;
  215. }
  216. else if (UINT8_MAX == idxA
  217. && 0 == bx::strncmp(channel.name, "A") )
  218. {
  219. idxA = ii;
  220. }
  221. }
  222. if (UINT8_MAX != idxR)
  223. {
  224. const bool asFloat = exrHeader.pixel_types[idxR] == TINYEXR_PIXELTYPE_FLOAT;
  225. uint32_t srcBpp = 32;
  226. uint32_t dstBpp = asFloat ? 32 : 16;
  227. format = asFloat ? TextureFormat::R32F : TextureFormat::R16F;
  228. uint32_t stepR = 1;
  229. uint32_t stepG = 0;
  230. uint32_t stepB = 0;
  231. uint32_t stepA = 0;
  232. if (UINT8_MAX != idxG)
  233. {
  234. srcBpp += 32;
  235. dstBpp = asFloat ? 64 : 32;
  236. format = asFloat ? TextureFormat::RG32F : TextureFormat::RG16F;
  237. stepG = 1;
  238. }
  239. if (UINT8_MAX != idxB)
  240. {
  241. srcBpp += 32;
  242. dstBpp = asFloat ? 128 : 64;
  243. format = asFloat ? TextureFormat::RGBA32F : TextureFormat::RGBA16F;
  244. stepB = 1;
  245. }
  246. if (UINT8_MAX != idxA)
  247. {
  248. srcBpp += 32;
  249. dstBpp = asFloat ? 128 : 64;
  250. format = asFloat ? TextureFormat::RGBA32F : TextureFormat::RGBA16F;
  251. stepA = 1;
  252. }
  253. data = (uint8_t*)BX_ALLOC(_allocator, exrImage.width * exrImage.height * dstBpp/8);
  254. const float zero = 0.0f;
  255. const float* srcR = UINT8_MAX == idxR ? &zero : (const float*)(exrImage.images)[idxR];
  256. const float* srcG = UINT8_MAX == idxG ? &zero : (const float*)(exrImage.images)[idxG];
  257. const float* srcB = UINT8_MAX == idxB ? &zero : (const float*)(exrImage.images)[idxB];
  258. const float* srcA = UINT8_MAX == idxA ? &zero : (const float*)(exrImage.images)[idxA];
  259. const uint32_t bytesPerPixel = dstBpp/8;
  260. for (uint32_t ii = 0, num = exrImage.width * exrImage.height; ii < num; ++ii)
  261. {
  262. float rgba[4] =
  263. {
  264. *srcR,
  265. *srcG,
  266. *srcB,
  267. *srcA,
  268. };
  269. bx::memCopy(&data[ii * bytesPerPixel], rgba, bytesPerPixel);
  270. srcR += stepR;
  271. srcG += stepG;
  272. srcB += stepB;
  273. srcA += stepA;
  274. }
  275. }
  276. FreeEXRImage(&exrImage);
  277. }
  278. FreeEXRHeader(&exrHeader);
  279. }
  280. ImageContainer* output = imageAlloc(_allocator
  281. , format
  282. , uint16_t(width)
  283. , uint16_t(height)
  284. , 0
  285. , 1
  286. , false
  287. , false
  288. , data
  289. );
  290. BX_FREE(_allocator, data);
  291. return output;
  292. }
  293. static ImageContainer* imageParseStbImage(bx::AllocatorI* _allocator, const void* _data, uint32_t _size)
  294. {
  295. const int isHdr = stbi_is_hdr_from_memory((const uint8_t*)_data, (int)_size);
  296. void* data;
  297. uint32_t width = 0;
  298. uint32_t height = 0;
  299. int comp = 0;
  300. if (isHdr) { data = stbi_loadf_from_memory((const uint8_t*)_data, (int)_size, (int*)&width, (int*)&height, &comp, 4); }
  301. else { data = stbi_load_from_memory ((const uint8_t*)_data, (int)_size, (int*)&width, (int*)&height, &comp, 0); }
  302. if (NULL == data)
  303. {
  304. return NULL;
  305. }
  306. bgfx::TextureFormat::Enum format;
  307. if (isHdr)
  308. {
  309. format = bgfx::TextureFormat::RGBA32F;
  310. }
  311. else
  312. {
  313. if (1 == comp) { format = bgfx::TextureFormat::R8; }
  314. else if (2 == comp) { format = bgfx::TextureFormat::RG8; }
  315. else if (3 == comp) { format = bgfx::TextureFormat::RGB8; }
  316. else/*if (4 == comp)*/ { format = bgfx::TextureFormat::RGBA8; }
  317. }
  318. ImageContainer* output = imageAlloc(_allocator
  319. , format
  320. , uint16_t(width)
  321. , uint16_t(height)
  322. , 0
  323. , 1
  324. , false
  325. , false
  326. , data
  327. );
  328. stbi_image_free(data);
  329. return output;
  330. }
  331. ImageContainer* imageParse(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, TextureFormat::Enum _dstFormat)
  332. {
  333. ImageContainer* input = imageParseBgfx (_allocator, _data, _size) ;
  334. input = NULL == input ? imageParseLodePng (_allocator, _data, _size) : input;
  335. input = NULL == input ? imageParseTinyExr (_allocator, _data, _size) : input;
  336. input = NULL == input ? imageParseStbImage(_allocator, _data, _size) : input;
  337. if (NULL == input)
  338. {
  339. return NULL;
  340. }
  341. _dstFormat = TextureFormat::Count == _dstFormat
  342. ? input->m_format
  343. : _dstFormat
  344. ;
  345. if (_dstFormat == input->m_format)
  346. {
  347. return input;
  348. }
  349. ImageContainer* output = imageConvert(_allocator, _dstFormat, *input);
  350. imageFree(input);
  351. return output;
  352. }
  353. } // namespace bgfx