Browse Source

Image code cleanup, preparing for split into new lib.

Branimir Karadžić 8 years ago
parent
commit
e492eb5132

+ 1 - 1
examples/common/bgfx_utils.h

@@ -8,7 +8,7 @@
 
 
 #include <bx/pixelformat.h>
 #include <bx/pixelformat.h>
 #include <bgfx/bgfx.h>
 #include <bgfx/bgfx.h>
-#include "image.h"
+#include "image_decode.h"
 
 
 ///
 ///
 void* load(const char* _filePath, uint32_t* _size = NULL);
 void* load(const char* _filePath, uint32_t* _size = NULL);

+ 35 - 7
examples/common/image.cpp → examples/common/image_decode.cpp

@@ -24,23 +24,50 @@ BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4100) // error C4100: '' : unreferenced formal
 #include <tinyexr/tinyexr.h>
 #include <tinyexr/tinyexr.h>
 BX_PRAGMA_DIAGNOSTIC_POP()
 BX_PRAGMA_DIAGNOSTIC_POP()
 
 
+BX_PRAGMA_DIAGNOSTIC_PUSH();
+BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4127) // warning C4127: conditional expression is constant
 #define LODEPNG_NO_COMPILE_ENCODER
 #define LODEPNG_NO_COMPILE_ENCODER
 #define LODEPNG_NO_COMPILE_DISK
 #define LODEPNG_NO_COMPILE_DISK
 #define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
 #define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
 #define LODEPNG_NO_COMPILE_ERROR_TEXT
 #define LODEPNG_NO_COMPILE_ERROR_TEXT
 #define LODEPNG_NO_COMPILE_ALLOCATORS
 #define LODEPNG_NO_COMPILE_ALLOCATORS
 #define LODEPNG_NO_COMPILE_CPP
 #define LODEPNG_NO_COMPILE_CPP
