<HTML>
<HEAD>
<TITLE>Re: [Insight-users] A bug in itkAnalyzeImageIO?</TITLE>
</HEAD>
<BODY>
<FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'>Jian,<BR>
<BR>
The problem is actually much more complicated than that. &nbsp;Both NIFTI and Analyze use the same file name extensions, and it is impossible during the write phase to determine which one to use.<BR>
<BR>
The default behavior of ITK is to write a 2 file nifti (NIFTI_FTYPE_2) file when presented with an &#8220;.hdr&#8221; or &#8220;.img&#8221; file extension. &nbsp;In this case the .hdr file will have 352 bytes and will conform to the NIFTI 2 file standard that supports using quaternions for defining the orientation, and the file will be supported by SPM, FSL, and many other packages.<BR>
<BR>
You should also note that to use the legacy Analyze7.5 file writer it must be explicitly specified to the ITK IO mechanism. &nbsp;In this case the orientation will be defined by the 6 codes.<BR>
<BR>
The last time I checked, both MRIcro and the analyze75read have different interpretations of the Analyze75 .hdr format. &nbsp;I believe that MRIcro does not respect orientation.<BR>
<BR>
<BR>
If you look at line 957of itkNiftiImageIO.cxx there is a little documentation describing that the itkNiftiIO filter will write out Nifti-1 formatted images.<BR>
<BR>
============<BR>
When a user requests writing a file with a &#8220;.hdr&#8221; or &#8220;.img&#8221; file extension, the request is ambiguous as the the desired type. &nbsp;For many of the functional imaging tools (AFNI, FSL, SPM, BRAINSFit, BRAINS2,....) they have agreed to adopt the nifti-1 standard conventions for the file header information in order to bring some level of consistency across applications.<BR>
<BR>
If you change line 952 from &#8220; &nbsp;&nbsp;&nbsp;this-&gt;m_NiftiImage-&gt;nifti_type = NIFTI_FTYPE_NIFTI1_2;&#8221; to &#8220; this-&gt;m_NiftiImage-&gt;nifti_type = NIFTI_FTYPE_ANALYZE;&#8221; then I belive that a 348 byte header consistent with the MayoClinic definition of the dsr (<a href="http://www.mayo.edu/bir/PDF/ANALYZE75.pdf)">http://www.mayo.edu/bir/PDF/ANALYZE75.pdf)</a> will be written to disk.<BR>
<BR>
=============<BR>
Desired TODO list: &nbsp;All the the itkAnalyzeIO functionality has been back ported to the external niftilib libraries that are used as the basis for the itkNiftiIO. &nbsp;The entire functionality of the itkAnalyzeIO can be done with the nifti filter using the NIFTI_FTYPE_ANALYZE flag for writing. &nbsp;This would preserve backwards compatibility and make maintenance much easier in the future. &nbsp;The only thing that would need to be figured out is how to trigger the legacy mode (NIFTI_FTYPE_ANALYZE) rather than the nifti mode (NIFTI_FTYPE_NIFTI_2).<BR>
<BR>
I hope this helps,<BR>
Hans<BR>
<BR>
-- <BR>
Hans J. Johnson, Ph.D.<BR>
Hans-johnson@uiowa.edu<BR>
<BR>
278 GH<BR>
The University of Iowa<BR>
Iowa City, IA 52241<BR>
(319) 353 8587<BR>
<BR>
<BR>
<HR ALIGN=CENTER SIZE="3" WIDTH="95%"><B>From: </B>Bill Lorensen &lt;bill.lorensen@gmail.com&gt;<BR>
<B>Date: </B>Fri, 8 Feb 2008 18:27:02 -0500<BR>
<B>To: </B>Jian Wu &lt;eewujian@yahoo.com&gt;<BR>
<B>Cc: </B>&lt;insight-users@itk.org&gt;, Hans Johnson &lt;hans-johnson@uiowa.edu&gt;, kent williams &lt;nkwmailinglists@gmail.com&gt;<BR>
<B>Subject: </B>Re: [Insight-users] A bug in itkAnalyzeImageIO?<BR>
<BR>
Jian,<BR>
&nbsp;<BR>
This looks like a bug to me. Can you make a bug report following the instructions in:<BR>
<a href="http://itk.org/Wiki/ITK_Procedure_for_Contributing_Bug_Fixes">http://itk.org/Wiki/ITK_Procedure_for_Contributing_Bug_Fixes</a><BR>
&nbsp;<BR>
Thanks,<BR>
&nbsp;<BR>
Bill<BR>
On Fri, Feb 8, 2008 at 5:46 PM, Jian Wu &lt;eewujian@yahoo.com&gt; wrote:<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'>Hi, Bill,<BR>
I updated Nifti codes from CVS and got the same errors. But I found out what caused the problem. The data type of input image is unsigned short, which is not supported by Analyze 7.5. If the image was saved as Analyze format without type casting, the converted image could not be opened by the standard Analyze image reader. It would be nice if the NiftiImageIO could give a warning or an error message to prevent it from happening.<BR>
<FONT COLOR="#888888"><BR>
Jian<BR>
<BR>
</FONT>----- Original Message ----<BR>
From: Bill Lorensen &lt;bill.lorensen@gmail.com&gt;<BR>
To: Jian Wu &lt;eewujian@yahoo.com&gt;<BR>
Cc: insight-users@itk.org; Hans Johnson &lt;hans-johnson@uiowa.edu&gt;; kent williams &lt;nkwmailinglists@gmail.com&gt;<BR>
Sent: Thursday, February 7, 2008 11:34:09 PM<BR>
Subject: Re: [Insight-users] A bug in itkAnalyzeImageIO?<BR>
<BR>
Jian,<BR>
&nbsp;<BR>
There were a bunch of changes in the nifti code recently. Can you update and retry?<BR>
&nbsp;<BR>
Bill<BR>
<BR>
On Thu, Feb 7, 2008 at 5:43 PM, Jian Wu &lt;eewujian@yahoo.com&gt; wrote:<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'>Hi, Bill,<BR>
<BR>
Thanks for the prompt reply. Actually, I have tried to use itkNiftiImageIO. Without explicitly specifying an ImageIO class, the ITK would use itkNiftiImageIO by default if the images have the extension of 'hdr' or 'img'. I tried and failed in the first place. That's why I tried to explicitly use itkAnalyzeImageIO later. When itkNiftiImageIO was used, the Matlab reader gave me exactly the same error message. Even worse, the converted analyze image could not be opened by MIRcro. A simple test code is attached here, very similar to the previous one.<BR>
<BR>
Jian<BR>
<BR>
Command line:<BR>
ImageReadWrite brainweb1e1a10f20.mha brainweb1e1a10f20.img <BR>
<BR>
<BR>
Source Code:<BR>
/*=========================================================================<BR>
<BR>
&nbsp;&nbsp;Program: &nbsp;&nbsp;Insight Segmentation &amp; Registration Toolkit<BR>
&nbsp;&nbsp;Module: &nbsp;&nbsp;&nbsp;ImageReadWrite.cxx,v $ <BR>
<BR>
<BR>
=========================================================================*/<BR>
#if defined(_MSC_VER)<BR>
#pragma warning ( disable : 4786 )<BR>
#endif<BR>
<BR>
#ifdef __BORLANDC__<BR>
#define ITK_LEAN_AND_MEAN<BR>
#endif<BR>
<BR>
#include &quot;itkImageFileReader.h&quot;<BR>
#include &quot;itkImageFileWriter.h&quot;<BR>
#include &quot;itkImage.h&quot;<BR>
<BR>
int main( int argc, char ** argv )<BR>
{<BR>
&nbsp;&nbsp;if( argc &lt; 3 )<BR>
&nbsp;&nbsp;&nbsp;&nbsp;{<BR>
&nbsp;&nbsp;&nbsp;&nbsp;std::cerr &lt;&lt; &quot;Usage: &quot; &lt;&lt; std::endl;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;std::cerr &lt;&lt; argv[0] &lt;&lt; &quot; inputImageFile &nbsp;outputImageFile &quot; &lt;&lt; std::endl;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;return EXIT_FAILURE;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
<BR>
&nbsp;&nbsp;typedef unsigned short &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PixelType;<BR>
&nbsp;&nbsp;const &nbsp;&nbsp;unsigned int &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dimension = 3;<BR>
&nbsp;&nbsp;typedef itk::Image&lt; PixelType, Dimension &gt; &nbsp;&nbsp;&nbsp;ImageType;<BR>
<BR>
&nbsp;&nbsp;typedef itk::ImageFileReader&lt; ImageType &gt; &nbsp;ReaderType;<BR>
&nbsp;&nbsp;typedef itk::ImageFileWriter&lt; ImageType &gt; &nbsp;WriterType;<BR>
<BR>
&nbsp;&nbsp;ReaderType::Pointer reader = ReaderType::New();<BR>
&nbsp;&nbsp;WriterType::Pointer writer = WriterType::New();<BR>
<BR>
&nbsp;&nbsp;const char * inputFilename &nbsp;= argv[1];<BR>
&nbsp;&nbsp;const char * outputFilename = argv[2];<BR>
<BR>
&nbsp;&nbsp;reader-&gt;SetFileName( inputFilename &nbsp;);<BR>
&nbsp;&nbsp;writer-&gt;SetFileName( outputFilename );<BR>
<BR>
&nbsp;&nbsp;writer-&gt;SetInput( reader-&gt;GetOutput() );<BR>
<BR>
&nbsp;&nbsp;try <BR>
&nbsp;&nbsp;&nbsp;&nbsp;{ <BR>
&nbsp;&nbsp;&nbsp;&nbsp;writer-&gt;Update(); <BR>
&nbsp;&nbsp;&nbsp;&nbsp;} <BR>
&nbsp;&nbsp;catch( itk::ExceptionObject &amp; err ) <BR>
&nbsp;&nbsp;&nbsp;&nbsp;{ <BR>
&nbsp;&nbsp;&nbsp;&nbsp;std::cerr &lt;&lt; &quot;ExceptionObject caught !&quot; &lt;&lt; std::endl; <BR>
&nbsp;&nbsp;&nbsp;&nbsp;std::cerr &lt;&lt; err &lt;&lt; std::endl; <BR>
&nbsp;&nbsp;&nbsp;&nbsp;return EXIT_FAILURE;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;} <BR>
<BR>
&nbsp;&nbsp;return EXIT_SUCCESS;<BR>
}<BR>
<BR>
<BR>
<BR>
----- Original Message ----<BR>
From: Bill Lorensen &lt;bill.lorensen@gmail.com&gt;<BR>
To: Jian Wu &lt;eewujian@yahoo.com&gt;<BR>
Cc: insight-users@itk.org; Hans Johnson &lt;hans-johnson@uiowa.edu&gt;; kent williams &lt;nkwmailinglists@gmail.com&gt;<BR>
Sent: Thursday, February 7, 2008 2:50:30 PM<BR>
Subject: Re: [Insight-users] A bug in itkAnalyzeImageIO?<BR>
<BR>
Jian,<BR>
Can you please try the same program with itkNiftiImageIO instead of itkAnalyzeImageIO? The nifti reader/writer should produce valid analyze files. We are looking at replacing itkAnalyzeImageIO's implementation with itkNiftiImageIO. Nifti is a file format with a substantial and active community. If we do this, we will keep the Analyze API.<BR>
<BR>
Thanks,<BR>
<BR>
Bill<BR>
<BR>
On Thu, Feb 7, 2008 at 2:25 PM, Jian Wu &lt;eewujian@yahoo.com&gt; wrote:<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'>Hi,<BR>
I'm doing image format conversion from other image types to Analyze 7.5. The program did not give me any error message. However I doubt it may not handle the image header information properly. The images I generated in Analyze 7.5 format can be viewed using MRIcro image viewer. But when I tried to opened it using Matlab function analyze75read, it gave me an error message &quot;Reference to non-existent field 'ImgDataType'.&quot; I studies the ITK source code and found out the line 1128 of itkAnalyzeImageIO.cxx states:<BR>
&nbsp;&nbsp;&nbsp;switch( this-&gt;m_Hdr.dime.datatype)<BR>
Here &quot;this-&gt;m_Hdr&quot; has not been filled with proper image information yet when the input image is not in Analyze image format. I think &quot;this-&gt;m_ComponentType&quot; should be referred instead. My test code is attached here. I used &quot;brainweb1e1a10f20.mha&quot; as the input image.<BR>
<BR>
Jian<BR>
<BR>
Command line:<BR>
ImageReadWriteAnalyze brainweb1e1a10f20.mha brainweb1e1a10f20.img<BR>
<BR>
Source Code:<BR>
<BR>
/*=========================================================================<BR>
<BR>
&nbsp;Program: &nbsp;&nbsp;Insight Segmentation &amp; Registration Toolkit<BR>
&nbsp;Module: &nbsp;&nbsp;&nbsp;$ ImageReadWriteAnalyze.cxx $<BR>
&nbsp;Language: &nbsp;C++<BR>
&nbsp;Date: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$Date: 2008/02/07 $<BR>
&nbsp;Version: &nbsp;&nbsp;$Revision: 1.0 $<BR>
&nbsp;Author: &nbsp;&nbsp;&nbsp;&nbsp;Jian Wu<BR>
<BR>
=========================================================================*/<BR>
#if defined(_MSC_VER)<BR>
#pragma warning ( disable : 4786 )<BR>
#endif<BR>
<BR>
#ifdef __BORLANDC__<BR>
#define ITK_LEAN_AND_MEAN<BR>
#endif<BR>
<BR>
#include &quot;itkImageFileReader.h&quot;<BR>
#include &quot;itkImageFileWriter.h&quot;<BR>
#include &quot;itkAnalyzeImageIO.h&quot;<BR>
<BR>
#include &quot;itkImage.h&quot;<BR>
<BR>
<BR>
int main( int argc, char ** argv )<BR>
{<BR>
&nbsp;if( argc &lt; 3 )<BR>
&nbsp;&nbsp;&nbsp;{<BR>
&nbsp;&nbsp;&nbsp;std::cerr &lt;&lt; &quot;Usage: &quot; &lt;&lt; std::endl;<BR>
&nbsp;&nbsp;&nbsp;std::cerr &lt;&lt; argv[0] &lt;&lt; &quot; inputImageFile &nbsp;outputImageFile &quot; &lt;&lt; std::endl;<BR>
&nbsp;&nbsp;&nbsp;return EXIT_FAILURE;<BR>
&nbsp;&nbsp;&nbsp;}<BR>
<BR>
&nbsp;typedef unsigned short &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PixelType;<BR>
&nbsp;const &nbsp;&nbsp;unsigned int &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dimension = 3;<BR>
&nbsp;typedef itk::Image&lt; PixelType, Dimension &gt; &nbsp;&nbsp;&nbsp;ImageType;<BR>
<BR>
&nbsp;typedef itk::ImageFileReader&lt; ImageType &gt; &nbsp;ReaderType;<BR>
&nbsp;typedef itk::ImageFileWriter&lt; ImageType &gt; &nbsp;WriterType;<BR>
&nbsp;typedef itk::AnalyzeImageIO &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ImageIOType;<BR>
<BR>
<BR>
&nbsp;ReaderType::Pointer reader = ReaderType::New();<BR>
&nbsp;WriterType::Pointer writer = WriterType::New();<BR>
&nbsp;ImageIOType::Pointer analyzeIO = ImageIOType::New();<BR>
<BR>
&nbsp;const char * inputFilename &nbsp;= argv[1];<BR>
&nbsp;const char * outputFilename = argv[2];<BR>
<BR>
&nbsp;reader-&gt;SetFileName( inputFilename &nbsp;);<BR>
&nbsp;writer-&gt;SetFileName( outputFilename );<BR>
<BR>
&nbsp;writer-&gt;SetInput( reader-&gt;GetOutput() );<BR>
&nbsp;writer-&gt;SetImageIO( analyzeIO );<BR>
<BR>
&nbsp;try<BR>
&nbsp;&nbsp;&nbsp;{<BR>
&nbsp;&nbsp;&nbsp;writer-&gt;Update();<BR>
&nbsp;&nbsp;&nbsp;}<BR>
&nbsp;catch( itk::ExceptionObject &amp; err )<BR>
&nbsp;&nbsp;&nbsp;{<BR>
&nbsp;&nbsp;&nbsp;std::cerr &lt;&lt; &quot;ExceptionObject caught !&quot; &lt;&lt; std::endl;<BR>
&nbsp;&nbsp;&nbsp;std::cerr &lt;&lt; err &lt;&lt; std::endl;<BR>
&nbsp;&nbsp;&nbsp;return EXIT_FAILURE;<BR>
&nbsp;&nbsp;&nbsp;}<BR>
<BR>
&nbsp;return EXIT_SUCCESS;<BR>
}<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;____________________________________________________________________________________<BR>
Looking for last minute shopping deals?<BR>
Find them fast with Yahoo! Search. &nbsp;<a href="http://tools.search.yahoo.com/newsearch/category.php?category=shopping">http://tools.search.yahoo.com/newsearch/category.php?category=shopping</a><BR>
_______________________________________________<BR>
Insight-users mailing list<BR>
Insight-users@itk.org<BR>
<a href="http://www.itk.org/mailman/listinfo/insight-users">http://www.itk.org/mailman/listinfo/insight-users</a><BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'><BR>
<BR>
<BR>
<HR ALIGN=CENTER SIZE="1" WIDTH="100%">Never miss a thing. Make Yahoo your homepage. &lt;http://us.rd.yahoo.com/evt=51438/*<a href="http://www.yahoo.com/r/hs>">http://www.yahoo.com/r/hs&gt;</a> &nbsp;<BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'><BR>
<BR>
<BR>
<HR ALIGN=CENTER SIZE="1" WIDTH="100%">Looking for last minute shopping deals? Find them fast with Yahoo! Search. &lt;http://us.rd.yahoo.com/evt=51734/*<a href="http://tools.search.yahoo.com/newsearch/category.php?category=shopping>">http://tools.search.yahoo.com/newsearch/category.php?category=shopping&gt;</a> <BR>
<BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'><BR>
</SPAN></FONT>
</BODY>
</HTML>