diff --git a/src/asset.cpp b/src/asset.cpp index 697f51f..296ed6d 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -179,9 +179,8 @@ bool Asset::open_url(const std::string& url, std::function progress close(); LOG("download %s", url.c_str()); m_current_url = url; - std::vector data; curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &tmp_data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_data_handler_asset); #ifdef __ANDROID__ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); @@ -200,9 +199,10 @@ bool Asset::open_url(const std::string& url, std::function progress { if (m_data) delete m_data; - m_data = new uint8_t[data.size()]; - std::copy(data.begin(), data.end(), m_data); - m_len = data.size(); + m_data = new uint8_t[tmp_data.size()]; + std::copy(tmp_data.begin(), tmp_data.end(), m_data); + m_len = tmp_data.size(); + tmp_data.clear(); return true; } } diff --git a/src/asset.h b/src/asset.h index 110a360..27e6ee1 100644 --- a/src/asset.h +++ b/src/asset.h @@ -17,6 +17,7 @@ public: bool m_stop_async = false; std::function on_progress; std::future remote_future; + std::vector tmp_data; std::string m_current_path; std::string m_current_url; diff --git a/src/node_image.cpp b/src/node_image.cpp index 9e7f491..4424057 100644 --- a/src/node_image.cpp +++ b/src/node_image.cpp @@ -135,9 +135,18 @@ bool NodeImage::set_image(const std::string& path) void NodeImage::load_url(const std::string& url) { + m_remote_header_decoded = false; m_remote_asset = std::make_shared(); m_remote_asset->open_url_async(url, [this] (float progress) -> bool { + int w, h, c; + if (!m_remote_header_decoded && + stbi_info_from_memory(m_remote_asset->tmp_data.data(), m_remote_asset->tmp_data.size(), &w, &h, &c)) + { + if (m_autosize) + SetAspectRatio((float)w / (float)h); + m_remote_header_decoded = true; + } return true; }, [this] (bool success) { diff --git a/src/node_image.h b/src/node_image.h index 30c678a..0f41770 100644 --- a/src/node_image.h +++ b/src/node_image.h @@ -21,6 +21,7 @@ public: uint16_t m_tex_id; std::shared_ptr m_remote_asset; std::shared_ptr m_remote_texture; + bool m_remote_header_decoded = false; static void static_init(); virtual Node* clone_instantiate() const override; virtual void clone_copy(Node* dest) const override;