MantisBT - ITK
View Issue Details
0007165ITKpublic2008-06-05 04:562010-10-21 12:31
woerz 
Bill Lorensen 
normalmajoralways
closedopen 
 
 
0007165: Itk writes "signed short" tiff-images without setting tag 339 (TIFFTAG_SAMPLEFORMAT), i.e. the images are treated as UNsigned
I use the writer class to save 3D signed short images like:

typedef itk::Image< short, 3 > ImageType;
typedef itk::ImageFileWriter< ImageType > WriterType;
typename WriterType::Pointer writerImage = WriterType::New();
writerImage->SetInput(someImage);
writerImage->SetFileName("image."+ext);
writerImage->Update();

When I use ext="hdr" as an extension the resulting image is fine and can be correctly opened, e.g., with ImageJ.
But when I use ext="tif" then the resulting tif-image is not fine since the tif-tag 339 (TIFFTAG_SAMPLEFORMAT) is not set. It should be set to 2 (SAMPLEFORMAT_INT). When I open the tif-image with ImageJ, ImageJ assumes the default value of 1 (SAMPLEFORMAT_UINT) for the missing tag 339, which gives a wrong result.
No tags attached.
Issue History
2008-06-05 04:56woerzNew Issue
2009-05-22 07:26Bill LorensenStatusnew => assigned
2009-05-22 07:26Bill LorensenAssigned To => Bill Lorensen
2009-06-12 14:19Bill LorensenNote Added: 0016715
2009-06-12 14:19Bill LorensenStatusassigned => feedback
2009-06-15 19:55Bill LorensenNote Added: 0016726
2009-07-01 14:21Bradley LowekampNote Added: 0016798
2009-07-02 15:07Bradley LowekampNote Added: 0016810
2009-07-02 15:08Bradley LowekampNote Edited: 0016810
2009-07-02 15:08Bradley LowekampNote Edited: 0016810
2009-07-03 11:57Bill LorensenNote Added: 0016814
2009-07-03 11:58Bill LorensenStatusfeedback => resolved
2010-10-21 12:31Gabe HartStatusresolved => closed

Notes
(0016715)
Bill Lorensen   
2009-06-12 14:19   
I tried to reproduce the problem. I ran Insight/Testing/Code/IO/itkImageFileWriterTest with an argument foo.tif.

I ran tiffdump on the file and it reported:
Magic: 0x4949 <little-endian> Version: 0x2a
Directory 0: offset 58 (0x3a) next 0 (0)
ImageWidth (256) SHORT (3) 1<5>
ImageLength (257) SHORT (3) 1<5>
BitsPerSample (258) SHORT (3) 1<16>
Compression (259) SHORT (3) 1<1>
Photometric (262) SHORT (3) 1<1>
StripOffsets (273) LONG (4) 1<8>
Orientation (274) SHORT (3) 1<1>
SamplesPerPixel (277) SHORT (3) 1<1>
RowsPerStrip (278) SHORT (3) 1<819>
StripByteCounts (279) LONG (4) 1<50>
PlanarConfig (284) SHORT (3) 1<1>
Software (305) ASCII (2) 15<InsightToolkit\0>
SampleFormat (339) SHORT (3) 1<2>

Note that the SampleFormat is set correctly to the value 2, which indicates two's complement signed integer data. The resul seems correct. I looked at itkTIFFImageIO.cxx and see that in the InternalWrite method the folloing logic is correct:
  if(this->GetComponentType() == SHORT
    || this->GetComponentType() == CHAR)
    {
    TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
    }

In summary, I cannot reproduce the reporter's problem.

(0016726)
Bill Lorensen   
2009-06-15 19:55   
Reminder sent to: woerz

Please see my note on this bug. I cannot reproduce your problem.
(0016798)
Bradley Lowekamp   
2009-07-01 14:21   
I believe I just ran into this problem too. I saved and tiff image as short and photoshop did not deal with it correctly. I will try to narrow this now... and produce a test case.
(0016810)
Bradley Lowekamp   
2009-07-02 15:07   
(edited on: 2009-07-02 15:08)
The reporter's provided code is 3-D and the test is 2-D. Writing a "3-D" tif image I am able to reproduce the problem, get the following from tiff dump:
test.tif:
Magic: 0x4949 <little-endian> Version: 0x2a
Directory 0: offset 571656 (0x8b908) next 0 (0)
SubFileType (254) LONG (4) 1<2>
ImageWidth (256) SHORT (3) 1<638>
ImageLength (257) SHORT (3) 1<448>
BitsPerSample (258) SHORT (3) 1<16>
Compression (259) SHORT (3) 1<1>
Photometric (262) SHORT (3) 1<1>
StripOffsets (273) LONG (4) 75<8 7664 15320 22976 30632 38288 45944 53600 61256 68912 76568 84224 91880 99536 107192 114848 122504 130160 137816 145472 153128 160784 168440 176096 ...>
Orientation (274) SHORT (3) 1<1>
SamplesPerPixel (277) SHORT (3) 1<1>
RowsPerStrip (278) SHORT (3) 1<6>
StripByteCounts (279) LONG (4) 75<7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 7656 ...>
PlanarConfig (284) SHORT (3) 1<1>
PageNumber (297) SHORT (3) 2<1 0>
Software (305) ASCII (2) 15<InsightToolkit\0>

The TiffImageIO also reports that the file is unsigned short also.

Perhaps this attribute needs to be set for each page in itkTIFFImageIO?

(0016814)
Bill Lorensen   
2009-07-03 11:57   
As Brad pointed out, the problem was with 3D tigg images. I was able to reporduce the bug. I added code to set the SampleFormat properly if the image is 3D.