texturec.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2011-2013 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 "dds.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, bool _yflip)
  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. long int fsize(FILE* _file)
  76. {
  77. long int pos = ftell(_file);
  78. fseek(_file, 0L, SEEK_END);
  79. long int size = ftell(_file);
  80. fseek(_file, pos, SEEK_SET);
  81. return size;
  82. }
  83. int main(int _argc, const char* _argv[])
  84. {
  85. bx::CommandLine cmdLine(_argc, _argv);
  86. FILE* file = fopen(_argv[1], "rb");
  87. uint32_t size = fsize(file);
  88. const Memory* mem = alloc(size);
  89. size_t readSize = fread(mem->data, 1, size, file);
  90. BX_UNUSED(readSize);
  91. fclose(file);
  92. Dds dds;
  93. if (parseDds(dds, mem) )
  94. {
  95. bool decompress = cmdLine.hasArg('d');
  96. if (decompress
  97. || 0 == dds.m_type)
  98. {
  99. for (uint8_t side = 0, numSides = dds.m_cubeMap ? 6 : 1; side < numSides; ++side)
  100. {
  101. uint32_t width = dds.m_width;
  102. uint32_t height = dds.m_height;
  103. for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
  104. {
  105. width = uint32_max(1, width);
  106. height = uint32_max(1, height);
  107. Mip mip;
  108. if (getRawImageData(dds, side, lod, mem, mip) )
  109. {
  110. uint32_t dstpitch = width*4;
  111. uint8_t* bits = (uint8_t*)malloc(dstpitch*height);
  112. if (width != mip.m_width
  113. || height != mip.m_height)
  114. {
  115. uint8_t* temp = (uint8_t*)realloc(NULL, mip.m_width*mip.m_height*4);
  116. mip.decode(temp);
  117. uint32_t srcpitch = mip.m_width*4;
  118. for (uint32_t yy = 0; yy < height; ++yy)
  119. {
  120. uint8_t* src = &temp[yy*srcpitch];
  121. uint8_t* dst = &bits[yy*dstpitch];
  122. for (uint32_t xx = 0; xx < width; ++xx)
  123. {
  124. memcpy(dst, src, 4);
  125. dst += 4;
  126. src += 4;
  127. }
  128. }
  129. free(temp);
  130. }
  131. else
  132. {
  133. mip.decode(bits);
  134. }
  135. char filePath[256];
  136. bx::snprintf(filePath, sizeof(filePath), "mip%d_%d.tga", side, lod);
  137. saveTga(filePath, width, height, dstpitch, bits);
  138. free(bits);
  139. }
  140. width >>= 1;
  141. height >>= 1;
  142. }
  143. }
  144. }
  145. else
  146. {
  147. for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
  148. {
  149. Mip mip;
  150. if (getRawImageData(dds, 0, lod, mem, mip) )
  151. {
  152. char filePath[256];
  153. bx::snprintf(filePath, sizeof(filePath), "mip%d.bin", lod);
  154. file = fopen(filePath, "wb");
  155. fwrite(mip.m_data, 1, mip.m_size, file);
  156. fclose(file);
  157. }
  158. }
  159. }
  160. }
  161. return EXIT_SUCCESS;
  162. }