Browse Source

Improve configurability. Issue #104. (#105)

Branimir Karadžić 6 days ago
parent
commit
b43fea9eae
4 changed files with 98 additions and 74 deletions
  1. 1 1
      scripts/bimg_decode.lua
  2. 51 39
      src/config.h
  3. 30 30
      src/image.cpp
  4. 16 4
      src/image_decode.cpp

+ 1 - 1
scripts/bimg_decode.lua

@@ -30,7 +30,7 @@ project "bimg_decode"
 
 	if _OPTIONS["with-libheif"] then
 		defines {
-			"BIMG_DECODE_HEIF=1",
+			"BIMG_CONFIG_PARSE_HEIF=1",
 		}
 	end
 

+ 51 - 39
src/config.h

@@ -8,56 +8,68 @@
 
 #include <bx/bx.h>
 
-#ifndef BIMG_DECODE_ENABLE
-#	define BIMG_DECODE_ENABLE 1
-#endif // BIMG_DECODE_ENABLE
+#ifndef BIMG_CONFIG_DECODE_ENABLE
+#	define BIMG_CONFIG_DECODE_ENABLE 1
+#endif // BIMG_CONFIG_DECODE_ENABLE
 
-#ifndef BIMG_DECODE_BC1
-#	define BIMG_DECODE_BC1 BIMG_DECODE_ENABLE
-#endif // BIMG_DECODE_BC1
+#ifndef BIMG_CONFIG_DECODE_BC1
+#	define BIMG_CONFIG_DECODE_BC1 BIMG_CONFIG_DECODE_ENABLE
+#endif // BIMG_CONFIG_DECODE_BC1
 
-#ifndef BIMG_DECODE_BC2
-#	define BIMG_DECODE_BC2 BIMG_DECODE_ENABLE
-#endif // BIMG_DECODE_BC2
+#ifndef BIMG_CONFIG_DECODE_BC2
+#	define BIMG_CONFIG_DECODE_BC2 BIMG_CONFIG_DECODE_ENABLE
+#endif // BIMG_CONFIG_DECODE_BC2
 
-#ifndef BIMG_DECODE_BC3
-#	define BIMG_DECODE_BC3 BIMG_DECODE_ENABLE
-#endif // BIMG_DECODE_BC3
+#ifndef BIMG_CONFIG_DECODE_BC3
+#	define BIMG_CONFIG_DECODE_BC3 BIMG_CONFIG_DECODE_ENABLE
+#endif // BIMG_CONFIG_DECODE_BC3
 
-#ifndef BIMG_DECODE_BC4
-#	define BIMG_DECODE_BC4 BIMG_DECODE_ENABLE
-#endif // BIMG_DECODE_BC4
+#ifndef BIMG_CONFIG_DECODE_BC4
+#	define BIMG_CONFIG_DECODE_BC4 BIMG_CONFIG_DECODE_ENABLE
+#endif // BIMG_CONFIG_DECODE_BC4
 
-#ifndef BIMG_DECODE_BC5
-#	define BIMG_DECODE_BC5 BIMG_DECODE_ENABLE
-#endif // BIMG_DECODE_BC5
+#ifndef BIMG_CONFIG_DECODE_BC5
+#	define BIMG_CONFIG_DECODE_BC5 BIMG_CONFIG_DECODE_ENABLE
+#endif // BIMG_CONFIG_DECODE_BC5
 
-#ifndef BIMG_DECODE_BC6
-#	define BIMG_DECODE_BC6 BIMG_DECODE_ENABLE
-#endif // BIMG_DECODE_BC6
+#ifndef BIMG_CONFIG_DECODE_BC6
+#	define BIMG_CONFIG_DECODE_BC6 BIMG_CONFIG_DECODE_ENABLE
+#endif // BIMG_CONFIG_DECODE_BC6
 
-#ifndef BIMG_DECODE_BC7
-#	define BIMG_DECODE_BC7 BIMG_DECODE_ENABLE
-#endif // BIMG_DECODE_BC7
+#ifndef BIMG_CONFIG_DECODE_BC7
+#	define BIMG_CONFIG_DECODE_BC7 BIMG_CONFIG_DECODE_ENABLE
+#endif // BIMG_CONFIG_DECODE_BC7
 
