Grok 20.3.2
TileComponent.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#pragma once
19
20#include "geometry.h"
22#include "htconfig.h"
23#include "SparseCanvas.h"
24#include "TileComponentWindow.h"
25
26const bool DEBUG_TILE_COMPONENT = false;
27
28namespace grk
29{
30
36struct TileComponent : public Rect32
37{
44 regionWindow_(nullptr), wholeTileDecompress_(true), isCompressor_(false), window_(nullptr),
45 tccp_(nullptr)
46 {}
47
51
53 {
54 if(resolutions_)
55 {
56 for(auto resno = 0U; resno < num_resolutions_; ++resno)
57 {
58 const auto res = resolutions_ + resno;
59 for(auto bandIndex = 0U; bandIndex < 3; ++bandIndex)
60 {
61 auto band = res->band + bandIndex;
62 for(auto prc : band->precincts_)
63 delete prc;
64 band->precincts_.clear();
65 }
66 }
67 delete[] resolutions_;
68 }
69 dealloc();
70 }
71
79 bool allocRegionWindow(uint32_t numres, bool truncatedTile)
80 {
81 Rect32 temp(0, 0, 0, 0);
82 bool first = true;
83
84 // 1. find outside bounds of all relevant code blocks, in relative coordinates
85 for(uint8_t resno = 0U; resno < numres; ++resno)
86 {
87 const auto res = &resolutions_[resno];
88 for(auto bandIndex = 0U; bandIndex < res->numBands_; ++bandIndex)
89 {
90 auto band = res->band + bandIndex;
91 auto roi = window_->getBandWindowPadded(resno, band->orientation_);
92 for(auto precinct : band->precincts_)
93 {
94 if(precinct->empty())
95 continue;
96 auto cblk_grid = precinct->getCblkGrid();
97 auto cblk_expn = precinct->getCblkExpn();
98 auto roi_grid = roi->scaleDownPow2(cblk_expn).clip(cblk_grid);
99 auto w = cblk_grid.width();
100 for(auto j = roi_grid.y0; j < roi_grid.y1; ++j)
101 {
102 uint32_t cblkno =
103 (uint32_t)((roi_grid.x0 - cblk_grid.x0()) + (uint64_t)(j - cblk_grid.y0()) * w);
104 for(auto i = roi_grid.x0; i < roi_grid.x1; ++i)
105 {
106 auto cblkBounds = precinct->getCodeBlockBounds(cblkno);
107
108 // transform from canvas coordinates
109 // to buffer coordinates (relative to associated resolution origin)
110 uint32_t x = cblkBounds.x0() - band->x0;
111 uint32_t y = cblkBounds.y0() - band->y0;
112 if(band->orientation_ & 1)
113 {
114 auto prev_res = resolutions_ + resno - 1;
115 x += prev_res->width();
116 }
117 if(band->orientation_ & 2)
118 {
119 auto prev_res = resolutions_ + resno - 1;
120 y += prev_res->height();
121 }
122 // add to union of code block bounds
123 if(first)
124 {
125 temp = Rect32(x, y, x + cblkBounds.width(), y + cblkBounds.height());
126 first = false;
127 }
128 else
129 {
130 temp =
131 temp.rectUnion(Rect32(x, y, x + cblkBounds.width(), y + cblkBounds.height()));
132 }
133 cblkno++;
134 }
135 }
136 }
137 }
138 }
139
140 // 2. create (padded) sparse canvas, in buffer space,
141 const uint32_t blockSizeExp = 6;
142 temp.grow_IN_PLACE(8);
143 auto regionWindow = new SparseCanvas<int32_t, blockSizeExp, blockSizeExp>(temp);
144
145 // 3. allocate sparse blocks
146 for(uint8_t resno = 0; resno < numres; ++resno)
147 {
148 const auto res = resolutions_ + resno;
149 for(auto bandIndex = 0U; bandIndex < res->numBands_; ++bandIndex)
150 {
151 auto band = res->band + bandIndex;
152 auto roi = window_->getBandWindowPadded(resno, band->orientation_);
153 for(auto precinct : band->precincts_)
154 {
155 if(precinct->empty())
156 continue;
157 auto cblk_grid = precinct->getCblkGrid();
158 auto cblk_expn = precinct->getCblkExpn();
159 auto roi_grid = roi->scaleDownPow2(cblk_expn).clip(cblk_grid);
160 auto w = cblk_grid.width();
161 for(auto gridY = roi_grid.y0; gridY < roi_grid.y1; ++gridY)
162 {
163 uint32_t cblkno =
164 (uint32_t)((roi_grid.x0 - cblk_grid.x0()) + (uint64_t)(gridY - cblk_grid.y0()) * w);
165 for(auto gridX = roi_grid.x0; gridX < roi_grid.x1; ++gridX)
166 {
167 auto cblkBounds = precinct->getCodeBlockBounds(cblkno);
168
169 // transform from canvas coordinates
170 // to buffer coordinates (relative to associated resolution origin)
171 uint32_t x = cblkBounds.x0() - band->x0;
172 uint32_t y = cblkBounds.y0() - band->y0;
173 if(band->orientation_ & 1)
174 {
175 auto prev_res = resolutions_ + resno - 1;
176 x += prev_res->width();
177 }
178 if(band->orientation_ & 2)
179 {
180 auto prev_res = resolutions_ + resno - 1;
181 y += prev_res->height();
182 }
183
184 if(!regionWindow->alloc(Rect32(x, y, x + cblkBounds.width(), y + cblkBounds.height()),
185 truncatedTile))
186 {
187 delete regionWindow;
188 throw std::runtime_error("unable to allocate sparse array");
189 }
190 cblkno++;
191 }
192 }
193 }
194 }
195 }
196
197 if(regionWindow_)
198 delete regionWindow_;
199 regionWindow_ = regionWindow;
200
201 return true;
202 }
203
211 {
212 auto maxResolution = resolutions_ + num_resolutions_ - 1;
213 if(!maxResolution->intersection(windowBounds).valid())
214 {
215 grklog.error("Decompress region (%u,%u,%u,%u) must overlap image bounds (%u,%u,%u,%u)",
217 maxResolution->x0, maxResolution->y0, maxResolution->x1, maxResolution->y1);
218 return false;
219 }
220
221 return true;
222 }
223
232 void createWindow(Rect32 unreducedTileCompOrImageCompWindow)
233 {
234 dealloc();
235 if(use16BitDwt_)
236 {
240 Rect32(unreducedTileCompOrImageCompWindow), num_resolutions_,
242 }
243 else
244 {
248 Rect32(unreducedTileCompOrImageCompWindow), num_resolutions_,
250 }
251 }
252
257 void dealloc(void)
258 {
259 delete regionWindow_;
260 regionWindow_ = nullptr;
261 delete window_;
262 window_ = nullptr;
263 }
264
278 void init(Resolution* resolutions, bool isCompressor, bool wholeTileDecompress, uint8_t reduce,
280 {
281 delete[] resolutions_;
282 resolutions_ = resolutions;
286 isCompressor_ = isCompressor;
287 if(!isCompressor_)
288 wholeTileDecompress_ = wholeTileDecompress;
289 tccp_ = tccp;
290 update(reduce);
291 }
292
298
299 void update(uint8_t reduce)
300 {
302 tccp_->numresolutions_ < reduce ? 1 : (uint8_t)(tccp_->numresolutions_ - reduce);
303 auto highestNumberOfResolutions =
305 auto hightestResolution = resolutions_ + highestNumberOfResolutions - 1;
306 setRect(hightestResolution);
307 }
308
312 template<typename T>
314 {
315 return static_cast<TileComponentWindow<T>*>(window_);
316 }
325 bool is16BitDwt() const
326 {
327 return use16BitDwt_;
328 }
329 void setUse16BitDwt(bool use16Bit)
330 {
331 use16BitDwt_ = use16Bit;
332 }
333 // Type-independent operations through ITileComponentWindow interface
335 {
336 return window_->alloc();
337 }
339 {
340 return window_->bounds();
341 }
343 {
344 return window_->unreducedBounds();
345 }
346 const Rect32* getBandWindowPadded(uint8_t resno, t1::eBandOrientation orientation) const
347 {
348 return window_->getBandWindowPadded(resno, orientation);
349 }
350 void transferWindowData(void** data, uint32_t* stride)
351 {
352 window_->transferData(data, stride);
353 }
354 void attachWindowData(void* data, uint32_t stride)
355 {
356 window_->attachData(data, stride);
357 }
358 uint32_t highestResStride() const
359 {
360 return window_->highestResStride();
361 }
362 uint64_t windowStridedArea() const
363 {
364 return window_->stridedArea();
365 }
366
372 {
374 }
375
384
390 void postProcessBlock(int32_t* srcData, t1::DecompressBlockExec* block)
391 {
392 window_->postProcessBlock(srcData, block, regionWindow_);
393 }
394
402 void postProcessBlockHT(int32_t* srcData, t1::DecompressBlockExec* block, uint16_t stride)
403 {
404 window_->postProcessBlockHT(srcData, block, stride, regionWindow_);
405 }
406
411 Resolution* resolutions_; // in canvas coordinates
432
433private:
455 bool use16BitDwt_ = false;
461};
462
463} // namespace grk
const bool DEBUG_TILE_COMPONENT
Definition TileComponent.h:26
Definition ISparseCanvas.h:42
Definition SparseCanvas.h:46
eBandOrientation
Definition t1_common.h:28
ResWindow.
Definition CompressedChunkCache.h:36
ILogger & grklog
Definition Logger.cpp:24
Rect< uint32_t > Rect32
Definition geometry.h:64
Type-erased interface for tile component windows.
Definition TileComponentWindow.h:58
Stores the maximum number of layers read corresponding to packet data that has been read,...
Definition PacketProgressionState.h:32
Rect< T > rectUnion(const Rect *rhs) const
Definition geometry.h:397
void setRect(const Rect *rhs)
Definition geometry.h:297
Rect< T > & grow_IN_PLACE(T boundary)
Definition geometry.h:444
Stores a tile component resolution's dimensions, sub bands and other information.
Definition Resolution.h:31
Tile-component coding parameters.
Definition CodingParams.h:54
uint8_t numresolutions_
number of resolutions
Definition CodingParams.h:59
ITileComponentWindow * window_
ITileComponentWindow (type-erased, points to TileComponentWindow<int32_t> or <int16_t>)
Definition TileComponent.h:454
TileComponentWindow< int32_t > * getWindow() const
Definition TileComponent.h:317
TileComponentWindow< T > * typedWindow() const
Gets typed window (static_cast from type-erased pointer).
Definition TileComponent.h:313
uint32_t highestResStride() const
Definition TileComponent.h:358
bool allocWindow()
Definition TileComponent.h:334
uint64_t windowStridedArea() const
Definition TileComponent.h:362
void init(Resolution *resolutions, bool isCompressor, bool wholeTileDecompress, uint8_t reduce, TileComponentCodingParams *tccp)
Initalizes tile component.
Definition TileComponent.h:278
uint8_t num_resolutions_
number of resolutions
Definition TileComponent.h:416
bool allocRegionWindow(uint32_t numres, bool truncatedTile)
Allocates a region window.
Definition TileComponent.h:79
bool is16BitDwt() const
Definition TileComponent.h:325
bool isWholeTileDecoding()
Checks if whole tile will be decoded.
Definition TileComponent.h:371
void transferWindowData(void **data, uint32_t *stride)
Definition TileComponent.h:350
Rect32 windowBounds() const
Definition TileComponent.h:338
const Rect32 * getBandWindowPadded(uint8_t resno, t1::eBandOrientation orientation) const
Definition TileComponent.h:346
~TileComponent()
Destroys the Tile Component object.
Definition TileComponent.h:52
void createWindow(Rect32 unreducedTileCompOrImageCompWindow)
Creates tile component window.
Definition TileComponent.h:232
void setUse16BitDwt(bool use16Bit)
Definition TileComponent.h:329
Resolution * resolutions_
array of Resolution
Definition TileComponent.h:411
TileComponent()
Constructs a new Tile Component object.
Definition TileComponent.h:42
bool use16BitDwt_
Definition TileComponent.h:455
void postProcessBlockHT(int32_t *srcData, t1::DecompressBlockExec *block, uint16_t stride)
Post processes HTJ2K code block via virtual dispatch on window.
Definition TileComponent.h:402
TileComponentCodingParams * tccp_
TileComponentCodingParams
Definition TileComponent.h:460
uint8_t resolutions_to_decompress_
number of desired resolutions to decompress
Definition TileComponent.h:421
bool wholeTileDecompress_
true if whole tile will be decompressed
Definition TileComponent.h:443
void dealloc(void)
Deallocates component resources.
Definition TileComponent.h:257
Rect32 windowUnreducedBounds() const
Definition TileComponent.h:342
ISparseCanvas< int32_t > * getRegionWindow()
Gets return window.
Definition TileComponent.h:380
ISparseCanvas< int32_t > * regionWindow_
@ISparseCanvas for region window
Definition TileComponent.h:438
void update(uint8_t reduce)
Differential decompression update.
Definition TileComponent.h:299
void attachWindowData(void *data, uint32_t stride)
Definition TileComponent.h:354
PacketProgressionState currentPacketProgressionState_
current PacketProgressionState
Definition TileComponent.h:426
TileComponentWindow< int16_t > * getWindow16() const
Definition TileComponent.h:321
PacketProgressionState nextPacketProgressionState_
next PacketProgressionState
Definition TileComponent.h:431
void postProcessBlock(int32_t *srcData, t1::DecompressBlockExec *block)
Post processes code block via virtual dispatch on window.
Definition TileComponent.h:390
bool canCreateWindow(Rect32 windowBounds)
Check if a window can be created.
Definition TileComponent.h:210
bool isCompressor_
true if compression is occcurring
Definition TileComponent.h:448
Definition TileComponentWindow.h:197
Definition BlockExec.h:64