| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							| 
									
										
										
										
											2020-06-01 21:32:54 +02:00
										 |  |  |     virtual ~ImageDecoderPlugin() { } | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  |     virtual IntSize size() = 0; | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     virtual RefPtr<Gfx::Bitmap> bitmap() = 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; | 
					
						
							|  |  |  |     virtual ImageFrameDescriptor frame(size_t i) = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2020-06-01 21:32:54 +02:00
										 |  |  |     ImageDecoderPlugin() { } | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |     static NonnullRefPtr<ImageDecoder> create(const u8* data, size_t size) { return adopt_ref(*new ImageDecoder(data, size)); } | 
					
						
							|  |  |  |     static NonnullRefPtr<ImageDecoder> create(const ByteBuffer& data) { return adopt_ref(*new ImageDecoder(data.data(), data.size())); } | 
					
						
							| 
									
										
										
										
											2019-10-19 19:56:49 +02:00
										 |  |  |     ~ImageDecoder(); | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 15:28:04 +02:00
										 |  |  |     bool is_valid() const { return m_plugin; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     IntSize size() const { return m_plugin ? m_plugin->size() : IntSize(); } | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     int width() const { return size().width(); } | 
					
						
							|  |  |  |     int height() const { return size().height(); } | 
					
						
							| 
									
										
										
										
											2020-02-15 00:58:14 +01:00
										 |  |  |     RefPtr<Gfx::Bitmap> bitmap() const; | 
					
						
							| 
									
										
										
										
											2020-06-13 15:28:04 +02:00
										 |  |  |     void set_volatile() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_plugin) | 
					
						
							|  |  |  |             m_plugin->set_volatile(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-07-24 22:49:48 +02:00
										 |  |  |     [[nodiscard]] bool set_nonvolatile(bool& was_purged) { return m_plugin ? m_plugin->set_nonvolatile(was_purged) : false; } | 
					
						
							| 
									
										
										
										
											2020-06-13 15:28:04 +02:00
										 |  |  |     bool sniff() const { return m_plugin ? m_plugin->sniff() : false; } | 
					
						
							|  |  |  |     bool is_animated() const { return m_plugin ? m_plugin->is_animated() : false; } | 
					
						
							|  |  |  |     size_t loop_count() const { return m_plugin ? m_plugin->loop_count() : 0; } | 
					
						
							|  |  |  |     size_t frame_count() const { return m_plugin ? m_plugin->frame_count() : 0; } | 
					
						
							|  |  |  |     ImageFrameDescriptor frame(size_t i) const { return m_plugin ? m_plugin->frame(i) : ImageFrameDescriptor(); } | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2019-10-19 19:56:49 +02:00
										 |  |  |     ImageDecoder(const u8*, size_t); | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 19:56:49 +02:00
										 |  |  |     mutable OwnPtr<ImageDecoderPlugin> m_plugin; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |