image_decode.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /*
  2. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bimg/blob/master/LICENSE
  4. */
  5. #include "bimg_p.h"
  6. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-function")
  7. BX_PRAGMA_DIAGNOSTIC_PUSH()
  8. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wtype-limits")
  9. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-parameter")
  10. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-value")
  11. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG("-Wdeprecated-declarations")
  12. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4018) // warning C4018: '<': signed/unsigned mismatch
  13. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4100) // error C4100: '' : unreferenced formal parameter
  14. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4389) // warning C4389 : '==' : signed / unsigned mismatch
  15. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4505) // warning C4505: 'tinyexr::miniz::def_realloc_func': unreferenced local function has been removed
  16. #define MINIZ_NO_ARCHIVE_APIS
  17. #define MINIZ_NO_STDIO
  18. #define TINYEXR_IMPLEMENTATION
  19. #include <tinyexr/tinyexr.h>
  20. BX_PRAGMA_DIAGNOSTIC_POP()
  21. BX_PRAGMA_DIAGNOSTIC_PUSH();
  22. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4127) // warning C4127: conditional expression is constant
  23. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4267) // warning C4267: '=' : conversion from 'size_t' to 'unsigned short', possible loss of data
  24. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4334) // warning C4334: '<<' : result of 32 - bit shift implicitly converted to 64 bits(was 64 - bit shift intended ? )
  25. #define LODEPNG_NO_COMPILE_ENCODER
  26. #define LODEPNG_NO_COMPILE_DISK
  27. #define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
  28. #define LODEPNG_NO_COMPILE_ALLOCATORS
  29. #define LODEPNG_NO_COMPILE_CPP
  30. #include <lodepng/lodepng.cpp>
  31. BX_PRAGMA_DIAGNOSTIC_POP();
  32. #if BIMG_DECODE_HEIF
  33. # include <libheif/heif.h>
  34. #endif // BIMG_DECODE_HEIF
  35. void* lodepng_malloc(size_t _size)
  36. {
  37. return ::malloc(_size);
  38. }
  39. void* lodepng_realloc(void* _ptr, size_t _size)
  40. {
  41. return ::realloc(_ptr, _size);
  42. }
  43. void lodepng_free(void* _ptr)
  44. {
  45. ::free(_ptr);
  46. }
  47. BX_PRAGMA_DIAGNOSTIC_PUSH();
  48. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wint-to-pointer-cast")
  49. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wmissing-field-initializers");
  50. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow");
  51. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wsign-compare");
  52. BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wunused-but-set-variable");
  53. BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Warray-bounds");
  54. #if BX_COMPILER_GCC >= 60000
  55. BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wmisleading-indentation");
  56. BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wshift-negative-value");
  57. # if BX_COMPILER_GCC >= 70000
  58. BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wimplicit-fallthrough");
  59. # endif // BX_COMPILER_GCC >= 70000
  60. #endif // BX_COMPILER_GCC >= 60000_
  61. #define STBI_MALLOC(_size) lodepng_malloc(_size)
  62. #define STBI_REALLOC(_ptr, _size) lodepng_realloc(_ptr, _size)
  63. #define STBI_FREE(_ptr) lodepng_free(_ptr)
  64. #define STB_IMAGE_IMPLEMENTATION
  65. #define STB_IMAGE_STATIC
  66. #include <stb/stb_image.h>
  67. BX_PRAGMA_DIAGNOSTIC_POP();
  68. namespace bimg
  69. {
  70. static ImageContainer* imageParseLodePng(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, bx::Error* _err)
  71. {
  72. BX_ERROR_SCOPE(_err);
  73. static uint8_t pngMagic[] = { 0x89, 0x50, 0x4E, 0x47, 0x0d, 0x0a };
  74. if (0 != bx::memCmp(_data, pngMagic, sizeof(pngMagic) ) )
  75. {
  76. return NULL;
  77. }
  78. ImageContainer* output = NULL;
  79. bimg::TextureFormat::Enum format = bimg::TextureFormat::RGBA8;
  80. uint32_t width = 0;
  81. uint32_t height = 0;
  82. LodePNGState state;
  83. lodepng_state_init(&state);
  84. state.decoder.color_convert = 0;
  85. uint8_t* data = NULL;
  86. const uint32_t lodePngError = lodepng_decode(&data, &width, &height, &state, (uint8_t*)_data, _size);
  87. if (0 != lodePngError)
  88. {
  89. BX_ERROR_SET(_err, BIMG_ERROR, "lodepng_decode failed.");
  90. }
  91. else
  92. {
  93. bool palette = false;
  94. bool supported = false;
  95. switch (state.info_raw.bitdepth)
  96. {
  97. case 1:
  98. case 2:
  99. case 4:
  100. palette = LCT_PALETTE == state.info_raw.colortype;
  101. format = palette ? bimg::TextureFormat::RGBA8 : bimg::TextureFormat::R8;
  102. supported = true;
  103. break;
  104. case 8:
  105. switch (state.info_raw.colortype)
  106. {
  107. case LCT_GREY:
  108. format = bimg::TextureFormat::R8;
  109. supported = true;
  110. break;
  111. case LCT_GREY_ALPHA:
  112. format = bimg::TextureFormat::RG8;
  113. supported = true;
  114. break;
  115. case LCT_RGB:
  116. format = bimg::TextureFormat::RGB8;
  117. supported = true;
  118. break;
  119. case LCT_RGBA:
  120. format = bimg::TextureFormat::RGBA8;
  121. supported = true;
  122. break;
  123. case LCT_PALETTE:
  124. format = bimg::TextureFormat::RGBA8;
  125. palette = true;
  126. supported = true;
  127. break;
  128. case LCT_MAX_OCTET_VALUE:
  129. break;
  130. }
  131. break;
  132. case 16:
  133. switch (state.info_raw.colortype)
  134. {
  135. case LCT_GREY:
  136. for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
  137. {
  138. uint16_t* rgba = (uint16_t*)data + ii;
  139. rgba[0] = bx::toHostEndian(rgba[0], false);
  140. }
  141. format = bimg::TextureFormat::R16;
  142. supported = true;
  143. break;
  144. case LCT_GREY_ALPHA:
  145. for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
  146. {
  147. uint16_t* rgba = (uint16_t*)data + ii*2;
  148. rgba[0] = bx::toHostEndian(rgba[0], false);
  149. rgba[1] = bx::toHostEndian(rgba[1], false);
  150. }
  151. format = bimg::TextureFormat::RG16;
  152. supported = true;
  153. break;
  154. case LCT_RGB:
  155. for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
  156. {
  157. uint16_t* rgba = (uint16_t*)data + ii*3;
  158. rgba[0] = bx::toHostEndian(rgba[0], false);
  159. rgba[1] = bx::toHostEndian(rgba[1], false);
  160. rgba[2] = bx::toHostEndian(rgba[2], false);
  161. }
  162. format = bimg::TextureFormat::RGBA16;
  163. supported = true;
  164. break;
  165. case LCT_RGBA:
  166. for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
  167. {
  168. uint16_t* rgba = (uint16_t*)data + ii*4;
  169. rgba[0] = bx::toHostEndian(rgba[0], false);
  170. rgba[1] = bx::toHostEndian(rgba[1], false);
  171. rgba[2] = bx::toHostEndian(rgba[2], false);
  172. rgba[3] = bx::toHostEndian(rgba[3], false);
  173. }
  174. format = bimg::TextureFormat::RGBA16;
  175. supported = true;
  176. break;
  177. case LCT_PALETTE:
  178. break;
  179. case LCT_MAX_OCTET_VALUE:
  180. break;
  181. }
  182. break;
  183. default:
  184. break;
  185. }
  186. if (supported)
  187. {
  188. const uint8_t* copyData = data;
  189. TextureFormat::Enum dstFormat = format;
  190. if (palette) {
  191. copyData = NULL;
  192. }
  193. else if (1 == state.info_raw.bitdepth
  194. || 2 == state.info_raw.bitdepth
  195. || 4 == state.info_raw.bitdepth)
  196. {
  197. copyData = NULL;
  198. }
  199. else if (16 == state.info_raw.bitdepth
  200. && LCT_RGB == state.info_raw.colortype)
  201. {
  202. dstFormat = bimg::TextureFormat::RGBA16;
  203. copyData = NULL;
  204. }
  205. output = imageAlloc(_allocator
  206. , dstFormat
  207. , uint16_t(width)
  208. , uint16_t(height)
  209. , 0
  210. , 1
  211. , false
  212. , false
  213. , copyData
  214. );
  215. if (palette)
  216. {
  217. if (1 == state.info_raw.bitdepth)
  218. {
  219. for (uint32_t ii = 0, num = width*height/8; ii < num; ++ii)
  220. {
  221. uint8_t* dst = (uint8_t*)output->m_data + ii*32;
  222. bx::memCopy(dst, state.info_raw.palette + ( (data[ii]>>7)&0x1)*4, 4);
  223. bx::memCopy(dst + 4, state.info_raw.palette + ( (data[ii]>>6)&0x1)*4, 4);
  224. bx::memCopy(dst + 8, state.info_raw.palette + ( (data[ii]>>5)&0x1)*4, 4);
  225. bx::memCopy(dst + 12, state.info_raw.palette + ( (data[ii]>>4)&0x1)*4, 4);
  226. bx::memCopy(dst + 16, state.info_raw.palette + ( (data[ii]>>3)&0x1)*4, 4);
  227. bx::memCopy(dst + 20, state.info_raw.palette + ( (data[ii]>>2)&0x1)*4, 4);
  228. bx::memCopy(dst + 24, state.info_raw.palette + ( (data[ii]>>1)&0x1)*4, 4);
  229. bx::memCopy(dst + 28, state.info_raw.palette + ( data[ii] &0x1)*4, 4);
  230. }
  231. }
  232. else if (2 == state.info_raw.bitdepth)
  233. {
  234. for (uint32_t ii = 0, num = width*height/4; ii < num; ++ii)
  235. {
  236. uint8_t* dst = (uint8_t*)output->m_data + ii*16;
  237. bx::memCopy(dst, state.info_raw.palette + ( (data[ii]>>6)&0x3)*4, 4);
  238. bx::memCopy(dst + 4, state.info_raw.palette + ( (data[ii]>>4)&0x3)*4, 4);
  239. bx::memCopy(dst + 8, state.info_raw.palette + ( (data[ii]>>2)&0x3)*4, 4);
  240. bx::memCopy(dst + 12, state.info_raw.palette + ( data[ii] &0x3)*4, 4);
  241. }
  242. }
  243. else if (4 == state.info_raw.bitdepth)
  244. {
  245. for (uint32_t ii = 0, num = width*height/2; ii < num; ++ii)
  246. {
  247. uint8_t* dst = (uint8_t*)output->m_data + ii*8;
  248. bx::memCopy(dst, state.info_raw.palette + ( (data[ii]>>4)&0xf)*4, 4);
  249. bx::memCopy(dst + 4, state.info_raw.palette + ( data[ii] &0xf)*4, 4);
  250. }
  251. }
  252. else
  253. {
  254. for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
  255. {
  256. bx::memCopy( (uint8_t*)output->m_data + ii*4, state.info_raw.palette + data[ii]*4, 4);
  257. }
  258. }
  259. }
  260. else if (1 == state.info_raw.bitdepth)
  261. {
  262. for (uint32_t ii = 0, num = width*height/8; ii < num; ++ii)
  263. {
  264. uint8_t* src = (uint8_t*)data + ii;
  265. uint8_t eightBits = src[0];
  266. uint8_t* dst = (uint8_t*)output->m_data + ii*8;
  267. dst[0] = uint8_t( (eightBits>>7)&0x1)*255;
  268. dst[1] = uint8_t( (eightBits>>6)&0x1)*255;
  269. dst[2] = uint8_t( (eightBits>>5)&0x1)*255;
  270. dst[3] = uint8_t( (eightBits>>4)&0x1)*255;
  271. dst[4] = uint8_t( (eightBits>>3)&0x1)*255;
  272. dst[5] = uint8_t( (eightBits>>2)&0x1)*255;
  273. dst[6] = uint8_t( (eightBits>>1)&0x1)*255;
  274. dst[7] = uint8_t( (eightBits )&0x1)*255;
  275. }
  276. }
  277. else if (2 == state.info_raw.bitdepth)
  278. {
  279. for (uint32_t ii = 0, num = width*height/4; ii < num; ++ii)
  280. {
  281. uint8_t* src = (uint8_t*)data + ii;
  282. uint8_t eightBits = src[0];
  283. uint8_t* dst = (uint8_t*)output->m_data + ii*4;
  284. // Note: not exactly precise.
  285. // Correct way: dst[0] = uint8_t(float( (eightBits>>6)&0x3)*(255.0f/4.0f) );
  286. dst[0] = uint8_t(uint32_t( ( (eightBits>>6)&0x3)*64)&0xff);
  287. dst[1] = uint8_t(uint32_t( ( (eightBits>>4)&0x3)*64)&0xff);
  288. dst[2] = uint8_t(uint32_t( ( (eightBits>>2)&0x3)*64)&0xff);
  289. dst[3] = uint8_t(uint32_t( ( (eightBits )&0x3)*64)&0xff);
  290. }
  291. }
  292. else if (4 == state.info_raw.bitdepth)
  293. {
  294. for (uint32_t ii = 0, num = width*height/2; ii < num; ++ii)
  295. {
  296. uint8_t* src = (uint8_t*)data + ii;
  297. uint8_t eightBits = src[0];
  298. uint8_t* dst = (uint8_t*)output->m_data + ii*2;
  299. // Note: not exactly precise.
  300. // Correct way: dst[0] = uint8_t(float( (eightBits>>4)&0xf)*(255.0f/16.0f) );
  301. dst[0] = uint8_t(uint32_t( ( (eightBits>>4)&0xf)*16)&0xff);
  302. dst[1] = uint8_t(uint32_t( ( (eightBits )&0xf)*16)&0xff);
  303. }
  304. }
  305. else if (16 == state.info_raw.bitdepth
  306. && LCT_RGB == state.info_raw.colortype)
  307. {
  308. for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
  309. {
  310. const uint16_t* src = (uint16_t*)data + ii*3;
  311. uint16_t* dst = (uint16_t*)output->m_data + ii*4;
  312. dst[0] = src[0];
  313. dst[1] = src[1];
  314. dst[2] = src[2];
  315. dst[3] = UINT16_MAX;
  316. }
  317. }
  318. switch (state.info_raw.colortype) //Check for alpha values
  319. {
  320. case LCT_GREY:
  321. case LCT_RGB:
  322. break;
  323. case LCT_GREY_ALPHA:
  324. if (8 == state.info_raw.bitdepth)
  325. {
  326. for (uint32_t ii = 0, num = width * height; ii < num; ++ii)
  327. {
  328. const uint8_t* rgba = (uint8_t*)data + ii * 2;
  329. bool has_alpha = rgba[1] < UINT8_MAX;
  330. if (has_alpha)
  331. {
  332. output->m_hasAlpha = has_alpha;
  333. break;
  334. }
  335. }
  336. }
  337. else if (16 == state.info_raw.bitdepth)
  338. {
  339. for (uint32_t ii = 0, num = width * height; ii < num; ++ii)
  340. {
  341. const uint16_t* rgba = (uint16_t*)data + ii * 2;
  342. bool has_alpha = rgba[1] < UINT16_MAX;
  343. if (has_alpha)
  344. {
  345. output->m_hasAlpha = has_alpha;
  346. break;
  347. }
  348. }
  349. }
  350. break;
  351. case LCT_RGBA:
  352. if (8 == state.info_raw.bitdepth)
  353. {
  354. for (uint32_t ii = 0, num = width * height; ii < num; ++ii)
  355. {
  356. const uint8_t* dst = (uint8_t*)output->m_data + ii * 4;
  357. bool has_alpha = dst[3] < UINT8_MAX;
  358. if (has_alpha)
  359. {
  360. output->m_hasAlpha = has_alpha;
  361. break;
  362. }
  363. }
  364. }
  365. else if (16 == state.info_raw.bitdepth)
  366. {
  367. for (uint32_t ii = 0, num = width * height; ii < num; ++ii)
  368. {
  369. const uint16_t* dst = (uint16_t*)output->m_data + ii * 4;
  370. bool has_alpha = dst[3] < UINT16_MAX;
  371. if (has_alpha)
  372. {
  373. output->m_hasAlpha = has_alpha;
  374. break;
  375. }
  376. }
  377. }
  378. break;
  379. case LCT_PALETTE:
  380. output->m_hasAlpha = lodepng_has_palette_alpha(&state.info_raw);
  381. break;
  382. case LCT_MAX_OCTET_VALUE:
  383. break;
  384. }
  385. }
  386. else
  387. {
  388. BX_ERROR_SET(_err, BIMG_ERROR, "PNG: Unsupported format.");
  389. }
  390. }
  391. lodepng_state_cleanup(&state);
  392. lodepng_free(data);
  393. return output;
  394. }
  395. static void errorSetTinyExr(int _result, bx::Error* _err)
  396. {
  397. switch (_result)
  398. {
  399. case TINYEXR_ERROR_INVALID_MAGIC_NUMBER: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image. Invalid magic number."); break;
  400. case TINYEXR_ERROR_INVALID_EXR_VERSION: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image. Invalid EXR version."); break;
  401. case TINYEXR_ERROR_INVALID_ARGUMENT: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image. Invalid argument."); break;
  402. case TINYEXR_ERROR_INVALID_DATA: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image. Invalid data."); break;
  403. case TINYEXR_ERROR_INVALID_FILE: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image. Invalid file."); break;
  404. // case TINYEXR_ERROR_INVALID_PARAMETER: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image. Invalid parameter."); break;
  405. case TINYEXR_ERROR_CANT_OPEN_FILE: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image. Can't open file."); break;
  406. case TINYEXR_ERROR_UNSUPPORTED_FORMAT: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image. Unsupported format."); break;
  407. case TINYEXR_ERROR_INVALID_HEADER: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image. Invalid header."); break;
  408. case TINYEXR_ERROR_UNSUPPORTED_FEATURE: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image. Unsupported feature."); break;
  409. case TINYEXR_ERROR_CANT_WRITE_FILE: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image. Can't write file."); break;
  410. case TINYEXR_ERROR_SERIALZATION_FAILED: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image. Serialization failed."); break;
  411. default: BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Failed to parse image."); break;
  412. }
  413. }
  414. static ImageContainer* imageParseTinyExr(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, bx::Error* _err)
  415. {
  416. BX_ERROR_SCOPE(_err);
  417. EXRVersion exrVersion;
  418. int result = ParseEXRVersionFromMemory(&exrVersion, (uint8_t*)_data, _size);
  419. if (TINYEXR_SUCCESS != result)
  420. {
  421. return NULL;
  422. }
  423. bimg::TextureFormat::Enum format = bimg::TextureFormat::RGBA8;
  424. uint32_t width = 0;
  425. uint32_t height = 0;
  426. bool hasAlpha = false;
  427. uint8_t* data = NULL;
  428. const char* err = NULL;
  429. EXRHeader exrHeader;
  430. result = ParseEXRHeaderFromMemory(&exrHeader, &exrVersion, (uint8_t*)_data, _size, &err);
  431. if (TINYEXR_SUCCESS == result)
  432. {
  433. EXRImage exrImage;
  434. InitEXRImage(&exrImage);
  435. result = LoadEXRImageFromMemory(&exrImage, &exrHeader, (uint8_t*)_data, _size, &err);
  436. if (TINYEXR_SUCCESS == result)
  437. {
  438. uint8_t idxR = UINT8_MAX;
  439. uint8_t idxG = UINT8_MAX;
  440. uint8_t idxB = UINT8_MAX;
  441. uint8_t idxA = UINT8_MAX;
  442. for (uint8_t ii = 0, num = uint8_t(exrHeader.num_channels); ii < num; ++ii)
  443. {
  444. const EXRChannelInfo& channel = exrHeader.channels[ii];
  445. if (UINT8_MAX == idxR
  446. && 0 == bx::strCmp(channel.name, "R") )
  447. {
  448. idxR = ii;
  449. }
  450. else if (UINT8_MAX == idxG
  451. && 0 == bx::strCmp(channel.name, "G") )
  452. {
  453. idxG = ii;
  454. }
  455. else if (UINT8_MAX == idxB
  456. && 0 == bx::strCmp(channel.name, "B") )
  457. {
  458. idxB = ii;
  459. }
  460. else if (UINT8_MAX == idxA
  461. && 0 == bx::strCmp(channel.name, "A") )
  462. {
  463. idxA = ii;
  464. }
  465. }
  466. if (UINT8_MAX != idxR)
  467. {
  468. const bool asFloat = exrHeader.pixel_types[idxR] == TINYEXR_PIXELTYPE_FLOAT;
  469. uint32_t dstBpp = asFloat ? 32 : 16;
  470. format = asFloat ? TextureFormat::R32F : TextureFormat::R16F;
  471. uint32_t stepR = 1;
  472. uint32_t stepG = 0;
  473. uint32_t stepB = 0;
  474. uint32_t stepA = 0;
  475. if (UINT8_MAX != idxG)
  476. {
  477. dstBpp = asFloat ? 64 : 32;
  478. format = asFloat ? TextureFormat::RG32F : TextureFormat::RG16F;
  479. stepG = 1;
  480. }
  481. if (UINT8_MAX != idxB)
  482. {
  483. dstBpp = asFloat ? 128 : 64;
  484. format = asFloat ? TextureFormat::RGBA32F : TextureFormat::RGBA16F;
  485. stepB = 1;
  486. }
  487. if (UINT8_MAX != idxA)
  488. {
  489. dstBpp = asFloat ? 128 : 64;
  490. format = asFloat ? TextureFormat::RGBA32F : TextureFormat::RGBA16F;
  491. stepA = 1;
  492. }
  493. data = (uint8_t*)bx::alloc(_allocator, (size_t)exrImage.width * exrImage.height * dstBpp/8);
  494. width = exrImage.width;
  495. height = exrImage.height;
  496. if (asFloat)
  497. {
  498. const float zero = 0.0f;
  499. const float* srcR = UINT8_MAX == idxR ? &zero : (const float*)(exrImage.images)[idxR];
  500. const float* srcG = UINT8_MAX == idxG ? &zero : (const float*)(exrImage.images)[idxG];
  501. const float* srcB = UINT8_MAX == idxB ? &zero : (const float*)(exrImage.images)[idxB];
  502. const float* srcA = UINT8_MAX == idxA ? &zero : (const float*)(exrImage.images)[idxA];
  503. const uint32_t bytesPerPixel = dstBpp/8;
  504. for (uint32_t ii = 0, num = exrImage.width * exrImage.height; ii < num; ++ii)
  505. {
  506. float rgba[4] =
  507. {
  508. *srcR,
  509. *srcG,
  510. *srcB,
  511. *srcA,
  512. };
  513. bx::memCopy(&data[ii * bytesPerPixel], rgba, bytesPerPixel);
  514. hasAlpha |= (hasAlpha || rgba[3] < 1.0f);
  515. srcR += stepR;
  516. srcG += stepG;
  517. srcB += stepB;
  518. srcA += stepA;
  519. }
  520. }
  521. else
  522. {
  523. const uint16_t zero = 0;
  524. const uint16_t* srcR = UINT8_MAX == idxR ? &zero : (const uint16_t*)(exrImage.images)[idxR];
  525. const uint16_t* srcG = UINT8_MAX == idxG ? &zero : (const uint16_t*)(exrImage.images)[idxG];
  526. const uint16_t* srcB = UINT8_MAX == idxB ? &zero : (const uint16_t*)(exrImage.images)[idxB];
  527. const uint16_t* srcA = UINT8_MAX == idxA ? &zero : (const uint16_t*)(exrImage.images)[idxA];
  528. const uint32_t bytesPerPixel = dstBpp/8;
  529. for (uint32_t ii = 0, num = exrImage.width * exrImage.height; ii < num; ++ii)
  530. {
  531. uint16_t rgba[4] =
  532. {
  533. *srcR,
  534. *srcG,
  535. *srcB,
  536. *srcA,
  537. };
  538. bx::memCopy(&data[ii * bytesPerPixel], rgba, bytesPerPixel);
  539. hasAlpha |= (hasAlpha || rgba[3] < UINT16_MAX);
  540. srcR += stepR;
  541. srcG += stepG;
  542. srcB += stepB;
  543. srcA += stepA;
  544. }
  545. }
  546. }
  547. else
  548. {
  549. BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Couldn't find R channel.");
  550. }
  551. FreeEXRImage(&exrImage);
  552. }
  553. else
  554. {
  555. errorSetTinyExr(result, _err);
  556. }
  557. FreeEXRHeader(&exrHeader);
  558. }
  559. else
  560. {
  561. errorSetTinyExr(result, _err);
  562. }
  563. ImageContainer* output = NULL;
  564. if (NULL != data)
  565. {
  566. output = imageAlloc(_allocator
  567. , format
  568. , uint16_t(width)
  569. , uint16_t(height)
  570. , 0
  571. , 1
  572. , false
  573. , false
  574. , data
  575. );
  576. bx::free(_allocator, data);
  577. output->m_hasAlpha = hasAlpha;
  578. }
  579. return output;
  580. }
  581. static ImageContainer* imageParseStbImage(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, bx::Error* _err)
  582. {
  583. BX_ERROR_SCOPE(_err);
  584. const int isHdr = stbi_is_hdr_from_memory( (const uint8_t*)_data, (int)_size);
  585. void* data;
  586. uint32_t width = 0;
  587. uint32_t height = 0;
  588. int comp = 0;
  589. if (isHdr)
  590. {
  591. data = stbi_loadf_from_memory( (const uint8_t*)_data, (int)_size, (int*)&width, (int*)&height, &comp, 4);
  592. }
  593. else
  594. {
  595. data = stbi_load_from_memory ( (const uint8_t*)_data, (int)_size, (int*)&width, (int*)&height, &comp, 0);
  596. }
  597. if (NULL == data)
  598. {
  599. return NULL;
  600. }
  601. bimg::TextureFormat::Enum format = bimg::TextureFormat::RGBA8;
  602. if (isHdr)
  603. {
  604. format = bimg::TextureFormat::RGBA32F;
  605. }
  606. else
  607. {
  608. switch (comp)
  609. {
  610. case 1: format = bimg::TextureFormat::R8; break;
  611. case 2: format = bimg::TextureFormat::RG8; break;
  612. case 3: format = bimg::TextureFormat::RGB8; break;
  613. default: break;
  614. }
  615. }
  616. ImageContainer* output = imageAlloc(_allocator
  617. , format
  618. , bx::narrowCast<uint16_t>(width)
  619. , bx::narrowCast<uint16_t>(height)
  620. , 0
  621. , 1
  622. , false
  623. , false
  624. , data
  625. );
  626. stbi_image_free(data);
  627. return output;
  628. }
  629. static ImageContainer* imageParseJpeg(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, bx::Error* _err)
  630. {
  631. bx::MemoryReader reader(_data, _size);
  632. bx::Error err;
  633. uint16_t magic = 0;
  634. bx::readHE(&reader, magic, false, &err);
  635. if (!err.isOk()
  636. || 0xffd8 != magic)
  637. {
  638. return NULL;
  639. }
  640. Orientation::Enum orientation = Orientation::R0;
  641. while (err.isOk() )
  642. {
  643. bx::readHE(&reader, magic, false, &err);
  644. uint16_t size;
  645. bx::readHE(&reader, size, false, &err);
  646. if (!err.isOk() )
  647. {
  648. return NULL;
  649. }
  650. if (0xffe1 != magic)
  651. {
  652. bx::seek(&reader, size-2);
  653. continue;
  654. }
  655. char exif00[6];
  656. bx::read(&reader, exif00, 6, &err);
  657. if (0 == bx::memCmp(exif00, "Exif\0\0", 6) )
  658. {
  659. uint16_t iimm = 0;
  660. bx::read(&reader, iimm, &err);
  661. const bool littleEndian = iimm == 0x4949; //II - Intel - little endian
  662. if (!err.isOk()
  663. && !littleEndian
  664. && iimm != 0x4d4d) // MM - Motorola - big endian
  665. {
  666. return NULL;
  667. }
  668. bx::readHE(&reader, magic, littleEndian, &err);
  669. if (!err.isOk()
  670. || 0x2a != magic)
  671. {
  672. return NULL;
  673. }
  674. uint32_t ifd0;
  675. bx::readHE(&reader, ifd0, littleEndian, &err);
  676. if (!err.isOk()
  677. || 8 > ifd0)
  678. {
  679. return NULL;
  680. }
  681. bx::seek(&reader, ifd0-8);
  682. uint16_t numEntries;
  683. bx::readHE(&reader, numEntries, littleEndian, &err);
  684. for (uint32_t ii = 0; err.isOk() && ii < numEntries; ++ii)
  685. {
  686. // Reference(s):
  687. // - EXIF Tags
  688. // https://web.archive.org/web/20190218005249/https://sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html
  689. //
  690. uint16_t tag;
  691. bx::readHE(&reader, tag, littleEndian, &err);
  692. uint16_t format;
  693. bx::readHE(&reader, format, littleEndian, &err);
  694. uint32_t length;
  695. bx::readHE(&reader, length, littleEndian, &err);
  696. uint32_t data;
  697. bx::readHE(&reader, data, littleEndian, &err);
  698. switch (tag)
  699. {
  700. case 0x112: // orientation
  701. if (3 == format)
  702. {
  703. bx::seek(&reader, -4);
  704. uint16_t u16;
  705. bx::readHE(&reader, u16, littleEndian, &err);
  706. uint16_t pad;
  707. bx::read(&reader, pad, &err);
  708. switch (u16)
  709. {
  710. default:
  711. case 1: orientation = Orientation::R0; break; // Horizontal (normal)
  712. case 2: orientation = Orientation::HFlip; break; // Mirror horizontal
  713. case 3: orientation = Orientation::R180; break; // Rotate 180
  714. case 4: orientation = Orientation::VFlip; break; // Mirror vertical
  715. case 5: orientation = Orientation::HFlipR270; break; // Mirror horizontal and rotate 270 CW
  716. case 6: orientation = Orientation::R90; break; // Rotate 90 CW
  717. case 7: orientation = Orientation::HFlipR90; break; // Mirror horizontal and rotate 90 CW
  718. case 8: orientation = Orientation::R270; break; // Rotate 270 CW
  719. }
  720. }
  721. break;
  722. default:
  723. break;
  724. }
  725. }
  726. }
  727. break;
  728. }
  729. ImageContainer* image = imageParseStbImage(_allocator, _data, _size, _err);
  730. if (NULL != image)
  731. {
  732. image->m_orientation = orientation;
  733. }
  734. return image;
  735. }
  736. static ImageContainer* imageParseLibHeif(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, bx::Error* _err)
  737. {
  738. #if BIMG_DECODE_HEIF
  739. heif_context* ctx = heif_context_alloc();
  740. heif_context_read_from_memory_without_copy(ctx, _data, _size, NULL);
  741. heif_image_handle* handle;
  742. heif_context_get_primary_image_handle(ctx, &handle);
  743. heif_image* image;
  744. heif_decode_image(handle, &image, heif_colorspace_RGB, heif_chroma_interleaved_RGBA, NULL);
  745. int32_t srcStride;
  746. const uint8_t* data = heif_image_get_plane_readonly(image, heif_channel_interleaved, &srcStride);
  747. ImageContainer* output = NULL;
  748. if (NULL != data)
  749. {
  750. const bimg::TextureFormat::Enum format = bimg::TextureFormat::RGBA8;
  751. const int32_t width = heif_image_handle_get_width(handle);
  752. const int32_t height = heif_image_handle_get_height(handle);
  753. const int32_t dstStride = width*4;
  754. output = imageAlloc(_allocator
  755. , format
  756. , bx::narrowCast<uint16_t>(width)
  757. , bx::narrowCast<uint16_t>(height)
  758. , 0
  759. , 1
  760. , false
  761. , false
  762. , NULL
  763. );
  764. bx::memCopy(output->m_data, dstStride, data, srcStride, dstStride, height);
  765. }
  766. heif_image_release(image);
  767. heif_image_handle_release(handle);
  768. heif_context_free(ctx);
  769. BX_UNUSED(_err);
  770. return output;
  771. #else
  772. BX_UNUSED(_allocator, _data, _size, _err);
  773. return NULL;
  774. #endif // BIMG_DECODE_HEIF
  775. }
  776. ImageContainer* imageParse(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, TextureFormat::Enum _dstFormat, bx::Error* _err)
  777. {
  778. BX_ERROR_SCOPE(_err);
  779. ImageContainer* input = imageParseDds (_allocator, _data, _size, _err) ;
  780. input = NULL == input ? imageParseKtx (_allocator, _data, _size, _err) : input;
  781. input = NULL == input ? imageParsePvr3 (_allocator, _data, _size, _err) : input;
  782. input = NULL == input ? imageParseGnf (_allocator, _data, _size, _err) : input;
  783. input = NULL == input ? imageParseLodePng (_allocator, _data, _size, _err) : input;
  784. input = NULL == input ? imageParseTinyExr (_allocator, _data, _size, _err) : input;
  785. input = NULL == input ? imageParseJpeg (_allocator, _data, _size, _err) : input;
  786. input = NULL == input ? imageParseStbImage(_allocator, _data, _size, _err) : input;
  787. input = NULL == input ? imageParseLibHeif (_allocator, _data, _size, _err) : input;
  788. if (NULL == input)
  789. {
  790. return NULL;
  791. }
  792. _dstFormat = TextureFormat::Count == _dstFormat
  793. ? input->m_format
  794. : _dstFormat
  795. ;
  796. if (_dstFormat == input->m_format)
  797. {
  798. return input;
  799. }
  800. ImageContainer* output = imageConvert(_allocator, _dstFormat, *input);
  801. imageFree(input);
  802. return output;
  803. }
  804. } // namespace bimg