ImageImporterMain.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. using namespace anki;
  7. static const char* USAGE = R"(Usage: %s in_files out_file [options]
  8. Options:
  9. -t <type> : Image type. One of: 2D, 3D, Cube, 2DArray
  10. -no-alpha : If the image has alpha don't store it. By default it stores it
  11. -store-s3tc <0|1> : Store S3TC images. Default is 1
  12. -store-raw <0|1> : Store RAW images. Default is 0
  13. -mip-count <number> : Max number of mipmaps. By default store until 4x4
  14. )";
  15. static Error parseCommandLineArgs(int argc, char** argv, ImageImporterConfig& config,
  16. DynamicArrayAuto<StringAuto>& filenames)
  17. {
  18. config.m_compressions = ImageBinaryDataCompression::NONE;
  19. config.m_noAlpha = false;
  20. // Parse config
  21. if(argc < 3)
  22. {
  23. return Error::USER_DATA;
  24. }
  25. for(I i = 1; i < argc; i++)
  26. {
  27. if(CString(argv[i]) == "-t")
  28. {
  29. ++i;
  30. if(i >= argc)
  31. {
  32. return Error::USER_DATA;
  33. }
  34. if(CString(argv[i]) == "2D")
  35. {
  36. config.m_type = ImageBinaryType::_2D;
  37. }
  38. else if(CString(argv[i]) == "3D")
  39. {
  40. config.m_type = ImageBinaryType::_3D;
  41. }
  42. else if(CString(argv[i]) == "Cube")
  43. {
  44. config.m_type = ImageBinaryType::CUBE;
  45. }
  46. else if(CString(argv[i]) == "2DArray")
  47. {
  48. config.m_type = ImageBinaryType::_2D_ARRAY;
  49. }
  50. else
  51. {
  52. return Error::USER_DATA;
  53. }
  54. }
  55. else if(CString(argv[i]) == "-no-alpha")
  56. {
  57. config.m_noAlpha = true;
  58. }
  59. else if(CString(argv[i]) == "-store-s3tc")
  60. {
  61. ++i;
  62. if(i >= argc)
  63. {
  64. return Error::USER_DATA;
  65. }
  66. if(CString(argv[i]) == "1")
  67. {
  68. config.m_compressions |= ImageBinaryDataCompression::S3TC;
  69. }
  70. else if(CString(argv[i]) != "0")
  71. {
  72. return Error::USER_DATA;
  73. }
  74. }
  75. else if(CString(argv[i]) == "-store-raw")
  76. {
  77. ++i;
  78. if(i >= argc)
  79. {
  80. return Error::USER_DATA;
  81. }
  82. if(CString(argv[i]) == "1")
  83. {
  84. config.m_compressions |= ImageBinaryDataCompression::RAW;
  85. }
  86. else if(CString(argv[i]) != "0")
  87. {
  88. return Error::USER_DATA;
  89. }
  90. }
  91. else if(CString(argv[i]) == "-mip-count")
  92. {
  93. ++i;
  94. if(i >= argc)
  95. {
  96. return Error::USER_DATA;
  97. }
  98. ANKI_CHECK(CString(argv[i]).toNumber(config.m_mipmapCount));
  99. }
  100. else
  101. {
  102. filenames.emplaceBack(filenames.getAllocator(), argv[i]);
  103. }
  104. }
  105. const PtrSize listSize = filenames.getSize();
  106. if(listSize < 3)
  107. {
  108. return Error::USER_DATA;
  109. }
  110. DynamicArrayAuto<CString> cfilenames(filenames.getAllocator(), 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. if(parseCommandLineArgs(argc, argv, config, filenames))
  126. {
  127. ANKI_IMPORTER_LOGE(USAGE, argv[0]);
  128. return 1;
  129. }
  130. if(importImage(config))
  131. {
  132. ANKI_IMPORTER_LOGE("Importing failed");
  133. return 1;
  134. }
  135. return 0;
  136. }