-#include <lodepng/lodepng.h>
+#include <lodepng/lodepng.cpp>
+BX_PRAGMA_DIAGNOSTIC_POP();
 
 
-typedef unsigned char stbi_uc;
-extern "C" int stbi_is_hdr_from_memory(stbi_uc const* _buffer, int _len);
-extern "C" stbi_uc* stbi_load_from_memory(stbi_uc const* _buffer, int _len, int* _x, int* _y, int* _comp, int _req_comp);
-extern "C" float* stbi_loadf_from_memory(stbi_uc const* _buffer, int _len, int* _x, int* _y, int* _comp, int _req_comp);
-extern "C" void stbi_image_free(void* _ptr);
-extern void lodepng_free(void* _ptr);
+void* lodepng_malloc(size_t _size)
+{
+	return ::malloc(_size);
+}
+
+void* lodepng_realloc(void* _ptr, size_t _size)
+{
+	return ::realloc(_ptr, _size);
+}
+
+void lodepng_free(void* _ptr)
+{
+	::free(_ptr);
+}
+
+BX_PRAGMA_DIAGNOSTIC_PUSH();
+BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wmissing-field-initializers");
+BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow");
+BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wint-to-pointer-cast")
+#if BX_COMPILER_GCC >= 60000
+BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wmisleading-indentation");
+BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wshift-negative-value");
+#endif // BX_COMPILER_GCC >= 60000_
+#define STBI_MALLOC(_size)        lodepng_malloc(_size)
+#define STBI_REALLOC(_ptr, _size) lodepng_realloc(_ptr, _size)
+#define STBI_FREE(_ptr)           lodepng_free(_ptr)
+#define STB_IMAGE_IMPLEMENTATION
+#include <stb/stb_image.c>
+BX_PRAGMA_DIAGNOSTIC_POP();
 
 
 namespace bgfx
 namespace bgfx
 {
 {
+#if !defined(BGFX_IMAGE_H_HEADER_GUARD)
 	struct ImageMip
 	struct ImageMip
 	{
 	{
 		TextureFormat::Enum m_format;
 		TextureFormat::Enum m_format;
@@ -52,6 +79,7 @@ namespace bgfx
 		bool     m_hasAlpha;
 		bool     m_hasAlpha;
 		const uint8_t* m_data;
 		const uint8_t* m_data;
 	};
 	};
+#endif // !defined(BGFX_IMAGE_H_HEADER_GUARD)
 
 
 	uint32_t imageGetSize(
 	uint32_t imageGetSize(
 		  TextureInfo* _info
 		  TextureInfo* _info

+ 10 - 8
examples/common/image.h → examples/common/image_decode.h

@@ -8,6 +8,7 @@
 
 
 namespace bgfx
 namespace bgfx
 {
 {
+#if !defined(BGFX_IMAGE_H_HEADER_GUARD)
 	///
 	///
 	struct ImageContainer
 	struct ImageContainer
 	{
 	{
@@ -30,14 +31,6 @@ namespace bgfx
 		bool     m_srgb;
 		bool     m_srgb;
 	};
 	};
 
 
-	///
-	ImageContainer* imageParse(
-		  bx::AllocatorI* _allocator
-		, const void* _data
-		, uint32_t _size
-		, TextureFormat::Enum _dstFormat = TextureFormat::Count
-		);
-
 	///
 	///
 	ImageContainer* imageAlloc(
 	ImageContainer* imageAlloc(
 		  bx::AllocatorI* _allocator
 		  bx::AllocatorI* _allocator
@@ -56,6 +49,15 @@ namespace bgfx
 
 
 	/// Converts format to string.
 	/// Converts format to string.
 	const char* getName(TextureFormat::Enum _format);
 	const char* getName(TextureFormat::Enum _format);
+#endif // !defined(BGFX_IMAGE_H_HEADER_GUARD)
+
+	///
+	ImageContainer* imageParse(
+		  bx::AllocatorI* _allocator
+		, const void* _data
+		, uint32_t _size
+		, TextureFormat::Enum _dstFormat = TextureFormat::Count
+		);
 
 
 } // namespace bgfx
 } // namespace bgfx
 
 

+ 2 - 23
examples/common/nanovg/nanovg.cpp

@@ -37,31 +37,13 @@ BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wunused-result");
 #include "fontstash.h"
 #include "fontstash.h"
 BX_PRAGMA_DIAGNOSTIC_POP();
 BX_PRAGMA_DIAGNOSTIC_POP();
 
 
-BX_PRAGMA_DIAGNOSTIC_PUSH();
-BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4127) // warning C4127: conditional expression is constant
 #define LODEPNG_NO_COMPILE_ENCODER
 #define LODEPNG_NO_COMPILE_ENCODER
 #define LODEPNG_NO_COMPILE_DISK
 #define LODEPNG_NO_COMPILE_DISK
 #define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
 #define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
 #define LODEPNG_NO_COMPILE_ERROR_TEXT
 #define LODEPNG_NO_COMPILE_ERROR_TEXT
 #define LODEPNG_NO_COMPILE_ALLOCATORS
 #define LODEPNG_NO_COMPILE_ALLOCATORS
 #define LODEPNG_NO_COMPILE_CPP
 #define LODEPNG_NO_COMPILE_CPP
-#include <lodepng/lodepng.cpp>
-BX_PRAGMA_DIAGNOSTIC_POP();
-
-void* lodepng_malloc(size_t _size)
-{
-	return ::malloc(_size);
-}
-
-void* lodepng_realloc(void* _ptr, size_t _size)
-{
-	return ::realloc(_ptr, _size);
-}
-
-void lodepng_free(void* _ptr)
-{
-	::free(_ptr);
-}
+#include <lodepng/lodepng.h>
 
 
 BX_PRAGMA_DIAGNOSTIC_PUSH();
 BX_PRAGMA_DIAGNOSTIC_PUSH();
 BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wmissing-field-initializers");
 BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wmissing-field-initializers");
@@ -71,10 +53,7 @@ BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wint-to-pointer-cast")
 BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wmisleading-indentation");
 BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wmisleading-indentation");
 BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wshift-negative-value");
 BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wshift-negative-value");
 #endif // BX_COMPILER_GCC >= 60000_
 #endif // BX_COMPILER_GCC >= 60000_
-#define STBI_MALLOC(_size)        lodepng_malloc(_size)
-#define STBI_REALLOC(_ptr, _size) lodepng_realloc(_ptr, _size)
-#define STBI_FREE(_ptr)           lodepng_free(_ptr)
-#define STB_IMAGE_IMPLEMENTATION
+
 #include <stb/stb_image.c>
 #include <stb/stb_image.c>
 BX_PRAGMA_DIAGNOSTIC_POP();
 BX_PRAGMA_DIAGNOSTIC_POP();
 
 

+ 2 - 0
scripts/texturec.lua

@@ -14,9 +14,11 @@ project "texturec"
 		path.join(BGFX_DIR, "3rdparty"),
 		path.join(BGFX_DIR, "3rdparty"),
 		path.join(BGFX_DIR, "3rdparty/nvtt"),
 		path.join(BGFX_DIR, "3rdparty/nvtt"),
 		path.join(BGFX_DIR, "3rdparty/iqa/include"),
 		path.join(BGFX_DIR, "3rdparty/iqa/include"),
+		path.join(BGFX_DIR, "examples/common"),
 	}
 	}
 
 
 	files {
 	files {
+		path.join(BGFX_DIR, "examples/common/image_decode.*"),
 		path.join(BGFX_DIR, "src/image.*"),
 		path.join(BGFX_DIR, "src/image.*"),
 		path.join(BGFX_DIR, "3rdparty/libsquish/**.cpp"),
 		path.join(BGFX_DIR, "3rdparty/libsquish/**.cpp"),
 		path.join(BGFX_DIR, "3rdparty/libsquish/**.h"),
 		path.join(BGFX_DIR, "3rdparty/libsquish/**.h"),

+ 11 - 199
tools/texturec/texturec.cpp

@@ -3,6 +3,7 @@
  * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  */
  */
 
 
+#include <stdio.h>
 #include <bx/allocator.h>
 #include <bx/allocator.h>
 #include <bx/readerwriter.h>
 #include <bx/readerwriter.h>
 #include <bx/endian.h>
 #include <bx/endian.h>
@@ -10,6 +11,7 @@
 #include <bgfx/bgfx.h>
 #include <bgfx/bgfx.h>
 
 
 #include "image.h"
 #include "image.h"
+#include "image_decode.h"
 
 
 #include <libsquish/squish.h>
 #include <libsquish/squish.h>
 #include <etc1/etc1.h>
 #include <etc1/etc1.h>
@@ -23,50 +25,6 @@ extern "C" {
 #include <iqa.h>
 #include <iqa.h>
 }
 }
 
 
-#define LODEPNG_NO_COMPILE_ENCODER
-#define LODEPNG_NO_COMPILE_DISK
-#define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
-#define LODEPNG_NO_COMPILE_ERROR_TEXT
-#define LODEPNG_NO_COMPILE_ALLOCATORS
-#define LODEPNG_NO_COMPILE_CPP
-#include <lodepng/lodepng.cpp>
-
-void* lodepng_malloc(size_t _size)
-{
-	return ::malloc(_size);
-}
-
-void* lodepng_realloc(void* _ptr, size_t _size)
-{
-	return ::realloc(_ptr, _size);
-}
-
-void lodepng_free(void* _ptr)
-{
-	::free(_ptr);
-}
-
-BX_PRAGMA_DIAGNOSTIC_PUSH();
-BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wmissing-field-initializers");
-BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow");
-BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wint-to-pointer-cast")
-#define STBI_MALLOC(_size)        lodepng_malloc(_size)
-#define STBI_REALLOC(_ptr, _size) lodepng_realloc(_ptr, _size)
-#define STBI_FREE(_ptr)           lodepng_free(_ptr)
-#define STB_IMAGE_IMPLEMENTATION
-#include <stb/stb_image.c>
-BX_PRAGMA_DIAGNOSTIC_POP();
-
-BX_PRAGMA_DIAGNOSTIC_PUSH()
-BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wtype-limits")
-BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-parameter")
-BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-value")
-BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4100) // error C4100: '' : unreferenced formal parameter
-#define MINIZ_NO_STDIO
-#define TINYEXR_IMPLEMENTATION
-#include <tinyexr/tinyexr.h>
-BX_PRAGMA_DIAGNOSTIC_POP()
-
 #if 0
 #if 0
 #	define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
 #	define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
 #endif // DEBUG
 #endif // DEBUG
@@ -78,142 +36,6 @@ BX_PRAGMA_DIAGNOSTIC_POP()
 
 
 namespace bgfx
 namespace bgfx
 {
 {
-	bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size, void** _out)
-	{
-		*_out = NULL;
-		bool loaded = imageParse(_imageContainer, _data, _size);
-		if (!loaded)
-		{
-			bgfx::TextureFormat::Enum format = bgfx::TextureFormat::RGBA8;
-			uint32_t bpp = 32;
-
-			uint32_t width  = 0;
-			uint32_t height = 0;
-
-			uint8_t* out = NULL;
-			static uint8_t pngMagic[] = { 0x89, 0x50, 0x4E, 0x47, 0x0d, 0x0a };
-			if (0 == memcmp(_data, pngMagic, sizeof(pngMagic) ) )
-			{
-				unsigned error;
-				LodePNGState state;
-				lodepng_state_init(&state);
-				state.decoder.color_convert = 0;
-				error = lodepng_decode(&out, &width, &height, &state, (uint8_t*)_data, _size);
-
-				if (0 == error)
-				{
-					*_out = out;
-
-					switch (state.info_raw.bitdepth)
-					{
-					case 8:
-						switch (state.info_raw.colortype)
-						{
-						case LCT_GREY:
-							format = bgfx::TextureFormat::R8;
-							bpp    = 8;
-							break;
-
-						case LCT_GREY_ALPHA:
-							format = bgfx::TextureFormat::RG8;
-							bpp    = 16;
-							break;
-
-						case LCT_RGB:
-							format = bgfx::TextureFormat::RGB8;
-							bpp    = 24;
-							break;
-
-						case LCT_RGBA:
-							format = bgfx::TextureFormat::RGBA8;
-							bpp    = 32;
-							break;
-
-						case LCT_PALETTE:
-							break;
-						}
-						break;
-
-					case 16:
-						switch (state.info_raw.colortype)
-						{
-						case LCT_GREY:
-							for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
-							{
-								uint16_t* rgba = (uint16_t*)out + ii;
-								rgba[0] = bx::toHostEndian(rgba[0], false);
-							}
-							format = bgfx::TextureFormat::R16;
-							bpp    = 16;
-							break;
-
-						case LCT_GREY_ALPHA:
-							for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
-							{
-								uint16_t* rgba = (uint16_t*)out + ii*2;
-								rgba[0] = bx::toHostEndian(rgba[0], false);
-								rgba[1] = bx::toHostEndian(rgba[1], false);
-							}
-							format = bgfx::TextureFormat::RG16;
-							bpp    = 32;
-							break;
-
-						case LCT_RGBA:
-							for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
-							{
-								uint16_t* rgba = (uint16_t*)out + ii*4;
-								rgba[0] = bx::toHostEndian(rgba[0], false);
-								rgba[1] = bx::toHostEndian(rgba[1], false);
-								rgba[2] = bx::toHostEndian(rgba[2], false);
-								rgba[3] = bx::toHostEndian(rgba[3], false);
-							}
-							format = bgfx::TextureFormat::RGBA16;
-							bpp    = 64;
-							break;
-
-						case LCT_RGB:
-						case LCT_PALETTE:
-							break;
-						}
-						break;
-
-					default:
-						break;
-					}
-				}
-
-				lodepng_state_cleanup(&state);
-			}
-			else
-			{
-				int comp = 0;
-				*_out = stbi_load_from_memory( (uint8_t*)_data, _size, (int*)&width, (int*)&height, &comp, 4);
-			}
-
-			loaded = NULL != *_out;
-
-			if (loaded)
-			{
-				_imageContainer.m_data      = *_out;
-				_imageContainer.m_size      = width*height*bpp/8;
-				_imageContainer.m_offset    = 0;
-				_imageContainer.m_width     = width;
-				_imageContainer.m_height    = height;
-				_imageContainer.m_depth     = 1;
-				_imageContainer.m_numLayers = 1;
-				_imageContainer.m_format    = format;
-				_imageContainer.m_numMips   = 1;
-				_imageContainer.m_hasAlpha  = true;
-				_imageContainer.m_cubeMap   = false;
-				_imageContainer.m_ktx       = false;
-				_imageContainer.m_ktxLE     = false;
-				_imageContainer.m_srgb      = false;
-			}
-		}
-
-		return loaded;
-	}
-
 	bool imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format)
 	bool imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format)
 	{
 	{
 		TextureFormat::Enum format = TextureFormat::Enum(_format);
 		TextureFormat::Enum format = TextureFormat::Enum(_format);
@@ -262,7 +84,7 @@ namespace bgfx
 
 
 						for (uint32_t ii = 0; ii < 16; ++ii)
 						for (uint32_t ii = 0; ii < 16; ++ii)
 						{ // BGRx
 						{ // BGRx
-							memcpy(&block[ii*4], &ptr[(ii%4)*pitch + (ii&~3)], 4);
+							bx::memCopy(&block[ii*4], &ptr[(ii%4)*pitch + (ii&~3)], 4);
 							bx::xchg(block[ii*4+0], block[ii*4+2]);
 							bx::xchg(block[ii*4+0], block[ii*4+2]);
 						}
 						}
 
 
@@ -301,7 +123,7 @@ namespace bgfx
 			return true;
 			return true;
 
 
 		case TextureFormat::RGBA8:
 		case TextureFormat::RGBA8:
-			memcpy(_dst, _src, _width*_height*4);
+			bx::memCopy(_dst, _src, _width*_height*4);
 			return true;
 			return true;
 
 
 		default:
 		default:
@@ -569,22 +391,14 @@ int main(int _argc, const char* _argv[])
 	{
 	{
 		using namespace bgfx;
 		using namespace bgfx;
 
 
-		uint8_t* decodedImage = NULL;
-		ImageContainer input;
+		ImageContainer* input = imageParse(&allocator, inputData, inputSize);
 
 
-		bool loaded = imageParse(input, inputData, inputSize, (void**)&decodedImage);
-		if (NULL != decodedImage)
+		if (NULL != input)
 		{
 		{
 			BX_FREE(&allocator, inputData);
 			BX_FREE(&allocator, inputData);
 
 
-			inputData = (uint8_t*)input.m_data;
-			inputSize = input.m_size;
-		}
-
-		if (loaded)
-		{
 			const char* type = cmdLine.findOption('t');
 			const char* type = cmdLine.findOption('t');
-			bgfx::TextureFormat::Enum format = input.m_format;
+			bgfx::TextureFormat::Enum format = input->m_format;
 
 
 			if (NULL != type)
 			if (NULL != type)
 			{
 			{
@@ -600,7 +414,7 @@ int main(int _argc, const char* _argv[])
 			ImageContainer* output = NULL;
 			ImageContainer* output = NULL;
 
 
 			ImageMip mip;
 			ImageMip mip;
-			if (imageGetRawData(input, 0, 0, inputData, inputSize, mip) )
+			if (imageGetRawData(*input, 0, 0, input->m_data, input->m_size, mip) )
 			{
 			{
 				uint8_t numMips = mips
 				uint8_t numMips = mips
 					? imageGetNumMips(format, mip.m_width, mip.m_height)
 					? imageGetNumMips(format, mip.m_width, mip.m_height)
@@ -682,7 +496,7 @@ int main(int _argc, const char* _argv[])
 
 
 					BX_FREE(&allocator, rgbaDst);
 					BX_FREE(&allocator, rgbaDst);
 				}
 				}
-				else if (8 != getBlockInfo(input.m_format).rBits)
+				else if (8 != getBlockInfo(input->m_format).rBits)
 				{
 				{
 					output = imageAlloc(&allocator, format, mip.m_width, mip.m_height, 0, 1, false, mips);
 					output = imageAlloc(&allocator, format, mip.m_width, mip.m_height, 0, 1, false, mips);
 
 
@@ -782,7 +596,7 @@ int main(int _argc, const char* _argv[])
 						, TextureFormat::RGBA8
 						, TextureFormat::RGBA8
 						);
 						);
 					temp = BX_ALLOC(&allocator, size);
 					temp = BX_ALLOC(&allocator, size);
-					memset(temp, 0, size);
+					bx::memSet(temp, 0, size);
 					uint8_t* rgba = (uint8_t*)temp;
 					uint8_t* rgba = (uint8_t*)temp;
 
 
 					imageDecodeToRgba8(rgba
 					imageDecodeToRgba8(rgba
@@ -797,7 +611,7 @@ int main(int _argc, const char* _argv[])
 					if (iqa)
 					if (iqa)
 					{
 					{
 						ref = BX_ALLOC(&allocator, size);
 						ref = BX_ALLOC(&allocator, size);
-						memcpy(ref, rgba, size);
+						bx::memCopy(ref, rgba, size);
 					}
 					}
 
 
 					imageEncodeFromRgba8(output->m_data, rgba, dstMip.m_width, dstMip.m_height, format);
 					imageEncodeFromRgba8(output->m_data, rgba, dstMip.m_width, dstMip.m_height, format);
@@ -879,8 +693,6 @@ int main(int _argc, const char* _argv[])
 			help("Failed to load input file.");
 			help("Failed to load input file.");
 			return EXIT_FAILURE;
 			return EXIT_FAILURE;
 		}
 		}
-
-		BX_FREE(&allocator, inputData);
 	}
 	}
 
 
 	return EXIT_SUCCESS;
 	return EXIT_SUCCESS;

+ 1 - 1
tools/texturev/texturev.cpp

@@ -24,7 +24,7 @@
 #include <string>
 #include <string>
 namespace stl = tinystl;
 namespace stl = tinystl;
 
 
-#include "image.h"
+#include "image_decode.h"
 
 
 #include <bgfx/embedded_shader.h>
 #include <bgfx/embedded_shader.h>