Added option to build with libheif.

This commit is contained in:
Бранимир Караџић
2024-06-11 17:58:39 -07:00
parent 98a40e8533
commit c4268ed766
5 changed files with 78 additions and 3 deletions

View File

@@ -17,6 +17,12 @@ project "bimg_decode"
path.join(BIMG_DIR, "src/image_decode.*"), path.join(BIMG_DIR, "src/image_decode.*"),
} }
if _OPTIONS["with-libheif"] then
defines {
"BIMG_DECODE_HEIF=1",
}
end
using_bx() using_bx()
configuration { "linux-*" } configuration { "linux-*" }

View File

@@ -18,6 +18,11 @@ newoption {
description = "Enable building tools.", description = "Enable building tools.",
} }
newoption {
trigger = "with-libheif",
description = "Enable building with libheif HEIF and AVIF file format decoder.",
}
solution "bimg" solution "bimg"
configurations { configurations {
"Debug", "Debug",

View File

@@ -21,6 +21,14 @@ project "texturec"
"bimg", "bimg",
} }
if _OPTIONS["with-libheif"] then
links {
"heif",
}
configuration {}
end
using_bx() using_bx()
configuration { "mingw-*" } configuration { "mingw-*" }

View File

@@ -56,4 +56,8 @@
# define BIMG_DECODE_ETC2 BIMG_DECODE_ENABLE # define BIMG_DECODE_ETC2 BIMG_DECODE_ENABLE
#endif // BIMG_DECODE_ETC2 #endif // BIMG_DECODE_ETC2
#ifndef BIMG_DECODE_HEIF
# define BIMG_DECODE_HEIF 0
#endif // BIMG_DECODE_HEIF
#endif // BIMG_CONFIG_H_HEADER_GUARD #endif // BIMG_CONFIG_H_HEADER_GUARD

View File

@@ -34,6 +34,10 @@ BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4334) // warning C4334: '<<' : result of 32 -
#include <lodepng/lodepng.cpp> #include <lodepng/lodepng.cpp>
BX_PRAGMA_DIAGNOSTIC_POP(); BX_PRAGMA_DIAGNOSTIC_POP();
#if BIMG_DECODE_HEIF
# include <libheif/heif.h>
#endif // BIMG_DECODE_HEIF
void* lodepng_malloc(size_t _size) void* lodepng_malloc(size_t _size)
{ {
return ::malloc(_size); return ::malloc(_size);
@@ -632,7 +636,6 @@ namespace bimg
output->m_hasAlpha = hasAlpha; output->m_hasAlpha = hasAlpha;
} }
return output; return output;
} }
@@ -679,8 +682,8 @@ namespace bimg
ImageContainer* output = imageAlloc(_allocator ImageContainer* output = imageAlloc(_allocator
, format , format
, uint16_t(width) , bx::narrowCast<uint16_t>(width)
, uint16_t(height) , bx::narrowCast<uint16_t>(height)
, 0 , 0
, 1 , 1
, false , false
@@ -828,6 +831,54 @@ namespace bimg
return image; return image;
} }
static ImageContainer* imageParseLibHeif(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, bx::Error* _err)
{
#if BIMG_DECODE_HEIF
heif_context* ctx = heif_context_alloc();
heif_context_read_from_memory_without_copy(ctx, _data, _size, NULL);
heif_image_handle* handle;
heif_context_get_primary_image_handle(ctx, &handle);
heif_image* image;
heif_decode_image(handle, &image, heif_colorspace_RGB, heif_chroma_interleaved_RGBA, NULL);
int32_t stride;
const uint8_t* data = heif_image_get_plane_readonly(image, heif_channel_interleaved, &stride);
ImageContainer* output = NULL;
if (NULL != data)
{
const bimg::TextureFormat::Enum format = bimg::TextureFormat::RGBA8;
const int32_t width = heif_image_handle_get_width(handle);
const int32_t height = heif_image_handle_get_height(handle);
output = imageAlloc(_allocator
, format
, bx::narrowCast<uint16_t>(width)
, bx::narrowCast<uint16_t>(height)
, 0
, 1
, false
, false
, data
);
}
heif_image_release(image);
heif_image_handle_release(handle);
heif_context_free(ctx);
BX_UNUSED(_err);
return output;
#else
BX_UNUSED(_allocator, _data, _size, _err);
return NULL;
#endif // BIMG_DECODE_HEIF
}
ImageContainer* imageParse(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, TextureFormat::Enum _dstFormat, bx::Error* _err) ImageContainer* imageParse(bx::AllocatorI* _allocator, const void* _data, uint32_t _size, TextureFormat::Enum _dstFormat, bx::Error* _err)
{ {
BX_ERROR_SCOPE(_err); BX_ERROR_SCOPE(_err);
@@ -840,6 +891,7 @@ namespace bimg
input = NULL == input ? imageParseTinyExr (_allocator, _data, _size, _err) : input; input = NULL == input ? imageParseTinyExr (_allocator, _data, _size, _err) : input;
input = NULL == input ? imageParseJpeg (_allocator, _data, _size, _err) : input; input = NULL == input ? imageParseJpeg (_allocator, _data, _size, _err) : input;
input = NULL == input ? imageParseStbImage(_allocator, _data, _size, _err) : input; input = NULL == input ? imageParseStbImage(_allocator, _data, _size, _err) : input;
input = NULL == input ? imageParseLibHeif (_allocator, _data, _size, _err) : input;
if (NULL == input) if (NULL == input)
{ {