texturec.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale = false, bool _yflip = false)
  28. {
  29. FILE* file = fopen(_filePath, "wb");
  30. if ( NULL != file )
  31. {
  32. uint8_t type = _grayscale ? 3 : 2;
  33. uint8_t bpp = _grayscale ? 8 : 32;
  34. putc(0, file);
  35. putc(0, file);
  36. putc(type, file);
  37. putc(0, file);
  38. putc(0, file);
  39. putc(0, file);
  40. putc(0, file);
  41. putc(0, file);
  42. putc(0, file);
  43. putc(0, file);
  44. putc(0, file);
  45. putc(0, file);
  46. putc(_width&0xff, file);
  47. putc( (_width>>8)&0xff, file);
  48. putc(_height&0xff, file);
  49. putc( (_height>>8)&0xff, file);
  50. putc(bpp, file);
  51. putc(32, file);
  52. uint32_t dstPitch = _width*bpp/8;
  53. if (_yflip)
  54. {
  55. uint8_t* data = (uint8_t*)_src + dstPitch*_height - _srcPitch;
  56. for (uint32_t yy = 0; yy < _height; ++yy)
  57. {
  58. fwrite(data, dstPitch, 1, file);
  59. data -= _srcPitch;
  60. }
  61. }
  62. else
  63. {
  64. uint8_t* data = (uint8_t*)_src;
  65. for (uint32_t yy = 0; yy < _height; ++yy)
  66. {
  67. fwrite(data, dstPitch, 1, file);
  68. data += _srcPitch;
  69. }
  70. }
  71. fclose(file);
  72. }
  73. }
  74. }
  75. int main(int _argc, const char* _argv[])
  76. {
  77. bx::CommandLine cmdLine(_argc, _argv);
  78. const char* inputFileName = cmdLine.findOption('i');
  79. if (NULL == inputFileName)
  80. {
  81. return 0;
  82. }
  83. bx::CrtFileReader reader;
  84. bx::open(&reader, inputFileName);
  85. uint32_t size = (uint32_t)bx::getSize(&reader);
  86. const Memory* mem = alloc(size);
  87. bx::read(&reader, mem->data, mem->size);
  88. bx::close(&reader);
  89. ImageContainer imageContainer;
  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, mip.m_data, mip.m_width, mip.m_height, mip.m_width*4, mip.m_format);
  131. }
  132. char filePath[256];
  133. bx::snprintf(filePath, sizeof(filePath), "mip%d_%d.tga", side, lod);
  134. saveTga(filePath, width, height, dstpitch, bits);
  135. free(bits);
  136. }
  137. width >>= 1;
  138. height >>= 1;
  139. }
  140. }
  141. }
  142. else
  143. {
  144. for (uint32_t lod = 0, num = imageContainer.m_numMips; lod < num; ++lod)
  145. {
  146. ImageMip mip;
  147. if (imageGetRawData(imageContainer, 0, lod, mem->data, mem->size, mip) )
  148. {
  149. char filePath[256];
  150. bx::snprintf(filePath, sizeof(filePath), "mip%d.bin", lod);
  151. bx::CrtFileWriter writer;
  152. bx::open(&writer, filePath);
  153. printf("mip%d, size %d\n", lod, mip.m_size);
  154. bx::write(&writer, mip.m_data, mip.m_size);
  155. bx::close(&writer);
  156. }
  157. }
  158. }
  159. }
  160. return EXIT_SUCCESS;
  161. }