ImageImporterMain.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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-raw <0|1> : Store RAW images. Default is 0
  14. -mip-count <number> : Max number of mipmaps. By default store until 4x4
  15. )";
  16. static Error parseCommandLineArgs(int argc, char** argv, ImageImporterConfig& config,
  17. DynamicArrayAuto<StringAuto>& filenames, DynamicArrayAuto<CString>& cfilenames)
  18. {
  19. config.m_compressions = ImageBinaryDataCompression::S3TC;
  20. config.m_noAlpha = false;
  21. // Parse config
  22. if(argc < 3)
  23. {
  24. return Error::USER_DATA;
  25. }
  26. for(I i = 1; i < argc; i++)
  27. {
  28. if(CString(argv[i]) == "-t")
  29. {
  30. ++i;
  31. if(i >= argc)
  32. {
  33. return Error::USER_DATA;
  34. }
  35. if(CString(argv[i]) == "2D")
  36. {
  37. config.m_type = ImageBinaryType::_2D;
  38. }
  39. else if(CString(argv[i]) == "3D")
  40. {
  41. config.m_type = ImageBinaryType::_3D;
  42. }
  43. else if(CString(argv[i]) == "Cube")
  44. {
  45. config.m_type = ImageBinaryType::CUBE;
  46. }
  47. else if(CString(argv[i]) == "2DArray")
  48. {
  49. config.m_type = ImageBinaryType::_2D_ARRAY;
  50. }
  51. else
  52. {
  53. return Error::USER_DATA;
  54. }
  55. }
  56. else if(CString(argv[i]) == "-no-alpha")
  57. {
  58. config.m_noAlpha = true;
  59. }
  60. else if(CString(argv[i]) == "-store-s3tc")
  61. {
  62. ++i;
  63. if(i >= argc)
  64. {
  65. return Error::USER_DATA;
  66. }
  67. if(CString(argv[i]) == "1")
  68. {
  69. config.m_compressions |= ImageBinaryDataCompression::S3TC;
  70. }
  71. else if(CString(argv[i]) != "0")
  72. {
  73. return Error::USER_DATA;
  74. }
  75. }
  76. else if(CString(argv[i]) == "-store-raw")
  77. {
  78. ++i;
  79. if(i >= argc)
  80. {
  81. return Error::USER_DATA;
  82. }
  83. if(CString(argv[i]) == "1")
  84. {
  85. config.m_compressions |= ImageBinaryDataCompression::RAW;
  86. }
  87. else if(CString(argv[i]) != "0")
  88. {
  89. return Error::USER_DATA;
  90. }
  91. }
  92. else if(CString(argv[i]) == "-mip-count")
  93. {
  94. ++i;
  95. if(i >= argc)
  96. {
  97. return Error::USER_DATA;
  98. }
  99. ANKI_CHECK(CString(argv[i]).toNumber(config.m_mipmapCount));
  100. }
  101. else
  102. {
  103. filenames.emplaceBack(filenames.getAllocator(), argv[i]);
  104. }
  105. }
  106. if(filenames.getSize() < 2)
  107. {
  108. return Error::USER_DATA;
  109. }
  110. cfilenames.create(filenames.getSize());
  111. for(U32 i = 0; i < filenames.getSize(); ++i)
  112. {
  113. cfilenames[i] = filenames[i];
  114. }
  115. config.m_inputFilenames = ConstWeakArray<CString>(&cfilenames[0], cfilenames.getSize() - 1);
  116. config.m_outFilename = cfilenames.getBack();
  117. return Error::NONE;
  118. }
  119. int main(int argc, char** argv)
  120. {
  121. HeapAllocator<U8> alloc(allocAligned, nullptr);
  122. ImageImporterConfig config;
  123. config.m_allocator = alloc;
  124. DynamicArrayAuto<StringAuto> filenames(alloc);
  125. DynamicArrayAuto<CString> cfilenames(alloc);
  126. if(parseCommandLineArgs(argc, argv, config, filenames, cfilenames))
  127. {
  128. ANKI_IMPORTER_LOGE(USAGE, argv[0]);
  129. return 1;
  130. }
  131. StringAuto tmp(alloc);
  132. if(getTempDirectory(tmp))
  133. {
  134. ANKI_IMPORTER_LOGE("getTempDirectory() failed");
  135. return 1;
  136. }
  137. config.m_tempDirectory = tmp;
  138. StringAuto p(alloc);
  139. getParentFilepath(argv[0], p);
  140. StringAuto compressonatorPath(alloc);
  141. compressonatorPath.sprintf("%s/../../ThirdParty/Bin/Compressonator:%s", p.cstr(), getenv("PATH"));
  142. config.m_compressonatorPath = compressonatorPath;
  143. if(importImage(config))
  144. {
  145. ANKI_IMPORTER_LOGE("Importing failed");
  146. return 1;
  147. }
  148. return 0;
  149. }