/src/serenity/Userland/Libraries/LibGfx/CMYKBitmap.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2024, Nico Weber <thakis@chromium.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/ByteBuffer.h> |
10 | | #include <AK/Format.h> |
11 | | #include <AK/NonnullRefPtr.h> |
12 | | #include <AK/RefCounted.h> |
13 | | #include <LibGfx/Bitmap.h> |
14 | | #include <LibGfx/Size.h> |
15 | | |
16 | | namespace Gfx { |
17 | | |
18 | | struct CMYK { |
19 | | u8 c; |
20 | | u8 m; |
21 | | u8 y; |
22 | | u8 k; |
23 | | |
24 | | int operator<=>(CMYK const&) const = default; |
25 | | }; |
26 | | |
27 | | class CMYKBitmap : public RefCounted<CMYKBitmap> { |
28 | | public: |
29 | | static ErrorOr<NonnullRefPtr<CMYKBitmap>> create_with_size(IntSize const& size); |
30 | | |
31 | 0 | IntSize const& size() const { return m_size; } |
32 | | |
33 | | [[nodiscard]] CMYK* scanline(int y); |
34 | | [[nodiscard]] CMYK const* scanline(int y) const; |
35 | | |
36 | | [[nodiscard]] CMYK* begin(); |
37 | | [[nodiscard]] CMYK* end(); |
38 | 0 | [[nodiscard]] size_t data_size() const { return m_data.size(); } |
39 | | |
40 | | ErrorOr<RefPtr<Bitmap>> to_low_quality_rgb() const; |
41 | | |
42 | | private: |
43 | | CMYKBitmap(IntSize const& size, ByteBuffer data) |
44 | 131 | : m_size(size) |
45 | 131 | , m_data(move(data)) |
46 | 131 | { |
47 | 131 | } |
48 | | |
49 | | IntSize m_size; |
50 | | ByteBuffer m_data; |
51 | | |
52 | | mutable RefPtr<Bitmap> m_rgb_bitmap; |
53 | | }; |
54 | | |
55 | | inline CMYK* CMYKBitmap::scanline(int y) |
56 | 24.4M | { |
57 | 24.4M | VERIFY(y >= 0 && y < m_size.height()); |
58 | 24.4M | return reinterpret_cast<CMYK*>(m_data.data() + y * m_size.width() * sizeof(CMYK)); |
59 | 24.4M | } |
60 | | |
61 | | inline CMYK const* CMYKBitmap::scanline(int y) const |
62 | 6.75M | { |
63 | 6.75M | VERIFY(y >= 0 && y < m_size.height()); |
64 | 6.75M | return reinterpret_cast<CMYK const*>(m_data.data() + y * m_size.width() * sizeof(CMYK)); |
65 | 6.75M | } |
66 | | |
67 | | inline CMYK* CMYKBitmap::begin() |
68 | 110 | { |
69 | 110 | return reinterpret_cast<CMYK*>(m_data.data()); |
70 | 110 | } |
71 | | |
72 | | inline CMYK* CMYKBitmap::end() |
73 | 0 | { |
74 | 0 | return reinterpret_cast<CMYK*>(m_data.data() + data_size()); |
75 | 0 | } |
76 | | |
77 | | } |
78 | | |
79 | | namespace AK { |
80 | | |
81 | | template<> |
82 | | struct Formatter<Gfx::CMYK> : Formatter<FormatString> { |
83 | | ErrorOr<void> format(FormatBuilder& builder, Gfx::CMYK const& value) |
84 | 0 | { |
85 | 0 | return Formatter<FormatString>::format(builder, "{:#02x}{:02x}{:02x}{:02x}"sv, value.c, value.m, value.y, value.k); |
86 | 0 | } |
87 | | }; |
88 | | |
89 | | } |