ddsdump.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 2011-2012 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. #define BX_NAMESPACE 1
  16. #include <bx/bx.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 saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale, bool _yflip)
  29. {
  30. FILE* file = fopen(_filePath, "wb");
  31. if ( NULL != file )
  32. {
  33. uint8_t type = _grayscale ? 3 : 2;
  34. uint8_t bpp = _grayscale ? 8 : 32;
  35. putc(0, file);
  36. putc(0, file);
  37. putc(type, 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(0, file);
  47. putc(_width&0xff, file);
  48. putc( (_width>>8)&0xff, file);
  49. putc(_height&0xff, file);
  50. putc( (_height>>8)&0xff, file);
  51. putc(bpp, file);
  52. putc(32, file);
  53. uint32_t dstPitch = _width*bpp/8;
  54. if (_yflip)
  55. {
  56. uint8_t* data = (uint8_t*)_src + dstPitch*_height - _srcPitch;
  57. for (uint32_t yy = 0; yy < _height; ++yy)
  58. {
  59. fwrite(data, dstPitch, 1, file);
  60. data -= _srcPitch;
  61. }
  62. }
  63. else
  64. {
  65. uint8_t* data = (uint8_t*)_src;
  66. for (uint32_t yy = 0; yy < _height; ++yy)
  67. {
  68. fwrite(data, dstPitch, 1, file);
  69. data += _srcPitch;
  70. }
  71. }
  72. fclose(file);
  73. }
  74. }
  75. }
  76. long int fsize(FILE* _file)
  77. {
  78. long int pos = ftell(_file);
  79. fseek(_file, 0L, SEEK_END);
  80. long int size = ftell(_file);
  81. fseek(_file, pos, SEEK_SET);
  82. return size;
  83. }
  84. int main(int _argc, const char* _argv[])
  85. {
  86. CommandLine cmdLine(_argc, _argv);
  87. FILE* file = fopen(_argv[1], "rb");
  88. uint32_t size = fsize(file);
  89. const Memory* mem = alloc(size);
  90. size_t readSize = fread(mem->data, 1, size, file);
  91. BX_UNUSED(readSize);
  92. fclose(file);
  93. Dds dds;
  94. if (parseDds(dds, mem) )
  95. {
  96. bool decompress = cmdLine.hasArg('d');
  97. if (decompress
  98. || 0 == dds.m_type)
  99. {
  100. for (uint8_t side = 0, numSides = dds.m_cubeMap ? 6 : 1; side < numSides; ++side)
  101. {
  102. uint32_t width = dds.m_width;
  103. uint32_t height = dds.m_height;
  104. for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
  105. {
  106. width = uint32_max(1, width);
  107. height = uint32_max(1, height);
  108. Mip mip;
  109. if (getRawImageData(dds, side, lod, mem, mip) )
  110. {
  111. uint32_t dstpitch = width*4;
  112. uint8_t* bits = (uint8_t*)malloc(dstpitch*height);
  113. if (width != mip.m_width
  114. || height != mip.m_height)
  115. {
  116. uint8_t* temp = (uint8_t*)realloc(NULL, mip.m_width*mip.m_height*4);
  117. mip.decode(temp);
  118. uint32_t srcpitch = mip.m_width*4;
  119. for (uint32_t yy = 0; yy < height; ++yy)
  120. {
  121. uint8_t* src = &temp[yy*srcpitch];
  122. uint8_t* dst = &bits[yy*dstpitch];
  123. for (uint32_t xx = 0; xx < width; ++xx)
  124. {
  125. memcpy(dst, src, 4);
  126. dst += 4;
  127. src += 4;
  128. }
  129. }
  130. free(temp);
  131. }
  132. else
  133. {
  134. mip.decode(bits);
  135. }
  136. char filePath[256];
  137. _snprintf(filePath, sizeof(filePath), "mip%d_%d.tga", side, lod);
  138. saveTga(filePath, width, height, dstpitch, bits);
  139. free(bits);
  140. }
  141. width >>= 1;
  142. height >>= 1;
  143. }
  144. }
  145. }
  146. else
  147. {
  148. for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
  149. {
  150. Mip mip;
  151. if (getRawImageData(dds, 0, lod, mem, mip) )
  152. {
  153. char filePath[256];
  154. _snprintf(filePath, sizeof(filePath), "mip%d.bin", lod);
  155. file = fopen(filePath, "wb");
  156. fwrite(mip.m_data, 1, mip.m_size, file);
  157. fclose(file);
  158. }
  159. }
  160. }
  161. }
  162. return EXIT_SUCCESS;
  163. }