From 5f6641c38f0364541528bb641f8b7aabe2d2308d Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Tue, 19 Aug 2025 19:13:46 +0200 Subject: [PATCH] load_icons: read files only once --- common/utils/load_icon.cpp | 77 +++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 43 deletions(-) diff --git a/common/utils/load_icon.cpp b/common/utils/load_icon.cpp index 390e848f..a85fb9db 100644 --- a/common/utils/load_icon.cpp +++ b/common/utils/load_icon.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -46,7 +45,7 @@ std::vector load_file(const std::filesystem::path & filename) template requires std::is_integral_v -T read(std::span & buffer) +T read(std::span & buffer) { if (sizeof(T) > buffer.size()) throw std::runtime_error{"File truncated"}; @@ -65,27 +64,22 @@ T read(std::span & buffer) template requires std::is_integral_v -std::span read(std::span & buffer, size_t count) +std::span read(std::span & buffer, size_t count) { if (count * sizeof(T) > buffer.size()) throw std::runtime_error{"File truncated"}; - std::span value{reinterpret_cast(buffer.data()), count}; + std::span value{reinterpret_cast(buffer.data()), count}; buffer = buffer.subspan(count * sizeof(T)); return value; } -std::vector try_load_svg(const std::filesystem::path & filename, int size) +std::vector try_load_svg(const std::vector & data, int size) { // TODO check if aspect ratio needs to be handled, dpi struct deleter { - void operator()(GFile * ptr) - { - g_object_unref(ptr); - } - void operator()(RsvgHandle * ptr) { g_object_unref(ptr); @@ -102,15 +96,13 @@ std::vector try_load_svg(const std::filesystem::path & filename, in } }; - using GFile_ptr = std::unique_ptr; using RsvgHandle_ptr = std::unique_ptr; using cairo_surface_t_ptr = std::unique_ptr; using cairo_t_ptr = std::unique_ptr; GError * error{}; - GFile_ptr file{g_file_new_for_path(filename.c_str())}; - RsvgHandle_ptr handle{rsvg_handle_new_from_gfile_sync(file.get(), RSVG_HANDLE_FLAGS_NONE, nullptr, &error)}; + RsvgHandle_ptr handle{rsvg_handle_new_from_data((guint8 *)data.data(), data.size(), &error)}; if (!handle) throw std::runtime_error{std::string{"Could not open: "} + error->message}; @@ -150,7 +142,7 @@ std::vector try_load_svg(const std::filesystem::path & filename, in return {{size, size, 32, std::move(png)}}; } -std::vector try_load_ico(const std::filesystem::path & filename) +std::vector try_load_ico(const std::vector & ico) { const uint32_t ICO_PNG_MAGIC = 0x474e5089; @@ -190,8 +182,6 @@ std::vector try_load_ico(const std::filesystem::path & filename) static_assert(rowstride(32, 32) == 128); static_assert(rowstride(33, 32) == 132); - auto ico = load_file(filename); - std::span header{ico}; auto reserved = read(header); @@ -199,7 +189,7 @@ std::vector try_load_ico(const std::filesystem::path & filename) auto icon_count = read(header); if (reserved != 0 or resource_type != 1) - throw std::runtime_error{"Cannot read icon: " + filename.native()}; + throw std::runtime_error{"Cannot read icon"}; std::vector icons; @@ -215,13 +205,13 @@ std::vector try_load_ico(const std::filesystem::path & filename) auto offset = read(header); if (offset + size > ico.size()) - throw std::runtime_error{"Cannot read icon: " + filename.native()}; + throw std::runtime_error{"Cannot read icon"}; auto data = std::span{ico}.subspan(offset, size); if (reserved != 0) - throw std::runtime_error{"Cannot read icon: " + filename.native()}; + throw std::runtime_error{"Cannot read icon"}; if (planes != 0 and planes != 1) - throw std::runtime_error{"Cannot read icon: " + filename.native()}; + throw std::runtime_error{"Cannot read icon"}; auto magic = read(data); @@ -283,7 +273,7 @@ std::vector try_load_ico(const std::filesystem::path & filename) auto w = width; auto h = height / 2; - std::span palette; // BGRX + std::span palette; // BGRX if (bpp <= 16) { @@ -298,8 +288,8 @@ std::vector try_load_ico(const std::filesystem::path & filename) size_t and_stride = rowstride(w, 1); size_t dst_stride = 4 * w; - std::span xor_map = read(data, xor_stride * h); - std::span and_map = read(data, and_stride * h); + std::span xor_map = read(data, xor_stride * h); + std::span and_map = read(data, and_stride * h); std::vector dest_buffer; dest_buffer.resize(h * dst_stride); @@ -309,8 +299,8 @@ std::vector try_load_ico(const std::filesystem::path & filename) case 1: for (int y = 0; y < h; y++) { - uint8_t * src_xor_row = xor_map.data() + xor_stride * y; - uint8_t * src_and_row = and_map.data() + and_stride * y; + const uint8_t * src_xor_row = xor_map.data() + xor_stride * y; + const uint8_t * src_and_row = and_map.data() + and_stride * y; uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y); for (int x = 0, i = 0; x < w; x++) @@ -337,8 +327,8 @@ std::vector try_load_ico(const std::filesystem::path & filename) case 4: for (int y = 0; y < h; y++) { - uint8_t * src_xor_row = xor_map.data() + xor_stride * y; - uint8_t * src_and_row = and_map.data() + and_stride * y; + const uint8_t * src_xor_row = xor_map.data() + xor_stride * y; + const uint8_t * src_and_row = and_map.data() + and_stride * y; uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y); for (int x = 0, i = 0; x < w; x++) @@ -365,8 +355,8 @@ std::vector try_load_ico(const std::filesystem::path & filename) case 8: for (int y = 0; y < h; y++) { - uint8_t * src_xor_row = xor_map.data() + xor_stride * y; - uint8_t * src_and_row = and_map.data() + and_stride * y; + const uint8_t * src_xor_row = xor_map.data() + xor_stride * y; + const uint8_t * src_and_row = and_map.data() + and_stride * y; uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y); for (int x = 0, i = 0; x < w; x++) @@ -393,8 +383,8 @@ std::vector try_load_ico(const std::filesystem::path & filename) case 16: for (int y = 0; y < h; y++) { - uint8_t * src_xor_row = xor_map.data() + xor_stride * y; - uint8_t * src_and_row = and_map.data() + and_stride * y; + const uint8_t * src_xor_row = xor_map.data() + xor_stride * y; + const uint8_t * src_and_row = and_map.data() + and_stride * y; uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y); for (int x = 0, i = 0; x < w; x++) @@ -423,8 +413,8 @@ std::vector try_load_ico(const std::filesystem::path & filename) case 24: for (int y = 0; y < h; y++) { - uint8_t * src_xor_row = xor_map.data() + xor_stride * y; - uint8_t * src_and_row = and_map.data() + and_stride * y; + const uint8_t * src_xor_row = xor_map.data() + xor_stride * y; + const uint8_t * src_and_row = and_map.data() + and_stride * y; uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y); for (int x = 0, i = 0; x < w; x++) @@ -447,7 +437,7 @@ std::vector try_load_ico(const std::filesystem::path & filename) case 32: for (int y = 0; y < h; y++) { - uint8_t * src_xor_row = (uint8_t *)xor_map.data() + xor_stride * y; + const uint8_t * src_xor_row = xor_map.data() + xor_stride * y; uint8_t * dst_row = dest_buffer.data() + dst_stride * (h - 1 - y); memcpy(dst_row, src_xor_row, dst_stride); @@ -481,15 +471,15 @@ std::vector try_load_ico(const std::filesystem::path & filename) return icons; } -std::vector try_load_zip(const std::filesystem::path & filename) +std::vector try_load_zip(const std::vector & data) { std::unique_ptr zip{archive_read_new(), &archive_read_free}; archive_read_support_filter_all(zip.get()); archive_read_support_format_all(zip.get()); - if (auto rc = archive_read_open_filename(zip.get(), filename.c_str(), 1'000'000); rc != ARCHIVE_OK) - throw std::runtime_error{"Cannot open " + filename.native() + ": " + archive_error_string(zip.get())}; + if (auto rc = archive_read_open_memory(zip.get(), data.data(), data.size()); rc != ARCHIVE_OK) + throw std::runtime_error{std::string("Cannot open ZIP: ") + archive_error_string(zip.get())}; archive_entry * entry; std::vector icons; @@ -532,9 +522,8 @@ std::vector try_load_zip(const std::filesystem::path & filename) return icons; } -std::vector try_load_png(const std::filesystem::path & filename) +std::vector try_load_png(const std::vector & png_data) { - auto png_data = load_file(filename); std::vector icons; // Try reading the PNG @@ -576,30 +565,32 @@ const std::vector & wivrn::load_icon(const std::filesystem::path & if (it != icon_cache.end() and not it->second.empty()) return it->second; + auto data = load_file(filename); + try { - return icon_cache.emplace(filename, try_load_svg(filename, 256)).first->second; + return icon_cache.emplace(filename, try_load_svg(data, 256)).first->second; } catch (...) {} try { - return icon_cache.emplace(filename, try_load_ico(filename)).first->second; + return icon_cache.emplace(filename, try_load_ico(data)).first->second; } catch (...) {} try { - return icon_cache.emplace(filename, try_load_zip(filename)).first->second; + return icon_cache.emplace(filename, try_load_zip(data)).first->second; } catch (...) {} try { - return icon_cache.emplace(filename, try_load_png(filename)).first->second; + return icon_cache.emplace(filename, try_load_png(data)).first->second; } catch (...) {}