1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-29 14:25:07 +00:00
serenity/Userland/Libraries/LibGfx/QOILoader.h
Nico Weber 307712b398 LibGfx: Add a method to ImageDecoderPlugin for reading ICC data
This probably won't be the final API for getting color spaces
from images, since some formats just store an "is sRGB?" flag
instead of a full profile. Instead, once everything works,
we probably want to give every Bitmap a pointer to some
color space abstraction.

But we can always change this later, once things are further along
and better understood.
2023-01-27 17:26:48 +00:00

72 lines
1.9 KiB
C++

/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <LibCore/Stream.h>
#include <LibGfx/Forward.h>
#include <LibGfx/ImageDecoder.h>
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 };
OwnPtr<Core::Stream::Stream> stream {};
QOIHeader header {};
RefPtr<Bitmap> bitmap;
Optional<Error> error;
};
class QOIImageDecoderPlugin final : public ImageDecoderPlugin {
public:
static ErrorOr<bool> sniff(ReadonlyBytes);
static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes);
virtual ~QOIImageDecoderPlugin() override = default;
virtual IntSize size() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile(bool& was_purged) override;
virtual bool initialize() override;
virtual bool is_animated() override { return false; }
virtual size_t loop_count() override { return 0; }
virtual size_t frame_count() override { return 1; }
virtual ErrorOr<ImageFrameDescriptor> frame(size_t index) override;
virtual ErrorOr<Optional<ReadonlyBytes>> icc_data() override;
private:
ErrorOr<void> decode_header_and_update_context(Core::Stream::Stream&);
ErrorOr<void> decode_image_and_update_context(Core::Stream::Stream&);
QOIImageDecoderPlugin(NonnullOwnPtr<Core::Stream::Stream>);
OwnPtr<QOILoadingContext> m_context;
};
}
template<>
struct AK::Traits<Gfx::QOIHeader> : public GenericTraits<Gfx::QOIHeader> {
static constexpr bool is_trivially_serializable() { return true; }
};