texturec.cpp 4.8 KB

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