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