-#ifndef BIMG_DECODE_ATC
-#	define BIMG_DECODE_ATC BIMG_DECODE_ENABLE
-#endif // BIMG_DECODE_ATC
+#ifndef BIMG_CONFIG_DECODE_ATC
+#	define BIMG_CONFIG_DECODE_ATC BIMG_CONFIG_DECODE_ENABLE
+#endif // BIMG_CONFIG_DECODE_ATC
 
-#ifndef BIMG_DECODE_ASTC
-#	define BIMG_DECODE_ASTC BIMG_DECODE_ENABLE
-#endif // BIMG_DECODE_ASTC
+#ifndef BIMG_CONFIG_DECODE_ASTC
+#	define BIMG_CONFIG_DECODE_ASTC BIMG_CONFIG_DECODE_ENABLE
+#endif // BIMG_CONFIG_DECODE_ASTC
 
-#ifndef BIMG_DECODE_ETC1
-#	define BIMG_DECODE_ETC1 BIMG_DECODE_ENABLE
-#endif // BIMG_DECODE_ETC1
+#ifndef BIMG_CONFIG_DECODE_ETC1
+#	define BIMG_CONFIG_DECODE_ETC1 BIMG_CONFIG_DECODE_ENABLE
+#endif // BIMG_CONFIG_DECODE_ETC1
 
-#ifndef BIMG_DECODE_ETC2
-#	define BIMG_DECODE_ETC2 BIMG_DECODE_ENABLE
-#endif // BIMG_DECODE_ETC2
+#ifndef BIMG_CONFIG_DECODE_ETC2
+#	define BIMG_CONFIG_DECODE_ETC2 BIMG_CONFIG_DECODE_ENABLE
+#endif // BIMG_CONFIG_DECODE_ETC2
 
-#ifndef BIMG_DECODE_HEIF
-#	define BIMG_DECODE_HEIF 0
-#endif // BIMG_DECODE_HEIF
+#ifndef BIMG_CONFIG_PARSE_EXR
+#	define BIMG_CONFIG_PARSE_EXR 1
+#endif // BIMG_CONFIG_PARSE_EXR
+
+#ifndef BIMG_CONFIG_PARSE_HEIF
+#	define BIMG_CONFIG_PARSE_HEIF 0
+#endif // BIMG_CONFIG_PARSE_HEIF
+
+#ifndef BIMG_CONFIG_PARSE_JPEG
+#	define BIMG_CONFIG_PARSE_JPEG 1
+#endif // BIMG_CONFIG_PARSE_JPEG
+
+#ifndef BIMG_CONFIG_PARSE_PNG
+#	define BIMG_CONFIG_PARSE_PNG 1
+#endif // BIMG_CONFIG_PARSE_PNG
 
 #endif // BIMG_CONFIG_H_HEADER_GUARD

+ 30 - 30
src/image.cpp

