[Insight-users] TiffIO strange behaviour

Ivan Kubarev Ivan.Kubarev at materialise.kiev.ua
Wed Aug 21 06:51:00 EDT 2013


Hello everyone,
It seems that I found bug in RGBA tiff image handling.
I'm trying to load  RGBA tiff image and save it  using following code:

  typedef itk::Image<uint16_t> ImageType;

  auto reader = itk::ImageFileReader<ImageType>::New();
  reader->SetFileName("D://tmp//some.tif");
  reader->SetDebug(true);
  reader->Update();

  auto writer = itk::ImageFileWriter<ImageType>::New();
  writer->SetFileName("D://tmp//tifcheck.tif");
  writer->SetInput(reader->GetOutput());
  writer->Update();

but resulting tifcheck.tif image is just black rectangle.

During bug  investigation I've found that itkTiffIO handles alpha value in a strange way:
1) itkTIFFImageIO.cxx: int TIFFImageIO::EvaluateImageAt(void *out, void *in)
  ...
    case TIFFImageIO::RGB_:
      ....
       red   = *( source );
        green = *( source + 1 );
        blue  = *( source + 2 );
        *( image )   = red;
        *( image + 1 ) = green;
        *( image + 2 ) = blue;
        if ( m_InternalImage->m_SamplesPerPixel == 4 )
          {
          alpha = *( source + 3 ); // 255 in my example
          *( image + 3 ) = 255 - alpha; // 0
          }
2) After reading it ImageReader converts rgba values to CIE luminance(itkConvertPixelBuffer.txx):

::ConvertRGBAToGray(InputPixelType* inputData,
                    OutputPixelType* outputData , size_t size)
....
// this is an ugly implementation of the simple equation
    // greval = (.2125 * red + .7154 * green + .0721 * blue) / alpha
    //
    double tempval =
      ((2125.0 * static_cast< double >( * inputData)
        + 7154.0 * static_cast< double >( *( inputData + 1 ))
        + 0721.0 * static_cast< double >( *( inputData + 2 ))) / 10000.0)
      * static_cast< double >(*( inputData + 3)) // 0!!
      / maxAlpha;
    inputData += 4;
    OutputComponentType val = static_cast< OutputComponentType >( tempval );
    OutputConvertTraits::SetNthComponent(0, *outputData++, val);

So, as the result, I get fully black picture.
After patching TiffIO class, by changing 255 - alpha to alpha;  code example listed above works fine for me.

So here is my question: is it really bug, or I'm doing something wrong?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20130821/b1334e92/attachment.htm>


More information about the Insight-users mailing list