Branimir Karadžić 8 lat temu
rodzic
commit
155740489f
3 zmienionych plików z 192 dodań i 83 usunięć
  1. 155 23
      include/bimg/bimg.h
  2. 34 10
      src/image.cpp
  3. 3 50
      src/image_decode.cpp

+ 155 - 23
include/bimg/bimg.h

@@ -8,7 +8,6 @@
 
 #include <stdint.h> // uint32_t
 #include <stdlib.h> // NULL
-#include <bx/pixelformat.h>
 
 namespace bx
 {
@@ -21,6 +20,9 @@ namespace bx
 
 namespace bimg
 {
+	typedef void (*PackFn)(void*, const float*);
+	typedef void (*UnpackFn)(float*, const void*);
+
 	/// Texture format enum.
 	///
 	/// Notation:
@@ -241,45 +243,109 @@ namespace bimg
 		);
 
 	///
-	void imageSolid(void* _dst, uint32_t _width, uint32_t _height, uint32_t _solid);
+	void imageSolid(
+		  void* _dst
+		, uint32_t _width
+		, uint32_t _height
+		, uint32_t _solid
+		);
 
 	///
-	void imageCheckerboard(void* _dst, uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1);
+	void imageCheckerboard(
+		  void* _dst
+		, uint32_t _width
+		, uint32_t _height
+		, uint32_t _step
+		, uint32_t _0
+		, uint32_t _1
+		);
 
 	///
-	void imageRgba8Downsample2x2(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src);
+	void imageRgba8Downsample2x2(
+		  void* _dst
+		, uint32_t _width
+		, uint32_t _height
+		, uint32_t _pitch
+		, const void* _src
+		);
 
 	///
-	void imageRgba32fToLinear(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src);
+	void imageRgba32fToLinear(
+		  void* _dst
+		, uint32_t _width
+		, uint32_t _height
+		, uint32_t _pitch
+		, const void* _src
+		);
 
 	///
-	void imageRgba32fToGamma(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src);
+	void imageRgba32fToGamma(
+		  void* _dst
+		, uint32_t _width
+		, uint32_t _height
+		, uint32_t _pitch
+		, const void* _src
+		);
 
 	///
-	void imageRgba32fLinearDownsample2x2(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src);
+	void imageRgba32fLinearDownsample2x2(
+		  void* _dst
+		, uint32_t _width
+		, uint32_t _height
+		, uint32_t _pitch
+		, const void* _src
+		);
 
 	///
-	void imageRgba32fDownsample2x2NormalMap(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src);
+	void imageRgba32fDownsample2x2NormalMap(
+		  void* _dst
+		, uint32_t _width
+		, uint32_t _height
+		, uint32_t _pitch
+		, const void* _src
+		);
 
 	///
-	void imageSwizzleBgra8(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src);
+	void imageSwizzleBgra8(
+		  void* _dst
+		, uint32_t _width
+		, uint32_t _height
+		, uint32_t _pitch
+		, const void* _src
+		);
 
 	///
-	void imageCopy(void* _dst, uint32_t _height, uint32_t _srcPitch, const void* _src, uint32_t _dstPitch);
+	void imageCopy(
+		  void* _dst
+		, uint32_t _height
+		, uint32_t _srcPitch
+		, const void* _src
+		, uint32_t _dstPitch
+		);
 
 	///
-	void imageCopy(void* _dst, uint32_t _width, uint32_t _height, uint32_t _bpp, uint32_t _pitch, const void* _src);
+	void imageCopy(
+		  void* _dst
+		, uint32_t _width
+		, uint32_t _height
+		, uint32_t _bpp
+		, uint32_t _pitch
+		, const void* _src
+		);
 
 	///
-	bool imageConvert(TextureFormat::Enum _dstFormat, TextureFormat::Enum _srcFormat);
+	bool imageConvert(
+		  TextureFormat::Enum _dstFormat
+		, TextureFormat::Enum _srcFormat
+		);
 
 	///
 	void imageConvert(
 		  void* _dst
 		, uint32_t _bpp
-		, bx::PackFn _pack
+		, PackFn _pack
 		, const void* _src
-		, bx::UnpackFn _unpack
+		, UnpackFn _unpack
 		, uint32_t _size
 		);
 
@@ -287,10 +353,10 @@ namespace bimg
 	void imageConvert(
 		  void* _dst
 		, uint32_t _dstBpp
-		, bx::PackFn _pack
+		, PackFn _pack
 		, const void* _src
 		, uint32_t _srcBpp
-		, bx::UnpackFn _unpack
+		, UnpackFn _unpack
 		, uint32_t _width
 		, uint32_t _height
 		, uint32_t _srcPitch
@@ -314,6 +380,13 @@ namespace bimg
 		, uint32_t _size
 		);
 
+	///
+	ImageContainer* imageConvert(
+		  bx::AllocatorI* _allocator
+		, TextureFormat::Enum _dstFormat
+		, const ImageContainer& _input
+		);
+
 	///
 	ImageContainer* imageAlloc(
 		  bx::AllocatorI* _allocator
@@ -328,7 +401,9 @@ namespace bimg
 		);
 
 	///
-	void imageFree(ImageContainer* _imageContainer);
+	void imageFree(
+		  ImageContainer* _imageContainer
+		);
 
 	///
 	void imageWriteTga(
@@ -365,22 +440,79 @@ namespace bimg
 		);
 
 	///
-	bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader);
+	bool imageParse(
+		  ImageContainer& _imageContainer
+		, bx::ReaderSeekerI* _reader
+		);
+
+	///
+	bool imageParse(
+		  ImageContainer& _imageContainer
+		, const void* _data
+		, uint32_t _size
+		);
+
+	///
+	ImageContainer* imageParseDds(
+		  bx::AllocatorI* _allocator
+		, const void* _src
+		, uint32_t _size
+		);
+
+	///
+	ImageContainer* imageParseKtx(
+		  bx::AllocatorI* _allocator
+		, const void* _src
+		, uint32_t _size
+		);
 
 	///
