From bad78945ceba425f6a80e3b8dca2414d592970eb Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 2 Aug 2019 20:51:00 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E5=90=8D=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/modules/asura-core/Image/StbDecoder.cpp | 73 ++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 source/modules/asura-core/Image/StbDecoder.cpp (limited to 'source/modules/asura-core/Image/StbDecoder.cpp') diff --git a/source/modules/asura-core/Image/StbDecoder.cpp b/source/modules/asura-core/Image/StbDecoder.cpp new file mode 100644 index 0000000..8d3537c --- /dev/null +++ b/source/modules/asura-core/Image/StbDecoder.cpp @@ -0,0 +1,73 @@ +#include + +#include "StbDecoder.h" + +#define STB_IMAGE_IMPLEMENTATION +#include + +using namespace AEGraphics; + +namespace_begin(AsuraEngine) +namespace_begin(Image) + +bool STBDecoder::CanDecode(IO::DataBuffer& buffer) +{ + int w = 0; + int h = 0; + int comp = 0; + + int status = stbi_info_from_memory((const stbi_uc*)buffer.GetData(), buffer.GetSize(), &w, &h, &comp); + + return status == 1 && w > 0 && h > 0; +} + +void STBDecoder::Decode(IO::DataBuffer& db, ImageData& imageData) +{ + const stbi_uc *buffer = (const stbi_uc *)db.GetData(); + // databuffer数据长 + int bufferlen = db.GetSize(); + + int width, height; + int comp = 0; + byte* data = nullptr; + ColorFormat format = COLOR_FORMAT_UNKNOWN; + std::size_t size = 0; + + if (stbi_is_hdr_from_memory(buffer, bufferlen)) + { + // 4个channel都是float + data = (byte*)stbi_loadf_from_memory(buffer, bufferlen, &width, &height, &comp, STBI_rgb_alpha); + format = COLOR_FORMAT_RGBA32F; + size = width * height * 4 * sizeof(float); + } + else + { + data = (byte*)stbi_load_from_memory(buffer, bufferlen, &width, &height, &comp, STBI_rgb_alpha); + format = COLOR_FORMAT_RGBA8; + size = width * height * 4; + } + if (data) + { + imageData.Lock(); + + if (imageData.pixels) + free(imageData.pixels); + imageData.pixels = (byte*)data; + imageData.format = format; + imageData.width = width; + imageData.height = height; + imageData.size = size; + + imageData.Unlock(); + } + else + { + const char *err = stbi_failure_reason(); + if (err == nullptr) + err = "unknown error"; + throw Exception("Could not decode image with stb_image (%s).", err); + } +} + +namespace_end +namespace_end \ No newline at end of file -- cgit v1.1-26-g67d0