Grok 15.1.0
TileComponentWindow.h
Go to the documentation of this file.
1
17#pragma once
18
19#include "grk_includes.h"
20#include <stdexcept>
21#include <algorithm>
22
23/*
24 Various coordinate systems are used to describe regions in the tile component buffer.
25
26 1) Canvas coordinates: JPEG 2000 global image coordinates.
27
28 2) Tile component coordinates: canvas coordinates with sub-sampling applied
29
30 3) Band coordinates: coordinates relative to a specified sub-band's origin
31
32 4) Buffer coordinates: coordinate system where all resolutions are translated
33 to common origin (0,0). If each code block is translated relative to the origin of the
34 resolution that **it belongs to**, the blocks are then all in buffer coordinate system
35
36 Note: the name of any method or variable returning non canvas coordinates is appended
37 with "REL", to signify relative coordinates.
38
39 */
40
41#include "ResWindow.h"
42
43namespace grk
44{
45
46template<class T>
47constexpr T getFilterPad(bool lossless)
48{
49 return lossless ? 1 : 2;
50}
51
52template<typename T>
54{
55 TileComponentWindowBase(bool isCompressor, bool lossless, bool wholeTileDecompress,
56 grk_rect32 unreducedTileComp, grk_rect32 reducedTileComp,
57 grk_rect32 unreducedImageCompWindow, uint8_t numresolutions,
58 uint8_t reducedNumResolutions)
59 : unreducedBounds_(unreducedTileComp), bounds_(reducedTileComp), compress_(isCompressor),
60 wholeTileDecompress_(wholeTileDecompress)
61 {
62 assert(reducedNumResolutions > 0);
63 auto currentRes = unreducedTileComp;
64 for(uint8_t i = 0; i < numresolutions; ++i)
65 {
66 bool finalResolution = i == numresolutions - 1;
67 ResSimple r(currentRes, finalResolution);
68 resolution_.push_back(r);
69 if(!finalResolution)
70 currentRes = ResSimple::getBandWindow(1, 0, currentRes);
71 }
72 std::reverse(resolution_.begin(), resolution_.end());
73
74 // generate bounds
75 unreducedBounds_ = unreducedImageCompWindow.intersection(unreducedBounds_);
76 assert(unreducedBounds_.valid());
77 bounds_ = unreducedImageCompWindow.scaleDownCeilPow2(
78 (uint32_t)(numresolutions - reducedNumResolutions));
79 bounds_ = bounds_.intersection(reducedTileComp);
80 assert(bounds_.valid());
81
82 // fill resolutions vector
83 auto tileCompAtRes = resolution_[reducedNumResolutions - 1];
84 auto tileCompAtLowerRes =
85 reducedNumResolutions > 1 ? resolution_[reducedNumResolutions - 2] : ResSimple();
86 // create resolution buffers
87 auto highestResWindow = new ResWindow<T>(
88 numresolutions, (uint8_t)(reducedNumResolutions - 1U), nullptr, tileCompAtRes,
89 tileCompAtLowerRes, bounds_, unreducedBounds_, unreducedTileComp,
90 wholeTileDecompress ? 0 : getFilterPad<uint32_t>(lossless));
91 // setting top level prevents allocation of tileCompBandWindows buffers
92 if(!useBandWindows())
93 highestResWindow->disableBandWindowAllocation();
94
95 // create windows for all resolutions except highest resolution
96 for(uint8_t resno = 0; resno < reducedNumResolutions - 1; ++resno)
97 {
98 // resolution window == LL band window of next highest resolution
99 auto resWindow =
100 ResSimple::getBandWindow((uint8_t)(numresolutions - 1 - resno), 0, unreducedBounds_);
101 resWindows.push_back(
102 new ResWindow<T>(numresolutions, resno,
103 useBandWindows() ? nullptr : highestResWindow->getResWindowBufferREL(),
104 resolution_[resno], resno > 0 ? resolution_[resno - 1] : ResSimple(),
105 resWindow, unreducedBounds_, unreducedTileComp,
106 wholeTileDecompress ? 0 : getFilterPad<uint32_t>(lossless)));
107 }
108 resWindows.push_back(highestResWindow);
109 }
111 {
112 for(auto& b : this->resWindows)
113 delete b;
114 }
115
121 {
122 return bounds_;
123 }
125 {
126 return unreducedBounds_;
127 }
128 bool alloc()
129 {
130 for(auto& b : resWindows)
131 {
132 if(!b->alloc(!compress_))
133 return false;
134 }
135
136 return true;
137 }
138
139protected:
140 bool useBandWindows() const
141 {
142 return !this->wholeTileDecompress_;
143 }
144 // windowed bounds for windowed decompress, otherwise full bounds
145 std::vector<ResWindow<T>*> resWindows;
146 /******************************************************/
147 // decompress: unreduced/reduced image component window
148 // compress: unreduced/reduced tile component
151 /******************************************************/
152
153 std::vector<ResSimple> resolution_;
156};
157
158template<typename T>
160{
162 TileComponentWindow(bool isCompressor, bool lossless, bool wholeTileDecompress,
163 grk_rect32 unreducedTileComp, grk_rect32 reducedTileComp,
164 grk_rect32 unreducedImageCompWindow, uint8_t numresolutions,
165 uint8_t reducedNumResolutions)
166 : TileComponentWindowBase<T>(isCompressor, lossless, wholeTileDecompress, unreducedTileComp,
167 reducedTileComp, unreducedImageCompWindow, numresolutions,
168 reducedNumResolutions)
169
170 {}
172
187 void toRelativeCoordinates(uint8_t resno, eBandOrientation orientation, uint32_t& offsetx,
188 uint32_t& offsety) const
189 {
190 assert(resno < this->resolution_.size());
191
192 auto res = this->resolution_[resno];
193 auto band = res.tileBand + getBandIndex(resno, orientation);
194
195 // get offset relative to band
196 offsetx -= band->x0;
197 offsety -= band->y0;
198
199 if(useBufferCoordinatesForCodeblock() && resno > 0)
200 {
201 auto resLower = this->resolution_[resno - 1U];
202
203 if(orientation & 1)
204 offsetx += resLower.width();
205 if(orientation & 2)
206 offsety += resLower.height();
207 }
208 }
209 template<typename F>
210 void postProcess(Buf2dAligned& src, uint8_t resno, eBandOrientation bandOrientation,
211 DecompressBlockExec* block)
212 {
214 dst = getCodeBlockDestWindowREL(resno, bandOrientation);
215 dst.copyFrom<F>(src, F(block));
216 }
217
228 eBandOrientation orientation) const
229 {
230 assert(resno < this->resolution_.size());
231 assert(resno > 0 || orientation == BAND_ORIENT_LL);
232
233 if(resno == 0 && (this->compress_ || this->wholeTileDecompress_))
234 return this->resWindows[0]->getResWindowBufferREL();
235
236 return this->resWindows[resno]->getBandWindowBufferPaddedREL(orientation);
237 }
238
248 getBandWindowBufferPaddedSimple(uint8_t resno, eBandOrientation orientation) const
249 {
250 assert(resno < this->resolution_.size());
251 assert(resno > 0 || orientation == BAND_ORIENT_LL);
252
253 if(resno == 0 && (this->compress_ || this->wholeTileDecompress_))
254 return this->resWindows[0]->getResWindowBufferSimple();
255
256 return this->resWindows[resno]->getBandWindowBufferPaddedSimple(orientation);
257 }
258
268 eBandOrientation orientation) const
269 {
270 assert(resno < this->resolution_.size());
271 assert(resno > 0 || orientation == BAND_ORIENT_LL);
272
273 if(resno == 0 && (this->compress_ || this->wholeTileDecompress_))
274 return this->resWindows[0]->getResWindowBufferSimpleF();
275
276 return this->resWindows[resno]->getBandWindowBufferPaddedSimpleF(orientation);
277 }
278
286 const grk_rect32* getBandWindowPadded(uint8_t resno, eBandOrientation orientation) const
287 {
288 return this->resWindows[resno]->getBandWindowPadded(orientation);
289 }
290 /*
291 * Get intermediate split window
292 *
293 * @param orientation 0 for upper split window, and 1 for lower split window
294 */
295 const Buf2dAligned* getResWindowBufferSplitREL(uint8_t resno, eSplitOrientation orientation) const
296 {
297 assert(resno > 0 && resno < this->resolution_.size());
298
299 return this->resWindows[resno]->getResWindowBufferSplitREL(orientation);
300 }
301 /*
302 * Get intermediate split window simple buffer
303 *
304 * @param orientation 0 for upper split window, and 1 for lower split window
305 */
307 eSplitOrientation orientation) const
308 {
309 return getResWindowBufferSplitREL(resno, orientation)->simple();
310 }
311
312 /*
313 * Get intermediate split window simpleF buffer
314 *
315 * @param orientation 0 for upper split window, and 1 for lower split window
316 */
318 eSplitOrientation orientation) const
319 {
320 return getResWindowBufferSplitREL(resno, orientation)->simpleF();
321 }
322
329 const Buf2dAligned* getResWindowBufferREL(uint32_t resno) const
330 {
331 return this->resWindows[resno]->getResWindowBufferREL();
332 }
333
340 {
341 return getResWindowBufferREL(resno)->simple();
342 }
343
350 {
351 return getResWindowBufferREL(resno)->simpleF();
352 }
353
360 {
362 }
363
373
382 uint64_t stridedArea(void) const
383 {
384 auto win = getResWindowBufferHighestREL();
385 return (uint64_t)win->stride * win->height();
386 }
387
388 // set data to buf without owning it
389 void attach(T* buffer, uint32_t stride)
390 {
391 getResWindowBufferHighestREL()->attach(buffer, stride);
392 }
393 // transfer data to buf, and cease owning it
394 void transfer(T** buffer, uint32_t* stride)
395 {
396 getResWindowBufferHighestREL()->transfer(buffer, stride);
397 }
398
399private:
407 const Buf2dAligned* getCodeBlockDestWindowREL(uint8_t resno, eBandOrientation orientation) const
408 {
410 : getBandWindowBufferPaddedREL(resno, orientation);
411 }
412
418 {
419 return this->resWindows.back()->getResWindowBufferREL();
420 }
422 {
423 return this->compress_ || !this->wholeTileDecompress_;
424 }
425 uint8_t getBandIndex(uint8_t resno, eBandOrientation orientation) const
426 {
427 uint8_t index = 0;
428 if(resno > 0)
429 index = (uint8_t)orientation - 1;
430
431 return index;
432 }
433};
434
435} // namespace grk
Copyright (C) 2016-2025 Grok Image Compression Inc.
Definition ICacheable.h:20
grk_rect< uint32_t > grk_rect32
Definition geometry.h:61
eSplitOrientation
Definition ResWindow.h:45
constexpr T getFilterPad(bool lossless)
Definition TileComponentWindow.h:47
eBandOrientation
Definition ResSimple.h:23
@ BAND_ORIENT_LL
Definition ResSimple.h:24
Definition BlockExec.h:45
Definition ResSimple.h:43
static grk_rect32 getBandWindow(uint8_t numDecomps, uint8_t orientation, grk_rect32 tileCompWindowUnreduced)
Get band window (in tile component coordinates) for specified number of decompositions.
Definition ResSimple.h:77
ResWindow.
Definition ResWindow.h:70
grk_rect32 bounds() const
Get bounds of tile component (canvas coordinates) decompress: reduced canvas coordinates of window co...
Definition TileComponentWindow.h:120
bool wholeTileDecompress_
Definition TileComponentWindow.h:155
grk_rect32 unreducedBounds_
Definition TileComponentWindow.h:149
grk_rect32 unreducedBounds() const
Definition TileComponentWindow.h:124
bool compress_
Definition TileComponentWindow.h:154
TileComponentWindowBase(bool isCompressor, bool lossless, bool wholeTileDecompress, grk_rect32 unreducedTileComp, grk_rect32 reducedTileComp, grk_rect32 unreducedImageCompWindow, uint8_t numresolutions, uint8_t reducedNumResolutions)
Definition TileComponentWindow.h:55
grk_rect32 bounds_
Definition TileComponentWindow.h:150
virtual ~TileComponentWindowBase()
Definition TileComponentWindow.h:110
bool alloc()
Definition TileComponentWindow.h:128
std::vector< ResWindow< T > * > resWindows
Definition TileComponentWindow.h:145
std::vector< ResSimple > resolution_
Definition TileComponentWindow.h:153
bool useBandWindows() const
Definition TileComponentWindow.h:140
const Buf2dAligned * getBandWindowBufferPaddedREL(uint8_t resno, eBandOrientation orientation) const
Get padded band window buffer.
Definition TileComponentWindow.h:227
const grk_buf2d_simple< float > getResWindowBufferSplitSimpleF(uint8_t resno, eSplitOrientation orientation) const
Definition TileComponentWindow.h:317
const Buf2dAligned * getResWindowBufferREL(uint32_t resno) const
Get resolution window.
Definition TileComponentWindow.h:329
Buf2dAligned * getResWindowBufferHighestREL(void) const
Get highest resolution window.
Definition TileComponentWindow.h:417
grk_buf2d_simple< float > getResWindowBufferHighestSimpleF(void) const
Get highest resolution window.
Definition TileComponentWindow.h:378
grk_buf2d_simple< int32_t > getResWindowBufferHighestSimple(void) const
Get highest resolution window.
Definition TileComponentWindow.h:369
const grk_buf2d_simple< int32_t > getResWindowBufferSimple(uint32_t resno) const
Get resolution window.
Definition TileComponentWindow.h:339
const Buf2dAligned * getCodeBlockDestWindowREL(uint8_t resno, eBandOrientation orientation) const
Get code block destination window.
Definition TileComponentWindow.h:407
const grk_buf2d_simple< float > getResWindowBufferSimpleF(uint32_t resno) const
Get resolution window.
Definition TileComponentWindow.h:349
const Buf2dAligned * getResWindowBufferSplitREL(uint8_t resno, eSplitOrientation orientation) const
Definition TileComponentWindow.h:295
const grk_rect32 * getBandWindowPadded(uint8_t resno, eBandOrientation orientation) const
Get padded band window.
Definition TileComponentWindow.h:286
uint8_t getBandIndex(uint8_t resno, eBandOrientation orientation) const
Definition TileComponentWindow.h:425
void transfer(T **buffer, uint32_t *stride)
Definition TileComponentWindow.h:394
void toRelativeCoordinates(uint8_t resno, eBandOrientation orientation, uint32_t &offsetx, uint32_t &offsety) const
Transform code block offsets from canvas coordinates to either band coordinates (relative to sub band...
Definition TileComponentWindow.h:187
const grk_buf2d_simple< int32_t > getBandWindowBufferPaddedSimple(uint8_t resno, eBandOrientation orientation) const
Get padded band window buffer.
Definition TileComponentWindow.h:248
void postProcess(Buf2dAligned &src, uint8_t resno, eBandOrientation bandOrientation, DecompressBlockExec *block)
Definition TileComponentWindow.h:210
uint32_t getResWindowBufferHighestStride(void) const
Get highest resolution window.
Definition TileComponentWindow.h:359
const grk_buf2d_simple< int32_t > getResWindowBufferSplitSimple(uint8_t resno, eSplitOrientation orientation) const
Definition TileComponentWindow.h:306
TileComponentWindow(bool isCompressor, bool lossless, bool wholeTileDecompress, grk_rect32 unreducedTileComp, grk_rect32 reducedTileComp, grk_rect32 unreducedImageCompWindow, uint8_t numresolutions, uint8_t reducedNumResolutions)
Definition TileComponentWindow.h:162
const grk_buf2d_simple< float > getBandWindowBufferPaddedSimpleF(uint8_t resno, eBandOrientation orientation) const
Get padded band window buffer.
Definition TileComponentWindow.h:267
bool useBufferCoordinatesForCodeblock() const
Definition TileComponentWindow.h:421
uint64_t stridedArea(void) const
Definition TileComponentWindow.h:382
void attach(T *buffer, uint32_t stride)
Definition TileComponentWindow.h:389
grk_buf2d< T, AllocatorAligned > Buf2dAligned
Definition TileComponentWindow.h:161
Definition buffer.h:206
Definition buffer.h:230
void transfer(T **buffer, uint32_t *strd)
Definition buffer.h:326
void copyFrom(const grk_buf2d &src, F filter)
Definition buffer.h:347
void attach(T *buffer, uint32_t strd)
Definition buffer.h:292
grk_buf2d_simple< float > simpleF(void) const
Definition buffer.h:251
grk_buf2d_simple< T > simple(void) const
Definition buffer.h:247
uint32_t stride
Definition buffer.h:391
grk_rect< T > intersection(const grk_rect< T > rhs) const
Definition geometry.h:281
grk_rect< T > scaleDownCeilPow2(uint32_t power) const
Definition geometry.h:268