diff --git a/Userland/Libraries/LibGUI/CMakeLists.txt b/Userland/Libraries/LibGUI/CMakeLists.txt index 0f92ea9d3a..86cf325379 100644 --- a/Userland/Libraries/LibGUI/CMakeLists.txt +++ b/Userland/Libraries/LibGUI/CMakeLists.txt @@ -155,5 +155,5 @@ set(GENERATED_SOURCES ) serenity_lib(LibGUI gui) -target_link_libraries(LibGUI PRIVATE LibCore LibFileSystem LibGfx LibIPC LibThreading LibRegex LibConfig LibUnicode) +target_link_libraries(LibGUI PRIVATE LibCore LibFileSystem LibGfx LibImageDecoderClient LibIPC LibThreading LibRegex LibConfig LibUnicode) target_link_libraries(LibGUI PUBLIC LibSyntax) diff --git a/Userland/Libraries/LibGUI/FileSystemModel.cpp b/Userland/Libraries/LibGUI/FileSystemModel.cpp index 8aaae5a847..99880d1356 100644 --- a/Userland/Libraries/LibGUI/FileSystemModel.cpp +++ b/Userland/Libraries/LibGUI/FileSystemModel.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -697,11 +698,34 @@ struct ThumbnailCache { }; static Threading::MutexProtected s_thumbnail_cache {}; +static Threading::MutexProtected> s_image_decoder_client {}; static ErrorOr> render_thumbnail(StringView path) { - Gfx::IntSize thumbnail_size { 32, 32 }; - auto bitmap = TRY(Gfx::Bitmap::load_from_file(path, 1, thumbnail_size)); + Core::EventLoop event_loop; + Gfx::IntSize const thumbnail_size { 32, 32 }; + + auto file = TRY(Core::MappedFile::map(path)); + auto decoded_image = TRY(s_image_decoder_client.with_locked([=, &file](auto& maybe_client) -> ErrorOr> { + if (!maybe_client) { + maybe_client = TRY(ImageDecoderClient::Client::try_create()); + maybe_client->on_death = []() { + s_image_decoder_client.with_locked([](auto& client) { + client = nullptr; + }); + }; + } + + auto mime_type = Core::guess_mime_type_based_on_filename(path); + auto decoded_image = maybe_client->decode_image(file->bytes(), thumbnail_size, mime_type); + if (!decoded_image.has_value()) + return Error::from_string_literal("Unable to decode the image."); + + return decoded_image; + })); + + auto bitmap = decoded_image.value().frames[0].bitmap; + auto thumbnail = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, thumbnail_size)); double scale = min(thumbnail_size.width() / (double)bitmap->width(), thumbnail_size.height() / (double)bitmap->height());