Grok 12.0.1
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,
60 wholeTileDecompress_(wholeTileDecompress)
61 {
64 for(uint8_t i = 0; i < numresolutions; ++i)
65 {
66 bool finalResolution = i == numresolutions - 1;
68 resolution_.push_back(r);
71 }
72 std::reverse(resolution_.begin(), resolution_.end());
73
74 // generate bounds
78 (uint32_t)(numresolutions - reducedNumResolutions));
81
82 // fill resolutions vector
86 // create resolution buffers
88 numresolutions, (uint8_t)(reducedNumResolutions - 1U), nullptr, tileCompAtRes,
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(new ResWindow<T>(
102 numresolutions, resno,
103 useBandWindows() ? nullptr : highestResWindow->getResWindowBufferREL(),
104 resolution_[resno], resno > 0 ? resolution_[resno - 1] : ResSimple(), resWindow,
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 }
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
139 protected:
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,
166 : TileComponentWindowBase<T>(isCompressor, lossless, wholeTileDecompress, unreducedTileComp,
169
170 {}
172
188 uint32_t& offsety) const
189 {
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,
212 {
214 dst = getCodeBlockDestWindowREL(resno, bandOrientation);
215 dst.copyFrom<F>(src, F(block));
216 }
217
228 eBandOrientation orientation) const
229 {
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 }
249 {
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 }
269 {
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
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 */
296 eSplitOrientation orientation) const
297 {
298 assert(resno > 0 && resno < this->resolution_.size());
299
300 return this->resWindows[resno]->getResWindowBufferSplitREL(orientation);
301 }
302 /*
303 * Get intermediate split window simple buffer
304 *
305 * @param orientation 0 for upper split window, and 1 for lower split window
306 */
309 {
310 return getResWindowBufferSplitREL(resno, orientation)->simple();
311 }
312
313 /*
314 * Get intermediate split window simpleF buffer
315 *
316 * @param orientation 0 for upper split window, and 1 for lower split window
317 */
319 eSplitOrientation orientation) const
320 {
321 return getResWindowBufferSplitREL(resno, orientation)->simpleF();
322 }
323
331 {
332 return this->resWindows[resno]->getResWindowBufferREL();
333 }
341 {
342 return getResWindowBufferREL(resno)->simple();
343 }
351 {
352 return getResWindowBufferREL(resno)->simpleF();
353 }
354
364
384 {
386 return (uint64_t)win->stride * win->height();
387 }
388
389 // set data to buf without owning it
390 void attach(T* buffer, uint32_t stride)
391 {
392 getResWindowBufferHighestREL()->attach(buffer, stride);
393 }
394 // transfer data to buf, and cease owning it
395 void transfer(T** buffer, uint32_t* stride)
396 {
397 getResWindowBufferHighestREL()->transfer(buffer, stride);
398 }
399
400 private:
409 {
412 : getBandWindowBufferPaddedREL(resno, orientation);
413 }
420 {
421 return this->resWindows.back()->getResWindowBufferREL();
422 }
424 {
425 return this->compress_ || !this->wholeTileDecompress_;
426 }
428 {
429 uint8_t index = 0;
430 if(resno > 0)
431 index = (uint8_t)orientation - 1;
432
433 return index;
434 }
435};
436
437} // namespace grk
Copyright (C) 2016-2024 Grok Image Compression Inc.
Definition ICacheable.h:20
void grk_read(const uint8_t *buffer, TYPE *value, uint32_t numBytes)
Definition BufferedStream.h:239
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
Definition TileComponentWindow.h:54
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
Definition TileComponentWindow.h:160
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:318
const Buf2dAligned * getResWindowBufferREL(uint32_t resno) const
Get resolution window.
Definition TileComponentWindow.h:330
Buf2dAligned * getResWindowBufferHighestREL(void) const
Get highest resolution window.
Definition TileComponentWindow.h:419
grk_buf2d_simple< float > getResWindowBufferHighestSimpleF(void) const
Get highest resolution window.
Definition TileComponentWindow.h:379
grk_buf2d_simple< int32_t > getResWindowBufferHighestSimple(void) const
Get highest resolution window.
Definition TileComponentWindow.h:370
const grk_buf2d_simple< int32_t > getResWindowBufferSimple(uint32_t resno) const
Get resolution window.
Definition TileComponentWindow.h:340
const Buf2dAligned * getCodeBlockDestWindowREL(uint8_t resno, eBandOrientation orientation) const
Get code block destination window.
Definition TileComponentWindow.h:408
const grk_buf2d_simple< float > getResWindowBufferSimpleF(uint32_t resno) const
Get resolution window.
Definition TileComponentWindow.h:350
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:427
void transfer(T **buffer, uint32_t *stride)
Definition TileComponentWindow.h:395
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:360
const grk_buf2d_simple< int32_t > getResWindowBufferSplitSimple(uint8_t resno, eSplitOrientation orientation) const
Definition TileComponentWindow.h:308
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:268
bool useBufferCoordinatesForCodeblock() const
Definition TileComponentWindow.h:423
uint64_t stridedArea(void) const
Definition TileComponentWindow.h:383
void attach(T *buffer, uint32_t stride)
Definition TileComponentWindow.h:390
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:327
void attach(T *buffer, uint32_t strd)
Definition buffer.h:293
grk_buf2d_simple< float > simpleF(void) const
Definition buffer.h:252
grk_buf2d_simple< T > simple(void) const
Definition buffer.h:248
uint32_t stride
Definition buffer.h:392
grk_rect< T > intersection(const grk_rect< T > rhs) const
Definition geometry.h:281
bool valid(void) const
Definition geometry.h:185
grk_rect< T > scaleDownCeilPow2(uint32_t power) const
Definition geometry.h:268