/src/serenity/Userland/Libraries/LibGfx/ImageFormats/QOILoader.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Forward.h> |
10 | | #include <LibGfx/Forward.h> |
11 | | #include <LibGfx/ImageFormats/ImageDecoder.h> |
12 | | |
13 | | namespace Gfx { |
14 | | |
15 | | // Decoder for the "Quite OK Image" format (v1.0). |
16 | | // https://qoiformat.org/qoi-specification.pdf |
17 | | |
18 | | struct [[gnu::packed]] QOIHeader { |
19 | | char magic[4]; |
20 | | u32 width; |
21 | | u32 height; |
22 | | u8 channels; |
23 | | u8 colorspace; |
24 | | }; |
25 | | |
26 | | struct QOILoadingContext { |
27 | | enum class State { |
28 | | NotDecoded = 0, |
29 | | HeaderDecoded, |
30 | | ImageDecoded, |
31 | | Error, |
32 | | }; |
33 | | State state { State::NotDecoded }; |
34 | | OwnPtr<Stream> stream {}; |
35 | | QOIHeader header {}; |
36 | | RefPtr<Bitmap> bitmap; |
37 | | }; |
38 | | |
39 | | class QOIImageDecoderPlugin final : public ImageDecoderPlugin { |
40 | | public: |
41 | | static bool sniff(ReadonlyBytes); |
42 | | static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes); |
43 | | |
44 | 375 | virtual ~QOIImageDecoderPlugin() override = default; |
45 | | |
46 | | virtual IntSize size() override; |
47 | | |
48 | | virtual ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) override; |
49 | | |
50 | | private: |
51 | | ErrorOr<void> decode_header_and_update_context(); |
52 | | ErrorOr<void> decode_image_and_update_context(); |
53 | | |
54 | | QOIImageDecoderPlugin(NonnullOwnPtr<Stream>); |
55 | | |
56 | | OwnPtr<QOILoadingContext> m_context; |
57 | | }; |
58 | | |
59 | | } |
60 | | |
61 | | template<> |
62 | | struct AK::Traits<Gfx::QOIHeader> : public DefaultTraits<Gfx::QOIHeader> { |
63 | 0 | static constexpr bool is_trivially_serializable() { return true; } |
64 | | }; |