Skip to content

Commit

Permalink
Added check for tile byte size when passing through pre-encoded JPEG …
Browse files Browse the repository at this point in the history
…tiles for TIFF as these can occasionally be larger than the byte size of raw data
  • Loading branch information
ruven committed Oct 1, 2024
1 parent 573d914 commit 67f26db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
01/10/2024:
- Added check for tile byte size when passing through pre-encoded JPEG tiles for TIFF as these can occasionally
be larger than the byte size of raw data.


25/09/2024:
- Added support for directly reading standard JPEG input images. Implementation uses libjpeg's partial decoding
and DCT-scaling functions to optimize decoding as much as is possible and supports ICC, XMP and EXIF metadata.
Expand Down
21 changes: 19 additions & 2 deletions src/TPTImage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,22 @@ RawTile TPTImage::getTile( int x, int y, unsigned int res, int layers, unsigned

if( ( TIFFGetField( tiff, TIFFTAG_JPEGTABLES, &count, &jpeg_tables ) != 0 ) && ( count > 4 ) ){

// Get actual number of bytes in this tile as it may be larger than the raw data size
uint64_t *bytecounts;
if( !TIFFGetField( tiff, TIFFTAG_TILEBYTECOUNTS, &bytecounts ) ){
throw file_error( "TPTImage :: Unable to get byte count for tile " + tile );
}

// Total size necessary is byte size for this tile + JPEG tables size
uint32_t bytes = bytecounts[tile] + count - 4;

// If larger, re-allocate sufficient buffer space
if( (uint64_t) bytes > rawtile.capacity ){
if( IIPImage::logging ) logfile << "TPTImage :: byte count for tile larger than raw data size: " << bytes << endl;
rawtile.deallocate( rawtile.data );
rawtile.allocate( bytes );
}

// Store last 2 bytes of the JPEG table data itself for use later - skip over the final 2 byte EOI marker
unsigned char table_end[2];
table_end[0] = ((unsigned char*)jpeg_tables)[count-4];
Expand All @@ -561,7 +577,7 @@ RawTile TPTImage::getTile( int x, int y, unsigned int res, int layers, unsigned
// overwrite the end of the table data with the SOI which preceeds the image data in the tile stream
int pos = count - 4;

int length = TIFFReadRawTile( tiff, (ttile_t) tile, (tdata_t) &(((unsigned char*)rawtile.data)[pos]), -1 );
int length = TIFFReadRawTile( tiff, (ttile_t) tile, (tdata_t) &(((unsigned char*)rawtile.data)[pos]), rawtile.capacity );
if( length == -1 ){
throw file_error( "TPTImage :: TIFFReadRawTile() failed for JPEG-encoded tile for " + getFileName( x, y ) );
}
Expand All @@ -581,7 +597,7 @@ RawTile TPTImage::getTile( int x, int y, unsigned int res, int layers, unsigned

#ifdef COMPRESSION_WEBP
else if( requested_encoding == ImageEncoding::WEBP && compression == COMPRESSION_WEBP ){
int length = TIFFReadRawTile( tiff, (ttile_t) tile, (tdata_t) rawtile.data, -1 );
int length = TIFFReadRawTile( tiff, (ttile_t) tile, (tdata_t) rawtile.data, rawtile.capacity );
if( length == -1 ){
throw file_error( "TPTImage :: TIFFReadRawTile() failed for WebP-encoded tile for " + getFileName( x, y ) );
}
Expand All @@ -598,6 +614,7 @@ RawTile TPTImage::getTile( int x, int y, unsigned int res, int layers, unsigned
throw file_error( "TPTImage :: TIFFReadEncodedTile() failed for " + getFileName( x, y ) );
}
rawtile.dataLength = length;
rawtile.quality = 100;
rawtile.compressionType = ImageEncoding::RAW;
}

Expand Down

0 comments on commit 67f26db

Please sign in to comment.