[Insight-developers] TIFF Spacing Units

Karthik Krishnan karthik.krishnan at kitware.com
Thu Aug 11 03:04:56 EDT 2011


Thanks Luis.

On Thu, Aug 11, 2011 at 4:33 AM, Luis Ibanez <luis.ibanez at kitware.com>wrote:

> Karthik,
>
> When applying the fix from:
>
>
> http://www.itk.org/Bug/bug_relationship_graph.php?bug_id=11455&graph=dependency
>
> in the Gerrit patch
> http://review.source.kitware.com/#change,2431
>
> Brad pointed out correctly that ITK itself doesn't have a
> notion of specific units.
>
> The code here "assumes" that ITK units are millimeters.
>

I agree. We could re-write that patch as :

diff --git a/Code/IO/itkTIFFImageIO.cxx b/Code/IO/itkTIFFImageIO.cxx
index 2a48e6e..ffda5e9 100644
--- a/Code/IO/itkTIFFImageIO.cxx
+++ b/Code/IO/itkTIFFImageIO.cxx
@@ -1663,7 +1663,8 @@ void TIFFImageIO::InternalWrite(const void *buffer)
     }

   int    scomponents = this->GetNumberOfComponents();
-  double resolution = -1;
+  float  resolution_x = ( float ) (m_Spacing[0] != 0 ? 1.0 / m_Spacing[0] : 0);
+  float  resolution_y = ( float ) (m_Spacing[1] != 0 ? 1.0 / m_Spacing[1] : 0);
   uint32 rowsperstrip = ( uint32 ) - 1;
   int    bps;

@@ -1794,10 +1795,10 @@ void TIFFImageIO::InternalWrite(const void *buffer)
     TIFFSetField( tif,
                   TIFFTAG_ROWSPERSTRIP,
                   TIFFDefaultStripSize(tif, rowsperstrip) );
-    if ( resolution > 0 )
+    if ( resolution_x > 0 && resolution_y > 0 )
       {
-      TIFFSetField(tif, TIFFTAG_XRESOLUTION, resolution);
-      TIFFSetField(tif, TIFFTAG_YRESOLUTION, resolution);
+      TIFFSetField(tif, TIFFTAG_XRESOLUTION, resolution_x);
+      TIFFSetField(tif, TIFFTAG_YRESOLUTION, resolution_y);
       TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);

}



>
> It actually exposes a deficiency in the metadata of
> the ITK image.
>
> Note that one option is to tell TIFF that the Units
> are Unknown...
>
> http://www.awaresystems.be/imaging/tiff/tifftags/resolutionunit.html
>
> but then TIFF assumes the X and Y spacing to be the same...
>

We do that, not the tiff library.

In itkTIFFImageIO.cxx, we decide that if the TIFFTAG_RESOLUTIONUNIT is
RESUNIT_NONE, we will not read the spacing and assume that its {1,1} and
ignore the provided spacing in whatever units are provided, which may be a
bit high-handed ? I suggest that in addition to the patch in the bug
tracker, we add this fragment on the read part :


  // If we have some spacing information we use it
  if ( m_InternalImage->m_ResolutionUnit > 0
       && m_InternalImage->m_XResolution > 0
       && m_InternalImage->m_YResolution > 0
       )
    {
    if ( m_InternalImage->m_ResolutionUnit == 2 ) // inches
      {
      m_Spacing[0] = 25.4 / m_InternalImage->m_XResolution;
      m_Spacing[1] = 25.4 / m_InternalImage->m_YResolution;
      }
    else if ( m_InternalImage->m_ResolutionUnit == 3 ) // cm
      {
      m_Spacing[0] = 10.0 / m_InternalImage->m_XResolution;
      m_Spacing[1] = 10.0 / m_InternalImage->m_YResolution;
      }

    + // Propoased addition to that patch
    + else if ( m_InternalImage->m_ResolutionUnit == RESUNIT_NONE /* 1 */ )
    +   {
    +   m_Spacing[0] = 1.0 / m_InternalImage->m_XResolution;
    +   m_Spacing[1] = 1.0 / m_InternalImage->m_YResolution;
    +   }
    }

Thanks
--
karthik


>
>
> Any suggestions ?
>
>
>     Thanks
>
>
>         Luis
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20110811/f03a21d6/attachment.htm>


More information about the Insight-developers mailing list