diff options
author | chai <chaifix@163.com> | 2019-08-07 21:08:47 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-08-07 21:08:47 +0800 |
commit | 0c391fdbce5a079cf03e483eb6174dd47806163d (patch) | |
tree | b06cd7a9d0ae0d9bb9e82f3dcb786dfce11f8628 /Source/modules/asura-core/Image/StbDecoder.cpp | |
parent | 9686368e58e25cbd6dc37d686bdd2be3f80486d6 (diff) |
*misc
Diffstat (limited to 'Source/modules/asura-core/Image/StbDecoder.cpp')
-rw-r--r-- | Source/modules/asura-core/Image/StbDecoder.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/Source/modules/asura-core/Image/StbDecoder.cpp b/Source/modules/asura-core/Image/StbDecoder.cpp new file mode 100644 index 0000000..101b148 --- /dev/null +++ b/Source/modules/asura-core/Image/StbDecoder.cpp @@ -0,0 +1,73 @@ +#include <asura-base/Exception.h> + +#include "StbDecoder.h" + +#define STB_IMAGE_IMPLEMENTATION +#include <stb/stb_image.h> + +using namespace AEGraphics; + +namespace_begin(AsuraEngine) +namespace_begin(Image) + +bool STBDecoder::CanDecode(FileSystem::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(FileSystem::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)) + { + // 4channelfloat + 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 |