| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-07-27 01:12:53 +02:00
										 |  |  |  * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:32:54 +02:00
										 |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | #include <AK/OwnPtr.h>
 | 
					
						
							|  |  |  | #include <AK/RefCounted.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | #include <AK/RefPtr.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-21 20:55:37 +01:00
										 |  |  | #include <LibGfx/Bitmap.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 12:04:00 +01:00
										 |  |  | #include <LibGfx/Size.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | namespace Gfx { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Bitmap; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-25 00:19:06 +01:00
										 |  |  | static constexpr size_t maximum_width_for_decoded_images = 16384; | 
					
						
							|  |  |  | static constexpr size_t maximum_height_for_decoded_images = 16384; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | struct ImageFrameDescriptor { | 
					
						
							|  |  |  |     RefPtr<Bitmap> image; | 
					
						
							|  |  |  |     int duration { 0 }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 19:56:49 +02:00
										 |  |  | class ImageDecoderPlugin { | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-03-14 13:26:37 -06:00
										 |  |  |     virtual ~ImageDecoderPlugin() = default; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  |     virtual IntSize size() = 0; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 20:50:58 +01:00
										 |  |  |     virtual void set_volatile() = 0; | 
					
						
							| 
									
										
										
										
											2021-07-24 22:49:48 +02:00
										 |  |  |     [[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) = 0; | 
					
						
							| 
									
										
										
										
											2019-12-18 20:50:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 13:51:00 +01:00
										 |  |  |     virtual bool sniff() = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  |     virtual bool is_animated() = 0; | 
					
						
							|  |  |  |     virtual size_t loop_count() = 0; | 
					
						
							|  |  |  |     virtual size_t frame_count() = 0; | 
					
						
							| 
									
										
										
										
											2021-11-20 14:29:33 +01:00
										 |  |  |     virtual ErrorOr<ImageFrameDescriptor> frame(size_t index) = 0; | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2022-03-14 13:26:37 -06:00
										 |  |  |     ImageDecoderPlugin() = default; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 19:56:49 +02:00
										 |  |  | class ImageDecoder : public RefCounted<ImageDecoder> { | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-01-15 21:50:32 +02:00
										 |  |  |     static RefPtr<ImageDecoder> try_create_for_raw_bytes(ReadonlyBytes); | 
					
						
							|  |  |  |     static RefPtr<ImageDecoder> try_create_for_raw_bytes_with_known_path(StringView path, ReadonlyBytes); | 
					
						
							| 
									
										
										
										
											2022-03-14 13:26:37 -06:00
										 |  |  |     ~ImageDecoder() = default; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-27 01:12:53 +02:00
										 |  |  |     IntSize size() const { return m_plugin->size(); } | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     int width() const { return size().width(); } | 
					
						
							|  |  |  |     int height() const { return size().height(); } | 
					
						
							| 
									
										
										
										
											2021-07-27 01:12:53 +02:00
										 |  |  |     void set_volatile() { m_plugin->set_volatile(); } | 
					
						
							|  |  |  |     [[nodiscard]] bool set_nonvolatile(bool& was_purged) { return m_plugin->set_nonvolatile(was_purged); } | 
					
						
							|  |  |  |     bool sniff() const { return m_plugin->sniff(); } | 
					
						
							|  |  |  |     bool is_animated() const { return m_plugin->is_animated(); } | 
					
						
							|  |  |  |     size_t loop_count() const { return m_plugin->loop_count(); } | 
					
						
							|  |  |  |     size_t frame_count() const { return m_plugin->frame_count(); } | 
					
						
							| 
									
										
										
										
											2021-11-20 14:29:33 +01:00
										 |  |  |     ErrorOr<ImageFrameDescriptor> frame(size_t index) const { return m_plugin->frame(index); } | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-07-27 01:12:53 +02:00
										 |  |  |     explicit ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin>); | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-27 01:12:53 +02:00
										 |  |  |     NonnullOwnPtr<ImageDecoderPlugin> mutable m_plugin; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |