/src/serenity/Userland/Libraries/LibGfx/ImageFormats/QMArithmeticDecoder.h
Line | Count | Source |
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/Span.h> |
10 | | |
11 | | namespace Gfx { |
12 | | |
13 | | // This is the arithmetic decoder described in Annex E of the JBIG2 spec. |
14 | | // See JBIG2Loader.cpp for the JBIG2 spec link. |
15 | | // |
16 | | // It's also used in JPEG2000 and also described in Annex C of the JPEG2000 spec. |
17 | | // See JPEG2000Loader.cpp for the JPEG2000 spec link. |
18 | | |
19 | | // E.3 Arithmetic decoding procedure, but with the changes described in |
20 | | // Annex G Arithmetic decoding procedure (software conventions). |
21 | | // Exposed for testing. |
22 | | class QMArithmeticDecoder { |
23 | | public: |
24 | | struct Context { |
25 | | u8 I { 0 }; // Index I stored for context CX (E.2.4) |
26 | | u8 is_mps { 0 }; // "More probable symbol" (E.1.1). 0 or 1. |
27 | | }; |
28 | | |
29 | | static ErrorOr<QMArithmeticDecoder> initialize(ReadonlyBytes data); |
30 | | |
31 | | bool get_next_bit(Context& context); |
32 | | |
33 | | private: |
34 | | QMArithmeticDecoder(ReadonlyBytes data) |
35 | 19.3k | : m_data(data) |
36 | 19.3k | { |
37 | 19.3k | } |
38 | | |
39 | | ReadonlyBytes m_data; |
40 | | |
41 | | // The code below uses names from the spec, so that the algorithms look exactly like the flowcharts in the spec. |
42 | | |
43 | | // Abbreviations: |
44 | | // "CX": "Context" (E.1) |
45 | | // "D": "Decision" (as in "encoder input" / "decoder output") (E.1) |
46 | | // "I(CX)": "Index I stored for context CX" (E.2.4) |
47 | | // "MPS": "More probable symbol" (E.1.1) |
48 | | // "LPS": "Less probable symbol" (E.1.1) |
49 | | |
50 | | void INITDEC(); |
51 | | u8 DECODE(); // Returns a single decoded bit. |
52 | | u8 MPS_EXCHANGE(); |
53 | | u8 LPS_EXCHANGE(); |
54 | | void RENORMD(); |
55 | | void BYTEIN(); |
56 | | |
57 | | u8 B(size_t offset = 0) const; // Byte pointed to by BP. |
58 | | size_t BP { 0 }; // Pointer into compressed data. |
59 | | |
60 | | // E.3.1 Decoder code register conventions |
61 | | u32 C { 0 }; // Consists of u16 C_high, C_low. |
62 | | u16 A { 0 }; // Current value of the fraction. Fixed precision; 0x8000 is equivalent to 0.75. |
63 | | |
64 | | u8 CT { 0 }; // Count of the number of bits in C. |
65 | | |
66 | | Context* CX { nullptr }; |
67 | 468M | static u8& I(Context* cx) { return cx->I; } |
68 | 226M | static u8& MPS(Context* cx) { return cx->is_mps; } |
69 | | static u16 Qe(u16); |
70 | | static u8 NMPS(u16); |
71 | | static u8 NLPS(u16); |
72 | | static u8 SWITCH(u16); |
73 | | }; |
74 | | |
75 | | } |