mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 19:22:45 +00:00 
			
		
		
		
	ImageDecoder: Add plumbing to allow a client to request an ideal size
This is only used for vector graphics format where requesting the true displayed size leads to visual enhancement.
This commit is contained in:
		
							parent
							
								
									4c23f0e142
								
							
						
					
					
						commit
						e56eb11dee
					
				
					 7 changed files with 13 additions and 13 deletions
				
			
		|  | @ -237,7 +237,7 @@ ErrorOr<void> ViewWidget::try_open_file(String const& path, Core::File& file) | ||||||
|         // Use out-of-process decoding for raster formats.
 |         // Use out-of-process decoding for raster formats.
 | ||||||
|         auto client = TRY(ImageDecoderClient::Client::try_create()); |         auto client = TRY(ImageDecoderClient::Client::try_create()); | ||||||
|         auto mime_type = Core::guess_mime_type_based_on_filename(path); |         auto mime_type = Core::guess_mime_type_based_on_filename(path); | ||||||
|         auto decoded_image = client->decode_image(file_data, mime_type); |         auto decoded_image = client->decode_image(file_data, OptionalNone {}, mime_type); | ||||||
|         if (!decoded_image.has_value()) { |         if (!decoded_image.has_value()) { | ||||||
|             return Error::from_string_literal("Failed to decode image"); |             return Error::from_string_literal("Failed to decode image"); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  | @ -57,7 +57,7 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::decode_bitmap(ReadonlyBytes bitmap_da | ||||||
|     auto optional_mime_type = guessed_mime_type.map([](auto mime_type) { return mime_type.to_byte_string(); }); |     auto optional_mime_type = guessed_mime_type.map([](auto mime_type) { return mime_type.to_byte_string(); }); | ||||||
| 
 | 
 | ||||||
|     // FIXME: Find a way to avoid the memory copying here.
 |     // FIXME: Find a way to avoid the memory copying here.
 | ||||||
|     auto maybe_decoded_image = client->decode_image(bitmap_data, optional_mime_type); |     auto maybe_decoded_image = client->decode_image(bitmap_data, OptionalNone {}, optional_mime_type); | ||||||
|     if (!maybe_decoded_image.has_value()) |     if (!maybe_decoded_image.has_value()) | ||||||
|         return Error::from_string_literal("Image decode failed"); |         return Error::from_string_literal("Image decode failed"); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -20,7 +20,7 @@ void Client::die() | ||||||
|         on_death(); |         on_death(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Optional<DecodedImage> Client::decode_image(ReadonlyBytes encoded_data, Optional<ByteString> mime_type) | Optional<DecodedImage> Client::decode_image(ReadonlyBytes encoded_data, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type) | ||||||
| { | { | ||||||
|     if (encoded_data.is_empty()) |     if (encoded_data.is_empty()) | ||||||
|         return {}; |         return {}; | ||||||
|  | @ -33,7 +33,7 @@ Optional<DecodedImage> Client::decode_image(ReadonlyBytes encoded_data, Optional | ||||||
|     auto encoded_buffer = encoded_buffer_or_error.release_value(); |     auto encoded_buffer = encoded_buffer_or_error.release_value(); | ||||||
| 
 | 
 | ||||||
|     memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size()); |     memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size()); | ||||||
|     auto response_or_error = try_decode_image(move(encoded_buffer), mime_type); |     auto response_or_error = try_decode_image(move(encoded_buffer), ideal_size, mime_type); | ||||||
| 
 | 
 | ||||||
|     if (response_or_error.is_error()) { |     if (response_or_error.is_error()) { | ||||||
|         dbgln("ImageDecoder died heroically"); |         dbgln("ImageDecoder died heroically"); | ||||||
|  |  | ||||||
|  | @ -32,7 +32,7 @@ class Client final | ||||||
| public: | public: | ||||||
|     Client(NonnullOwnPtr<Core::LocalSocket>); |     Client(NonnullOwnPtr<Core::LocalSocket>); | ||||||
| 
 | 
 | ||||||
|     Optional<DecodedImage> decode_image(ReadonlyBytes, Optional<ByteString> mime_type = {}); |     Optional<DecodedImage> decode_image(ReadonlyBytes, Optional<Gfx::IntSize> ideal_size = {}, Optional<ByteString> mime_type = {}); | ||||||
| 
 | 
 | ||||||
|     Function<void()> on_death; |     Function<void()> on_death; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -22,10 +22,10 @@ void ConnectionFromClient::die() | ||||||
|     Core::EventLoop::current().quit(0); |     Core::EventLoop::current().quit(0); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static void decode_image_to_bitmaps_and_durations_with_decoder(Gfx::ImageDecoder const& decoder, Vector<Gfx::ShareableBitmap>& bitmaps, Vector<u32>& durations) | static void decode_image_to_bitmaps_and_durations_with_decoder(Gfx::ImageDecoder const& decoder, Optional<Gfx::IntSize> ideal_size, Vector<Gfx::ShareableBitmap>& bitmaps, Vector<u32>& durations) | ||||||
| { | { | ||||||
|     for (size_t i = 0; i < decoder.frame_count(); ++i) { |     for (size_t i = 0; i < decoder.frame_count(); ++i) { | ||||||
|         auto frame_or_error = decoder.frame(i); |         auto frame_or_error = decoder.frame(i, ideal_size); | ||||||
|         if (frame_or_error.is_error()) { |         if (frame_or_error.is_error()) { | ||||||
|             bitmaps.append(Gfx::ShareableBitmap {}); |             bitmaps.append(Gfx::ShareableBitmap {}); | ||||||
|             durations.append(0); |             durations.append(0); | ||||||
|  | @ -37,7 +37,7 @@ static void decode_image_to_bitmaps_and_durations_with_decoder(Gfx::ImageDecoder | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static void decode_image_to_details(Core::AnonymousBuffer const& encoded_buffer, Optional<ByteString> const& known_mime_type, bool& is_animated, u32& loop_count, Vector<Gfx::ShareableBitmap>& bitmaps, Vector<u32>& durations) | static void decode_image_to_details(Core::AnonymousBuffer const& encoded_buffer, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> const& known_mime_type, bool& is_animated, u32& loop_count, Vector<Gfx::ShareableBitmap>& bitmaps, Vector<u32>& durations) | ||||||
| { | { | ||||||
|     VERIFY(bitmaps.size() == 0); |     VERIFY(bitmaps.size() == 0); | ||||||
|     VERIFY(durations.size() == 0); |     VERIFY(durations.size() == 0); | ||||||
|  | @ -54,10 +54,10 @@ static void decode_image_to_details(Core::AnonymousBuffer const& encoded_buffer, | ||||||
|     } |     } | ||||||
|     is_animated = decoder->is_animated(); |     is_animated = decoder->is_animated(); | ||||||
|     loop_count = decoder->loop_count(); |     loop_count = decoder->loop_count(); | ||||||
|     decode_image_to_bitmaps_and_durations_with_decoder(*decoder, bitmaps, durations); |     decode_image_to_bitmaps_and_durations_with_decoder(*decoder, ideal_size, bitmaps, durations); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Messages::ImageDecoderServer::DecodeImageResponse ConnectionFromClient::decode_image(Core::AnonymousBuffer const& encoded_buffer, Optional<ByteString> const& mime_type) | Messages::ImageDecoderServer::DecodeImageResponse ConnectionFromClient::decode_image(Core::AnonymousBuffer const& encoded_buffer, Optional<Gfx::IntSize> const& ideal_size, Optional<ByteString> const& mime_type) | ||||||
| { | { | ||||||
|     if (!encoded_buffer.is_valid()) { |     if (!encoded_buffer.is_valid()) { | ||||||
|         dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid"); |         dbgln_if(IMAGE_DECODER_DEBUG, "Encoded data is invalid"); | ||||||
|  | @ -68,7 +68,7 @@ Messages::ImageDecoderServer::DecodeImageResponse ConnectionFromClient::decode_i | ||||||
|     u32 loop_count = 0; |     u32 loop_count = 0; | ||||||
|     Vector<Gfx::ShareableBitmap> bitmaps; |     Vector<Gfx::ShareableBitmap> bitmaps; | ||||||
|     Vector<u32> durations; |     Vector<u32> durations; | ||||||
|     decode_image_to_details(encoded_buffer, mime_type, is_animated, loop_count, bitmaps, durations); |     decode_image_to_details(encoded_buffer, ideal_size, mime_type, is_animated, loop_count, bitmaps, durations); | ||||||
|     return { is_animated, loop_count, bitmaps, durations }; |     return { is_animated, loop_count, bitmaps, durations }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -26,7 +26,7 @@ public: | ||||||
| private: | private: | ||||||
|     explicit ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket>); |     explicit ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket>); | ||||||
| 
 | 
 | ||||||
|     virtual Messages::ImageDecoderServer::DecodeImageResponse decode_image(Core::AnonymousBuffer const&, Optional<ByteString> const& mime_type) override; |     virtual Messages::ImageDecoderServer::DecodeImageResponse decode_image(Core::AnonymousBuffer const&, Optional<Gfx::IntSize> const& ideal_size, Optional<ByteString> const& mime_type) override; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -3,5 +3,5 @@ | ||||||
| 
 | 
 | ||||||
| endpoint ImageDecoderServer | endpoint ImageDecoderServer | ||||||
| { | { | ||||||
|     decode_image(Core::AnonymousBuffer data, Optional<ByteString> mime_type) => (bool is_animated, u32 loop_count, Vector<Gfx::ShareableBitmap> bitmaps, Vector<u32> durations) |     decode_image(Core::AnonymousBuffer data, Optional<Gfx::IntSize> ideal_size, Optional<ByteString> mime_type) => (bool is_animated, u32 loop_count, Vector<Gfx::ShareableBitmap> bitmaps, Vector<u32> durations) | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lucas CHOLLET
						Lucas CHOLLET