Skip to content

Commit

Permalink
[Backport MR-4909] ENH: Suppress irrelevant TIFF warnings
Browse files Browse the repository at this point in the history
LibTIFF logs hundreds of messages like "TIFFReadDirectory: Warning, Unknown field with tag 65280 (0xff00) encountered." when reading a volume from a TIFF file list that contains custom tags.
Presence of custom tags is harmless and it should not flood the console or application log with warnings, because message display can significantly slow down image loading and excessive amount of irrelevant messages makes it difficult to notice important messages.

For background, see SlicerMorph/SlicerMorph#320
  • Loading branch information
lassoan authored and jcfr committed Oct 28, 2024
1 parent e18566d commit d12a45b
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions Modules/IO/TIFF/src/itkTIFFImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,34 @@

#include "itk_tiff.h"

#include "itksys/SystemTools.hxx"

namespace itk
{

TIFFErrorHandler _ITKTIFFsavedWarningHandler = nullptr;

// This warning handler suppresses log messages starting with "Unknown field with tag" because these kind of messages
// are harmless (they just mean that the TIFF file contains custom tags) and they would flood the console when reading images
// that contains hundreds of frames, hiding actual errors or relevant warnings.
static void
ITKTIFFTagNotFoundSuppressWarningHandler(const char * module, const char * fmt, va_list ap)
{
if (fmt != nullptr)
{
if (itksys::SystemTools::StringStartsWith(fmt, "Unknown field with tag"))
{
// ignore unknown field warnings
return;
}
}
// call the saved warning handler
if (_ITKTIFFsavedWarningHandler != nullptr)
{
_ITKTIFFsavedWarningHandler(module, fmt, ap);
}
}

bool
TIFFImageIO::CanReadFile(const char * file)
{
Expand All @@ -40,16 +65,18 @@ TIFFImageIO::CanReadFile(const char * file)
}

// Now check if this is a valid TIFF image
TIFFErrorHandler save = TIFFSetErrorHandler(nullptr);
int res = m_InternalImage->Open(file);
if (res)
{
TIFFSetErrorHandler(save);
return true;
}
m_InternalImage->Clean();
TIFFSetErrorHandler(save);
return false;
TIFFErrorHandler savedErrorHandler = TIFFSetErrorHandler(nullptr);
TIFFErrorHandler savedWarningHandler = TIFFSetWarningHandler(ITKTIFFTagNotFoundSuppressWarningHandler);
_ITKTIFFsavedWarningHandler = savedWarningHandler;
bool success = (m_InternalImage->Open(file) != 0);
if (success)
{
m_InternalImage->Clean();
}
TIFFSetErrorHandler(savedErrorHandler);
TIFFSetWarningHandler(savedWarningHandler);
_ITKTIFFsavedWarningHandler = nullptr;
return success;
}

void
Expand Down Expand Up @@ -152,6 +179,9 @@ TIFFImageIO::ReadVolume(void * buffer)
const size_t width{ m_InternalImage->m_Width };
const size_t height{ m_InternalImage->m_Height };

TIFFErrorHandler savedWarningHandler = TIFFSetWarningHandler(ITKTIFFTagNotFoundSuppressWarningHandler);
_ITKTIFFsavedWarningHandler = savedWarningHandler;

for (uint16_t page = 0; page < m_InternalImage->m_NumberOfPages; ++page)
{
if (m_InternalImage->m_IgnoredSubFiles > 0)
Expand All @@ -175,6 +205,9 @@ TIFFImageIO::ReadVolume(void * buffer)

TIFFReadDirectory(m_InternalImage->m_Image);
}

TIFFSetWarningHandler(savedWarningHandler);
_ITKTIFFsavedWarningHandler = nullptr;
}

void
Expand Down

0 comments on commit d12a45b

Please sign in to comment.