Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base64 Image support #1328

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Polyfills/Canvas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ target_link_libraries(Canvas
PRIVATE JsRuntimeInternal
PRIVATE GraphicsDeviceContext
PRIVATE UrlLib
PRIVATE base-n
PRIVATE napi_extensions)

set_property(TARGET Canvas PROPERTY FOLDER Polyfills)
Expand Down
4 changes: 4 additions & 0 deletions Polyfills/Canvas/Source/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ namespace Babylon::Polyfills

void Canvas::Impl::RemoveMonitoredResource(MonitoredResource* monitoredResource)
{
if (m_monitoredResources.empty())
{
return;
}
auto iter = std::find(m_monitoredResources.begin(), m_monitoredResources.end(), monitoredResource);
if (iter != m_monitoredResources.end())
{
Expand Down
52 changes: 37 additions & 15 deletions Polyfills/Canvas/Source/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "nanovg.h"
#include <cassert>
#include <napi/napi_pointer.h>
#include <basen.hpp>

namespace Babylon::Polyfills::Internal
{
Expand Down Expand Up @@ -97,13 +98,36 @@ namespace Babylon::Polyfills::Internal
}
}

bool NativeCanvasImage::SetBuffer(gsl::span<const std::byte> buffer)
{
m_imageContainer = bimg::imageParse(&m_allocator, buffer.data(), static_cast<uint32_t>(buffer.size_bytes()), bimg::TextureFormat::RGBA8);

if (m_imageContainer == nullptr)
{
return false;
}

m_width = m_imageContainer->m_width;
m_height = m_imageContainer->m_height;

if (!m_onloadHandlerRef.IsEmpty())
{
m_onloadHandlerRef.Call({});
}
return true;
}

void NativeCanvasImage::SetSrc(const Napi::CallbackInfo& info, const Napi::Value& value)
{
auto text{value.As<Napi::String>().Utf8Value()};
UrlLib::UrlRequest request{};
request.Open(UrlLib::UrlMethod::Get, text);
request.ResponseType(UrlLib::UrlResponseType::Buffer);
request.SendAsync().then(m_runtimeScheduler, *m_cancellationSource, [env{info.Env()}, this, request{std::move(request)}](arcana::expected<void, std::exception_ptr> result) {
request.SendAsync().then(m_runtimeScheduler, *m_cancellationSource, [env{info.Env()}, this, cancellationSource{m_cancellationSource}, request{std::move(request)}, text](arcana::expected<void, std::exception_ptr> result) {
if (cancellationSource->cancelled())
{
return;
}
CedricGuillemet marked this conversation as resolved.
Show resolved Hide resolved
if (result.has_error())
{
HandleLoadImageError(Napi::Error::New(env, result.error()));
Expand All @@ -113,27 +137,25 @@ namespace Babylon::Polyfills::Internal
Dispose();

auto buffer{request.ResponseBuffer()};
std::vector<uint8_t> base64Buffer;
if (buffer.data() == nullptr || buffer.size_bytes() == 0)
{
HandleLoadImageError(Napi::Error::New(env, "Image with provided source returned empty response."));
return;
// try with base64
static const std::string base64{"base64,"};
const auto pos = text.find(base64);
CedricGuillemet marked this conversation as resolved.
Show resolved Hide resolved
if (pos == std::string::npos)
{
HandleLoadImageError(Napi::Error::New(env, "Image with provided source returned empty response or invalid base64."));
return;
}
bn::decode_b64(text.begin() + pos + base64.length(), text.end(), std::back_inserter(base64Buffer));
buffer = {reinterpret_cast<std::byte*>(base64Buffer.data()), base64Buffer.size()};
}

m_imageContainer = bimg::imageParse(&m_allocator, buffer.data(), static_cast<uint32_t>(buffer.size_bytes()), bimg::TextureFormat::RGBA8);

if (m_imageContainer == nullptr)
if (!SetBuffer(buffer))
{
HandleLoadImageError(Napi::Error::New(env, "Unable to decode image with provided src."));
return;
}

m_width = m_imageContainer->m_width;
m_height = m_imageContainer->m_height;

if (!m_onloadHandlerRef.IsEmpty())
{
m_onloadHandlerRef.Call({});
}
});
}

Expand Down
1 change: 1 addition & 0 deletions Polyfills/Canvas/Source/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace Babylon::Polyfills::Internal
void SetOnload(const Napi::CallbackInfo&, const Napi::Value&);
void SetOnerror(const Napi::CallbackInfo&, const Napi::Value&);
void HandleLoadImageError(const Napi::Error& error);
bool SetBuffer(gsl::span<const std::byte> buffer);
void Dispose();

uint32_t m_width{1};
Expand Down