Grok 20.3.2
MarkerParser.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#include <functional>
19
20#pragma once
21
22namespace grk
23{
24
25typedef std::function<bool(uint8_t* headerData, uint16_t headerSize)> MARKER_CALLBACK;
26
28{
29 IMarkerProcessor(uint16_t ID);
30 virtual ~IMarkerProcessor() = default;
31
32 virtual bool process(uint8_t* headerData, uint16_t headerSize) const = 0;
33 uint16_t id;
34};
35
37{
38 MarkerProcessor(uint16_t ID, MARKER_CALLBACK f);
39 bool process(uint8_t* headerData, uint16_t headerSize) const override;
40
41private:
43};
44
46{
47 MarkerScratch(void);
48 ~MarkerScratch(void);
49 MarkerScratch(const MarkerScratch&) = delete;
51 bool process(const IMarkerProcessor* handler, uint16_t markerSize);
52 void setStream(IStream* stream);
53
54private:
55 uint8_t* buff_ = nullptr;
56 uint16_t len_ = 0;
57 IStream* stream_ = nullptr;
58};
59
61{
62 MarkerParser() = default;
64 MarkerParser(const MarkerParser&) = delete;
66 void add(const uint16_t id, IMarkerProcessor* processor);
67 void add(const std::initializer_list<std::pair<const uint16_t, IMarkerProcessor*>>& newMarkers);
68 void clearProcessors(void);
69 void setStream(IStream* stream, bool ownsStream);
70
72 uint16_t currId(void);
73 void synch(uint16_t markerId);
74
80 bool checkForIllegalTilePart(void);
81 void setSOT(void);
82
88 bool readSOTorEOC(void);
89
95 bool endOfCodeStream(void);
96
103 bool readSOTafterSOD(void);
104
105 bool readId(bool suppressWarning);
106 std::pair<bool, uint16_t> processMarker();
107 static bool readShort(IStream* stream, uint16_t* val);
108 bool process(const IMarkerProcessor* processor, uint16_t markerBodyLength);
109
111
112private:
113 static std::string markerString(uint16_t marker);
114
115 std::unordered_map<uint16_t, IMarkerProcessor*> processors_;
116 uint16_t currMarkerId_ = 0;
117 IStream* stream_ = nullptr;
118 bool ownsStream_ = false;
120 bool foundEOC_ = false;
121};
122
123} // namespace grk
ResWindow.
Definition CompressedChunkCache.h:36
std::function< bool(uint8_t *headerData, uint16_t headerSize)> MARKER_CALLBACK
Definition MarkerParser.h:25
Definition MarkerParser.h:28
virtual ~IMarkerProcessor()=default
virtual bool process(uint8_t *headerData, uint16_t headerSize) const =0
IMarkerProcessor(uint16_t ID)
Definition MarkerParser.cpp:40
uint16_t id
Definition MarkerParser.h:33
Definition IStream.h:60
IStream * getStream()
Definition MarkerParser.cpp:66
bool endOfCodeStream(void)
Checks if end of code stream has been reached.
Definition MarkerParser.cpp:107
MarkerParser & operator=(const MarkerParser &)=delete
MarkerParser(const MarkerParser &)=delete
IStream * stream_
Definition MarkerParser.h:117
bool process(const IMarkerProcessor *processor, uint16_t markerBodyLength)
Definition MarkerParser.cpp:160
std::pair< bool, uint16_t > processMarker()
Definition MarkerParser.cpp:165
void add(const uint16_t id, IMarkerProcessor *processor)
Definition MarkerParser.cpp:209
static std::string markerString(uint16_t marker)
Definition MarkerParser.cpp:261
static bool readShort(IStream *stream, uint16_t *val)
Definition MarkerParser.cpp:71
bool checkForIllegalTilePart(void)
Check for corrupt images with extra tile parts.
Definition MarkerParser.cpp:248
bool foundEOC_
Definition MarkerParser.h:120
bool readSOTorEOC(void)
Reads next marker, which should be either SOT or EOC.
Definition MarkerParser.cpp:95
void clearProcessors(void)
Definition MarkerParser.cpp:229
std::unordered_map< uint16_t, IMarkerProcessor * > processors_
Definition MarkerParser.h:115
void setStream(IStream *stream, bool ownsStream)
Definition MarkerParser.cpp:57
bool readSOTafterSOD(void)
Reads next SOT after SOD The marker could also be EOC.
Definition MarkerParser.cpp:112
IMarkerProcessor * currentProcessor(void)
Definition MarkerParser.cpp:236
~MarkerParser()
Definition MarkerParser.cpp:50
MarkerParser()=default
MarkerScratch scratch_
Definition MarkerParser.h:119
uint16_t currId(void)
Definition MarkerParser.cpp:85
bool readId(bool suppressWarning)
Definition MarkerParser.cpp:194
void synch(uint16_t markerId)
Definition MarkerParser.cpp:81
uint16_t currMarkerId_
Definition MarkerParser.h:116
void setSOT(void)
Definition MarkerParser.cpp:89
bool ownsStream_
Definition MarkerParser.h:118
MARKER_CALLBACK callback_
Definition MarkerParser.h:42
MarkerProcessor(uint16_t ID, MARKER_CALLBACK f)
Definition MarkerParser.cpp:41
bool process(uint8_t *headerData, uint16_t headerSize) const override
Definition MarkerParser.cpp:44
Definition MarkerParser.h:46
bool process(const IMarkerProcessor *handler, uint16_t markerSize)
Definition MarkerParser.cpp:328
uint16_t len_
Definition MarkerParser.h:56
void setStream(IStream *stream)
Definition MarkerParser.cpp:350
MarkerScratch & operator=(const MarkerScratch &)=delete
IStream * stream_
Definition MarkerParser.h:57
uint8_t * buff_
Definition MarkerParser.h:55
MarkerScratch(const MarkerScratch &)=delete
MarkerScratch(void)
Definition MarkerParser.cpp:321
~MarkerScratch(void)
Definition MarkerParser.cpp:324