[Insight-developers] reinterpret_cast question

Luis Ibanez luis . ibanez at kitware . com
Fri, 27 Jun 2003 11:49:58 -0400


Hi Jisung,

The reinterpret_cast<.> may not be the best option
for what you want to acomplish.

It may be safer to just use  ImageAdaptors.

You simply need to define a Pixel Accesor that
will make your image of Scalars look as an image
of Vectors.  (FixedArrays)

We already have an ImageAdaptor that makes the
reverse operation: make an image of Vectors look
like an image of scalars by selecting one of the
vector components.

You may want to take a look at the

        itkNthElementPixelAccessor.h

The PixelAccessor you need is the reverse of this one.

Once you have defined a pixel Accessor, you can
instantiate your ImageAdaptor on the fly, no need
to create a specific class. Simply pass the
PixelAccessor as template parameter.


typedef itk::ImageAdaptor< ScalarImage, ScalarToVectorPixelAccessor >
   myImageAdaptor;

myImageAdaptor::Pointer adaptor = myImageAdaptor::New();
adaptor->SetImage(  scalarImage );

After this, the image adaptor behaves like an Image of
vectors (or whatever type you defined in the PixelAccessor to
be the output pixel type).

The final trick will be to verify that the Statistical framework
is using Iterators that are "ImageAdaptor-friendly"


     Luis


---------------------
Jisung Kim wrote:

>Hi.
>
>I am doing an experiment with reinterpret_cast. The
>purpose of the experiment is to see if
>"reinterpret_cast"ing scalar pixel (int, chat, double)
>to itk::FixedArray< PixelType, 1> works.
>
>I checked in the itkDummyImageToListAdaptorTest in
>Testing/Code/Numerics/Statistics. The current
>implementation of ImageToListAdaptor class accepts
>only images with the pixel types of itk::FixedArray or
>its derived classes( itk::Vector, itk::Point,
>itk::RBGPixel...). Therefore, images with scalar
>pixels have to be converted before using them with
>ImageToListAdaptor class.
>
>In the new test program, I forced casting from scalar
>pixel to itk::FixedArray< PixelType, 1>. And it worked
>on all compilers. The casting works something like
>this:
>
>typedef int ScalarPixelType ;
>typedef itk::Image< ScalarPixelType, 3 >
>ScalarImageType ;
>typedef itk::FixedArrray< ScalarPixelType, 1 >
>ArrayPixelType ;
>
>...... allocates and fills the scalar image ......
>
>ArrayPixelType& arrayPixel = 
>*(reinterpret_cast< ArrayPixelType* >
>             ( &(scalarImage->GetPixel(index) ) ) ;
>
>std::cout << arrayPixel[0] << std::endl ;
>
>I'm not familiar with the reinterpret_cast and
>wondering if this is legitimate use of it.
>
>Any suggestions?
>
>Thanks,
>
>
>=====
>Jisung Kim
>bahrahm at yahoo . com
>106 Mason Farm Rd.
>129 Radiology Research Lab., CB# 7515
>Univ. of North Carolina at Chapel Hill
>Chapel Hill, NC 27599-7515
>
>__________________________________
>Do you Yahoo!?
>SBC Yahoo! DSL - Now only $29.95 per month!
>http://sbc . yahoo . com
>_______________________________________________
>Insight-developers mailing list
>Insight-developers at itk . org
>http://www . itk . org/mailman/listinfo/insight-developers
>
>  
>