ImageImporterMain.cpp 5.1 KB

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