2013-02-22 00:07:31 -05:00
|
|
|
/*
|
2015-01-01 18:04:46 -05:00
|
|
|
* Copyright 2011-2015 Branimir Karadzic. All rights reserved.
|
2013-02-22 00:07:31 -05:00
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
// Just hacking DDS loading code in here.
|
|
|
|
#include "bgfx_p.h"
|
|
|
|
|
2013-09-02 19:22:53 -04:00
|
|
|
#include "image.h"
|
2015-12-14 20:35:24 -05:00
|
|
|
#include <libsquish/squish.h>
|
2015-12-16 01:13:01 -05:00
|
|
|
#include <etc1/etc1.h>
|
2013-02-22 00:07:31 -05:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
# define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
#include <bx/bx.h>
|
2015-11-19 23:10:51 -05:00
|
|
|
#include <bx/allocator.h>
|
2013-02-22 00:07:31 -05:00
|
|
|
#include <bx/commandline.h>
|
|
|
|
#include <bx/uint32_t.h>
|
|
|
|
|
|
|
|
namespace bgfx
|
|
|
|
{
|
|
|
|
const Memory* alloc(uint32_t _size)
|
|
|
|
{
|
|
|
|
Memory* mem = (Memory*)::realloc(NULL, sizeof(Memory) + _size);
|
|
|
|
mem->size = _size;
|
|
|
|
mem->data = (uint8_t*)mem + sizeof(Memory);
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:40:23 -05:00
|
|
|
void release(const Memory* _mem)
|
2013-02-22 00:07:31 -05:00
|
|
|
{
|
2015-11-15 23:40:23 -05:00
|
|
|
Memory* mem = const_cast<Memory*>(_mem);
|
|
|
|
::free(mem);
|
|
|
|
}
|
2013-02-22 00:07:31 -05:00
|
|
|
|
2015-11-15 23:40:23 -05:00
|
|
|
} // namespace bgfx
|
|
|
|
|
|
|
|
void help(const char* _error = NULL)
|
|
|
|
{
|
|
|
|
if (NULL != _error)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error:\n%s\n\n", _error);
|
2013-02-22 00:07:31 -05:00
|
|
|
}
|
2015-11-15 23:40:23 -05:00
|
|
|
|
|
|
|
fprintf(stderr
|
|
|
|
, "texturec, bgfx texture compiler tool\n"
|
|
|
|
"Copyright 2011-2015 Branimir Karadzic. All rights reserved.\n"
|
|
|
|
"License: http://www.opensource.org/licenses/BSD-2-Clause\n\n"
|
|
|
|
);
|
2013-02-22 00:07:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int _argc, const char* _argv[])
|
|
|
|
{
|
2015-12-18 21:15:30 -05:00
|
|
|
using namespace bgfx;
|
|
|
|
|
2013-02-22 00:07:31 -05:00
|
|
|
bx::CommandLine cmdLine(_argc, _argv);
|
|
|
|
|
2015-11-15 23:40:23 -05:00
|
|
|
if (cmdLine.hasArg('h', "help") )
|
|
|
|
{
|
|
|
|
help();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2013-09-02 19:22:53 -04:00
|
|
|
|
2015-11-15 23:40:23 -05:00
|
|
|
const char* inputFileName = cmdLine.findOption('i');
|
2013-09-02 19:22:53 -04:00
|
|
|
if (NULL == inputFileName)
|
|
|
|
{
|
2015-11-15 23:40:23 -05:00
|
|
|
help("Input file must be specified.");
|
|
|
|
return EXIT_FAILURE;
|
2013-09-02 19:22:53 -04:00
|
|
|
}
|
|
|
|
|
2015-11-19 23:10:51 -05:00
|
|
|
const char* outputFileName = cmdLine.findOption('o');
|
|
|
|
if (NULL == outputFileName)
|
|
|
|
{
|
|
|
|
help("Output file must be specified.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-09-02 19:22:53 -04:00
|
|
|
bx::CrtFileReader reader;
|
2015-11-15 23:40:23 -05:00
|
|
|
if (0 != bx::open(&reader, inputFileName) )
|
|
|
|
{
|
|
|
|
help("Failed to open input file.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-12-18 21:15:30 -05:00
|
|
|
const bool mips = cmdLine.hasArg('m', "mips");
|
|
|
|
const char* type = cmdLine.findOption('t');
|
|
|
|
TextureFormat::Enum format = TextureFormat::BGRA8;
|
|
|
|
|
|
|
|
if (NULL != type)
|
|
|
|
{
|
|
|
|
if (0 == bx::stricmp(type, "bc1")
|
|
|
|
|| 0 == bx::stricmp(type, "dxt1") )
|
|
|
|
{
|
|
|
|
format = TextureFormat::BC1;
|
|
|
|
}
|
|
|
|
else if (0 == bx::stricmp(type, "bc2")
|
|
|
|
|| 0 == bx::stricmp(type, "dxt3") )
|
|
|
|
{
|
|
|
|
format = TextureFormat::BC2;
|
|
|
|
}
|
|
|
|
else if (0 == bx::stricmp(type, "bc3")
|
|
|
|
|| 0 == bx::stricmp(type, "dxt5") )
|
|
|
|
{
|
|
|
|
format = TextureFormat::BC3;
|
|
|
|
}
|
|
|
|
else if (0 == bx::stricmp(type, "etc1") )
|
|
|
|
{
|
|
|
|
format = TextureFormat::ETC1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 19:22:53 -04:00
|
|
|
uint32_t size = (uint32_t)bx::getSize(&reader);
|
2013-02-22 00:07:31 -05:00
|
|
|
const Memory* mem = alloc(size);
|
2013-09-02 19:22:53 -04:00
|
|
|
bx::read(&reader, mem->data, mem->size);
|
|
|
|
bx::close(&reader);
|
2013-02-22 00:07:31 -05:00
|
|
|
|
2013-09-02 19:22:53 -04:00
|
|
|
ImageContainer imageContainer;
|
2013-02-22 00:07:31 -05:00
|
|
|
|
2015-12-18 21:15:30 -05:00
|
|
|
bool loaded = imageParse(imageContainer, mem->data, mem->size);
|
|
|
|
if (!loaded)
|
2015-11-19 23:10:51 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-12-18 21:15:30 -05:00
|
|
|
BX_UNUSED(mips);
|
|
|
|
if (loaded)
|
2013-02-22 00:07:31 -05:00
|
|
|
{
|
2015-12-18 21:15:30 -05:00
|
|
|
bx::CrtAllocator allocator;
|
|
|
|
uint8_t* output = NULL;
|
2013-02-22 00:07:31 -05:00
|
|
|
|
2015-12-18 21:15:30 -05:00
|
|
|
ImageMip mip;
|
|
|
|
if (imageGetRawData(imageContainer, 0, 0, mem->data, mem->size, mip) )
|
2013-02-22 00:07:31 -05:00
|
|
|
{
|
2015-12-18 21:15:30 -05:00
|
|
|
uint8_t* rgba = (uint8_t*)BX_ALLOC(&allocator, imageGetSize(TextureFormat::RGBA8, mip.m_width, mip.m_height) );
|
2013-02-22 00:07:31 -05:00
|
|
|
|
2015-12-18 21:15:30 -05:00
|
|
|
imageDecodeToRgba8(rgba, mip.m_data, mip.m_width, mip.m_height, mip.m_width*mip.m_bpp/8, mip.m_format);
|
|
|
|
|
|
|
|
output = (uint8_t*)BX_ALLOC(&allocator, imageGetSize(format, mip.m_width, mip.m_height) );
|
|
|
|
|
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case TextureFormat::BC1:
|
|
|
|
case TextureFormat::BC2:
|
|
|
|
case TextureFormat::BC3:
|
|
|
|
squish::CompressImage(rgba, mip.m_width, mip.m_height, output
|
|
|
|
, format == TextureFormat::BC1 ? squish::kDxt1
|
|
|
|
: format == TextureFormat::BC2 ? squish::kDxt3
|
|
|
|
: squish::kDxt5
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::ETC1:
|
|
|
|
etc1_encode_image(rgba, mip.m_width, mip.m_height, 4, mip.m_width*4, output);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2013-02-22 00:07:31 -05:00
|
|
|
}
|
2015-12-18 21:15:30 -05:00
|
|
|
|
|
|
|
BX_FREE(&allocator, rgba);
|
2013-02-22 00:07:31 -05:00
|
|
|
}
|
2015-12-18 21:15:30 -05:00
|
|
|
|
|
|
|
if (NULL != output)
|
2013-02-22 00:07:31 -05:00
|
|
|
{
|
2015-12-18 21:15:30 -05:00
|
|
|
bx::CrtFileWriter writer;
|
|
|
|
if (0 == bx::open(&writer, outputFileName) )
|
2013-02-22 00:07:31 -05:00
|
|
|
{
|
2015-12-18 21:15:30 -05:00
|
|
|
if (NULL != bx::stristr(outputFileName, ".ktx") )
|
2013-02-22 00:07:31 -05:00
|
|
|
{
|
2015-12-18 21:15:30 -05:00
|
|
|
imageWriteKtx(&writer, imageContainer, mem->data, mem->size);
|
2013-02-22 00:07:31 -05:00
|
|
|
}
|
2015-12-18 21:15:30 -05:00
|
|
|
|
|
|
|
bx::close(&writer);
|
2013-02-22 00:07:31 -05:00
|
|
|
}
|
2015-12-18 21:15:30 -05:00
|
|
|
|
|
|
|
BX_FREE(&allocator, output);
|
2013-02-22 00:07:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:40:23 -05:00
|
|
|
release(mem);
|
|
|
|
|
2013-02-22 00:07:31 -05:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|