[Insight-users] TIFF Image compression

Zachary Pincus zpincus at stanford.edu
Tue, 20 Apr 2004 11:42:43 -0700


(I sent this to the developer list earlier, but it seems that that list=20=

may not be the appropriate forum for this.)

Hello,

=10I am trying to write out uncompressed TIFF images to disk.=20
Unfortunately, the following construction doesn't work:

...
writer->SetFileName=10("foo.tiff")
writer->CompressionOff();
writer->Update();

This is because itk::TIFFImageIO does not use the m_UseCompression=20
member variable (defined in itk::ImageIOBase, and set by the=20
CompressionOn/CompressionOff functions) to determine whether or not to=20=

use compression.

Instead, one must use the following construction:

TIFFImageIO::Pointer io =3D TIFFImageIO::New();
writer->SetFileName(outputFilename);
io->SetCompressionToNoCompression();
writer->SetImageIO(io);
writer->Update();

While it's nice that one *can* control the TIFF compression at a very=20
fine-grained level with the SetCompression... methods on the=20
TIFFImageIO object, it would be good for the TIFFImageIO class to also=20=

respect the results of CompressionOff() calls.

Unfortunately, this sort of leaves ITK painted into the corner with=20
regard to default behavior. Specifically, in ITK, TIFFs are by default=20=

compressed with PACKBITS compression. However, in its constructor, an=20
ImageFileWriter calls CompressionOff(). There appears to be no way to=20
have TiffImageIO *both* respect Compression[On|Off]() calls AND retain=20=

the current default behavior of ImageFileWriter and TIFFImageIO.

I suggest that it would be best to simply have TIFFImageIO objects=20
respect the m_UseCompression variable, and to automatically set=20
m_UseCompression to true when any SetCompressionTo... method is called.=20=

This would break code that relies on ITK writing PACKBITS-compressed=20
TIFFS by default, but wouldn't break any other code.

I have included diffs for a patch that does just this.

Thanks,

Zach Pincus

Department of Biochemistry and Program in Biomedical Informatics
Stanford University School of Medicine


diff itkTIFFImageIO.h itkTIFFImageIO-patch.h
112c112,128
<   itkSetMacro(Compression,int);
---
 > void SetCompression(int compression) {
 >       m_Compression =3D compression;
 >
 >       // This If block isn't strictly necessary:
 >       // m_UseCompression =3D true; would be sufficient.
 >       // However, it reads strangely for=20
SetCompression(NoCompression) to
 >       // then set m_UseCompression to true.
 >       // Doing it this way is probaly also less likely to break in=20
the future.
 >       if (compression =3D=3D NoCompression)
 >         {
 >         m_UseCompression =3D false;
 >         }
 >       else
 >         {
 >         m_UseCompression =3D true;
 >         }
 > }

diff itkTIFFImageIO.cxx itkTIFFImageIO-patch.cxx
867c867,868
<   switch ( m_Compression )
---
 >
 >   if (m_UseCompression)
869,873c870,881
<   case TIFFImageIO::PackBits: compression =3D COMPRESSION_PACKBITS;=20
break;
<   case TIFFImageIO::JPEG:     compression =3D COMPRESSION_JPEG; break;
<   case TIFFImageIO::Deflate:  compression =3D COMPRESSION_DEFLATE;=20
break;
<   case TIFFImageIO::LZW:      compression =3D COMPRESSION_LZW; break;
<   default: compression =3D COMPRESSION_NONE;
---
 >     switch ( m_Compression )
 >       {
 >       case TIFFImageIO::PackBits: compression =3D =
COMPRESSION_PACKBITS;=20
break;
 >       case TIFFImageIO::JPEG:     compression =3D COMPRESSION_JPEG;=20=

break;
 >       case TIFFImageIO::Deflate:  compression =3D =
COMPRESSION_DEFLATE;=20
break;
 >       case TIFFImageIO::LZW:      compression =3D COMPRESSION_LZW;=20
break;
 >       default: compression =3D COMPRESSION_NONE;
 >       }
 >     }
 >   else
 >     {
 >     compression =3D COMPRESSION_NONE;