2013-02-21 21:07:31 -08:00
/*
2015-01-01 15:04:46 -08:00
* Copyright 2011 - 2015 Branimir Karadzic . All rights reserved .
2013-02-21 21:07:31 -08: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"
using namespace bgfx ;
2013-09-02 16:22:53 -07:00
# include "image.h"
2013-02-21 21:07:31 -08:00
#if 0
# define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
# endif // DEBUG
# include <bx/bx.h>
2015-11-19 20:10:51 -08:00
# include <bx/allocator.h>
2013-02-21 21:07:31 -08: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 20:40:23 -08:00
void release ( const Memory * _mem )
2013-02-21 21:07:31 -08:00
{
2015-11-15 20:40:23 -08:00
Memory * mem = const_cast < Memory * > ( _mem ) ;
: : free ( mem ) ;
}
2013-02-21 21:07:31 -08:00
2015-11-15 20:40:23 -08:00
} // namespace bgfx
void help ( const char * _error = NULL )
{
if ( NULL ! = _error )
{
fprintf ( stderr , " Error: \n %s \n \n " , _error ) ;
2013-02-21 21:07:31 -08:00
}
2015-11-15 20:40:23 -08: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-21 21:07:31 -08:00
}
int main ( int _argc , const char * _argv [ ] )
{
bx : : CommandLine cmdLine ( _argc , _argv ) ;
2015-11-15 20:40:23 -08:00
if ( cmdLine . hasArg ( ' h ' , " help " ) )
{
help ( ) ;
return EXIT_FAILURE ;
}
2013-09-02 16:22:53 -07:00
2015-11-15 20:40:23 -08:00
const char * inputFileName = cmdLine . findOption ( ' i ' ) ;
2013-09-02 16:22:53 -07:00
if ( NULL = = inputFileName )
{
2015-11-15 20:40:23 -08:00
help ( " Input file must be specified. " ) ;
return EXIT_FAILURE ;
2013-09-02 16:22:53 -07:00
}
2015-11-19 20:10:51 -08:00
const char * outputFileName = cmdLine . findOption ( ' o ' ) ;
if ( NULL = = outputFileName )
{
help ( " Output file must be specified. " ) ;
return EXIT_FAILURE ;
}
2013-09-02 16:22:53 -07:00
bx : : CrtFileReader reader ;
2015-11-15 20:40:23 -08:00
if ( 0 ! = bx : : open ( & reader , inputFileName ) )
{
help ( " Failed to open input file. " ) ;
return EXIT_FAILURE ;
}
2013-09-02 16:22:53 -07:00
uint32_t size = ( uint32_t ) bx : : getSize ( & reader ) ;
2013-02-21 21:07:31 -08:00
const Memory * mem = alloc ( size ) ;
2013-09-02 16:22:53 -07:00
bx : : read ( & reader , mem - > data , mem - > size ) ;
bx : : close ( & reader ) ;
2013-02-21 21:07:31 -08:00
2013-09-02 16:22:53 -07:00
ImageContainer imageContainer ;
2013-02-21 21:07:31 -08:00
2015-11-19 20:10:51 -08:00
if ( imageParse ( imageContainer , mem - > data , mem - > size ) )
{
bx : : CrtFileWriter writer ;
if ( 0 = = bx : : open ( & writer , outputFileName ) )
{
if ( NULL ! = bx : : stristr ( outputFileName , " .ktx " ) )
{
imageWriteKtx ( & writer , imageContainer , mem - > data , mem - > size ) ;
}
bx : : close ( & writer ) ;
}
}
#if 0
2013-09-02 16:22:53 -07:00
if ( imageParse ( imageContainer , mem - > data , mem - > size ) )
2013-02-21 21:07:31 -08:00
{
bool decompress = cmdLine . hasArg ( ' d ' ) ;
if ( decompress
2013-09-08 21:03:03 -07:00
| | 0 = = imageContainer . m_format )
2013-02-21 21:07:31 -08:00
{
2013-09-02 16:22:53 -07:00
for ( uint8_t side = 0 , numSides = imageContainer . m_cubeMap ? 6 : 1 ; side < numSides ; + + side )
2013-02-21 21:07:31 -08:00
{
2015-11-19 20:10:51 -08:00
uint32_t width = imageContainer . m_width ;
2013-09-02 16:22:53 -07:00
uint32_t height = imageContainer . m_height ;
2013-02-21 21:07:31 -08:00
2013-09-02 16:22:53 -07:00
for ( uint32_t lod = 0 , num = imageContainer . m_numMips ; lod < num ; + + lod )
2013-02-21 21:07:31 -08:00
{
2015-11-19 20:10:51 -08:00
width = bx : : uint32_max ( 1 , width ) ;
2013-08-04 16:56:07 -07:00
height = bx : : uint32_max ( 1 , height ) ;
2013-02-21 21:07:31 -08:00
2013-09-08 21:03:03 -07:00
ImageMip mip ;
2013-09-02 16:22:53 -07:00
if ( imageGetRawData ( imageContainer , side , lod , mem - > data , mem - > size , mip ) )
2013-02-21 21:07:31 -08:00
{
uint32_t dstpitch = width * 4 ;
uint8_t * bits = ( uint8_t * ) malloc ( dstpitch * height ) ;
2015-11-19 20:10:51 -08:00
if ( width ! = mip . m_width
2013-02-21 21:07:31 -08:00
| | height ! = mip . m_height )
{
uint8_t * temp = ( uint8_t * ) realloc ( NULL , mip . m_width * mip . m_height * 4 ) ;
2013-11-07 22:59:17 -08:00
imageDecodeToBgra8 ( temp , mip . m_data , mip . m_width , mip . m_height , mip . m_width * 4 , mip . m_format ) ;
2013-02-21 21:07:31 -08:00
uint32_t srcpitch = mip . m_width * 4 ;
for ( uint32_t yy = 0 ; yy < height ; + + yy )
{
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 ;
}
}
free ( temp ) ;
}
else
{
2015-11-17 23:31:34 -08:00
imageDecodeToBgra8 ( bits
, mip . m_data
, mip . m_width
, mip . m_height
, mip . m_width * 4
, mip . m_format
) ;
2013-02-21 21:07:31 -08:00
}
char filePath [ 256 ] ;
2015-11-15 20:40:23 -08:00
bx : : snprintf ( filePath , sizeof ( filePath ) , " mip%d_%d.ktx " , side , lod ) ;
bx : : CrtFileWriter writer ;
if ( 0 = = bx : : open ( & writer , filePath ) )
{
if ( NULL ! = bx : : stristr ( filePath , " .ktx " ) )
{
2015-11-17 23:31:34 -08:00
imageWriteKtx ( & writer
, TextureFormat : : BGRA8
, false
, width
, height
, 0
, 1
, bits
) ;
2015-11-15 20:40:23 -08:00
}
else
{
imageWriteTga ( & writer , width , height , dstpitch , bits , false , false ) ;
}
bx : : close ( & writer ) ;
}
2013-02-21 21:07:31 -08:00
free ( bits ) ;
}
width > > = 1 ;
height > > = 1 ;
}
}
}
else
{
2013-09-02 16:22:53 -07:00
for ( uint32_t lod = 0 , num = imageContainer . m_numMips ; lod < num ; + + lod )
2013-02-21 21:07:31 -08:00
{
2013-09-08 21:03:03 -07:00
ImageMip mip ;
2013-09-02 16:22:53 -07:00
if ( imageGetRawData ( imageContainer , 0 , lod , mem - > data , mem - > size , mip ) )
2013-02-21 21:07:31 -08:00
{
char filePath [ 256 ] ;
bx : : snprintf ( filePath , sizeof ( filePath ) , " mip%d.bin " , lod ) ;
2013-09-02 16:22:53 -07:00
bx : : CrtFileWriter writer ;
2015-11-15 20:40:23 -08:00
if ( 0 = = bx : : open ( & writer , filePath ) )
{
printf ( " mip%d, size %d \n " , lod , mip . m_size ) ;
bx : : write ( & writer , mip . m_data , mip . m_size ) ;
bx : : close ( & writer ) ;
}
2013-02-21 21:07:31 -08:00
}
}
}
}
2015-11-19 20:10:51 -08:00
# endif // 0
2013-02-21 21:07:31 -08:00
2015-11-15 20:40:23 -08:00
release ( mem ) ;
2013-02-21 21:07:31 -08:00
return EXIT_SUCCESS ;
}