ddsdump.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include "bgfx_p.h"
  9. using namespace bgfx;
  10. #include "dds.h"
  11. #if 0
  12. # define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
  13. #endif // DEBUG
  14. #define BX_NAMESPACE 1
  15. #include <bx/bx.h>
  16. #include <bx/commandline.h>
  17. #include <bx/uint32_t.h>
  18. long int fsize(FILE* _file)
  19. {
  20. long int pos = ftell(_file);
  21. fseek(_file, 0L, SEEK_END);
  22. long int size = ftell(_file);
  23. fseek(_file, pos, SEEK_SET);
  24. return size;
  25. }
  26. int main(int _argc, const char* _argv[])
  27. {
  28. CommandLine cmdLine(_argc, _argv);
  29. FILE* file = fopen(_argv[1], "rb");
  30. uint32_t size = fsize(file);
  31. const Memory* mem = bgfx::alloc(size);
  32. fread(mem->data, 1, size, file);
  33. fclose(file);
  34. Dds dds;
  35. if (parseDds(dds, mem) )
  36. {
  37. bool decompress = cmdLine.hasArg('d');
  38. if (decompress
  39. || 0 == dds.m_type)
  40. {
  41. for (uint8_t side = 0, numSides = dds.m_cubeMap ? 6 : 1; side < numSides; ++side)
  42. {
  43. uint32_t width = dds.m_width;
  44. uint32_t height = dds.m_height;
  45. for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
  46. {
  47. width = uint32_max(1, width);
  48. height = uint32_max(1, height);
  49. Mip mip;
  50. if (getRawImageData(dds, side, lod, mem, mip) )
  51. {
  52. uint32_t dstpitch = width*4;
  53. uint8_t* bits = (uint8_t*)malloc(dstpitch*height);
  54. if (width != mip.m_width
  55. || height != mip.m_height)
  56. {
  57. uint8_t* temp = (uint8_t*)realloc(NULL, mip.m_width*mip.m_height*4);
  58. mip.decode(temp);
  59. uint32_t srcpitch = mip.m_width*4;
  60. for (uint32_t yy = 0; yy < height; ++yy)
  61. {
  62. uint8_t* src = &temp[yy*srcpitch];
  63. uint8_t* dst = &bits[yy*dstpitch];
  64. for (uint32_t xx = 0; xx < width; ++xx)
  65. {
  66. memcpy(dst, src, 4);
  67. dst += 4;
  68. src += 4;
  69. }
  70. }
  71. free(temp);
  72. }
  73. else
  74. {
  75. mip.decode(bits);
  76. }
  77. char filePath[256];
  78. _snprintf(filePath, sizeof(filePath), "mip%d_%d.tga", side, lod);
  79. bgfx::saveTga(filePath, width, height, dstpitch, bits);
  80. free(bits);
  81. }
  82. width >>= 1;
  83. height >>= 1;
  84. }
  85. }
  86. }
  87. else
  88. {
  89. for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
  90. {
  91. Mip mip;
  92. if (getRawImageData(dds, 0, lod, mem, mip) )
  93. {
  94. char filePath[256];
  95. _snprintf(filePath, sizeof(filePath), "mip%d.bin", lod);
  96. file = fopen(filePath, "wb");
  97. fwrite(mip.m_data, 1, mip.m_size, file);
  98. fclose(file);
  99. }
  100. }
  101. }
  102. }
  103. return EXIT_SUCCESS;
  104. }