<meta charset="utf-8"><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">Hello Insight mailing list!</span><div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">

<br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">I sent a copy of this email a couple of days ago, but I think it&#39;s been rejected because I attached a rather large image to it, so apologies if two of these come through :-)</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">I think have either found a bug in ITK, or I am doing something silly. Either way, I&#39;m really confused and I&#39;d really appreciate some advice...</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">I need to downsample two sets images of heart data and register them together, but one set consists of RGB bmps, and the other of RGBA bmps. A example of one of the original RGBA images can be found here:</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><a href="http://dl.dropbox.com/u/706648/0590.bmp">http://dl.dropbox.com/u/706648/0590.bmp</a></span></font></div>

<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">and an example of the output of an ITK program that has the problems I discuss can be found here:</span></font></div>

<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><a href="http://dl.dropbox.com/u/706648/0590_ITK_output.bmp">http://dl.dropbox.com/u/706648/0590_ITK_output.bmp</a></span></font></div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">When I try and read RGBA bmps with a reader of pixel type RGB, the output looks very different, because (I guess) the actual alpha channel has been interpreted as red, the actual red as green, and the actual green as blue (the original blue channel seems to be discarded).</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">Furthermore, when I read the RGBA bmp with a reader of pixel type RGBA, then explicitly populate new images of type itkImage&lt; unsigned char, 2 &gt; using either the number getter GetNthComponent() or the named getters GetRed(), GetGreen(), GetBlue() and GetAlpha(), as in the code below, the same problem is manifest:</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">// ConvertRGBAtoRGB.cxx</div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">

<div><div>#include &quot;itkRGBPixel.h&quot;</div><div>#include &quot;itkRGBAPixel.h&quot;</div><div>#include &quot;IOHelpers.hpp&quot;</div><div><br></div><div><br></div><div>using namespace std;</div><div><br></div><div>

int main( int argc, char * argv[] )</div><div>{</div><div>  if( argc != 3 ) {</div><div>    cerr &lt;&lt; &quot;Usage: &quot; &lt;&lt; endl;</div><div>    cerr &lt;&lt; argv[0] &lt;&lt; &quot;  inputImageFile outputDir&quot; &lt;&lt; endl;</div>

<div>    exit(EXIT_FAILURE);</div><div>  }</div><div>  </div><div>  string inputImageFile(argv[1]), outputDir(argv[2]);</div><div>  </div><div>  const     unsigned int   Dimension = 2;</div><div>  </div><div>  typedef itk::RGBAPixel&lt; unsigned char &gt; InputPixelType;</div>

<div>  typedef itk::RGBPixel&lt; unsigned char &gt;  OutputPixelType;</div><div>  </div><div>  typedef itk::Image&lt; InputPixelType,  Dimension &gt; InputImageType;</div><div>  typedef itk::Image&lt; OutputPixelType, Dimension &gt; OutputImageType;</div>

<div>  </div><div>  // Read RGBA image from file</div><div>  InputImageType::Pointer inputImage = readImage&lt; InputImageType &gt;( inputImageFile );</div><div>  </div><div>  // Build RGB image from RGBA image</div><div>

  // Initialise output image</div><div>  OutputImageType::RegionType region;</div><div>  region.SetSize( inputImage-&gt;GetLargestPossibleRegion().GetSize() );</div><div>  </div><div>  OutputImageType::Pointer outputImage = OutputImageType::New();</div>

<div>  outputImage-&gt;SetRegions( region );</div><div>  outputImage-&gt;CopyInformation( inputImage );</div><div>  outputImage-&gt;Allocate();</div><div>  </div><div>  // copy values from input image</div><div>  itk::ImageRegionConstIterator&lt; InputImageType &gt; cit(inputImage, region);</div>

<div>  itk::ImageRegionIterator&lt; OutputImageType &gt;     it(outputImage, region);</div><div>  for (cit.GoToBegin(), it.GoToBegin(); !it.IsAtEnd(); ++cit, ++it ) {</div><div>    it.Value().SetRed( cit.Value().GetRed() );</div>

<div>    it.Value().SetBlue( cit.Value().GetBlue() );</div><div>    it.Value().SetGreen( cit.Value().GetGreen() );</div><div>    </div><div>  }</div><div>  </div><div>  // write RGB image to file</div><div>  writeImage&lt; OutputImageType &gt;( outputImage, outputDir + &quot;RGB.bmp&quot; );</div>

<div>  </div><div>  // Write individual channels by name to file.</div><div>  #define writeChannelByName(Colour) \</div><div>  { \</div><div>   /* create image from named channel of input image */ \</div><div>    typedef itk::Image&lt; unsigned char &gt; ChannelType; \</div>

<div>    ChannelType::Pointer channel = ChannelType::New(); \</div><div>    channel-&gt;SetRegions( region ); \</div><div>    channel-&gt;CopyInformation( inputImage ); \</div><div>    channel-&gt;Allocate(); \</div><div>

    \</div><div>    itk::ImageRegionConstIterator&lt; InputImageType &gt; cit(inputImage, region); \</div><div>    itk::ImageRegionIterator&lt; ChannelType &gt; it(channel, region); \</div><div>    \</div><div>    for (cit.GoToBegin(), it.GoToBegin(); !it.IsAtEnd(); ++cit, ++it ) { \</div>

<div>      it.Set( cit.Value().Get ## Colour() ); \</div><div>    } \</div><div>    \</div><div>   /* write channel */ \</div><div>    writeImage&lt; ChannelType &gt;(channel, outputDir + #Colour + &quot;.bmp&quot; ); \</div>

<div>  }</div><div>  </div><div>  writeChannelByName(Red);</div><div>  writeChannelByName(Green);</div><div>  writeChannelByName(Blue);</div><div>  writeChannelByName(Alpha);</div><div>  </div><div>  return EXIT_SUCCESS;</div>

<div>}</div></div></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">// IOHelpers.hpp</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><div>#ifndef IO_HELPERS_HPP_</div><div>#define IO_HELPERS_HPP_</div><div><br></div><div>#include &quot;itkImage.h&quot;</div><div>
#include &quot;itkImageFileReader.h&quot;</div>
<div>#include &quot;itkImageFileWriter.h&quot;</div><div>#include &quot;itkTransformFileWriter.h&quot;</div><div><br></div><div>using namespace std;</div></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">

<br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><div>// Reader helper</div><div>template&lt;typename ImageType&gt;</div><div>typename ImageType::Pointer readImage(const string&amp; fileName) {</div>

<div>  typedef itk::ImageFileReader&lt; ImageType &gt; ReaderType;</div><div>  typename ReaderType::Pointer reader = ReaderType::New();</div><div><span style="white-space: pre-wrap; ">        </span></div><div>  reader-&gt;SetFileName( fileName.c_str() );</div>

<div><span style="white-space: pre-wrap; ">        </span></div><div>  try {</div><div>  <span style="white-space: pre-wrap; ">        </span>reader-&gt;Update();</div><div>  }</div><div><span style="white-space: pre-wrap; ">        </span>catch( itk::ExceptionObject &amp; err ) {</div>

<div>    cerr &lt;&lt; &quot;ExceptionObject caught !&quot; &lt;&lt; endl;</div><div>    cerr &lt;&lt; err &lt;&lt; endl;</div><div><span style="white-space: pre-wrap; ">                </span>exit(EXIT_FAILURE);</div><div><span style="white-space: pre-wrap; ">        </span>}</div>

<div><span style="white-space: pre-wrap; ">        </span></div><div>  return reader-&gt;GetOutput();</div><div>}</div><div><br></div><div>// Writer helpers</div><div>// const Data</div><div>template&lt;typename WriterType, typename DataType&gt;</div>