@@ -1394,7 +1394,7 @@ namespace bimg
 
 	static void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8])
 	{
-		if (!BX_ENABLED(BIMG_DECODE_BC2 || BIMG_DECODE_BC3) )
+		if (!BX_ENABLED(BIMG_CONFIG_DECODE_BC2 || BIMG_CONFIG_DECODE_BC3) )
 		{
 			return;
 		}
@@ -1430,7 +1430,7 @@ namespace bimg
 
 	static void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8])
 	{
-		if (!BX_ENABLED(BIMG_DECODE_BC1 || BIMG_DECODE_BC2 || BIMG_DECODE_BC3) )
+		if (!BX_ENABLED(BIMG_CONFIG_DECODE_BC1 || BIMG_CONFIG_DECODE_BC2 || BIMG_CONFIG_DECODE_BC3) )
 		{
 			return;
 		}
@@ -1486,7 +1486,7 @@ namespace bimg
 
 	static void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8])
 	{
-		if (!BX_ENABLED(BIMG_DECODE_BC2) )
+		if (!BX_ENABLED(BIMG_CONFIG_DECODE_BC2) )
 		{
 			return;
 		}
@@ -1500,7 +1500,7 @@ namespace bimg
 
 	static void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8])
 	{
-		if (!BX_ENABLED(BIMG_DECODE_BC3 || BIMG_DECODE_BC4 || BIMG_DECODE_BC5) )
+		if (!BX_ENABLED(BIMG_CONFIG_DECODE_BC3 || BIMG_CONFIG_DECODE_BC4 || BIMG_CONFIG_DECODE_BC5) )
 		{
 			return;
 		}
@@ -1880,7 +1880,7 @@ namespace bimg
 
 	static void decodeBlockBc6h(uint16_t _dst[16*3], const uint8_t _src[16], bool _signed)
 	{
-		if (!BX_ENABLED(BIMG_DECODE_BC6) )
+		if (!BX_ENABLED(BIMG_CONFIG_DECODE_BC6) )
 		{
 			return;
 		}
@@ -2305,7 +2305,7 @@ namespace bimg
 
 	static void decodeBlockBc6h(float _dst[16*4], const uint8_t _src[16])
 	{
-		if (!BX_ENABLED(BIMG_DECODE_BC6) )
+		if (!BX_ENABLED(BIMG_CONFIG_DECODE_BC6) )
 		{
 			return;
 		}
@@ -2358,7 +2358,7 @@ namespace bimg
 
 	static void decodeBlockBc7(uint8_t _dst[16*4], const uint8_t _src[16])
 	{
-		if (!BX_ENABLED(BIMG_DECODE_BC7) )
+		if (!BX_ENABLED(BIMG_CONFIG_DECODE_BC7) )
 		{
 			return;
 		}
@@ -2552,7 +2552,7 @@ namespace bimg
 	//
 	static void decodeBlockATC(uint8_t _dst[16*4], const uint8_t _src[8])
 	{
-		if (!BX_ENABLED(BIMG_DECODE_ATC) )
+		if (!BX_ENABLED(BIMG_CONFIG_DECODE_ATC) )
 		{
 			return;
 		}
@@ -2864,7 +2864,7 @@ namespace bimg
 
 	static void decodeBlockEtc12(uint8_t _dst[16*4], const uint8_t _src[8])
 	{
-		if (!BX_ENABLED(BIMG_DECODE_ETC1 || BIMG_DECODE_ETC2) )
+		if (!BX_ENABLED(BIMG_CONFIG_DECODE_ETC1 || BIMG_CONFIG_DECODE_ETC2) )
 		{
 			return;
 		}
@@ -3003,7 +3003,7 @@ namespace bimg
 
 	void decodeBlockEtc2Alpha(uint8_t _dst[16 * 4], const uint8_t _src[8])
 	{
-		if (!BX_ENABLED(BIMG_DECODE_ETC2))
+		if (!BX_ENABLED(BIMG_CONFIG_DECODE_ETC2))
 		{
 			return;
 		}
@@ -4565,7 +4565,7 @@ namespace bimg
 		switch (_srcFormat)
 		{
 		case TextureFormat::BC1:
-			if (BX_ENABLED(BIMG_DECODE_BC1) )
+			if (BX_ENABLED(BIMG_CONFIG_DECODE_BC1) )
 			{
 				for (uint32_t yy = 0; yy < height; ++yy)
 				{
@@ -4584,13 +4584,13 @@ namespace bimg
 			}
 			else
 			{
-				BX_WARN(false, "BC1 decoder is disabled (BIMG_DECODE_BC1).");
+				BX_WARN(false, "BC1 decoder is disabled (BIMG_CONFIG_DECODE_BC1).");
 				imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
 			}
 			break;
 
 		case TextureFormat::BC2:
-			if (BX_ENABLED(BIMG_DECODE_BC2) )
+			if (BX_ENABLED(BIMG_CONFIG_DECODE_BC2) )
 			{
 				for (uint32_t yy = 0; yy < height; ++yy)
 				{
@@ -4611,13 +4611,13 @@ namespace bimg
 			}
 			else
 			{
-				BX_WARN(false, "BC2 decoder is disabled (BIMG_DECODE_BC2).");
+				BX_WARN(false, "BC2 decoder is disabled (BIMG_CONFIG_DECODE_BC2).");
 				imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
 			}
 			break;
 
 		case TextureFormat::BC3:
-			if (BX_ENABLED(BIMG_DECODE_BC3) )
+			if (BX_ENABLED(BIMG_CONFIG_DECODE_BC3) )
 			{
 				for (uint32_t yy = 0; yy < height; ++yy)
 				{
@@ -4638,13 +4638,13 @@ namespace bimg
 			}
 			else
 			{
-				BX_WARN(false, "BC3 decoder is disabled (BIMG_DECODE_BC3).");
+				BX_WARN(false, "BC3 decoder is disabled (BIMG_CONFIG_DECODE_BC3).");
 				imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
 			}
 			break;
 
 		case TextureFormat::BC4:
-			if (BX_ENABLED(BIMG_DECODE_BC4) )
+			if (BX_ENABLED(BIMG_CONFIG_DECODE_BC4) )
 			{
 				for (uint32_t yy = 0; yy < height; ++yy)
 				{
@@ -4663,13 +4663,13 @@ namespace bimg
 			}
 			else
 			{
-				BX_WARN(false, "BC4 decoder is disabled (BIMG_DECODE_BC4).");
+				BX_WARN(false, "BC4 decoder is disabled (BIMG_CONFIG_DECODE_BC4).");
 				imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
 			}
 			break;
 
 		case TextureFormat::BC5:
-			if (BX_ENABLED(BIMG_DECODE_BC5) )
+			if (BX_ENABLED(BIMG_CONFIG_DECODE_BC5) )
 			{
 				for (uint32_t yy = 0; yy < height; ++yy)
 				{
@@ -4699,13 +4699,13 @@ namespace bimg
 			}
 			else
 			{
-				BX_WARN(false, "BC5 decoder is disabled (BIMG_DECODE_BC5).");
+				BX_WARN(false, "BC5 decoder is disabled (BIMG_CONFIG_DECODE_BC5).");
 				imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
 			}
 			break;
 
 		case TextureFormat::BC6H:
-			if (BX_ENABLED(BIMG_DECODE_BC6) )
+			if (BX_ENABLED(BIMG_CONFIG_DECODE_BC6) )
 			{
 				ImageContainer* rgba32f = imageAlloc(_allocator
 					, TextureFormat::RGBA32F
@@ -4722,13 +4722,13 @@ namespace bimg
 			}
 			else
 			{
-				BX_WARN(false, "BC6 decoder is disabled (BIMG_DECODE_BC6).");
+				BX_WARN(false, "BC6 decoder is disabled (BIMG_CONFIG_DECODE_BC6).");
 				imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
 			}
 			break;
 
 		case TextureFormat::BC7:
-			if (BX_ENABLED(BIMG_DECODE_BC7) )
+			if (BX_ENABLED(BIMG_CONFIG_DECODE_BC7) )
 			{
 				for (uint32_t yy = 0; yy < height; ++yy)
 				{
@@ -4747,14 +4747,14 @@ namespace bimg
 			}
 			else
 			{
-				BX_WARN(false, "BC7 decoder is disabled (BIMG_DECODE_BC7).");
+				BX_WARN(false, "BC7 decoder is disabled (BIMG_CONFIG_DECODE_BC7).");
 				imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
 			}
 			break;
 
 		case TextureFormat::ETC1:
 		case TextureFormat::ETC2:
-			if (BX_ENABLED(BIMG_DECODE_ETC1 || BIMG_DECODE_ETC2) )
+			if (BX_ENABLED(BIMG_CONFIG_DECODE_ETC1 || BIMG_CONFIG_DECODE_ETC2) )
 			{
 				for (uint32_t yy = 0; yy < height; ++yy)
 				{
@@ -4773,13 +4773,13 @@ namespace bimg
 			}
 			else
 			{
-				BX_WARN(false, "ETC1/ETC2 decoder is disabled (BIMG_DECODE_ETC1/ETC2).");
+				BX_WARN(false, "ETC1/ETC2 decoder is disabled (BIMG_CONFIG_DECODE_ETC1/ETC2).");
 				imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
 			}
 			break;
 
 		case TextureFormat::ETC2A:
-			if (BX_ENABLED(BIMG_DECODE_ETC2))
+			if (BX_ENABLED(BIMG_CONFIG_DECODE_ETC2))
 			{
 				for (uint32_t yy = 0; yy < height; ++yy)
 				{
@@ -4800,7 +4800,7 @@ namespace bimg
 			}
 			else
 			{
-				BX_WARN(false, "ETC2 decoder is disabled (BIMG_DECODE_ETC2).");
+				BX_WARN(false, "ETC2 decoder is disabled (BIMG_CONFIG_DECODE_ETC2).");
 				imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00));
 			}
 			break;
@@ -4997,7 +4997,7 @@ namespace bimg
 		case TextureFormat::ASTC10x10:
 		case TextureFormat::ASTC12x10:
 		case TextureFormat::ASTC12x12:
-			if (BX_ENABLED(BIMG_DECODE_ASTC) )
+			if (BX_ENABLED(BIMG_CONFIG_DECODE_ASTC) )
 			{
 					const bimg::ImageBlockInfo& astcBlockInfo = bimg::getBlockInfo(_srcFormat);
 
@@ -5070,7 +5070,7 @@ namespace bimg
 			}
 			else
 			{
-				BX_WARN(false, "ASTC decoder is disabled (BIMG_DECODE_ASTC).");
+				BX_WARN(false, "ASTC decoder is disabled (BIMG_CONFIG_DECODE_ASTC).");
 				imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
 			}
 			break;

+ 16 - 4
src/image_decode.cpp

@@ -22,6 +22,7 @@ BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4505) // warning C4505: 'tinyexr::miniz::def_r
 #include <tinyexr/tinyexr.h>
 BX_PRAGMA_DIAGNOSTIC_POP()
 
+#if BIMG_CONFIG_PARSE_PNG
 BX_PRAGMA_DIAGNOSTIC_PUSH();
 BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4127) // warning C4127: conditional expression is constant
 BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4267) // warning C4267: '=' : conversion from 'size_t' to 'unsigned short', possible loss of data
@@ -33,10 +34,11 @@ BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4334) // warning C4334: '<<' : result of 32 -
 #define LODEPNG_NO_COMPILE_CPP
 #include <lodepng/lodepng.cpp>
 BX_PRAGMA_DIAGNOSTIC_POP();
+#endif // BIMG_CONFIG_PARSE_PNG
 
-#if BIMG_DECODE_HEIF
+#if BIMG_CONFIG_PARSE_HEIF
 #	include <libheif/heif.h>
-#endif // BIMG_DECODE_HEIF
+#endif // BIMG_CONFIG_PARSE_HEIF
 
 void* lodepng_malloc(size_t _size)
 {
@@ -85,6 +87,7 @@ namespace bimg
 			return NULL;
 		}
 
+#if BIMG_CONFIG_PARSE_PNG
 		ImageContainer* output = NULL;
 		bimg::TextureFormat::Enum format = bimg::TextureFormat::RGBA8;
 		uint32_t width  = 0;
@@ -433,6 +436,10 @@ namespace bimg
 		lodepng_free(data);
 
 		return output;
+#else
+		BX_ERROR_SET(_err, BIMG_ERROR, "PNG parsing is disabled (BIMG_CONFIG_PARSE_PNG).");
+		return NULL;
+#endif // BIMG_CONFIG_PARSE_PNG
 	}
 
 	static void errorSetTinyExr(int _result, bx::Error* _err)
@@ -715,6 +722,7 @@ namespace bimg
 			return NULL;
 		}
 
+#if BIMG_CONFIG_PARSE_JPEG
 		Orientation::Enum orientation = Orientation::R0;
 
 		while (err.isOk() )
@@ -834,11 +842,15 @@ namespace bimg
 		}
 
 		return image;
+#else
+		BX_ERROR_SET(_err, BIMG_ERROR, "JPEG parsing is disabled (BIMG_CONFIG_PARSE_JPEG).");
+		return NULL;
+#endif // BIMG_CONFIG_PARSE_JPEG
 	}
 
 	static ImageContainer* imageParseLibHeif(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, bx::Error* _err)
 	{
-#if BIMG_DECODE_HEIF
+#if BIMG_CONFIG_PARSE_HEIF
 		heif_context* ctx = heif_context_alloc();
 
 		heif_context_read_from_memory_without_copy(ctx, _data, _size, NULL);
@@ -884,7 +896,7 @@ namespace bimg
 #else
 		BX_UNUSED(_allocator, _data, _size, _err);
 		return NULL;
-#endif // BIMG_DECODE_HEIF
+#endif // BIMG_CONFIG_PARSE_HEIF
 	}
 
 	ImageContainer* imageParse(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, TextureFormat::Enum _dstFormat, bx::Error* _err)