-	bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size);
+	ImageContainer* imageParsePvr3(
+		  bx::AllocatorI* _allocator
+		, const void* _src
+		, uint32_t _size
+		);
 
 	///
-	void imageDecodeToBgra8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format);
+	void imageDecodeToBgra8(
+		  void* _dst
+		, const void* _src
+		, uint32_t _width
+		, uint32_t _height
+		, uint32_t _pitch
+		, TextureFormat::Enum _format
+		);
 
 	///
-	void imageDecodeToRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format);
+	void imageDecodeToRgba8(
+		  void* _dst
+		, const void* _src
+		, uint32_t _width
+		, uint32_t _height
+		, uint32_t _pitch
+		, TextureFormat::Enum _format
+		);
 
 	///
-	void imageDecodeToRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format);
+	void imageDecodeToRgba32f(
+		  bx::AllocatorI* _allocator
+		, void* _dst
+		, const void* _src
+		, uint32_t _width
+		, uint32_t _height
+		, uint32_t _pitch
+		, TextureFormat::Enum _format
+		);
 
 	///
-	bool imageGetRawData(const ImageContainer& _imageContainer, uint16_t _side, uint8_t _lod, const void* _data, uint32_t _size, ImageMip& _mip);
+	bool imageGetRawData(
+		  const ImageContainer& _imageContainer
+		, uint16_t _side
+		, uint8_t _lod
+		, const void* _data
+		, uint32_t _size
+		, ImageMip& _mip
+		);
 
 } // namespace bimg
 

+ 34 - 10
src/image.cpp

@@ -688,8 +688,8 @@ namespace bimg
 
 	struct PackUnpack
 	{
-		bx::PackFn pack;
-		bx::UnpackFn unpack;
+		PackFn pack;
+		UnpackFn unpack;
 	};
 
 	static const PackUnpack s_packUnpack[] =
@@ -775,14 +775,14 @@ namespace bimg
 
 	bool imageConvert(TextureFormat::Enum _dstFormat, TextureFormat::Enum _srcFormat)
 	{
-		bx::UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
-		bx::PackFn   pack   = s_packUnpack[_dstFormat].pack;
+		UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
+		PackFn   pack   = s_packUnpack[_dstFormat].pack;
 		return NULL != pack
 			&& NULL != unpack
 			;
 	}
 
-	void imageConvert(void* _dst, uint32_t _bpp, bx::PackFn _pack, const void* _src, bx::UnpackFn _unpack, uint32_t _size)
+	void imageConvert(void* _dst, uint32_t _bpp, PackFn _pack, const void* _src, UnpackFn _unpack, uint32_t _size)
 	{
 		const uint8_t* src = (uint8_t*)_src;
 		uint8_t* dst = (uint8_t*)_dst;
@@ -797,7 +797,7 @@ namespace bimg
 		}
 	}
 
