ImageImporterMain.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. -to-linear : Convert sRGB to linear
  19. -to-srgb : Convert linear to sRGB
  20. -flip-image <0|1> : Flip the image. Default is 1
  21. )";
  22. static Error parseCommandLineArgs(int argc, char** argv, ImageImporterConfig& config,
  23. DynamicArrayAuto<StringAuto>& filenames, DynamicArrayAuto<CString>& cfilenames)
  24. {
  25. config.m_compressions = ImageBinaryDataCompression::S3TC | ImageBinaryDataCompression::ASTC;
  26. config.m_noAlpha = false;
  27. config.m_astcBlockSize = UVec2(8u);
  28. // Parse config
  29. if(argc < 3)
  30. {
  31. return Error::USER_DATA;
  32. }
  33. for(I i = 1; i < argc; i++)
  34. {
  35. if(CString(argv[i]) == "-t")
  36. {
  37. ++i;
  38. if(i >= argc)
  39. {
  40. return Error::USER_DATA;
  41. }
  42. if(CString(argv[i]) == "2D")
  43. {
  44. config.m_type = ImageBinaryType::_2D;
  45. }
  46. else if(CString(argv[i]) == "3D")
  47. {
  48. config.m_type = ImageBinaryType::_3D;
  49. }
  50. else if(CString(argv[i]) == "Cube")
  51. {
  52. config.m_type = ImageBinaryType::CUBE;
  53. }
  54. else if(CString(argv[i]) == "2DArray")
  55. {
  56. config.m_type = ImageBinaryType::_2D_ARRAY;
  57. }
  58. else
  59. {
  60. return Error::USER_DATA;
  61. }
  62. }
  63. else if(CString(argv[i]) == "-no-alpha")
  64. {
  65. config.m_noAlpha = true;
  66. }
  67. else if(CString(argv[i]) == "-store-s3tc")
  68. {
  69. ++i;
  70. if(i >= argc)
  71. {
  72. return Error::USER_DATA;
  73. }
  74. if(CString(argv[i]) == "1")
  75. {
  76. config.m_compressions |= ImageBinaryDataCompression::S3TC;
  77. }
  78. else if(CString(argv[i]) == "0")
  79. {
  80. config.m_compressions = config.m_compressions & ~ImageBinaryDataCompression::S3TC;
  81. }
  82. else
  83. {
  84. return Error::USER_DATA;
  85. }
  86. }
  87. else if(CString(argv[i]) == "-store-astc")
  88. {
  89. ++i;
  90. if(i >= argc)
  91. {
  92. return Error::USER_DATA;
  93. }
  94. if(CString(argv[i]) == "1")
  95. {
  96. config.m_compressions |= ImageBinaryDataCompression::ASTC;
  97. }
  98. else if(CString(argv[i]) == "0")
  99. {
  100. config.m_compressions = config.m_compressions & ~ImageBinaryDataCompression::ASTC;
  101. }
  102. else
  103. {
  104. return Error::USER_DATA;
  105. }
  106. }
  107. else if(CString(argv[i]) == "-store-raw")
  108. {
  109. ++i;
  110. if(i >= argc)
  111. {
  112. return Error::USER_DATA;
  113. }
  114. if(CString(argv[i]) == "1")
  115. {
  116. config.m_compressions |= ImageBinaryDataCompression::RAW;
  117. }
  118. else if(CString(argv[i]) == "0")
  119. {
  120. config.m_compressions = config.m_compressions & ~ImageBinaryDataCompression::RAW;
  121. }
  122. else
  123. {
  124. return Error::USER_DATA;
  125. }
  126. }
  127. else if(CString(argv[i]) == "-astc-block-size")
  128. {
  129. ++i;
  130. if(i >= argc)
  131. {
  132. return Error::USER_DATA;
  133. }
  134. if(CString(argv[i]) == "4x4")
  135. {
  136. config.m_astcBlockSize = UVec2(4u);
  137. }
  138. else if(CString(argv[i]) == "8x8")
  139. {
  140. config.m_astcBlockSize = UVec2(8u);
  141. }
  142. else
  143. {
  144. return Error::USER_DATA;
  145. }
  146. }
  147. else if(CString(argv[i]) == "-mip-count")
  148. {
  149. ++i;
  150. if(i >= argc)
  151. {
  152. return Error::USER_DATA;
  153. }
  154. ANKI_CHECK(CString(argv[i]).toNumber(config.m_mipmapCount));
  155. }
  156. else if(CString(argv[i]) == "-verbose")
  157. {
  158. LoggerSingleton::get().enableVerbosity(true);
  159. }
  160. else if(CString(argv[i]) == "-to-linear")
  161. {
  162. config.m_sRgbToLinear = true;
  163. }
  164. else if(CString(argv[i]) == "-to-srgb")
  165. {
  166. config.m_linearToSRgb = true;
  167. }
  168. else if(CString(argv[i]) == "-flip-image")
  169. {
  170. ++i;
  171. if(i >= argc)
  172. {
  173. return Error::USER_DATA;
  174. }
  175. if(CString(argv[i]) == "1")
  176. {
  177. config.m_flipImage = true;
  178. }
  179. else if(CString(argv[i]) == "0")
  180. {
  181. config.m_flipImage = false;
  182. }
  183. else
  184. {
  185. return Error::USER_DATA;
  186. }
  187. }
  188. else
  189. {
  190. filenames.emplaceBack(filenames.getAllocator(), argv[i]);
  191. }
  192. }
  193. if(filenames.getSize() < 2)
  194. {
  195. return Error::USER_DATA;
  196. }
  197. cfilenames.create(filenames.getSize());
  198. for(U32 i = 0; i < filenames.getSize(); ++i)
  199. {
  200. cfilenames[i] = filenames[i];
  201. }
  202. config.m_inputFilenames = ConstWeakArray<CString>(&cfilenames[0], cfilenames.getSize() - 1);
  203. config.m_outFilename = cfilenames.getBack();
  204. return Error::NONE;
  205. }
  206. int main(int argc, char** argv)
  207. {
  208. HeapAllocator<U8> alloc(allocAligned, nullptr);
  209. ImageImporterConfig config;
  210. config.m_allocator = alloc;
  211. DynamicArrayAuto<StringAuto> filenames(alloc);
  212. DynamicArrayAuto<CString> cfilenames(alloc);
  213. if(parseCommandLineArgs(argc, argv, config, filenames, cfilenames))
  214. {
  215. ANKI_IMPORTER_LOGE(USAGE, argv[0]);
  216. return 1;
  217. }
  218. StringAuto tmp(alloc);
  219. if(getTempDirectory(tmp))
  220. {
  221. ANKI_IMPORTER_LOGE("getTempDirectory() failed");
  222. return 1;
  223. }
  224. config.m_tempDirectory = tmp;
  225. StringAuto p(alloc);
  226. getParentFilepath(argv[0], p);
  227. StringAuto compressonatorPath(alloc);
  228. compressonatorPath.sprintf("%s/../../ThirdParty/Bin/Compressonator:%s", p.cstr(), getenv("PATH"));
  229. config.m_compressonatorPath = compressonatorPath;
  230. StringAuto astcencPath(alloc);
  231. astcencPath.sprintf("%s/../../ThirdParty/Bin:%s", p.cstr(), getenv("PATH"));
  232. config.m_astcencPath = astcencPath;
  233. ANKI_IMPORTER_LOGI("Image importing started: %s", config.m_outFilename.cstr());
  234. if(importImage(config))
  235. {
  236. ANKI_IMPORTER_LOGE("Importing failed");
  237. return 1;
  238. }
  239. ANKI_IMPORTER_LOGI("Image importing completed: %s", config.m_outFilename.cstr());
  240. return 0;
  241. }