texturec.cpp 4.9 KB

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