-	void imageConvert(void* _dst, uint32_t _dstBpp, bx::PackFn _pack, const void* _src, uint32_t _srcBpp, bx::UnpackFn _unpack, uint32_t _width, uint32_t _height, uint32_t _srcPitch)
+	void imageConvert(void* _dst, uint32_t _dstBpp, PackFn _pack, const void* _src, uint32_t _srcBpp, UnpackFn _unpack, uint32_t _width, uint32_t _height, uint32_t _srcPitch)
 	{
 		const uint8_t* src = (uint8_t*)_src;
 		uint8_t* dst = (uint8_t*)_dst;
@@ -817,8 +817,8 @@ namespace bimg
 
 	bool imageConvert(void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _srcPitch)
 	{
-		bx::UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
-		bx::PackFn   pack   = s_packUnpack[_dstFormat].pack;
+		UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
+		PackFn   pack   = s_packUnpack[_dstFormat].pack;
 		if (NULL == pack
 		||  NULL == unpack)
 		{
@@ -889,10 +889,19 @@ namespace bimg
 		return output;
 	}
 
-	ImageContainer* imageParseBgfx(bx::AllocatorI* _allocator, const void* _src, uint32_t _size)
+	typedef bool (*ParseFn)(ImageContainer&, bx::ReaderSeekerI*);
+
+	template<uint32_t magicT, ParseFn parseFnT>
+	ImageContainer* imageParseT(bx::AllocatorI* _allocator, const void* _src, uint32_t _size)
 	{
+		bx::MemoryReader reader(_src, _size);
+
+		uint32_t magic;
+		bx::read(&reader, magic);
+
 		ImageContainer imageContainer;
-		if (!imageParse(imageContainer, _src, _size) )
+		if (magicT == magic
+		&& !parseFnT(imageContainer, &reader) )
 		{
 			return NULL;
 		}
@@ -2117,6 +2126,11 @@ namespace bimg
 		return TextureFormat::Unknown != format;
 	}
 
+	ImageContainer* imageParseDds(bx::AllocatorI* _allocator, const void* _src, uint32_t _size)
+	{
+		return imageParseT<DDS_MAGIC, imageParseDds>(_allocator, _src, _size);
+	}
+
 // KTX
 #define KTX_MAGIC       BX_MAKEFOURCC(0xAB, 'K', 'T', 'X')
 #define KTX_HEADER_SIZE 64
@@ -2427,6 +2441,11 @@ namespace bimg
 		return TextureFormat::Unknown != format;
 	}
 
+	ImageContainer* imageParseKtx(bx::AllocatorI* _allocator, const void* _src, uint32_t _size)
+	{
+		return imageParseT<KTX_MAGIC, imageParseKtx>(_allocator, _src, _size);
+	}
+
 // PVR3
 #define PVR3_MAKE8CC(_a, _b, _c, _d, _e, _f, _g, _h) (uint64_t(BX_MAKEFOURCC(_a, _b, _c, _d) ) | (uint64_t(BX_MAKEFOURCC(_e, _f, _g, _h) )<<32) )
 
@@ -2578,6 +2597,11 @@ namespace bimg
 		return TextureFormat::Unknown != format;
 	}
 
+	ImageContainer* imageParsePvr3(bx::AllocatorI* _allocator, const void* _src, uint32_t _size)
+	{
+		return imageParseT<PVR3_MAGIC, imageParsePvr3>(_allocator, _src, _size);
+	}
+
 	bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
 	{
 		uint32_t magic;

+ 3 - 50
src/image_decode.cpp

@@ -65,55 +65,6 @@ BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wshift-negative-value");
 #include <stb/stb_image.h>
 BX_PRAGMA_DIAGNOSTIC_POP();
 
-namespace bimg
-{
-#if !defined(BIMG_IMAGE_H_HEADER_GUARD)
-	struct ImageMip
-	{
-		TextureFormat::Enum m_format;
-		uint32_t m_width;
-		uint32_t m_height;
-		uint32_t m_blockSize;
-		uint32_t m_size;
-		uint8_t  m_bpp;
-		bool     m_hasAlpha;
-		const uint8_t* m_data;
-	};
-#endif // !defined(BIMG_IMAGE_H_HEADER_GUARD)
-
-	uint32_t imageGetSize(
-		  TextureInfo* _info
-		, uint16_t _width
-		, uint16_t _height
-		, uint16_t _depth
-		, bool _cubeMap
-		, bool _hasMips
-		, uint16_t _numLayers
-		, TextureFormat::Enum _format
-		);
-
-	///
-	ImageContainer* imageParseBgfx(bx::AllocatorI* _allocator, const void* _src, uint32_t _size);
-
-	///
-	bool imageConvert(
-		  void* _dst
-		, TextureFormat::Enum _dstFormat
-		, const void* _src
-		, TextureFormat::Enum _srcFormat
-		, uint32_t _width
-		, uint32_t _height
-		);
-
-	///
-	ImageContainer* imageConvert(
-		  bx::AllocatorI* _allocator
-		, TextureFormat::Enum _dstFormat
-		, const ImageContainer& _input
-		);
-
-} // namespace bimg
-
 namespace bimg
 {
 	static ImageContainer* imageParseLodePng(bx::AllocatorI* _allocator, const void* _data, uint32_t _size)
@@ -411,7 +362,9 @@ namespace bimg
 
 	ImageContainer* imageParse(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, TextureFormat::Enum _dstFormat)
 	{
-		ImageContainer* input = imageParseBgfx    (_allocator, _data, _size)        ;
+		ImageContainer* input = imageParseDds     (_allocator, _data, _size)        ;
+		input = NULL == input ? imageParseKtx     (_allocator, _data, _size) : input;
+		input = NULL == input ? imageParsePvr3    (_allocator, _data, _size) : input;
 		input = NULL == input ? imageParseLodePng (_allocator, _data, _size) : input;
 		input = NULL == input ? imageParseTinyExr (_allocator, _data, _size) : input;
 		input = NULL == input ? imageParseStbImage(_allocator, _data, _size) : input;