QOILoader.h 1.5 KB

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