2
0

ddsdump.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. uint32_t width = dds.m_width;
  42. uint32_t height = dds.m_height;
  43. for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
  44. {
  45. width = uint32_max(1, width);
  46. height = uint32_max(1, height);
  47. Mip mip;
  48. if (getRawImageData(dds, lod, mem, mip) )
  49. {
  50. uint32_t dstpitch = width*4;
  51. uint8_t* bits = (uint8_t*)malloc(dstpitch*height);
  52. if (width != mip.m_width
  53. || height != mip.m_height)
  54. {
  55. uint8_t* temp = (uint8_t*)realloc(NULL, mip.m_width*mip.m_height*4);
  56. mip.decode(temp);
  57. uint32_t srcpitch = mip.m_width*4;
  58. for (uint32_t yy = 0; yy < height; ++yy)
  59. {
  60. uint8_t* src = &temp[yy*srcpitch];
  61. uint8_t* dst = &bits[yy*dstpitch];
  62. for (uint32_t xx = 0; xx < width; ++xx)
  63. {
  64. memcpy(dst, src, 4);
  65. dst += 4;
  66. src += 4;
  67. }
  68. }
  69. free(temp);
  70. }
  71. else
  72. {
  73. mip.decode(bits);
  74. }
  75. char filePath[256];
  76. _snprintf(filePath, sizeof(filePath), "mip%d.tga", lod);
  77. bgfx::saveTga(filePath, width, height, dstpitch, bits);
  78. free(bits);
  79. }
  80. width >>= 1;
  81. height >>= 1;
  82. }
  83. }
  84. else
  85. {
  86. for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
  87. {
  88. Mip mip;
  89. if (getRawImageData(dds, lod, mem, mip) )
  90. {
  91. char filePath[256];
  92. _snprintf(filePath, sizeof(filePath), "mip%d.bin", lod);
  93. file = fopen(filePath, "wb");
  94. fwrite(mip.m_data, 1, mip.m_size, file);
  95. fclose(file);
  96. }
  97. }
  98. }
  99. }
  100. return EXIT_SUCCESS;
  101. }