texturec.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. // Just hacking DDS loading code in here.
  9. #include "bgfx_p.h"
  10. using namespace bgfx;
  11. #include "image.h"
  12. #if 0
  13. # define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
  14. #endif // DEBUG
  15. #include <bx/bx.h>
  16. #include <bx/commandline.h>
  17. #include <bx/uint32_t.h>
  18. namespace bgfx
  19. {
  20. const Memory* alloc(uint32_t _size)
  21. {
  22. Memory* mem = (Memory*)::realloc(NULL, sizeof(Memory) + _size);
  23. mem->size = _size;
  24. mem->data = (uint8_t*)mem + sizeof(Memory);
  25. return mem;
  26. }
  27. void release(const Memory* _mem)
  28. {
  29. Memory* mem = const_cast<Memory*>(_mem);
  30. ::free(mem);
  31. }
  32. } // namespace bgfx
  33. void help(const char* _error = NULL)
  34. {
  35. if (NULL != _error)
  36. {
  37. fprintf(stderr, "Error:\n%s\n\n", _error);
  38. }
  39. fprintf(stderr
  40. , "texturec, bgfx texture compiler tool\n"
  41. "Copyright 2011-2015 Branimir Karadzic. All rights reserved.\n"
  42. "License: http://www.opensource.org/licenses/BSD-2-Clause\n\n"
  43. );
  44. }
  45. int main(int _argc, const char* _argv[])
  46. {
  47. bx::CommandLine cmdLine(_argc, _argv);
  48. if (cmdLine.hasArg('h', "help") )
  49. {
  50. help();
  51. return EXIT_FAILURE;
  52. }
  53. const char* inputFileName = cmdLine.findOption('i');
  54. if (NULL == inputFileName)
  55. {
  56. help("Input file must be specified.");
  57. return EXIT_FAILURE;
  58. }
  59. bx::CrtFileReader reader;
  60. if (0 != bx::open(&reader, inputFileName) )
  61. {
  62. help("Failed to open input file.");
  63. return EXIT_FAILURE;
  64. }
  65. uint32_t size = (uint32_t)bx::getSize(&reader);
  66. const Memory* mem = alloc(size);
  67. bx::read(&reader, mem->data, mem->size);
  68. bx::close(&reader);
  69. ImageContainer imageContainer;
  70. if (imageParse(imageContainer, mem->data, mem->size) )
  71. {
  72. bool decompress = cmdLine.hasArg('d');
  73. if (decompress
  74. || 0 == imageContainer.m_format)
  75. {
  76. for (uint8_t side = 0, numSides = imageContainer.m_cubeMap ? 6 : 1; side < numSides; ++side)
  77. {
  78. uint32_t width = imageContainer.m_width;
  79. uint32_t height = imageContainer.m_height;
  80. for (uint32_t lod = 0, num = imageContainer.m_numMips; lod < num; ++lod)
  81. {
  82. width = bx::uint32_max(1, width);
  83. height = bx::uint32_max(1, height);
  84. ImageMip mip;
  85. if (imageGetRawData(imageContainer, side, lod, mem->data, mem->size, mip) )
  86. {
  87. uint32_t dstpitch = width*4;
  88. uint8_t* bits = (uint8_t*)malloc(dstpitch*height);
  89. if (width != mip.m_width
  90. || height != mip.m_height)
  91. {
  92. uint8_t* temp = (uint8_t*)realloc(NULL, mip.m_width*mip.m_height*4);
  93. imageDecodeToBgra8(temp, mip.m_data, mip.m_width, mip.m_height, mip.m_width*4, mip.m_format);
  94. uint32_t srcpitch = mip.m_width*4;
  95. for (uint32_t yy = 0; yy < height; ++yy)
  96. {
  97. uint8_t* src = &temp[yy*srcpitch];
  98. uint8_t* dst = &bits[yy*dstpitch];
  99. for (uint32_t xx = 0; xx < width; ++xx)
  100. {
  101. memcpy(dst, src, 4);
  102. dst += 4;
  103. src += 4;
  104. }
  105. }
  106. free(temp);
  107. }
  108. else
  109. {
  110. imageDecodeToBgra8(bits
  111. , mip.m_data
  112. , mip.m_width
  113. , mip.m_height
  114. , mip.m_width*4
  115. , mip.m_format
  116. );
  117. }
  118. char filePath[256];
  119. bx::snprintf(filePath, sizeof(filePath), "mip%d_%d.ktx", side, lod);
  120. bx::CrtFileWriter writer;
  121. if (0 == bx::open(&writer, filePath) )
  122. {
  123. if (NULL != bx::stristr(filePath, ".ktx") )
  124. {
  125. imageWriteKtx(&writer
  126. , TextureFormat::BGRA8
  127. , false
  128. , width
  129. , height
  130. , 0
  131. , 1
  132. , bits
  133. );
  134. }
  135. else
  136. {
  137. imageWriteTga(&writer, width, height, dstpitch, bits, false, false);
  138. }
  139. bx::close(&writer);
  140. }
  141. free(bits);
  142. }
  143. width >>= 1;
  144. height >>= 1;
  145. }
  146. }
  147. }
  148. else
  149. {
  150. for (uint32_t lod = 0, num = imageContainer.m_numMips; lod < num; ++lod)
  151. {
  152. ImageMip mip;
  153. if (imageGetRawData(imageContainer, 0, lod, mem->data, mem->size, mip) )
  154. {
  155. char filePath[256];
  156. bx::snprintf(filePath, sizeof(filePath), "mip%d.bin", lod);
  157. bx::CrtFileWriter writer;
  158. if (0 == bx::open(&writer, filePath) )
  159. {
  160. printf("mip%d, size %d\n", lod, mip.m_size);
  161. bx::write(&writer, mip.m_data, mip.m_size);
  162. bx::close(&writer);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. release(mem);
  169. return EXIT_SUCCESS;
  170. }