mirror of
https://github.com/bkaradzic/bimg.git
synced 2026-02-17 20:52:38 +01:00
Adding errors to API.
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
#include <stdint.h> // uint32_t
|
||||
#include <stdlib.h> // NULL
|
||||
|
||||
#define BIMG_API_VERSION UINT32_C(1)
|
||||
#define BIMG_API_VERSION UINT32_C(2)
|
||||
|
||||
namespace bx
|
||||
{
|
||||
|
||||
@@ -23,17 +23,18 @@ namespace bimg
|
||||
};
|
||||
|
||||
///
|
||||
bool imageEncodeFromRgba8(
|
||||
void imageEncodeFromRgba8(
|
||||
void* _dst
|
||||
, const void* _src
|
||||
, uint32_t _width
|
||||
, uint32_t _height
|
||||
, TextureFormat::Enum _format
|
||||
, Quality::Enum _quality
|
||||
, bx::Error* _err = NULL
|
||||
);
|
||||
|
||||
///
|
||||
bool imageEncodeFromRgba32f(
|
||||
void imageEncodeFromRgba32f(
|
||||
bx::AllocatorI* _allocator
|
||||
, void* _dst
|
||||
, const void* _src
|
||||
@@ -41,6 +42,7 @@ namespace bimg
|
||||
, uint32_t _height
|
||||
, TextureFormat::Enum _format
|
||||
, Quality::Enum _quality
|
||||
, bx::Error* _err = NULL
|
||||
);
|
||||
|
||||
///
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
|
||||
#define BIMG_CHUNK_MAGIC_TEX BX_MAKEFOURCC('T', 'E', 'X', 0x0)
|
||||
|
||||
BX_ERROR_RESULT(BIMG_ERROR, BX_MAKEFOURCC('b', 'i', 'm', 'g') );
|
||||
|
||||
namespace bimg
|
||||
{
|
||||
struct Memory
|
||||
|
||||
@@ -35,8 +35,10 @@ namespace bimg
|
||||
};
|
||||
BX_STATIC_ASSERT(Quality::Count == BX_COUNTOF(s_squishQuality) );
|
||||
|
||||
bool imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, TextureFormat::Enum _format, Quality::Enum _quality)
|
||||
void imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, TextureFormat::Enum _format, Quality::Enum _quality, bx::Error* _err)
|
||||
{
|
||||
BX_ERROR_SCOPE(_err);
|
||||
|
||||
switch (_format)
|
||||
{
|
||||
case TextureFormat::BC1:
|
||||
@@ -52,19 +54,19 @@ namespace bimg
|
||||
: _format == TextureFormat::BC5 ? squish::kBc5
|
||||
: squish::kDxt1)
|
||||
);
|
||||
return true;
|
||||
break;
|
||||
|
||||
case TextureFormat::BC6H:
|
||||
nvtt::compressBC6H( (const uint8_t*)_src, _width, _height, 4, _dst);
|
||||
return true;
|
||||
break;
|
||||
|
||||
case TextureFormat::BC7:
|
||||
nvtt::compressBC7( (const uint8_t*)_src, _width, _height, 4, _dst);
|
||||
return true;
|
||||
break;
|
||||
|
||||
case TextureFormat::ETC1:
|
||||
etc1_encode_image( (const uint8_t*)_src, _width, _height, 4, _width*4, (uint8_t*)_dst);
|
||||
return true;
|
||||
break;
|
||||
|
||||
case TextureFormat::ETC2:
|
||||
{
|
||||
@@ -90,7 +92,7 @@ namespace bimg
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
|
||||
case TextureFormat::PTC14:
|
||||
{
|
||||
@@ -102,7 +104,7 @@ namespace bimg
|
||||
PvrTcEncoder::EncodeRgb4Bpp(_dst, bmp);
|
||||
bmp.data = NULL;
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
|
||||
case TextureFormat::PTC14A:
|
||||
{
|
||||
@@ -114,25 +116,29 @@ namespace bimg
|
||||
PvrTcEncoder::EncodeRgba4Bpp(_dst, bmp);
|
||||
bmp.data = NULL;
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
|
||||
case TextureFormat::BGRA8:
|
||||
imageSwizzleBgra8(_dst, _width, _height, _width*4, _src);
|
||||
return true;
|
||||
break;
|
||||
|
||||
case TextureFormat::RGBA8:
|
||||
bx::memCopy(_dst, _src, _width*_height*4);
|
||||
return true;
|
||||
break;
|
||||
|
||||
default:
|
||||
if (!imageConvert(_dst, _format, _src, TextureFormat::RGBA8, _width, _height) )
|
||||
{
|
||||
BX_ERROR_SET(_err, BIMG_ERROR, "Unable to convert between input/output formats!");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return imageConvert(_dst, _format, _src, TextureFormat::RGBA8, _width, _height);
|
||||
}
|
||||
|
||||
bool imageEncodeFromRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, TextureFormat::Enum _format, Quality::Enum _quality)
|
||||
void imageEncodeFromRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, TextureFormat::Enum _format, Quality::Enum _quality, bx::Error* _err)
|
||||
{
|
||||
BX_ERROR_SCOPE(_err);
|
||||
|
||||
const uint8_t* src = (const uint8_t*)_src;
|
||||
|
||||
switch (_format)
|
||||
@@ -154,7 +160,7 @@ namespace bimg
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
|
||||
case TextureFormat::BC5:
|
||||
{
|
||||
@@ -176,13 +182,15 @@ namespace bimg
|
||||
imageEncodeFromRgba8(_dst, temp, _width, _height, _format, _quality);
|
||||
BX_FREE(_allocator, temp);
|
||||
}
|
||||
return true;
|
||||
break;
|
||||
|
||||
default:
|
||||
if (!imageConvert(_dst, _format, _src, TextureFormat::RGBA32F, _width, _height) )
|
||||
{
|
||||
BX_ERROR_SET(_err, BIMG_ERROR, "Unable to convert between input/output formats!");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return imageConvert(_dst, _format, _src, TextureFormat::RGBA32F, _width, _height);
|
||||
}
|
||||
|
||||
void imageRgba32f11to01(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src)
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
#include <bx/crtimpl.h>
|
||||
#include <bx/uint32_t.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#define BIMG_TEXTUREC_VERSION_MAJOR 1
|
||||
#define BIMG_TEXTUREC_VERSION_MINOR 3
|
||||
|
||||
@@ -69,9 +71,10 @@ struct Options
|
||||
bool sdf;
|
||||
};
|
||||
|
||||
bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData, uint32_t _inputSize, const Options& _options)
|
||||
bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData, uint32_t _inputSize, const Options& _options, bx::Error* _err)
|
||||
{
|
||||
bool succeeded = true;
|
||||
BX_ERROR_SCOPE(_err);
|
||||
|
||||
const uint8_t* inputData = (uint8_t*)_inputData;
|
||||
|
||||
bimg::ImageContainer* output = NULL;
|
||||
@@ -167,7 +170,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData
|
||||
const uint8_t numMips = output->m_numMips;
|
||||
const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
|
||||
|
||||
for (uint16_t side = 0; side < numSides && succeeded; ++side)
|
||||
for (uint16_t side = 0; side < numSides && _err->isOk(); ++side)
|
||||
{
|
||||
bimg::ImageMip mip;
|
||||
if (bimg::imageGetRawData(*input, side, 0, input->m_data, input->m_size, mip) )
|
||||
@@ -233,9 +236,10 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData
|
||||
, dstMip.m_height
|
||||
, outputFormat
|
||||
, _options.quality
|
||||
, _err
|
||||
);
|
||||
|
||||
for (uint8_t lod = 1; lod < numMips; ++lod)
|
||||
for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
|
||||
{
|
||||
bimg::imageRgba32fDownsample2x2NormalMap(rgba
|
||||
, dstMip.m_width
|
||||
@@ -261,6 +265,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData
|
||||
, dstMip.m_height
|
||||
, outputFormat
|
||||
, _options.quality
|
||||
, _err
|
||||
);
|
||||
}
|
||||
|
||||
@@ -299,9 +304,11 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData
|
||||
, dstMip.m_height
|
||||
, outputFormat
|
||||
, _options.quality
|
||||
, _err
|
||||
);
|
||||
|
||||
if (1 < numMips)
|
||||
if (1 < numMips
|
||||
&& _err->isOk() )
|
||||
{
|
||||
bimg::imageRgba32fToLinear(rgba32f
|
||||
, mip.m_width
|
||||
@@ -310,7 +317,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData
|
||||
, rgba32f
|
||||
);
|
||||
|
||||
for (uint8_t lod = 1; lod < numMips; ++lod)
|
||||
for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
|
||||
{
|
||||
bimg::imageRgba32fLinearDownsample2x2(rgba32f
|
||||
, dstMip.m_width
|
||||
@@ -329,20 +336,15 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData
|
||||
, rgba32f
|
||||
);
|
||||
|
||||
succeeded = bimg::imageEncodeFromRgba32f(_allocator
|
||||
bimg::imageEncodeFromRgba32f(_allocator
|
||||
, dstData
|
||||
, rgbaDst
|
||||
, dstMip.m_width
|
||||
, dstMip.m_height
|
||||
, outputFormat
|
||||
, _options.quality
|
||||
, _err
|
||||
);
|
||||
|
||||
if (!succeeded)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -386,9 +388,10 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData
|
||||
, dstMip.m_height
|
||||
, outputFormat
|
||||
, _options.quality
|
||||
, _err
|
||||
);
|
||||
|
||||
for (uint8_t lod = 1; lod < numMips; ++lod)
|
||||
for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
|
||||
{
|
||||
bimg::imageRgba8Downsample2x2(rgba
|
||||
, dstMip.m_width
|
||||
@@ -406,6 +409,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData
|
||||
, dstMip.m_height
|
||||
, outputFormat
|
||||
, _options.quality
|
||||
, _err
|
||||
);
|
||||
}
|
||||
|
||||
@@ -439,7 +443,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData
|
||||
bimg::imageFree(input);
|
||||
}
|
||||
|
||||
if (!succeeded
|
||||
if (!_err->isOk()
|
||||
&& NULL != output)
|
||||
{
|
||||
bimg::imageFree(output);
|
||||
@@ -449,11 +453,16 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData
|
||||
return output;
|
||||
}
|
||||
|
||||
void help(const char* _error = NULL)
|
||||
void help(const char* _error = NULL, bool _showHelp = true)
|
||||
{
|
||||
if (NULL != _error)
|
||||
{
|
||||
fprintf(stderr, "Error:\n%s\n\n", _error);
|
||||
|
||||
if (!_showHelp)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr
|
||||
@@ -497,6 +506,21 @@ void help(const char* _error = NULL)
|
||||
);
|
||||
}
|
||||
|
||||
void help(const char* _str, const bx::Error& _err)
|
||||
{
|
||||
std::string str;
|
||||
if (_str != NULL)
|
||||
{
|
||||
str.append(_str);
|
||||
str.append(" ");
|
||||
}
|
||||
|
||||
const bx::StringView& sv = _err.getMessage();
|
||||
str.append(sv.getPtr(), sv.getTerm() - sv.getPtr() );
|
||||
|
||||
help(str.c_str(), false);
|
||||
}
|
||||
|
||||
int main(int _argc, const char* _argv[])
|
||||
{
|
||||
bx::CommandLine cmdLine(_argc, _argv);
|
||||
@@ -586,10 +610,11 @@ int main(int _argc, const char* _argv[])
|
||||
}
|
||||
}
|
||||
|
||||
bx::Error err;
|
||||
bx::CrtFileReader reader;
|
||||
if (!bx::open(&reader, inputFileName) )
|
||||
if (!bx::open(&reader, inputFileName, &err) )
|
||||
{
|
||||
help("Failed to open input file.");
|
||||
help("Failed to open input file.", err);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -598,24 +623,23 @@ int main(int _argc, const char* _argv[])
|
||||
uint32_t inputSize = (uint32_t)bx::getSize(&reader);
|
||||
uint8_t* inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
|
||||
|
||||
bx::Error err;
|
||||
bx::read(&reader, inputData, inputSize, &err);
|
||||
bx::close(&reader);
|
||||
|
||||
if (!err.isOk() )
|
||||
{
|
||||
help("Failed to read input file.");
|
||||
help("Failed to read input file.", err);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
bimg::ImageContainer* output = convert(&allocator, inputData, inputSize, options);
|
||||
bimg::ImageContainer* output = convert(&allocator, inputData, inputSize, options, &err);
|
||||
|
||||
BX_FREE(&allocator, inputData);
|
||||
|
||||
if (NULL != output)
|
||||
{
|
||||
bx::CrtFileWriter writer;
|
||||
if (bx::open(&writer, outputFileName) )
|
||||
if (bx::open(&writer, outputFileName, false, &err) )
|
||||
{
|
||||
if (NULL != bx::strFindI(saveAs, "ktx") )
|
||||
{
|
||||
@@ -626,7 +650,7 @@ int main(int _argc, const char* _argv[])
|
||||
}
|
||||
else
|
||||
{
|
||||
help("Failed to open output file.");
|
||||
help("Failed to open output file.", err);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -634,7 +658,7 @@ int main(int _argc, const char* _argv[])
|
||||
}
|
||||
else
|
||||
{
|
||||
help("No output generated.");
|
||||
help(NULL, err);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user