bgfx/tools/ddsdump.cpp

126 lines
2.7 KiB
C++
Raw Normal View History

2012-04-03 23:30:07 -04:00
/*
* Copyright 2011-2012 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
2012-04-03 23:30:07 -04:00
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bgfx_p.h"
using namespace bgfx;
#include "dds.h"
#if 0
# define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
#endif // DEBUG
#define BX_NAMESPACE 1
#include <bx/bx.h>
#include <bx/commandline.h>
#include <bx/uint32_t.h>
long int fsize(FILE* _file)
{
long int pos = ftell(_file);
fseek(_file, 0L, SEEK_END);
long int size = ftell(_file);
fseek(_file, pos, SEEK_SET);
return size;
}
int main(int _argc, const char* _argv[])
{
CommandLine cmdLine(_argc, _argv);
FILE* file = fopen(_argv[1], "rb");
uint32_t size = fsize(file);
const Memory* mem = bgfx::alloc(size);
fread(mem->data, 1, size, file);
fclose(file);
Dds dds;
if (parseDds(dds, mem) )
{
bool decompress = cmdLine.hasArg('d');
if (decompress
|| 0 == dds.m_type)
{
2012-06-09 21:25:50 -04:00
for (uint8_t side = 0, numSides = dds.m_cubeMap ? 6 : 1; side < numSides; ++side)
2012-04-03 23:30:07 -04:00
{
2012-06-09 21:25:50 -04:00
uint32_t width = dds.m_width;
uint32_t height = dds.m_height;
2012-04-03 23:30:07 -04:00
2012-06-09 21:25:50 -04:00
for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
2012-04-03 23:30:07 -04:00
{
2012-06-09 21:25:50 -04:00
width = uint32_max(1, width);
height = uint32_max(1, height);
2012-04-03 23:30:07 -04:00
2012-06-09 21:25:50 -04:00
Mip mip;
if (getRawImageData(dds, side, lod, mem, mip) )
2012-04-03 23:30:07 -04:00
{
2012-06-09 21:25:50 -04:00
uint32_t dstpitch = width*4;
uint8_t* bits = (uint8_t*)malloc(dstpitch*height);
2012-04-03 23:30:07 -04:00
2012-06-09 21:25:50 -04:00
if (width != mip.m_width
|| height != mip.m_height)
2012-04-03 23:30:07 -04:00
{
2012-06-09 21:25:50 -04:00
uint8_t* temp = (uint8_t*)realloc(NULL, mip.m_width*mip.m_height*4);
mip.decode(temp);
uint32_t srcpitch = mip.m_width*4;
2012-04-03 23:30:07 -04:00
2012-06-09 21:25:50 -04:00
for (uint32_t yy = 0; yy < height; ++yy)
2012-04-03 23:30:07 -04:00
{
2012-06-09 21:25:50 -04:00
uint8_t* src = &temp[yy*srcpitch];
uint8_t* dst = &bits[yy*dstpitch];
for (uint32_t xx = 0; xx < width; ++xx)
{
memcpy(dst, src, 4);
dst += 4;
src += 4;
}
2012-04-03 23:30:07 -04:00
}
2012-06-09 21:25:50 -04:00
free(temp);
}
else
{
mip.decode(bits);
2012-04-03 23:30:07 -04:00
}
2012-06-09 21:25:50 -04:00
char filePath[256];
_snprintf(filePath, sizeof(filePath), "mip%d_%d.tga", side, lod);
2012-04-03 23:30:07 -04:00
2012-06-09 21:25:50 -04:00
bgfx::saveTga(filePath, width, height, dstpitch, bits);
free(bits);
}
2012-04-03 23:30:07 -04:00
2012-06-09 21:25:50 -04:00
width >>= 1;
height >>= 1;
2012-04-03 23:30:07 -04:00
}
}
}
else
{
for (uint32_t lod = 0, num = dds.m_numMips; lod < num; ++lod)
{
Mip mip;
2012-06-09 21:25:50 -04:00
if (getRawImageData(dds, 0, lod, mem, mip) )
2012-04-03 23:30:07 -04:00
{
char filePath[256];
_snprintf(filePath, sizeof(filePath), "mip%d.bin", lod);
file = fopen(filePath, "wb");
fwrite(mip.m_data, 1, mip.m_size, file);
fclose(file);
}
}
}
}
return EXIT_SUCCESS;
2012-04-03 23:30:07 -04:00
}