Skip to content

Commit

Permalink
Quick fix for issue when writing and reading TIFFs compressed with JPEG
Browse files Browse the repository at this point in the history
  • Loading branch information
smistad committed Nov 27, 2024
1 parent d8019e9 commit 376d5f3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
14 changes: 6 additions & 8 deletions source/FAST/Data/Access/ImagePyramidAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ int ImagePyramidAccess::readTileFromTIFF(void *data, int x, int y, int level) {
const auto tileHeight = m_image->getLevelTileHeight(level);
const auto channels = m_image->getNrOfChannels();
TIFFSetDirectory(m_tiffHandle, level);
uint32_t tile_id = TIFFComputeTile(m_tiffHandle, x, y, 0, 0);
const uint32_t tile_id = TIFFComputeTile(m_tiffHandle, x, y, 0, 0);
if(TIFFGetStrileByteCount(m_tiffHandle, tile_id) == 1) { // Blank patch
if(channels == 1) {
std::memset(data, 0, tileWidth*tileHeight*channels);
Expand Down Expand Up @@ -558,7 +558,7 @@ int ImagePyramidAccess::readTileFromTIFF(void *data, int x, int y, int level) {
return bytesRead;
} else {
int bytesRead = 0;
if(m_compressionFormat == ImageCompression::JPEG) {
if(m_compressionFormat == ImageCompression::JPEG && m_image->isOMETIFF()) {
// Use libjpeg for decompression, as ome-tiff files doesn't seem to like tiff's internal jpeg
auto buffer = make_uninitialized_unique<char[]>(tileWidth*tileHeight*channels);
bytesRead = TIFFReadRawTile(m_tiffHandle, tile_id, buffer.get(), tileWidth*tileHeight*channels);
Expand All @@ -572,14 +572,12 @@ int ImagePyramidAccess::readTileFromTIFF(void *data, int x, int y, int level) {
jpeg_create_decompress(&cinfo);
jpeg_mem_src(&cinfo, (uchar*)buffer.get(), bytesRead);
int ret = jpeg_read_header(&cinfo, false);
if(ret != 1) {
throw Exception("Jpeg error..");
if(ret != JPEG_HEADER_OK) {
throw Exception("Unable to read JPEG header");
}
//cinfo.jpeg_color_space = JCS_YCbCr;
//cinfo.jpeg_color_space = JCS_RGB;
jpeg_start_decompress(&cinfo);
unsigned char* line = (uchar*)data;
while (cinfo.output_scanline < cinfo.output_height) {
uchar* line = (uchar*)data;
while(cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines (&cinfo, &line, 1);
line += channels*cinfo.output_width;
}
Expand Down
2 changes: 1 addition & 1 deletion source/FAST/Data/ImagePyramid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ImagePyramid::ImagePyramid(int width, int height, int channels, int patchWidth,
sprintf(str.get(), fmt, ap);
Reporter::warning() << "TIFF: " << module << ": " << str.get() << Reporter::end();
});
m_tiffHandle = TIFFOpen(m_tiffPath.c_str(), "w8");
m_tiffHandle = TIFFOpen(m_tiffPath.c_str(), "w8"); // 8 == Bigtiff (64 bit)
auto tiff = m_tiffHandle;
m_counter += 1;

Expand Down

0 comments on commit 376d5f3

Please sign in to comment.