texturec.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. // Just hacking DDS loading code in here.
  9. #include "bgfx_p.h"
  10. #include "image.h"
  11. #include <libsquish/squish.h>
  12. #include <etc1/etc1.h>
  13. #include <nvtt/nvtt.h>
  14. #include <pvrtc/PvrTcEncoder.h>
  15. #include <tinyexr/tinyexr.h>
  16. #define STB_IMAGE_IMPLEMENTATION
  17. #include <stb/stb_image.c>
  18. #if 0
  19. # define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
  20. #endif // DEBUG
  21. #include <bx/bx.h>
  22. #include <bx/allocator.h>
  23. #include <bx/commandline.h>
  24. #include <bx/uint32_t.h>
  25. namespace bgfx
  26. {
  27. const Memory* alloc(uint32_t _size)
  28. {
  29. Memory* mem = (Memory*)::realloc(NULL, sizeof(Memory) + _size);
  30. mem->size = _size;
  31. mem->data = (uint8_t*)mem + sizeof(Memory);
  32. return mem;
  33. }
  34. const Memory* makeRef(const void* _data, uint32_t _size, ReleaseFn _releaseFn, void* _userData)
  35. {
  36. BX_UNUSED(_releaseFn, _userData);
  37. Memory* mem = (Memory*)::realloc(NULL, sizeof(Memory) );
  38. mem->size = _size;
  39. mem->data = (uint8_t*)_data;
  40. return mem;
  41. }
  42. void release(const Memory* _mem)
  43. {
  44. Memory* mem = const_cast<Memory*>(_mem);
  45. ::free(mem);
  46. }
  47. void imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format)
  48. {
  49. TextureFormat::Enum format = TextureFormat::Enum(_format);
  50. switch (format)
  51. {
  52. case TextureFormat::BC1:
  53. case TextureFormat::BC2:
  54. case TextureFormat::BC3:
  55. case TextureFormat::BC4:
  56. case TextureFormat::BC5:
  57. squish::CompressImage( (const uint8_t*)_src, _width, _height, _dst
  58. , format == TextureFormat::BC2 ? squish::kDxt3
  59. : format == TextureFormat::BC3 ? squish::kDxt5
  60. : format == TextureFormat::BC4 ? squish::kBc4
  61. : format == TextureFormat::BC5 ? squish::kBc5
  62. : squish::kDxt1
  63. );
  64. break;
  65. case TextureFormat::BC6H:
  66. nvtt::compressBC6H( (const uint8_t*)_src, _width, _height, 4, _dst);
  67. break;
  68. case TextureFormat::BC7:
  69. nvtt::compressBC7( (const uint8_t*)_src, _width, _height, 4, _dst);
  70. break;
  71. case TextureFormat::ETC1:
  72. etc1_encode_image( (const uint8_t*)_src, _width, _height, 4, _width*4, (uint8_t*)_dst);
  73. break;
  74. case TextureFormat::ETC2:
  75. case TextureFormat::ETC2A:
  76. case TextureFormat::ETC2A1:
  77. case TextureFormat::PTC12:
  78. break;
  79. case TextureFormat::PTC14:
  80. {
  81. using namespace Javelin;
  82. RgbBitmap bmp;
  83. bmp.width = _width;
  84. bmp.height = _height;
  85. bmp.data = (uint8_t*)const_cast<void*>(_src);
  86. PvrTcEncoder::EncodeRgb4Bpp(_dst, bmp);
  87. bmp.data = NULL;
  88. }
  89. break;
  90. case TextureFormat::PTC12A:
  91. break;
  92. case TextureFormat::PTC14A:
  93. {
  94. using namespace Javelin;
  95. RgbaBitmap bmp;
  96. bmp.width = _width;
  97. bmp.height = _height;
  98. bmp.data = (uint8_t*)const_cast<void*>(_src);
  99. PvrTcEncoder::EncodeRgba4Bpp(_dst, bmp);
  100. bmp.data = NULL;
  101. }
  102. break;
  103. case TextureFormat::PTC22:
  104. case TextureFormat::PTC24:
  105. break;
  106. case TextureFormat::BGRA8:
  107. imageSwizzleBgra8(_width, _height, _width*4, _src, _dst);
  108. break;
  109. case TextureFormat::RGBA8:
  110. memcpy(_dst, _src, _width*_height*4);
  111. break;
  112. default:
  113. break;
  114. }
  115. }
  116. void imageEncodeFromRgba32f(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format)
  117. {
  118. TextureFormat::Enum format = TextureFormat::Enum(_format);
  119. switch (format)
  120. {
  121. case TextureFormat::RGBA8:
  122. break;
  123. default:
  124. break;
  125. }
  126. BX_UNUSED(_dst, _src, _width, _height, _format);
  127. }
  128. } // namespace bgfx
  129. void help(const char* _error = NULL)
  130. {
  131. if (NULL != _error)
  132. {
  133. fprintf(stderr, "Error:\n%s\n\n", _error);
  134. }
  135. fprintf(stderr
  136. , "texturec, bgfx texture compiler tool\n"
  137. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  138. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  139. );
  140. fprintf(stderr
  141. , "Usage: texturec -f <in> -o <out> -t <format>\n"
  142. "\n"
  143. "Supported input file types:\n"
  144. " *.png Portable Network Graphics\n"
  145. " *.tga Targa\n"
  146. " *.dds Direct Draw Surface\n"
  147. " *.ktx Khronos Texture\n"
  148. " *.pvr PowerVR\n"
  149. "\n"
  150. "Options:\n"
  151. " -f <file path> Input file path.\n"
  152. " -o <file path> Output file path (file will be written in KTX format).\n"
  153. " -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n"
  154. " -m, --mips Generate mip-maps.\n"
  155. " -n, --normalmap Input texture is normal map.\n"
  156. "\n"
  157. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  158. );
  159. }
  160. int main(int _argc, const char* _argv[])
  161. {
  162. bx::CommandLine cmdLine(_argc, _argv);
  163. if (cmdLine.hasArg('h', "help") )
  164. {
  165. help();
  166. return EXIT_FAILURE;
  167. }
  168. const char* inputFileName = cmdLine.findOption('f');
  169. if (NULL == inputFileName)
  170. {
  171. help("Input file must be specified.");
  172. return EXIT_FAILURE;
  173. }
  174. const char* outputFileName = cmdLine.findOption('o');
  175. if (NULL == outputFileName)
  176. {
  177. help("Output file must be specified.");
  178. return EXIT_FAILURE;
  179. }
  180. bx::CrtFileReader reader;
  181. if (0 != bx::open(&reader, inputFileName) )
  182. {
  183. help("Failed to open input file.");
  184. return EXIT_FAILURE;
  185. }
  186. const char* type = cmdLine.findOption('t');
  187. bgfx::TextureFormat::Enum format = bgfx::TextureFormat::BGRA8;
  188. if (NULL != type)
  189. {
  190. format = bgfx::getFormat(type);
  191. if (!isValid(format) )
  192. {
  193. help("Invalid format specified.");
  194. return EXIT_FAILURE;
  195. }
  196. }
  197. const bool mips = cmdLine.hasArg('m', "mips");
  198. const bool normalMap = cmdLine.hasArg('n', "normalmap");
  199. uint32_t size = (uint32_t)bx::getSize(&reader);
  200. const bgfx::Memory* mem = bgfx::alloc(size);
  201. bx::read(&reader, mem->data, mem->size);
  202. bx::close(&reader);
  203. {
  204. using namespace bgfx;
  205. uint8_t* decodedImage = NULL;
  206. ImageContainer imageContainer;
  207. bool loaded = imageParse(imageContainer, mem->data, mem->size);
  208. if (!loaded)
  209. {
  210. int width = 0;
  211. int height = 0;
  212. int comp = 0;
  213. decodedImage = stbi_load_from_memory( (uint8_t*)mem->data, mem->size, &width, &height, &comp, 4);
  214. loaded = NULL != decodedImage;
  215. if (loaded)
  216. {
  217. release(mem);
  218. mem = makeRef(decodedImage, width*height*4);
  219. imageContainer.m_data = mem->data;
  220. imageContainer.m_size = mem->size;
  221. imageContainer.m_offset = 0;
  222. imageContainer.m_width = width;
  223. imageContainer.m_height = height;
  224. imageContainer.m_depth = 1;
  225. imageContainer.m_format = bgfx::TextureFormat::RGBA8;
  226. imageContainer.m_numMips = 1;
  227. imageContainer.m_hasAlpha = true;
  228. imageContainer.m_cubeMap = false;
  229. imageContainer.m_ktx = false;
  230. imageContainer.m_ktxLE = false;
  231. imageContainer.m_srgb = false;
  232. }
  233. }
  234. if (loaded)
  235. {
  236. bx::CrtAllocator allocator;
  237. const Memory* output = NULL;
  238. ImageMip mip;
  239. if (imageGetRawData(imageContainer, 0, 0, mem->data, mem->size, mip) )
  240. {
  241. uint8_t numMips = mips
  242. ? imageGetNumMips(format, mip.m_width, mip.m_height)
  243. : 1
  244. ;
  245. void* temp = NULL;
  246. if (normalMap)
  247. {
  248. uint32_t size = imageGetSize(TextureFormat::RGBA32F, mip.m_width, mip.m_height);
  249. temp = BX_ALLOC(&allocator, size);
  250. float* rgba = (float*)temp;
  251. imageDecodeToRgba32f(&allocator
  252. , rgba
  253. , mip.m_data
  254. , mip.m_width
  255. , mip.m_height
  256. , mip.m_width*mip.m_bpp/8
  257. , TextureFormat::Enum(mip.m_format)
  258. );
  259. imageContainer.m_size = imageGetSize(format, mip.m_width, mip.m_height, 0, false, numMips);
  260. imageContainer.m_format = format;
  261. output = alloc(imageContainer.m_size);
  262. imageEncodeFromRgba32f(output->data, rgba, mip.m_width, mip.m_height, format);
  263. for (uint8_t lod = 1; lod < numMips; ++lod)
  264. {
  265. ImageMip mip1;
  266. imageGetRawData(imageContainer, 0, lod, output->data, output->size, mip1);
  267. uint8_t* data = const_cast<uint8_t*>(mip1.m_data);
  268. uint32_t width = bx::uint32_max(1, mip.m_width >>lod);
  269. uint32_t height = bx::uint32_max(1, mip.m_height>>lod);
  270. imageRgba32fDownsample2x2NormalMap(width, height, width*4, rgba, rgba);
  271. imageEncodeFromRgba32f(data, rgba, mip.m_width, mip.m_height, format);
  272. }
  273. }
  274. else
  275. {
  276. uint32_t size = imageGetSize(TextureFormat::RGBA8, mip.m_width, mip.m_height);
  277. temp = BX_ALLOC(&allocator, size);
  278. uint8_t* rgba = (uint8_t*)temp;
  279. imageContainer.m_size = imageGetSize(format, mip.m_width, mip.m_height, 0, false, numMips);
  280. imageContainer.m_format = format;
  281. output = alloc(imageContainer.m_size);
  282. imageEncodeFromRgba8(output->data, rgba, mip.m_width, mip.m_height, format);
  283. for (uint8_t lod = 1; lod < numMips; ++lod)
  284. {
  285. ImageMip mip1;
  286. imageGetRawData(imageContainer, 0, lod, output->data, output->size, mip1);
  287. uint8_t* data = const_cast<uint8_t*>(mip1.m_data);
  288. uint32_t width = bx::uint32_max(1, mip.m_width >>lod);
  289. uint32_t height = bx::uint32_max(1, mip.m_height>>lod);
  290. imageRgba8Downsample2x2(width, height, width*4, rgba, rgba);
  291. imageEncodeFromRgba8(data, rgba, mip.m_width, mip.m_height, format);
  292. }
  293. }
  294. BX_FREE(&allocator, temp);
  295. }
  296. if (NULL != output)
  297. {
  298. bx::CrtFileWriter writer;
  299. if (0 == bx::open(&writer, outputFileName) )
  300. {
  301. if (NULL != bx::stristr(outputFileName, ".ktx") )
  302. {
  303. imageWriteKtx(&writer, imageContainer, output->data, output->size);
  304. }
  305. bx::close(&writer);
  306. }
  307. release(output);
  308. }
  309. }
  310. release(mem);
  311. }
  312. return EXIT_SUCCESS;
  313. }