<div>void writeData(const typename DataType::ConstPointer data, const string&amp; fileName) {</div><div>  typename WriterType::Pointer writer = WriterType::New();</div><div><span style="white-space: pre-wrap; ">        </span></div>

<div><span style="white-space: pre-wrap; ">        </span>writer-&gt;SetInput( data );</div><div>  // writer-&gt;AddTransform( anotherTransform ); // only applies to writing transforms</div><div>  </div><div>  writer-&gt;SetFileName( fileName.c_str() );</div>

<div><span style="white-space: pre-wrap; ">        </span></div><div>  try {</div><div>  <span style="white-space: pre-wrap; ">        </span>writer-&gt;Update();</div><div>  }</div><div><span style="white-space: pre-wrap; ">        </span>catch( itk::ExceptionObject &amp; err ) {</div>

<div>    cerr &lt;&lt; &quot;ExceptionObject caught !&quot; &lt;&lt; endl;</div><div>    cerr &lt;&lt; err &lt;&lt; endl;</div><div><span style="white-space: pre-wrap; ">                </span>exit(EXIT_FAILURE);</div><div><span style="white-space: pre-wrap; ">        </span>}</div>

<div>}</div><div><br></div><div>// Data</div><div>template&lt;typename WriterType, typename DataType&gt;</div><div>void writeData(const typename DataType::Pointer data, const string&amp; fileName) {</div><div>  writeData&lt; WriterType, DataType &gt;( (typename DataType::ConstPointer) data, fileName);</div>

<div>}</div><div><br></div><div>// Const Image</div><div>template&lt;typename ImageType&gt;</div><div>void writeImage(const typename ImageType::ConstPointer image, const string&amp; fileName) {</div><div>  writeData&lt; itk::ImageFileWriter&lt; ImageType &gt;, ImageType &gt;( image, fileName );</div>

<div>}</div><div><br></div><div>// Image</div><div>template&lt;typename ImageType&gt;</div><div>void writeImage(const typename ImageType::Pointer image, const string&amp; fileName) {</div><div>  writeData&lt; itk::ImageFileWriter&lt; ImageType &gt;, ImageType &gt;( image, fileName );</div>

<div>}</div><div><br></div><div>#endif</div></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">

It seems like the internal RGBA image pointer is one char behind where it should be or something...?</div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">

I am using the current 3.20 release branch from the Git repo on a Penryn MacBook Pro running Snow Leopard, but I get the same problem on other linux boxes.</div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">

<br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">Unrelatedly, I&#39;m always keen for feedback on coding style, conformance, anything really, so please don&#39;t hold back!</div>

<div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; "><br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">Many thanks,</div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">

<br></div><div style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">Matt Gibb.</div><br></div>