Search code examples
c++tiffmultipagelibtiff

Reading a tiff file using libtiff in c++ : prevent warning printout


Reading a tiff file using libtiff in c++ : prevent warning printout

When reading a tiff file I get a warning message to the console every time an unknown tag is read by the function TIFFReadDirectory(). From an answer provided by the user Borovsky I understood that to prevent this, I need to :

“Start by creating class which inherits from TiffErrorHandler and overloads WarningHandler and WarningHandlerEx methods. Basically, you could do nothing in these methods. Then set an instance of you class as the error handler for the library with SetErrorHandler method. The method is static and you can set error handler before opening an image. “ The problem is that I am a beginner in C++ and although I understand how to implement the above, I do not seem to be able to find this TiffErrorHandler class. My code looks as follows:

class myTiffErrorHandler : public TIFFErrorHandler {  <- THE CLASS CANNOT BE FOUND

  public: 
      void WarningHandler() { }  
      void WarningHandlerEx() { } 

};

Then all I do in my main() is:

Main() {

   tif = TIFFOpen(“fn.tif”, "r"));  

int numOfFrames=0;

    do {
        numOfFrames++;
    } while (TIFFReadDirectory(tif));
}

Can someone help me figure this out? What am I doing wrong and where is this class being defined?

Thanks


Solution

  • The answer you refer to is for libtiff using C# in an object oriented wrapper, which is not the same as your situation using C++. What you need to do instead is to define a dummy warning/error handler, like this:

    void DummyHandler(const char* module, const char* fmt, va_list ap)
    {
        // ignore errors and warnings (or handle them your own way)
    }
    

    Then use the function TIFFSetWarningHandler to replace the default handler, like this:

    main() 
    {
        // disable warnings
        TIFFSetWarningHandler(DummyHandler);
    
        tif = TIFFOpen("fn.tif", "r");  
    
        int numOfFrames=0;
    
        do {
            numOfFrames++;
        } while (TIFFReadDirectory(tif));
    }
    

    Note that you could also call TIFFSetWarningHandler with a NULL argument, but I like to use a handler that is #ifdef'ed out in Release builds only so that I can still see warnings in Debug builds.