NormalMapTool.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Exception.h"
  24. #include "File.h"
  25. #include "Image.h"
  26. #include "StringUtils.h"
  27. #include <cstdlib>
  28. #include <iostream>
  29. #include <string>
  30. #include <stb_image.h>
  31. #include <stb_image_write.h>
  32. #include "DebugNew.h"
  33. int main(int argc, char** argv);
  34. void run(const std::vector<std::string>& arguments);
  35. void errorExit(const std::string& error);
  36. int main(int argc, char** argv)
  37. {
  38. std::vector<std::string> arguments;
  39. for (int i = 1; i < argc; ++i)
  40. arguments.push_back(std::string(argv[i]));
  41. try
  42. {
  43. run(arguments);
  44. }
  45. catch (Exception& e)
  46. {
  47. std::cout << e.whatStr() << std::endl;
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. void run(const std::vector<std::string>& arguments)
  53. {
  54. if (arguments.size() < 1)
  55. {
  56. errorExit(
  57. "Usage: NormalMapTool <inputfile>\n"
  58. "Output file will be saved as inputfile.dds"
  59. );
  60. }
  61. File source(arguments[0]);
  62. Image image;
  63. image.load(source);
  64. unsigned comp = image.getComponents();
  65. if ((comp < 3) && (comp > 4))
  66. errorExit("Image must contain 3 or 4 components");
  67. SharedArrayPtr<unsigned char> buffer(new unsigned char[image.getWidth() * image.getHeight() * 4]);
  68. unsigned char* srcData = image.getData();
  69. unsigned char* destData = buffer.getPtr();
  70. for (int y = 0; y < image.getHeight(); ++y)
  71. {
  72. for (int x = 0; x < image.getWidth(); ++x)
  73. {
  74. unsigned char r = *srcData++;
  75. unsigned char g = *srcData++;
  76. unsigned char b = *srcData++;
  77. if (comp == 4)
  78. srcData++;
  79. // Store X as alpha, and Y as color. Z can be reconstructed
  80. *destData++ = g;
  81. *destData++ = g;
  82. *destData++ = g;
  83. *destData++ = r;
  84. }
  85. }
  86. std::string tempDestName = split(arguments[0], '.')[0] + ".tga";
  87. stbi_write_tga(tempDestName.c_str(), image.getWidth(), image.getHeight(), 4, buffer.getPtr());
  88. std::string command = "texconv -f DXT5 -ft DDS -if NONE " + tempDestName;
  89. int ret = system(command.c_str());
  90. if (ret)
  91. errorExit("Failed to convert to DXT5");
  92. remove(tempDestName.c_str());
  93. }
  94. void errorExit(const std::string& error)
  95. {
  96. throw Exception(error);
  97. }