3

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

Appleshell
  • 7,088
  • 6
  • 47
  • 96
user2762182
  • 63
  • 2
  • 5

1 Answers1

2

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.

Roger Rowland
  • 25,885
  • 11
  • 72
  • 113
  • Thanks, I used the NULL option and it solved my problem. However, I like your approach of using an empty function but when I used TIFFSetWarningHandler(DummyHandler); the compiler indicated that I need to provide parameter inputs. I tried to make the input parameter to be void but that did not work either. Am I doing something wrong? – user2762182 May 21 '14 at 15:56
  • @user2762182 Well there's nothing wrong with the syntax above, it works fine for me - may be different with different platform/compiler etc. (I can't immediately think why though) I use Windows and Visual Studio - you? – Roger Rowland May 21 '14 at 19:51
  • I also use Windows 7, MSVC++ 2012 (Professional). I use the compilation without any special flags, but unfortunately I am not experienced enough to play with the TIFF function calls. It was a struggle for me to get the multi-page tiff reading function to work and the printout caused the whole process to be too slow. Your idea of using a empty function is very good and will solve another problem I have. – user2762182 May 22 '14 at 12:44