| 
									
										
										
										
											2021-12-21 01:17:13 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Forward.h>
 | 
					
						
							|  |  |  | #include <LibGfx/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-21 14:58:06 -04:00
										 |  |  | #include <LibGfx/ImageFormats/ImageDecoder.h>
 | 
					
						
							| 
									
										
										
										
											2021-12-21 01:17:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Gfx { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Decoder for the "Quite OK Image" format (v1.0).
 | 
					
						
							|  |  |  | // https://qoiformat.org/qoi-specification.pdf
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct [[gnu::packed]] QOIHeader { | 
					
						
							|  |  |  |     char magic[4]; | 
					
						
							|  |  |  |     u32 width; | 
					
						
							|  |  |  |     u32 height; | 
					
						
							|  |  |  |     u8 channels; | 
					
						
							|  |  |  |     u8 colorspace; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct QOILoadingContext { | 
					
						
							|  |  |  |     enum class State { | 
					
						
							|  |  |  |         NotDecoded = 0, | 
					
						
							|  |  |  |         HeaderDecoded, | 
					
						
							|  |  |  |         ImageDecoded, | 
					
						
							|  |  |  |         Error, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     State state { State::NotDecoded }; | 
					
						
							| 
									
										
										
										
											2023-02-10 01:00:18 +01:00
										 |  |  |     OwnPtr<Stream> stream {}; | 
					
						
							| 
									
										
										
										
											2021-12-21 01:17:13 +01:00
										 |  |  |     QOIHeader header {}; | 
					
						
							|  |  |  |     RefPtr<Bitmap> bitmap; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class QOIImageDecoderPlugin final : public ImageDecoderPlugin { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-02-26 18:02:50 +00:00
										 |  |  |     static bool sniff(ReadonlyBytes); | 
					
						
							| 
									
										
										
										
											2023-01-20 10:13:14 +02:00
										 |  |  |     static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 01:17:13 +01:00
										 |  |  |     virtual ~QOIImageDecoderPlugin() override = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual IntSize size() override; | 
					
						
							| 
									
										
										
										
											2023-07-07 18:37:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 22:20:06 +01:00
										 |  |  |     virtual ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) override; | 
					
						
							| 
									
										
										
										
											2021-12-21 01:17:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2023-07-13 23:16:05 -04:00
										 |  |  |     ErrorOr<void> decode_header_and_update_context(); | 
					
						
							|  |  |  |     ErrorOr<void> decode_image_and_update_context(); | 
					
						
							| 
									
										
										
										
											2021-12-21 01:17:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-10 01:00:18 +01:00
										 |  |  |     QOIImageDecoderPlugin(NonnullOwnPtr<Stream>); | 
					
						
							| 
									
										
										
										
											2023-01-20 10:13:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 01:17:13 +01:00
										 |  |  |     OwnPtr<QOILoadingContext> m_context; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-01-23 18:30:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | struct AK::Traits<Gfx::QOIHeader> : public GenericTraits<Gfx::QOIHeader> { | 
					
						
							|  |  |  |     static constexpr bool is_trivially_serializable() { return true; } | 
					
						
							|  |  |  | }; |