vtkTIFFReader misinterprets PackBits compressed RGB images
This issue was created automatically from an original Mantis Issue. Further discussion may take place here.
vtkTIFFReader misinterprets compressed RGB TIFF image as having 4 scalar components. Below is a sample Java code that illustrated the problem. It first read an uncompressed TIFF image (no problem here), saves it using vtkTIFFWriter (it by default is using PackBits compression) and reads it back. The original image reports having 3 scalar components, the saved and read image shows having 4 scalar components. The vtkImageDifference fails confused by mismatch in number of scalar components.
// Setup baseline reader
vtkTIFFReader baselineReader = new vtkTIFFReader();
baselineReader.SetFileName("data/baseline/Arrays.tif");
baselineReader.Update();
System.out.println("baselineReader.NumberOfScalarComponents: "+baselineReader.GetOutput().GetNumberOfScalarComponents());
// Dump baseline image to a file
vtkTIFFWriter writer = new vtkTIFFWriter();
writer.SetFileName("dump/Arrays.tif");
writer.SetInput(baselineReader.GetOutput());
writer.Write();
// Load back the dumped image
vtkTIFFReader dumpReader = new vtkTIFFReader();
dumpReader.SetFileName("dump/Arrays.tif");
dumpReader.Update();
System.out.println("dumpReader.NumberOfScalarComponents: "+dumpReader.GetOutput().GetNumberOfScalarComponents());
// Get ready to compute difference between images
vtkImageDifference imageDifference = new vtkImageDifference();
// Show imageDifference before initialization
System.out.println("Uninitialized error : "+imageDifference.GetError());
System.out.println("Uninitialized threshold : "+imageDifference.GetThreshold());
System.out.println("Uninitialized thresholdedError: "+imageDifference.GetThresholdedError());
// Imitialize imageDifference
imageDifference.SetInput(baselineReader.GetOutput());
imageDifference.SetImage(dumpReader.GetOutput());
imageDifference.Update();
System.out.println("imageDifference.NumberOfScalarComponents: "+imageDifference.GetOutput().GetNumberOfScalarComponents());
System.out.println("Initialized Error : "+imageDifference.GetError());
System.out.println("Initialized Threshold : "+imageDifference.GetThreshold());
System.out.println("Initialized ThresholdedError: "+imageDifference.GetThresholdedError());
Sample output of the test program:
baselineReader.NumberOfScalarComponents: 3 dumpReader.NumberOfScalarComponents: 4 Uninitialized error : 0.0 Uninitialized threshold : 16 Uninitialized thresholdedError: 0.0 ERROR: In /home/local/src/VTK/Imaging/vtkImageDifference.cxx, line 162 vtkImageDifference (0x8256718): Execute: Expecting 3 components (RGB)
imageDifference.NumberOfScalarComponents: 3 Initialized Error : 1000.0 Initialized Threshold : 16 Initialized ThresholdedError: 1000.0
The problem is critical as it break testing framework for Java VTK Examples (http://ij-plugins.sf.net/vtk-exaples). The testing framework dump content of render window to TIFF via vtkPanel.HardCopy() method (implemented with vtkTIFFWriter). There is no much way around it for a user of VTK as vtkPanel.Lock() and vtkPanel.UnLock() needed to making a hard copy are protected. So using, for instance, PNG as a dump format in not practical without overwriting vtkPanel.