PNGWriter.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2021, Pierre Hoffmeister
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Optional.h>
  9. #include <AK/Vector.h>
  10. #include <LibGfx/Forward.h>
  11. #include <LibGfx/ImageFormats/PNGShared.h>
  12. namespace Gfx {
  13. class PNGChunk;
  14. // This is not a nested struct to work around https://llvm.org/PR36684
  15. struct PNGWriterOptions {
  16. // Data for the iCCP chunk.
  17. // FIXME: Allow writing cICP, sRGB, or gAMA instead too.
  18. Optional<ReadonlyBytes> icc_data;
  19. };
  20. class PNGWriter {
  21. public:
  22. using Options = PNGWriterOptions;
  23. static ErrorOr<ByteBuffer> encode(Gfx::Bitmap const&, Options options = Options {});
  24. private:
  25. PNGWriter() = default;
  26. Vector<u8> m_data;
  27. ErrorOr<void> add_chunk(PNGChunk&);
  28. ErrorOr<void> add_png_header();
  29. ErrorOr<void> add_IHDR_chunk(u32 width, u32 height, u8 bit_depth, PNG::ColorType color_type, u8 compression_method, u8 filter_method, u8 interlace_method);
  30. ErrorOr<void> add_iCCP_chunk(ReadonlyBytes icc_data);
  31. ErrorOr<void> add_IDAT_chunk(Gfx::Bitmap const&);
  32. ErrorOr<void> add_IEND_chunk();
  33. };
  34. }