ImageImporterMain.cpp 5.5 KB

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