1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/*
* Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefPtr.h>
#include <AK/StringView.h>
#include <AK/Types.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/PortableImageLoaderCommon.h>
namespace Gfx {
template<class TFormatDetails>
struct PortableImageMapLoadingContext {
using FormatDetails = TFormatDetails;
enum class Type {
Unknown,
ASCII,
RAWBITS
};
enum class State {
NotDecoded = 0,
Error,
MagicNumber,
Width,
Height,
Maxval,
Bitmap,
Decoded
};
Type type { Type::Unknown };
State state { State::NotDecoded };
u8 const* data { nullptr };
size_t data_size { 0 };
size_t width { 0 };
size_t height { 0 };
FormatDetails format_details {};
RefPtr<Gfx::Bitmap> bitmap;
};
template<typename TContext>
class PortableImageDecoderPlugin final : public ImageDecoderPlugin {
public:
PortableImageDecoderPlugin(u8 const*, size_t);
virtual ~PortableImageDecoderPlugin() override = default;
virtual IntSize size() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;
virtual bool sniff() override;
virtual bool is_animated() override;
virtual size_t loop_count() override;
virtual size_t frame_count() override;
virtual ErrorOr<ImageFrameDescriptor> frame(size_t index) override;
private:
OwnPtr<TContext> m_context;
};
template<typename TContext>
PortableImageDecoderPlugin<TContext>::PortableImageDecoderPlugin(u8 const* data, size_t size)
{
m_context = make<TContext>();
m_context->data = data;
m_context->data_size = size;
}
template<typename TContext>
IntSize PortableImageDecoderPlugin<TContext>::size()
{
if (m_context->state == TContext::State::Error)
return {};
if (m_context->state < TContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return {};
}
return { m_context->width, m_context->height };
}
template<typename TContext>
void PortableImageDecoderPlugin<TContext>::set_volatile()
{
if (m_context->bitmap)
m_context->bitmap->set_volatile();
}
template<typename TContext>
bool PortableImageDecoderPlugin<TContext>::set_nonvolatile(bool& was_purged)
{
if (!m_context->bitmap)
return false;
return m_context->bitmap->set_nonvolatile(was_purged);
}
template<typename TContext>
bool PortableImageDecoderPlugin<TContext>::sniff()
{
using Context = TContext;
if (m_context->data_size < 2)
return false;
if (m_context->data[0] == 'P' && m_context->data[1] == Context::FormatDetails::ascii_magic_number)
return true;
if (m_context->data[0] == 'P' && m_context->data[1] == Context::FormatDetails::binary_magic_number)
return true;
return false;
}
template<typename TContext>
bool PortableImageDecoderPlugin<TContext>::is_animated()
{
return false;
}
template<typename TContext>
size_t PortableImageDecoderPlugin<TContext>::loop_count()
{
return 0;
}
template<typename TContext>
size_t PortableImageDecoderPlugin<TContext>::frame_count()
{
return 1;
}
template<typename TContext>
ErrorOr<ImageFrameDescriptor> PortableImageDecoderPlugin<TContext>::frame(size_t index)
{
if (index > 0)
return Error::from_string_literal("PortableImageDecoderPlugin: Invalid frame index");
if (m_context->state == TContext::State::Error)
return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed");
if (m_context->state < TContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return Error::from_string_literal("PortableImageDecoderPlugin: Decoding failed");
}
VERIFY(m_context->bitmap);
return ImageFrameDescriptor { m_context->bitmap, 0 };
}
}
|