ImageImporterMain.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Importer/ImageImporter.h>
  6. #include <AnKi/Util/Filesystem.h>
  7. using namespace anki;
  8. static const char* USAGE = R"(Usage: %s in_files out_file [options]
  9. Options:
  10. -t <type> : Image type. One of: 2D, 3D, Cube, 2DArray
  11. -no-alpha : If the image has alpha don't store it. By default it stores it
  12. -store-s3tc <0|1> : Store S3TC images. Default is 1
  13. -store-astc <0|1> : Store ASTC images. Default is 1
  14. -store-raw <0|1> : Store RAW images. Default is 0
  15. -mip-count <number> : Max number of mipmaps. By default store until 4x4
  16. -astc-block-size <XxY> : The size of the ASTC block size. eg 4x4. Default is 8x8
  17. -verbose : Verbose log
  18. )";
  19. static Error parseCommandLineArgs(int argc, char** argv, ImageImporterConfig& config,
  20. DynamicArrayAuto<StringAuto>& filenames, DynamicArrayAuto<CString>& cfilenames)
  21. {
  22. config.m_compressions = ImageBinaryDataCompression::S3TC | ImageBinaryDataCompression::ASTC;
  23. config.m_noAlpha = false;
  24. config.m_astcBlockSize = UVec2(8u);
  25. // Parse config
  26. if(argc < 3)
  27. {
  28. return Error::USER_DATA;
  29. }
  30. for(I i = 1; i < argc; i++)
  31. {
  32. if(CString(argv[i]) == "-t")
  33. {
  34. ++i;
  35. if(i >= argc)
  36. {
  37. return Error::USER_DATA;
  38. }
  39. if(CString(argv[i]) == "2D")
  40. {
  41. config.m_type = ImageBinaryType::_2D;
  42. }
  43. else if(CString(argv[i]) == "3D")
  44. {
  45. config.m_type = ImageBinaryType::_3D;
  46. }
  47. else if(CString(argv[i]) == "Cube")
  48. {
  49. config.m_type = ImageBinaryType::CUBE;
  50. }
  51. else if(CString(argv[i]) == "2DArray")
  52. {
  53. config.m_type = ImageBinaryType::_2D_ARRAY;
  54. }
  55. else
  56. {
  57. return Error::USER_DATA;
  58. }
  59. }
  60. else if(CString(argv[i]) == "-no-alpha")
  61. {
  62. config.m_noAlpha = true;
  63. }
  64. else if(CString(argv[i]) == "-store-s3tc")
  65. {
  66. ++i;
  67. if(i >= argc)
  68. {
  69. return Error::USER_DATA;
  70. }
  71. if(CString(argv[i]) == "1")
  72. {
  73. config.m_compressions |= ImageBinaryDataCompression::S3TC;
  74. }
  75. else if(CString(argv[i]) == "0")
  76. {
  77. config.m_compressions = config.m_compressions & ~ImageBinaryDataCompression::S3TC;
  78. }
  79. else
  80. {
  81. return Error::USER_DATA;
  82. }
  83. }
  84. else if(CString(argv[i]) == "-store-astc")
  85. {
  86. ++i;
  87. if(i >= argc)
  88. {
  89. return Error::USER_DATA;
  90. }
  91. if(CString(argv[i]) == "1")
  92. {
  93. config.m_compressions |= ImageBinaryDataCompression::ASTC;
  94. }
  95. else if(CString(argv[i]) == "0")
  96. {
  97. config.m_compressions = config.m_compressions & ~ImageBinaryDataCompression::ASTC;
  98. }
  99. else
  100. {
  101. return Error::USER_DATA;
  102. }
  103. }
  104. else if(CString(argv[i]) == "-store-raw")
  105. {
  106. ++i;
  107. if(i >= argc)
  108. {
  109. return Error::USER_DATA;
  110. }
  111. if(CString(argv[i]) == "1")
  112. {
  113. config.m_compressions |= ImageBinaryDataCompression::RAW;
  114. }
  115. else if(CString(argv[i]) == "0")
  116. {
  117. config.m_compressions = config.m_compressions & ~ImageBinaryDataCompression::RAW;
  118. }
  119. else
  120. {
  121. return Error::USER_DATA;
  122. }
  123. }
  124. else if(CString(argv[i]) == "-astc-block-size")
  125. {
  126. ++i;
  127. if(i >= argc)
  128. {
  129. return Error::USER_DATA;
  130. }
  131. if(CString(argv[i]) == "4x4")
  132. {
  133. config.m_astcBlockSize = UVec2(4u);
  134. }
  135. else if(CString(argv[i]) == "8x8")
  136. {
  137. config.m_astcBlockSize = UVec2(8u);
  138. }
  139. else
  140. {
  141. return Error::USER_DATA;
  142. }
  143. }
  144. else if(CString(argv[i]) == "-mip-count")
  145. {
  146. ++i;
  147. if(i >= argc)
  148. {
  149. return Error::USER_DATA;
  150. }
  151. ANKI_CHECK(CString(argv[i]).toNumber(config.m_mipmapCount));
  152. }
  153. else if(CString(argv[i]) == "-verbose")
  154. {
  155. LoggerSingleton::get().enableVerbosity(true);
  156. }
  157. else
  158. {
  159. filenames.emplaceBack(filenames.getAllocator(), argv[i]);
  160. }
  161. }
  162. if(filenames.getSize() < 2)
  163. {
  164. return Error::USER_DATA;
  165. }
  166. cfilenames.create(filenames.getSize());
  167. for(U32 i = 0; i < filenames.getSize(); ++i)
  168. {
  169. cfilenames[i] = filenames[i];
  170. }
  171. config.m_inputFilenames = ConstWeakArray<CString>(&cfilenames[0], cfilenames.getSize() - 1);
  172. config.m_outFilename = cfilenames.getBack();
  173. return Error::NONE;
  174. }
  175. int main(int argc, char** argv)
  176. {
  177. HeapAllocator<U8> alloc(allocAligned, nullptr);
  178. ImageImporterConfig config;
  179. config.m_allocator = alloc;
  180. DynamicArrayAuto<StringAuto> filenames(alloc);
  181. DynamicArrayAuto<CString> cfilenames(alloc);
  182. if(parseCommandLineArgs(argc, argv, config, filenames, cfilenames))
  183. {
  184. ANKI_IMPORTER_LOGE(USAGE, argv[0]);
  185. return 1;
  186. }
  187. StringAuto tmp(alloc);
  188. if(getTempDirectory(tmp))
  189. {
  190. ANKI_IMPORTER_LOGE("getTempDirectory() failed");
  191. return 1;
  192. }
  193. config.m_tempDirectory = tmp;
  194. StringAuto p(alloc);
  195. getParentFilepath(argv[0], p);
  196. StringAuto compressonatorPath(alloc);
  197. compressonatorPath.sprintf("%s/../../ThirdParty/Bin/Compressonator:%s", p.cstr(), getenv("PATH"));
  198. config.m_compressonatorPath = compressonatorPath;
  199. StringAuto astcencPath(alloc);
  200. astcencPath.sprintf("%s/../../ThirdParty/Bin:%s", p.cstr(), getenv("PATH"));
  201. config.m_astcencPath = astcencPath;
  202. ANKI_IMPORTER_LOGI("Image importing started: %s", config.m_outFilename.cstr());
  203. if(importImage(config))
  204. {
  205. ANKI_IMPORTER_LOGE("Importing failed");
  206. return 1;
  207. }
  208. ANKI_IMPORTER_LOGI("Image importing completed: %s", config.m_outFilename.cstr());
  209. return 0;
  210. }