<div>Anja,</div>
<div> </div>
<div>Yes, at compile time you will need to instantiate each type you expect. At runtime you can determine which type to use with something like this:</div>
<div><br>namespace itk<br>{<br> // Description:<br> // Get the PixelType and ComponentType from fileName<br> void GetImageType (std::string fileName,<br> ImageIOBase::IOPixelType &pixelType,<br>
ImageIOBase::IOComponentType &componentType)<br> {<br> typedef itk::Image<unsigned char, 3> ImageType;<br> itk::ImageFileReader<ImageType>::Pointer imageReader =<br> itk::ImageFileReader<ImageType>::New();<br>
imageReader->SetFileName(fileName.c_str());<br> imageReader->UpdateOutputInformation();</div>
<div> pixelType = imageReader->GetImageIO()->GetPixelType();<br> componentType = imageReader->GetImageIO()->GetComponentType();<br> }<br>}</div>
<div> </div>
<div>Then in your application do something like this:</div>
<div> </div>
<div> itk::ImageIOBase::IOPixelType pixelType;<br> itk::ImageIOBase::IOComponentType componentType;</div>
<div> try<br> {<br> itk::GetImageType (inputVolume, pixelType, componentType);</div>
<div> // This filter handles all types<br> <br> switch (componentType)<br> {<br> case itk::ImageIOBase::UCHAR:<br> return DoIt( static_cast<unsigned char>(0));<br> break;<br> case itk::ImageIOBase::CHAR:<br>
return DoIt( static_cast<char>(0));<br> break;<br> case itk::ImageIOBase::USHORT:<br> return DoIt( static_cast<unsigned short>(0));<br> break;<br> case itk::ImageIOBase::SHORT:<br>
return DoIt( static_cast<short>(0));<br> break;<br> case itk::ImageIOBase::UINT:<br> return DoIt( static_cast<unsigned int>(0));<br> break;<br> case itk::ImageIOBase::INT:<br>
return DoIt( static_cast<int>(0));<br> break;<br> case itk::ImageIOBase::ULONG:<br> return DoIt( static_cast<unsigned long>(0));<br> break;<br> case itk::ImageIOBase::LONG:<br>
return DoIt( static_cast<long>(0));<br> break;<br> case itk::ImageIOBase::FLOAT:<br> return DoIt( static_cast<float>(0));<br> break;<br> case itk::ImageIOBase::DOUBLE:<br>
return DoIt( static_cast<double>(0));<br> break;<br> case itk::ImageIOBase::UNKNOWNCOMPONENTTYPE:<br> default:<br> std::cout << "unknown component type" << std::endl;<br>
break;<br> }<br> }<br> catch( itk::ExceptionObject &excep)<br> {<br> std::cerr << argv[0] << ": exception caught !" << std::endl;<br> std::cerr << excep << std::endl;<br>
return EXIT_FAILURE;<br> }<br> return EXIT_SUCCESS;<br>}<br></div>
<div>where Doit is your method:</div>
<div>template<class T> int DoIt(T )<br>{<br>...</div>
<div>}</div>
<div> </div>
<div>CAUTION: your compiler may not be able to instantiate all type without having stack overflow or other problems, You can sometimes combine some of the types (like unsigned int and int) to reduce the number needed.</div>
<div> </div>
<div>Bill</div>
<div> </div>
<div><br> </div>
<div class="gmail_quote">On Wed, Feb 20, 2008 at 7:04 AM, Anja Ende <<a href="mailto:anja.ende@googlemail.com">anja.ende@googlemail.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Hi everyone,<br><br>A quick question about using ITK classes. I am using C++ and am having a bit of trouble understanding how I can use the templated classes without instantiating for every possible type combination.<br>
<br>A simple example:<br><br>I have a custom file reader where the data type and dimensions are something that is only available at run time..<br><br>So, now if I want to have a class member that uses a reader, I have to somehow define these template parameters at runtime... What I wanted was to have one reader which has one instance of the ImageImportFilter instance. However, to declare this class I need to define the template parameters for the ImportImageFilter at design time. So, now if a file is inputted with a different input data type and a dimension, I am stuck!<br>
<br>Any ideas??<br><br>Cheers,<br><font color="#888888"><br>Anja </font><br>_______________________________________________<br>Insight-users mailing list<br><a href="mailto:Insight-users@itk.org">Insight-users@itk.org</a><br>
<a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br><br></blockquote></div><br>