| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2024-01-07 21:48:03 -05:00
										 |  |  | #include <LibGfx/CMYKBitmap.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 12:04:00 +01:00
										 |  |  | #include <LibGfx/Size.h>
 | 
					
						
							| 
									
										
										
										
											2023-07-07 22:23:04 +01:00
										 |  |  | #include <LibGfx/VectorGraphic.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-05-03 17:12:54 +01:00
										 |  |  | struct ImageFrameDescriptor { | 
					
						
							|  |  |  |     RefPtr<Bitmap> image; | 
					
						
							|  |  |  |     int duration { 0 }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 22:23:04 +01:00
										 |  |  | struct VectorImageFrameDescriptor { | 
					
						
							|  |  |  |     RefPtr<VectorGraphic> image; | 
					
						
							|  |  |  |     int duration { 0 }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-07 20:41:32 -05:00
										 |  |  | enum class NaturalFrameFormat { | 
					
						
							|  |  |  |     RGB, | 
					
						
							| 
									
										
										
										
											2024-01-07 21:48:03 -05:00
										 |  |  |     CMYK, | 
					
						
							| 
									
										
										
										
											2024-01-07 20:41:32 -05:00
										 |  |  |     Vector, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 13:15:13 -04:00
										 |  |  |     // Each plugin should implement these static functions and register them in ImageDecoder.cpp
 | 
					
						
							|  |  |  |     // Implement sniff() if the file includes a magic number
 | 
					
						
							|  |  |  |     // static bool sniff(ReadonlyBytes);
 | 
					
						
							|  |  |  |     // Implement validate_before_create() otherwise
 | 
					
						
							|  |  |  |     // static ErrorOr<bool> validate_before_create(ReadonlyBytes);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // This function should be used to both create the context and parse the image header.
 | 
					
						
							|  |  |  |     // static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // This should always be available as gathered in create()
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  |     virtual IntSize size() = 0; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 13:29:12 -04:00
										 |  |  |     // Override this if the format supports animated images
 | 
					
						
							|  |  |  |     virtual bool is_animated() { return false; } | 
					
						
							|  |  |  |     virtual size_t loop_count() { return 0; } | 
					
						
							|  |  |  |     virtual size_t frame_count() { return 1; } | 
					
						
							|  |  |  |     virtual size_t first_animated_frame_index() { return 0; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 22:20:06 +01:00
										 |  |  |     virtual ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) = 0; | 
					
						
							| 
									
										
										
										
											2023-10-28 18:18:14 -04:00
										 |  |  |     virtual ErrorOr<Optional<ReadonlyBytes>> icc_data() { return OptionalNone {}; } | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-07 20:41:32 -05:00
										 |  |  |     virtual NaturalFrameFormat natural_frame_format() const { return NaturalFrameFormat::RGB; } | 
					
						
							| 
									
										
										
										
											2024-01-07 21:48:03 -05:00
										 |  |  |     virtual ErrorOr<NonnullRefPtr<CMYKBitmap>> cmyk_frame() { VERIFY_NOT_REACHED(); } | 
					
						
							| 
									
										
										
										
											2023-07-07 22:23:04 +01:00
										 |  |  |     virtual ErrorOr<VectorImageFrameDescriptor> vector_frame(size_t) { VERIFY_NOT_REACHED(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-12-16 17:49:34 +03:30
										 |  |  |     static RefPtr<ImageDecoder> try_create_for_raw_bytes(ReadonlyBytes, Optional<ByteString> mime_type = {}); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     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(); } | 
					
						
							| 
									
										
										
										
											2023-04-07 20:41:22 -06:00
										 |  |  |     size_t first_animated_frame_index() const { return m_plugin->first_animated_frame_index(); } | 
					
						
							| 
									
										
										
										
											2023-07-02 22:20:06 +01:00
										 |  |  |     ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) const { return m_plugin->frame(index, ideal_size); } | 
					
						
							| 
									
										
										
										
											2023-01-26 07:23:59 -05:00
										 |  |  |     ErrorOr<Optional<ReadonlyBytes>> icc_data() const { return m_plugin->icc_data(); } | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-07 20:41:32 -05:00
										 |  |  |     NaturalFrameFormat natural_frame_format() { return m_plugin->natural_frame_format(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-07 21:48:03 -05:00
										 |  |  |     // Call only if natural_frame_format() == NaturalFrameFormat::CMYK.
 | 
					
						
							|  |  |  |     ErrorOr<NonnullRefPtr<CMYKBitmap>> cmyk_frame(size_t) { return m_plugin->cmyk_frame(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-07 20:41:32 -05:00
										 |  |  |     // Call only if natural_frame_format() == NaturalFrameFormat::Vector.
 | 
					
						
							| 
									
										
										
										
											2023-07-07 22:23:04 +01:00
										 |  |  |     ErrorOr<VectorImageFrameDescriptor> vector_frame(size_t index) { return m_plugin->vector_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
										 |  |  | 
 | 
					
						
							|  |  |  | } |