@@ -152,13 +152,13 @@ bool T2Decompress::decompressPackets(uint16_t tile_no,
152
152
bool T2Decompress::decompressPacket (TileCodingParams *p_tcp,
153
153
const PacketIter *p_pi,
154
154
SparseBuffer *src_buf,
155
- uint64_t *p_data_read ) {
155
+ uint64_t *dataRead ) {
156
156
auto p_tile = tileProcessor->tile ;
157
157
auto res = &p_tile->comps [p_pi->compno ].tileCompResolution [p_pi->resno ];
158
158
bool read_data;
159
159
uint64_t bytesRead = 0 ;
160
160
uint64_t nb_total_bytes_read = 0 ;
161
- *p_data_read = 0 ;
161
+ *dataRead = 0 ;
162
162
if (!readPacketHeader (p_tcp, p_pi, &read_data, src_buf, &bytesRead))
163
163
return false ;
164
164
nb_total_bytes_read += bytesRead;
@@ -171,7 +171,7 @@ bool T2Decompress::decompressPacket(TileCodingParams *p_tcp,
171
171
}
172
172
nb_total_bytes_read += bytesRead;
173
173
}
174
- *p_data_read = nb_total_bytes_read;
174
+ *dataRead = nb_total_bytes_read;
175
175
return true ;
176
176
}
177
177
void T2Decompress::initSegment (DecompressCodeblock *cblk, uint32_t index, uint8_t cblk_sty,
@@ -197,7 +197,7 @@ bool T2Decompress::readPacketHeader(TileCodingParams *p_tcp,
197
197
const PacketIter *p_pi,
198
198
bool *p_is_data_present,
199
199
SparseBuffer *src_buf,
200
- uint64_t *p_data_read ) {
200
+ uint64_t *dataRead ) {
201
201
auto p_tile = tileProcessor->tile ;
202
202
auto res = &p_tile->comps [p_pi->compno ].tileCompResolution [p_pi->resno ];
203
203
auto p_src_data = src_buf->getCurrentChunkPtr ();
@@ -221,8 +221,8 @@ bool T2Decompress::readPacketHeader(TileCodingParams *p_tcp,
221
221
available_bytes -= 6 ;
222
222
}
223
223
}
224
- uint8_t ** header_data_start = &active_src;
225
- size_t * remaining_length = &available_bytes;
224
+ auto header_data_start = &active_src;
225
+ auto remaining_length = &available_bytes;
226
226
auto cp = tileProcessor->m_cp ;
227
227
if (cp->ppm_marker ) {
228
228
if (tileProcessor->m_tileIndex >= cp->ppm_marker ->m_tile_packet_headers .size ()){
@@ -239,7 +239,7 @@ bool T2Decompress::readPacketHeader(TileCodingParams *p_tcp,
239
239
}
240
240
if (*remaining_length==0 )
241
241
throw TruncatedPacketHeaderException ();
242
- uint8_t * header_data = *header_data_start;
242
+ auto header_data = *header_data_start;
243
243
uint32_t present = 0 ;
244
244
std::unique_ptr<BitIO> bio (new BitIO (header_data, *remaining_length, false ));
245
245
try {
@@ -331,7 +331,7 @@ bool T2Decompress::readPacketHeader(TileCodingParams *p_tcp,
331
331
cblk->numlenbits += increment;
332
332
uint32_t segno = 0 ;
333
333
if (!cblk->getNumSegments ()) {
334
- initSegment (cblk, segno ,p_tcp->tccps [p_pi->compno ].cblk_sty , true );
334
+ initSegment (cblk, 0 ,p_tcp->tccps [p_pi->compno ].cblk_sty , true );
335
335
} else {
336
336
segno = cblk->getNumSegments () - 1 ;
337
337
if (cblk->getSegment (segno)->numpasses == cblk->getSegment (segno)->maxpasses ) {
@@ -409,83 +409,69 @@ bool T2Decompress::readPacketHeader(TileCodingParams *p_tcp,
409
409
auto header_length = (size_t ) (header_data - *header_data_start);
410
410
// GRK_INFO("hdrlen=%u ", header_length);
411
411
// GRK_INFO("packet body\n");
412
- *remaining_length -= header_length;
413
- *header_data_start += header_length;
414
- *p_is_data_present = present;
415
- *p_data_read = (uint32_t ) (active_src - p_src_data);
416
- src_buf->incrementCurrentChunkOffset (*p_data_read );
412
+ *remaining_length -= header_length;
413
+ *header_data_start += header_length;
414
+ *p_is_data_present = present;
415
+ *dataRead = (uint32_t ) (active_src - p_src_data);
416
+ src_buf->incrementCurrentChunkOffset (*dataRead );
417
417
418
- if (!present && !*p_data_read )
418
+ if (!present && !*dataRead )
419
419
throw TruncatedPacketHeaderException ();
420
420
421
421
return true ;
422
422
}
423
423
bool T2Decompress::readPacketData (Resolution *res,
424
424
const PacketIter *p_pi,
425
425
SparseBuffer *src_buf,
426
- uint64_t *p_data_read ) {
426
+ uint64_t *dataRead ) {
427
427
for (uint32_t bandIndex = 0 ; bandIndex < res->numTileBandWindows ; ++bandIndex) {
428
428
auto band = res->tileBand + bandIndex;
429
+ if (band->isEmpty ())
430
+ continue ;
429
431
auto prc = band->getPrecinct (p_pi->precinctIndex );
430
432
if (!prc)
431
433
continue ;
432
434
for (uint64_t cblkno = 0 ; cblkno < prc->getNumCblks (); ++cblkno) {
433
435
auto cblk = prc->getDecompressedBlockPtr (cblkno);
434
- if (!cblk->numPassesInPacket ) {
435
- ++cblk;
436
+ if (!cblk->numPassesInPacket )
436
437
continue ;
437
- }
438
- Segment * seg = nullptr ;
439
- if (!cblk-> getNumSegments ()) {
438
+
439
+ auto seg = cblk-> getCurrentSegment () ;
440
+ if (!seg || (seg-> numpasses == seg-> maxpasses ))
440
441
seg = cblk->nextSegment ();
441
- cblk->compressedStream .len = 0 ;
442
- } else {
443
- seg = cblk->getCurrentSegment ();
444
- if (seg->numpasses == seg->maxpasses ) {
445
- seg = cblk->nextSegment ();
446
- }
447
- }
448
442
uint32_t numPassesInPacket = cblk->numPassesInPacket ;
449
443
do {
450
444
size_t maxLen = src_buf->getCurrentChunkLength ();
451
445
if (maxLen == 0 )
452
446
return true ;
453
- // reject truncated packet
454
447
if (((seg->numBytesInPacket ) > maxLen)) {
455
448
// HT doesn't tolerate truncated code blocks since decoding runs both forward and reverse.
456
449
// So, in this case, we ignore the entire code block
457
- if (tileProcessor->m_cp ->tcps [0 ].getIsHT ()){
450
+ if (tileProcessor->m_cp ->tcps [0 ].getIsHT ())
458
451
cblk->cleanUpSegBuffers ();
459
- }
460
452
seg->numBytesInPacket = 0 ;
461
453
seg->numpasses = 0 ;
462
454
break ;
463
455
}
464
- // initialize dataindex to current contiguous size of code block
465
- if (seg->numpasses == 0 )
466
- seg->dataindex = (uint32_t ) cblk->compressedStream .len ;
467
-
468
- // only add segment to seg_buffers if length is greater than zero
469
456
if (seg->numBytesInPacket ) {
470
457
// sanity check on seg->numBytesInPacket
471
458
if (UINT_MAX - seg->numBytesInPacket < seg->len ){
472
459
GRK_ERROR (" Segment packet length %d plus total segment length %d must be less than 2^32" ,
473
460
seg->numBytesInPacket , seg->len );
474
461
return false ;
475
462
}
476
- size_t max_seg_len = src_buf->getCurrentChunkLength ();
463
+ size_t maxSegmentLength = src_buf->getCurrentChunkLength ();
477
464
// correct for truncated packet
478
- if (seg->numBytesInPacket > max_seg_len)
479
- seg->numBytesInPacket = (uint32_t )max_seg_len;
480
- cblk->seg_buffers .push_back (new grkBufferU8 (src_buf->getCurrentChunkPtr (),
481
- seg->numBytesInPacket , false ));
482
- *(p_data_read) += seg->numBytesInPacket ;
465
+ if (seg->numBytesInPacket > maxSegmentLength)
466
+ seg->numBytesInPacket = (uint32_t )maxSegmentLength;
467
+ cblk->seg_buffers .push_back (new grkBufferU8 (src_buf->getCurrentChunkPtr (),seg->numBytesInPacket , false ));
483
468
src_buf->incrementCurrentChunkOffset (seg->numBytesInPacket );
484
- cblk->compressedStream .len += seg->numBytesInPacket ;
485
- seg->len += seg->numBytesInPacket ;
469
+ *dataRead += seg->numBytesInPacket ;
470
+ cblk->compressedStream .len += seg->numBytesInPacket ;
471
+ seg->len += seg->numBytesInPacket ;
486
472
}
487
- seg->numpasses += seg->numPassesInPacket ;
488
- numPassesInPacket -= seg->numPassesInPacket ;
473
+ seg->numpasses += seg->numPassesInPacket ;
474
+ numPassesInPacket -= seg->numPassesInPacket ;
489
475
if (numPassesInPacket > 0 )
490
476
seg = cblk->nextSegment ();
491
477
} while (numPassesInPacket > 0 );
@@ -497,39 +483,38 @@ bool T2Decompress::readPacketData(Resolution *res,
497
483
bool T2Decompress::skipPacket (TileCodingParams *p_tcp,
498
484
PacketIter *p_pi,
499
485
SparseBuffer *src_buf,
500
- uint64_t *p_data_read ) {
501
- uint64_t max_length = (uint64_t ) src_buf->getCurrentChunkLength ();
486
+ uint64_t *dataRead ) {
487
+ uint64_t maxDataLength = (uint64_t ) src_buf->getCurrentChunkLength ();
502
488
auto p_tile = tileProcessor->tile ;
503
489
504
- *p_data_read = 0 ;
490
+ *dataRead = 0 ;
505
491
uint64_t bytesRead = 0 ;
506
492
bool read_data;
507
493
if (!readPacketHeader (p_tcp, p_pi, &read_data, src_buf, &bytesRead))
508
494
return false ;
509
- uint64_t nb_totabytes_read = bytesRead;
510
- max_length -= bytesRead;
495
+ uint64_t totalBytesRead = bytesRead;
496
+ maxDataLength -= bytesRead;
511
497
512
498
/* we should read data for the packet */
513
499
if (read_data) {
514
500
bytesRead = 0 ;
515
501
if (!skipPacketData (&p_tile->comps [p_pi->compno ].tileCompResolution [p_pi->resno ], p_pi,
516
- &bytesRead, max_length )) {
502
+ &bytesRead, maxDataLength )) {
517
503
return false ;
518
504
}
519
505
src_buf->incrementCurrentChunkOffset (bytesRead);
520
- nb_totabytes_read += bytesRead;
506
+ totalBytesRead += bytesRead;
521
507
}
522
- *p_data_read = nb_totabytes_read ;
508
+ *dataRead = totalBytesRead ;
523
509
524
510
return true ;
525
511
}
526
512
bool T2Decompress::skipPacketData (Resolution *res,
527
513
PacketIter *p_pi,
528
- uint64_t *p_data_read,
529
- uint64_t max_length) {
530
- *p_data_read = 0 ;
514
+ uint64_t *dataRead,
515
+ uint64_t maxDataLength) {
531
516
for (uint32_t bandIndex = 0 ; bandIndex < res->numTileBandWindows ; ++bandIndex) {
532
- if (*p_data_read == max_length )
517
+ if (*dataRead == maxDataLength )
533
518
return true ;
534
519
auto band = res->tileBand + bandIndex;
535
520
if (band->isEmpty ())
@@ -538,40 +523,26 @@ bool T2Decompress::skipPacketData(Resolution *res,
538
523
if (!prc)
539
524
continue ;
540
525
for (uint64_t cblkno = 0 ; cblkno < prc->getNumCblks (); ++cblkno) {
541
- if (max_length - *p_data_read == 0 )
542
- return true ;
543
526
auto cblk = prc->getDecompressedBlockPtr (cblkno);
544
- if (!cblk->numPassesInPacket ) {
545
- /* nothing to do */
546
- ++cblk;
527
+ if (!cblk->numPassesInPacket )
547
528
continue ;
548
- }
549
- Segment *seg = nullptr ;
550
- if (!cblk->getNumSegments ()) {
529
+ auto seg = cblk->getCurrentSegment ();
530
+ if (!seg || (seg->numpasses == seg->maxpasses ))
551
531
seg = cblk->nextSegment ();
552
- cblk->compressedStream .len = 0 ;
553
- } else {
554
- seg = cblk->getCurrentSegment ();
555
- if (seg->numpasses == seg->maxpasses )
556
- seg = cblk->nextSegment ();
557
- }
558
532
uint32_t numPassesInPacket = cblk->numPassesInPacket ;
559
533
do {
560
- if (*p_data_read == max_length)
561
- return true ;
562
- /* Check possible overflow */
563
- if (((*p_data_read + seg->numBytesInPacket ) > max_length)) {
564
- GRK_WARN (" skip: segment bytes (%u) too large for remaining stream bytes (%u) in codeblock %u (p=%u, b=%u, r=%u, c=%u). Truncating segment" ,
565
- seg->numBytesInPacket , max_length - *p_data_read, cblkno,
534
+ if (((*dataRead + seg->numBytesInPacket ) > maxDataLength)) {
535
+ GRK_WARN (" skip packet data: segment bytes (%u) too large for remaining stream bytes (%u) in "
536
+ " codeblock %u (p=%u, b=%u, r=%u, c=%u). Truncating segment" ,
537
+ seg->numBytesInPacket , maxDataLength - *dataRead, cblkno,
566
538
p_pi->precinctIndex , bandIndex, p_pi->resno , p_pi->compno );
567
- seg->numBytesInPacket = (uint32_t )(max_length - *p_data_read );
539
+ seg->numBytesInPacket = (uint32_t )(maxDataLength - *dataRead );
568
540
}
569
-
570
- // GRK_INFO( "skip packet: p_data_read = %u, bytes in packet = %u ",
571
- // *p_data_read, seg->numBytesInPacket);
572
- *(p_data_read) += seg->numBytesInPacket ;
573
- seg->numpasses += seg->numPassesInPacket ;
574
- numPassesInPacket -= seg->numPassesInPacket ;
541
+ *dataRead += seg->numBytesInPacket ;
542
+ seg->numpasses += seg->numPassesInPacket ;
543
+ numPassesInPacket -= seg->numPassesInPacket ;
544
+ if (*dataRead == maxDataLength)
545
+ return true ;
575
546
if (numPassesInPacket > 0 )
576
547
seg = cblk->nextSegment ();
577
548
} while (numPassesInPacket > 0 );
0 commit comments