diff options
author | chai <chaifix@163.com> | 2019-03-14 23:12:54 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-03-14 23:12:54 +0800 |
commit | 8644e03586ac9c39741f62cbcbce87b18805538c (patch) | |
tree | 72021765f5262b7f2f7eebe299fd4e1aaa7c89b7 /Source/Asura.Engine/graphics/stb_decoder.cpp | |
parent | 8d8c4ff1664625e7428d0d31cd798d9321680cb2 (diff) |
*大小写
Diffstat (limited to 'Source/Asura.Engine/graphics/stb_decoder.cpp')
-rw-r--r-- | Source/Asura.Engine/graphics/stb_decoder.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/Source/Asura.Engine/graphics/stb_decoder.cpp b/Source/Asura.Engine/graphics/stb_decoder.cpp new file mode 100644 index 0000000..b14d0f3 --- /dev/null +++ b/Source/Asura.Engine/graphics/stb_decoder.cpp @@ -0,0 +1,65 @@ +#include "STBDecoder.h" + +#include "Exceptions/Exception.h" +#include "stb/stb_image.h" + +namespace AsuraEngine +{ + namespace Graphics + { + + bool STBDecoder::CanDecode(const Filesystem::DataBuffer& buffer) + { + int w = 0; + int h = 0; + int comp = 0; + + int status = stbi_info_from_memory((const stbi_uc*)buffer.data, buffer.size, &w, &h, &comp); + + return status == 1 && w > 0 && h > 0; + } + + void STBDecoder::Decode(const Filesystem::DataBuffer& db, ImageData& imageData) + { + const stbi_uc *buffer = (const stbi_uc *)db.data; + int bufferlen = db.size; + int width, height; + int comp = 0; + byte* data = nullptr; + PixelFormat format = PIXELFORMAT_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 = PIXELFORMAT_RGBA32F; + size = width * height * 4 * sizeof(float); + } + else + { + data = (byte*)stbi_load_from_memory(buffer, bufferlen, &width, &height, &comp, STBI_rgb_alpha); + format = PIXELFORMAT_ASTC_8x5; + size = width * height * 4; + } + if (data) + { + // ֤ڴ汻ͷţһϲûͷŵΪimage dataһԵģimageǶεġ + if (imageData.pixels) + delete[] imageData.pixels; + imageData.pixels = (byte*)data; + imageData.format = format; + imageData.width = width; + imageData.height = height; + } + else + { + const char *err = stbi_failure_reason(); + if (err == nullptr) + err = "unknown error"; + throw Exception("Could not decode image with stb_image (%s).", err); + } + } + + } +}
\ No newline at end of file |