texturec.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/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(uint8_t* _dst, const uint8_t* _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(_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(_src, _width, _height, 4, _dst);
  67. break;
  68. case TextureFormat::BC7:
  69. nvtt::compressBC7(_src, _width, _height, 4, _dst);
  70. break;
  71. case TextureFormat::ETC1:
  72. etc1_encode_image(_src, _width, _height, 4, _width*4, _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 = const_cast<uint8_t*>(_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 = const_cast<uint8_t*>(_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::RGBA8:
  107. memcpy(_dst, _src, _width*_height*4);
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. } // namespace bgfx
  114. void help(const char* _error = NULL)
  115. {
  116. if (NULL != _error)
  117. {
  118. fprintf(stderr, "Error:\n%s\n\n", _error);
  119. }
  120. fprintf(stderr
  121. , "texturec, bgfx texture compiler tool\n"
  122. "Copyright 2011-2015 Branimir Karadzic. All rights reserved.\n"
  123. "License: http://www.opensource.org/licenses/BSD-2-Clause\n\n"
  124. );
  125. }
  126. int main(int _argc, const char* _argv[])
  127. {
  128. using namespace bgfx;
  129. bx::CommandLine cmdLine(_argc, _argv);
  130. if (cmdLine.hasArg('h', "help") )
  131. {
  132. help();
  133. return EXIT_FAILURE;
  134. }
  135. const char* inputFileName = cmdLine.findOption('i');
  136. if (NULL == inputFileName)
  137. {
  138. help("Input file must be specified.");
  139. return EXIT_FAILURE;
  140. }
  141. const char* outputFileName = cmdLine.findOption('o');
  142. if (NULL == outputFileName)
  143. {
  144. help("Output file must be specified.");
  145. return EXIT_FAILURE;
  146. }
  147. bx::CrtFileReader reader;
  148. if (0 != bx::open(&reader, inputFileName) )
  149. {
  150. help("Failed to open input file.");
  151. return EXIT_FAILURE;
  152. }
  153. const bool mips = cmdLine.hasArg('m', "mips");
  154. const char* type = cmdLine.findOption('t');
  155. TextureFormat::Enum format = TextureFormat::BGRA8;
  156. if (NULL != type)
  157. {
  158. if (0 == bx::stricmp(type, "bc1")
  159. || 0 == bx::stricmp(type, "dxt1") )
  160. {
  161. format = TextureFormat::BC1;
  162. }
  163. else if (0 == bx::stricmp(type, "bc2")
  164. || 0 == bx::stricmp(type, "dxt3") )
  165. {
  166. format = TextureFormat::BC2;
  167. }
  168. else if (0 == bx::stricmp(type, "bc3")
  169. || 0 == bx::stricmp(type, "dxt5") )
  170. {
  171. format = TextureFormat::BC3;
  172. }
  173. else if (0 == bx::stricmp(type, "bc4") )
  174. {
  175. format = TextureFormat::BC4;
  176. }
  177. else if (0 == bx::stricmp(type, "bc5") )
  178. {
  179. format = TextureFormat::BC5;
  180. }
  181. else if (0 == bx::stricmp(type, "etc1") )
  182. {
  183. format = TextureFormat::ETC1;
  184. }
  185. else if (0 == bx::stricmp(type, "bc6h") )
  186. {
  187. format = TextureFormat::BC6H;
  188. }
  189. else if (0 == bx::stricmp(type, "bc7") )
  190. {
  191. format = TextureFormat::BC7;
  192. }
  193. else if (0 == bx::stricmp(type, "ptc14") )
  194. {
  195. format = TextureFormat::PTC14;
  196. }
  197. else if (0 == bx::stricmp(type, "ptc14a") )
  198. {
  199. format = TextureFormat::PTC14A;
  200. }
  201. else
  202. {
  203. help("Invalid format specified.");
  204. return EXIT_FAILURE;
  205. }
  206. }
  207. uint32_t size = (uint32_t)bx::getSize(&reader);
  208. const Memory* mem = alloc(size);
  209. bx::read(&reader, mem->data, mem->size);
  210. bx::close(&reader);
  211. uint8_t* decodedImage = NULL;
  212. ImageContainer imageContainer;
  213. bool loaded = imageParse(imageContainer, mem->data, mem->size);
  214. if (!loaded)
  215. {
  216. int width = 0;
  217. int height = 0;
  218. int comp = 0;
  219. decodedImage = stbi_load_from_memory( (uint8_t*)mem->data, mem->size, &width, &height, &comp, 4);
  220. loaded = NULL != decodedImage;
  221. if (loaded)
  222. {
  223. release(mem);
  224. mem = makeRef(decodedImage, width*height*4);
  225. imageContainer.m_data = mem->data;
  226. imageContainer.m_size = mem->size;
  227. imageContainer.m_offset = 0;
  228. imageContainer.m_width = width;
  229. imageContainer.m_height = height;
  230. imageContainer.m_depth = 1;
  231. imageContainer.m_format = bgfx::TextureFormat::RGBA8;
  232. imageContainer.m_numMips = 1;
  233. imageContainer.m_hasAlpha = true;
  234. imageContainer.m_cubeMap = false;
  235. imageContainer.m_ktx = false;
  236. imageContainer.m_ktxLE = false;
  237. imageContainer.m_srgb = false;
  238. }
  239. }
  240. BX_UNUSED(mips);
  241. if (loaded)
  242. {
  243. bx::CrtAllocator allocator;
  244. const Memory* output = NULL;
  245. ImageMip mip;
  246. if (imageGetRawData(imageContainer, 0, 0, mem->data, mem->size, mip) )
  247. {
  248. uint32_t size = imageGetSize(TextureFormat::RGBA8, mip.m_width, mip.m_height);
  249. uint8_t* rgba = (uint8_t*)BX_ALLOC(&allocator, size);
  250. imageDecodeToRgba8(rgba, mip.m_data, mip.m_width, mip.m_height, mip.m_width*mip.m_bpp/8, mip.m_format);
  251. imageContainer.m_size = imageGetSize(format, mip.m_width, mip.m_height);
  252. imageContainer.m_format = format;
  253. output = alloc(imageContainer.m_size);
  254. imageEncodeFromRgba8(output->data, rgba, mip.m_width, mip.m_height, format);
  255. BX_FREE(&allocator, rgba);
  256. }
  257. if (NULL != output)
  258. {
  259. bx::CrtFileWriter writer;
  260. if (0 == bx::open(&writer, outputFileName) )
  261. {
  262. if (NULL != bx::stristr(outputFileName, ".ktx") )
  263. {
  264. imageWriteKtx(&writer, imageContainer, output->data, output->size);
  265. }
  266. bx::close(&writer);
  267. }
  268. release(output);
  269. }
  270. }
  271. release(mem);
  272. return EXIT_SUCCESS;
  273. }