NormalMapTool.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "Context.h"
  24. #include "File.h"
  25. #include "Image.h"
  26. #include "ProcessUtils.h"
  27. #include "StringUtils.h"
  28. #include <cstdlib>
  29. #include <stb_image.h>
  30. #include <stb_image_write.h>
  31. #include "DebugNew.h"
  32. SharedPtr<Context> context_(new Context());
  33. int main(int argc, char** argv);
  34. void Run(const Vector<String>& arguments);
  35. int main(int argc, char** argv)
  36. {
  37. Vector<String> arguments;
  38. for (int i = 1; i < argc; ++i)
  39. arguments.Push(String(argv[i]));
  40. Run(arguments);
  41. return 0;
  42. }
  43. void Run(const Vector<String>& arguments)
  44. {
  45. if (arguments.Size() < 1)
  46. {
  47. ErrorExit(
  48. "Usage: NormalMapTool <inputfile>\n\n"
  49. "Output file will be saved as inputfile.dds\n"
  50. );
  51. }
  52. File source(context_);
  53. source.Open(arguments[0]);
  54. Image image(context_);
  55. if (!image.Load(source))
  56. ErrorExit("Could not load input file " + arguments[0]);
  57. unsigned comp = image.GetComponents();
  58. if ((comp < 3) && (comp > 4))
  59. ErrorExit("Image must contain 3 or 4 components");
  60. SharedArrayPtr<unsigned char> buffer(new unsigned char[image.GetWidth() * image.GetHeight() * 4]);
  61. unsigned char* srcData = image.GetData();
  62. unsigned char* destData = buffer.GetPtr();
  63. for (int y = 0; y < image.GetHeight(); ++y)
  64. {
  65. for (int x = 0; x < image.GetWidth(); ++x)
  66. {
  67. unsigned char r = *srcData++;
  68. unsigned char g = *srcData++;
  69. unsigned char b = *srcData++;
  70. if (comp == 4)
  71. srcData++;
  72. // Store X as alpha, and Y as color. Z can be reconstructed
  73. *destData++ = g;
  74. *destData++ = g;
  75. *destData++ = g;
  76. *destData++ = r;
  77. }
  78. }
  79. String tempDestName = arguments[0].Split('.')[0] + ".tga";
  80. stbi_write_tga(tempDestName.CString(), image.GetWidth(), image.GetHeight(), 4, buffer.GetPtr());
  81. String command = "texconv -f DXT5 -ft DDS -if NONE " + tempDestName;
  82. int ret = system(command.CString());
  83. if (ret)
  84. ErrorExit("Failed to convert to DXT5");
  85. remove(tempDestName.CString());
  86. }