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>
|
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 };
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
|
2023-07-07 22:23:04 +01:00
|
|
|
virtual bool is_vector() { return false; }
|
|
|
|
|
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
|
|
|
|
2023-07-07 22:23:04 +01:00
|
|
|
bool is_vector() { return m_plugin->is_vector(); }
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|