Grok  9.5.0
Precinct.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2021 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 #pragma once
17 #include "grk_includes.h"
18 
19 namespace grk
20 {
21 template<typename T, typename P>
22 class BlockCache : public SparseCache<T>
23 {
24  public:
25  BlockCache(uint64_t maxChunkSize, P* blockInitializer)
26  : SparseCache<T>(maxChunkSize), m_blockInitializer(blockInitializer)
27  {}
28  virtual ~BlockCache() = default;
29 
30  protected:
31  virtual T* create(uint64_t index) override
32  {
33  auto item = new T();
34  m_blockInitializer->initCodeBlock(item, index);
35  return item;
36  }
37 
38  private:
40 };
41 
43 {
44  PrecinctImpl(bool isCompressor, grkRectU32* bounds, grkPointU32 cblk_expn)
45  : enc(nullptr), dec(nullptr), m_bounds(*bounds), m_cblk_expn(cblk_expn),
46  m_isCompressor(isCompressor), incltree(nullptr), imsbtree(nullptr)
47  {
48  m_cblk_grid =
49  grkRectU32(floordivpow2(bounds->x0, cblk_expn.x), floordivpow2(bounds->y0, cblk_expn.y),
50  ceildivpow2<uint32_t>(bounds->x1, cblk_expn.x),
51  ceildivpow2<uint32_t>(bounds->y1, cblk_expn.y));
52  }
54  {
56  delete enc;
57  delete dec;
58  }
59  grkRectU32 getCodeBlockBounds(uint64_t cblkno)
60  {
61  auto cblk_start = grkPointU32(
62  (m_cblk_grid.x0 + (uint32_t)(cblkno % m_cblk_grid.width())) << m_cblk_expn.x,
63  (m_cblk_grid.y0 + (uint32_t)(cblkno / m_cblk_grid.width())) << m_cblk_expn.y);
64  auto cblk_bounds =
65  grkRectU32(cblk_start.x, cblk_start.y, cblk_start.x + (1U << m_cblk_expn.x),
66  cblk_start.y + (1U << m_cblk_expn.y));
67 
68  return cblk_bounds.intersection(&m_bounds);
69  }
70  bool initCodeBlocks(grkRectU32* bounds)
71  {
72  if((m_isCompressor && enc) || (!m_isCompressor && dec))
73  return true;
74  m_bounds = *bounds;
75  auto numBlocks = m_cblk_grid.area();
76  if(!numBlocks)
77  return true;
78  if(m_isCompressor)
79  enc = new BlockCache<CompressCodeblock, PrecinctImpl>(numBlocks, this);
80  else
82 
83  return true;
84  }
85  template<typename T>
86  bool initCodeBlock(T* block, uint64_t cblkno)
87  {
88  if(block->non_empty())
89  return true;
90  if(!block->init())
91  return false;
92  block->setRect(getCodeBlockBounds(cblkno));
93 
94  return true;
95  }
97  {
98  delete incltree;
99  incltree = nullptr;
100  delete imsbtree;
101  imsbtree = nullptr;
102  }
104  {
105  // if cw == 0 or ch == 0,
106  // then the precinct has no code blocks, therefore
107  // no need for inclusion and msb tag trees
108  auto grid_width = m_cblk_grid.width();
109  auto grid_height = m_cblk_grid.height();
110  if(grid_width > 0 && grid_height > 0)
111  {
112  if(!incltree)
113  {
114  try
115  {
116  incltree = new TagTreeU16(grid_width, grid_height);
117  }
118  catch(std::exception& e)
119  {
120  GRK_UNUSED(e);
121  GRK_WARN("No incltree created.");
122  }
123  }
124  return incltree;
125  }
126  return nullptr;
127  }
129  {
130  // if cw == 0 or ch == 0,
131  // then the precinct has no code blocks, therefore
132  // no need for inclusion and msb tag trees
133  auto grid_width = m_cblk_grid.width();
134  auto grid_height = m_cblk_grid.height();
135  if(grid_width > 0 && grid_height > 0)
136  {
137  if(!imsbtree)
138  {
139  try
140  {
141  imsbtree = new TagTreeU8(grid_width, grid_height);
142  }
143  catch(std::exception& e)
144  {
145  GRK_UNUSED(e);
146  GRK_WARN("No imsbtree created.");
147  }
148  }
149  return imsbtree;
150  }
151  return nullptr;
152  }
159 
160  private:
161  TagTreeU16* incltree; /* inclusion tree */
162  TagTreeU8* imsbtree; /* IMSB tree */
163 };
164 struct Precinct : public grkRectU32
165 {
166  Precinct(const grkRectU32& bounds, bool isCompressor, grkPointU32 cblk_expn)
167  : grkRectU32(bounds), precinctIndex(0),
168  impl(new PrecinctImpl(isCompressor, this, cblk_expn)), m_cblk_expn(cblk_expn)
169  {}
171  {
172  delete impl;
173  }
175  {
176  impl->deleteTagTrees();
177  }
179  {
180  return impl->getCodeBlockBounds(cblkno);
181  }
183  {
184  return impl->getIncludeTagTree();
185  }
187  {
188  return impl->getIMsbTagTree();
189  }
190  uint32_t getCblkGridwidth(void)
191  {
192  return impl->m_cblk_grid.width();
193  }
194  uint32_t getCblkGridHeight(void)
195  {
196  return impl->m_cblk_grid.height();
197  }
198  uint32_t getNominalBlockSize(void)
199  {
200  return (1U << impl->m_cblk_expn.x) * (1U << impl->m_cblk_expn.y);
201  }
202  uint64_t getNumCblks(void)
203  {
204  return impl->m_cblk_grid.area();
205  }
207  {
208  return getImpl()->enc->get(cblkno);
209  }
211  {
212  return getImpl()->dec->get(cblkno);
213  }
215  {
216  return getImpl()->dec->tryGet(cblkno);
217  }
219  {
220  return m_cblk_expn;
221  }
223  {
224  return impl->m_cblk_grid;
225  }
226  uint64_t precinctIndex;
227 
228  private:
232  {
233  impl->initCodeBlocks(this);
234  return impl;
235  }
236 };
237 
238 } // namespace grk
Definition: Precinct.h:23
BlockCache(uint64_t maxChunkSize, P *blockInitializer)
Definition: Precinct.h:25
virtual ~BlockCache()=default
P * m_blockInitializer
Definition: Precinct.h:39
virtual T * create(uint64_t index) override
Definition: Precinct.h:31
Definition: SparseCache.h:25
Tag tree.
Definition: TagTree.h:47
#define GRK_UNUSED(x)
Definition: grk_includes.h:87
Copyright (C) 2016-2021 Grok Image Compression Inc.
Definition: ICacheable.h:20
grkRect< uint32_t > grkRectU32
Definition: util.h:56
static uint32_t floordivpow2(uint32_t a, uint32_t b)
Divide an unsigned integer by a power of 2 and round downwards.
Definition: grk_intmath.h:48
grkPoint< uint32_t > grkPointU32
Definition: util.h:37
void GRK_WARN(const char *fmt,...)
Definition: logger.cpp:49
TagTree< uint16_t > TagTreeU16
Definition: TagTree.h:255
TagTree< uint8_t > TagTreeU8
Definition: TagTree.h:254
Definition: Codeblock.h:123
Definition: Codeblock.h:182
Definition: Precinct.h:165
uint32_t getCblkGridHeight(void)
Definition: Precinct.h:194
TagTreeU16 * getInclTree(void)
Definition: Precinct.h:182
Precinct(const grkRectU32 &bounds, bool isCompressor, grkPointU32 cblk_expn)
Definition: Precinct.h:166
TagTreeU8 * getImsbTree(void)
Definition: Precinct.h:186
uint64_t getNumCblks(void)
Definition: Precinct.h:202
DecompressCodeblock * tryGetDecompressedBlockPtr(uint64_t cblkno)
Definition: Precinct.h:214
CompressCodeblock * getCompressedBlockPtr(uint64_t cblkno)
Definition: Precinct.h:206
PrecinctImpl * impl
Definition: Precinct.h:229
uint32_t getNominalBlockSize(void)
Definition: Precinct.h:198
DecompressCodeblock * getDecompressedBlockPtr(uint64_t cblkno)
Definition: Precinct.h:210
~Precinct()
Definition: Precinct.h:170
uint64_t precinctIndex
Definition: Precinct.h:226
grkRectU32 getCblkGrid(void)
Definition: Precinct.h:222
grkPointU32 getCblkExpn(void)
Definition: Precinct.h:218
grkRectU32 getCodeBlockBounds(uint64_t cblkno)
Definition: Precinct.h:178
void deleteTagTrees()
Definition: Precinct.h:174
grkPointU32 m_cblk_expn
Definition: Precinct.h:230
uint32_t getCblkGridwidth(void)
Definition: Precinct.h:190
PrecinctImpl * getImpl(void)
Definition: Precinct.h:231
Definition: Precinct.h:43
TagTreeU16 * getIncludeTagTree(void)
Definition: Precinct.h:103
grkPointU32 m_cblk_expn
Definition: Precinct.h:157
TagTreeU8 * imsbtree
Definition: Precinct.h:162
grkRectU32 m_bounds
Definition: Precinct.h:156
TagTreeU8 * getIMsbTagTree(void)
Definition: Precinct.h:128
grkRectU32 m_cblk_grid
Definition: Precinct.h:155
~PrecinctImpl()
Definition: Precinct.h:53
bool initCodeBlocks(grkRectU32 *bounds)
Definition: Precinct.h:70
bool m_isCompressor
Definition: Precinct.h:158
BlockCache< CompressCodeblock, PrecinctImpl > * enc
Definition: Precinct.h:153
PrecinctImpl(bool isCompressor, grkRectU32 *bounds, grkPointU32 cblk_expn)
Definition: Precinct.h:44
bool initCodeBlock(T *block, uint64_t cblkno)
Definition: Precinct.h:86
grkRectU32 getCodeBlockBounds(uint64_t cblkno)
Definition: Precinct.h:59
void deleteTagTrees()
Definition: Precinct.h:96
TagTreeU16 * incltree
Definition: Precinct.h:161
BlockCache< DecompressCodeblock, PrecinctImpl > * dec
Definition: Precinct.h:154
T x
Definition: util.h:34
T y
Definition: util.h:35
T x1
Definition: util.h:96
T y0
Definition: util.h:96
uint64_t area(void) const
Definition: util.h:205
T width() const
Definition: util.h:209
T height() const
Definition: util.h:213
T y1
Definition: util.h:96
T x0
Definition: util.h:96