[Insight-users] TIFF Image compression

Julien Jomier jjomier at cs.unc.edu
Thu, 22 Apr 2004 14:39:36 -0400


Hi Zachary,

Thanks for the clear explanation and for providing the code.
I've checked the modifications in the CVS repository.

Let me know if it's not working as you expect,

Thanks again,

Julien

> -----Original Message-----
> From: insight-users-admin at itk.org=20
> [mailto:insight-users-admin at itk.org] On Behalf Of Zachary Pincus
> Sent: Tuesday, April 20, 2004 2:43 PM
> To: insight-users at itk.org
> Subject: [Insight-users] TIFF Image compression
>=20
>=20
> (I sent this to the developer list earlier, but it seems that=20
> that list=20
> may not be the appropriate forum for this.)
>=20
> Hello,
>=20
> =10I am trying to write out uncompressed TIFF images to disk.=20
> Unfortunately, the following construction doesn't work:
>=20
> ...
> writer->SetFileName=10("foo.tiff")
> writer->CompressionOff();
> writer->Update();
>=20
> 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=20
> or not to=20
> use compression.
>=20
> Instead, one must use the following construction:
>=20
> TIFFImageIO::Pointer io =3D TIFFImageIO::New();
> writer->SetFileName(outputFilename);
> io->SetCompressionToNoCompression();
> writer->SetImageIO(io);
> writer->Update();
>=20
> 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=20
> class to also=20
> respect the results of CompressionOff() calls.
>=20
> Unfortunately, this sort of leaves ITK painted into the corner with=20
> regard to default behavior. Specifically, in ITK, TIFFs are=20
> 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=20
> AND retain=20
> the current default behavior of ImageFileWriter and TIFFImageIO.
>=20
> 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=20
> 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.
>=20
> I have included diffs for a patch that does just this.
>=20
> Thanks,
>=20
> Zach Pincus
>=20
> Department of Biochemistry and Program in Biomedical=20
> Informatics Stanford University School of Medicine
>=20
>=20
> 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;
>  >         }
>  > }
>=20
> 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=20
> COMPRESSION_PACKBITS;=20
> break;
>  >       case TIFFImageIO::JPEG:     compression =3D COMPRESSION_JPEG; =

> break;
>  >       case TIFFImageIO::Deflate:  compression =3D=20
> COMPRESSION_DEFLATE;=20
> break;
>  >       case TIFFImageIO::LZW:      compression =3D COMPRESSION_LZW;=20
> break;
>  >       default: compression =3D COMPRESSION_NONE;
>  >       }
>  >     }
>  >   else
>  >     {
>  >     compression =3D COMPRESSION_NONE;
>=20
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org=20
> http://www.itk.org/mailman/listinfo/insight-> users
>=20