texturec.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #if 0
  16. # define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
  17. #endif // DEBUG
  18. #include <bx/bx.h>
  19. #include <bx/allocator.h>
  20. #include <bx/commandline.h>
  21. #include <bx/uint32_t.h>
  22. namespace bgfx
  23. {
  24. const Memory* alloc(uint32_t _size)
  25. {
  26. Memory* mem = (Memory*)::realloc(NULL, sizeof(Memory) + _size);
  27. mem->size = _size;
  28. mem->data = (uint8_t*)mem + sizeof(Memory);
  29. return mem;
  30. }
  31. void release(const Memory* _mem)
  32. {
  33. Memory* mem = const_cast<Memory*>(_mem);
  34. ::free(mem);
  35. }
  36. void imageEncodeFromRgba8(uint8_t* _dst, const uint8_t* _src, uint32_t _width, uint32_t _height, uint8_t _format)
  37. {
  38. TextureFormat::Enum format = TextureFormat::Enum(_format);
  39. switch (format)
  40. {
  41. case TextureFormat::BC1:
  42. case TextureFormat::BC2:
  43. case TextureFormat::BC3:
  44. case TextureFormat::BC4:
  45. case TextureFormat::BC5:
  46. squish::CompressImage(_src, _width, _height, _dst
  47. , format == TextureFormat::BC1 ? squish::kDxt1
  48. : format == TextureFormat::BC2 ? squish::kDxt3
  49. : format == TextureFormat::BC3 ? squish::kDxt5
  50. : format == TextureFormat::BC4 ? squish::kBc4
  51. : squish::kBc5
  52. );
  53. break;
  54. case TextureFormat::BC6H:
  55. nvtt::compressBC6H(_src, _width, _height, 4, _dst);
  56. break;
  57. case TextureFormat::BC7:
  58. nvtt::compressBC7(_src, _width, _height, 4, _dst);
  59. break;
  60. case TextureFormat::ETC1:
  61. etc1_encode_image(_src, _width, _height, 4, _width*4, _dst);
  62. break;
  63. case TextureFormat::ETC2:
  64. case TextureFormat::ETC2A:
  65. case TextureFormat::ETC2A1:
  66. case TextureFormat::PTC12:
  67. break;
  68. case TextureFormat::PTC14:
  69. {
  70. using namespace Javelin;
  71. RgbBitmap bmp;
  72. bmp.width = _width;
  73. bmp.height = _height;
  74. bmp.data = const_cast<uint8_t*>(_src);
  75. PvrTcEncoder::EncodeRgb4Bpp(_dst, bmp);
  76. bmp.data = NULL;
  77. }
  78. break;
  79. case TextureFormat::PTC12A:
  80. break;
  81. case TextureFormat::PTC14A:
  82. {
  83. using namespace Javelin;
  84. RgbaBitmap bmp;
  85. bmp.width = _width;
  86. bmp.height = _height;
  87. bmp.data = const_cast<uint8_t*>(_src);
  88. PvrTcEncoder::EncodeRgba4Bpp(_dst, bmp);
  89. bmp.data = NULL;
  90. }
  91. break;
  92. case TextureFormat::PTC22:
  93. case TextureFormat::PTC24:
  94. break;
  95. default:
  96. break;
  97. }
  98. }
  99. } // namespace bgfx
  100. void help(const char* _error = NULL)
  101. {
  102. if (NULL != _error)
  103. {
  104. fprintf(stderr, "Error:\n%s\n\n", _error);
  105. }
  106. fprintf(stderr
  107. , "texturec, bgfx texture compiler tool\n"
  108. "Copyright 2011-2015 Branimir Karadzic. All rights reserved.\n"
  109. "License: http://www.opensource.org/licenses/BSD-2-Clause\n\n"
  110. );
  111. }
  112. int main(int _argc, const char* _argv[])
  113. {
  114. using namespace bgfx;
  115. bx::CommandLine cmdLine(_argc, _argv);
  116. if (cmdLine.hasArg('h', "help") )
  117. {
  118. help();
  119. return EXIT_FAILURE;
  120. }
  121. const char* inputFileName = cmdLine.findOption('i');
  122. if (NULL == inputFileName)
  123. {
  124. help("Input file must be specified.");
  125. return EXIT_FAILURE;
  126. }
  127. const char* outputFileName = cmdLine.findOption('o');
  128. if (NULL == outputFileName)
  129. {
  130. help("Output file must be specified.");
  131. return EXIT_FAILURE;
  132. }
  133. bx::CrtFileReader reader;
  134. if (0 != bx::open(&reader, inputFileName) )
  135. {
  136. help("Failed to open input file.");
  137. return EXIT_FAILURE;
  138. }
  139. const bool mips = cmdLine.hasArg('m', "mips");
  140. const char* type = cmdLine.findOption('t');
  141. TextureFormat::Enum format = TextureFormat::BGRA8;
  142. if (NULL != type)
  143. {
  144. if (0 == bx::stricmp(type, "bc1")
  145. || 0 == bx::stricmp(type, "dxt1") )
  146. {
  147. format = TextureFormat::BC1;
  148. }
  149. else if (0 == bx::stricmp(type, "bc2")
  150. || 0 == bx::stricmp(type, "dxt3") )
  151. {
  152. format = TextureFormat::BC2;
  153. }
  154. else if (0 == bx::stricmp(type, "bc3")
  155. || 0 == bx::stricmp(type, "dxt5") )
  156. {
  157. format = TextureFormat::BC3;
  158. }
  159. else if (0 == bx::stricmp(type, "bc4") )
  160. {
  161. format = TextureFormat::BC4;
  162. }
  163. else if (0 == bx::stricmp(type, "bc5") )
  164. {
  165. format = TextureFormat::BC5;
  166. }
  167. else if (0 == bx::stricmp(type, "etc1") )
  168. {
  169. format = TextureFormat::ETC1;
  170. }
  171. else if (0 == bx::stricmp(type, "bc6h") )
  172. {
  173. format = TextureFormat::BC6H;
  174. }
  175. else if (0 == bx::stricmp(type, "bc7") )
  176. {
  177. format = TextureFormat::BC7;
  178. }
  179. else if (0 == bx::stricmp(type, "ptc14") )
  180. {
  181. format = TextureFormat::PTC14;
  182. }
  183. else if (0 == bx::stricmp(type, "ptc14a") )
  184. {
  185. format = TextureFormat::PTC14A;
  186. }
  187. }
  188. uint32_t size = (uint32_t)bx::getSize(&reader);
  189. const Memory* mem = alloc(size);
  190. bx::read(&reader, mem->data, mem->size);
  191. bx::close(&reader);
  192. ImageContainer imageContainer;
  193. bool loaded = imageParse(imageContainer, mem->data, mem->size);
  194. if (!loaded)
  195. {
  196. }
  197. BX_UNUSED(mips);
  198. if (loaded)
  199. {
  200. bx::CrtAllocator allocator;
  201. uint8_t* output = NULL;
  202. ImageMip mip;
  203. if (imageGetRawData(imageContainer, 0, 0, mem->data, mem->size, mip) )
  204. {
  205. uint8_t* rgba = (uint8_t*)BX_ALLOC(&allocator, imageGetSize(TextureFormat::RGBA8, mip.m_width, mip.m_height) );
  206. imageDecodeToRgba8(rgba, mip.m_data, mip.m_width, mip.m_height, mip.m_width*mip.m_bpp/8, mip.m_format);
  207. output = (uint8_t*)BX_ALLOC(&allocator, imageGetSize(format, mip.m_width, mip.m_height) );
  208. imageContainer.m_format = format;
  209. imageEncodeFromRgba8(output, rgba, mip.m_width, mip.m_height, format);
  210. BX_FREE(&allocator, rgba);
  211. }
  212. if (NULL != output)
  213. {
  214. bx::CrtFileWriter writer;
  215. if (0 == bx::open(&writer, outputFileName) )
  216. {
  217. if (NULL != bx::stristr(outputFileName, ".ktx") )
  218. {
  219. imageWriteKtx(&writer, imageContainer, mem->data, mem->size);
  220. }
  221. bx::close(&writer);
  222. }
  223. BX_FREE(&allocator, output);
  224. }
  225. }
  226. release(mem);
  227. return EXIT_SUCCESS;
  228. }