[Insight-developers] Writing/Extending own ImageIOBase class

Luis Ibanez luis.ibanez at kitware.com
Wed Oct 31 08:50:06 EDT 2007


Hi Klaus,

Please look at the following example of a minimalistic
ImageIO class.

In particular to the Read() method in itkYAFFImageIO.cxx in
lines 117 to 161.

The buffer is supposed to be the block of memory for storing
your pixel data in 3D.

If you have an image of pixel type unsigned short, with
size in pixels = {17,35,29}


the first 17 pixels will be stored in the first 34 bytes
of the buffer, (shorts use 2bytes per pixel) as:


unsigned short * pixels = static_cast< unsigned short * >( buffer );

pixels[0] = unsigned short of pixel 0
pixels[1] = unsigned short of pixel 1
...
pixels[16] = unsigned short of pixel 16
             // (which is the 17th pixels since we start at 0)


Then the next element of the buffer will have the first pixel of
the second line in the image, the line starting at index (0,1,0)

pixels[17] = first pixel from second line,
pixels[18] = second pixel from second line,
..
pixels[33] = last pixel from second line,


then, the next line...

pixels[34] = first pixel from third line,
pixels[35] = second pixel from third line,
..
pixels[50] = last pixel from third line,

and so on... until you get to the last line of the first slice
which starts in index (0,34,0)


Then you continue with
the first line of the second slice
the second line of the second slice

....


    Regards,


       Luis



---------------------
Klaus Drechsler wrote:
> Hi,
> 
> I am writing a new class that inherited from ImageIObase to support a new filetype. My problem right now is, that I do not know exactly how to deal with the buffer within the function ::Read(void* buffer).
> 
> My data that I read from the file is 3-dimensional. 
> How do I store the read values (unsigned shorts) into the buffer? (layout of the buffer).
> 
> Is it for example like this:
> buffer[0] = x1
> buffer[1] = y1
> buffer[2] = z1
> ...
> 
> I would really appriciate any help or a pointer to some useful documentation (I looked at various files distributed with itk but I did not understand this part very well)
> 
> Best regards
> 
> Klaus Drechsler
> 
> 
> 
-------------- next part --------------
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkYAFFImageIOFactory.h,v $
  Language:  C++
  Date:      $Date: 2007/03/22 14:28:51 $
  Version:   $Revision: 1.15 $

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __itkYAFFImageIOFactory_h
#define __itkYAFFImageIOFactory_h

#include "itkObjectFactoryBase.h"
#include "itkImageIOBase.h"

namespace itk
{
/** \class YAFFImageIOFactory
 * \brief YAFF : Yet Another File Format: is a fake file format introduced
                 with the sole purpose of testing the streaming capabilities
                 of ITK.

         
   \warning    DO NOT USE THIS FILE FORMAT FOR ANY SERIOUS PURPOSE !.


   Consider it only a place holder for very basic testing.
 */
class ITK_EXPORT YAFFImageIOFactory : public ObjectFactoryBase
{
public:  
  /** Standard class typedefs. */
  typedef YAFFImageIOFactory       Self;
  typedef ObjectFactoryBase        Superclass;
  typedef SmartPointer<Self>       Pointer;
  typedef SmartPointer<const Self> ConstPointer;
  
  /** Class methods used to interface with the registered factories. */
  virtual const char* GetITKSourceVersion() const;
  virtual const char* GetDescription() const;
  
  /** Method for class instantiation. */
  itkFactorylessNewMacro(Self);
  static YAFFImageIOFactory* FactoryNew() { return new YAFFImageIOFactory;}

  /** Run-time type information (and related methods). */
  itkTypeMacro(YAFFImageIOFactory, ObjectFactoryBase);

  /** Register one factory of this type  */
  static void RegisterOneFactory()
    {
    YAFFImageIOFactory::Pointer metaFactory = YAFFImageIOFactory::New();
    ObjectFactoryBase::RegisterFactory(metaFactory);
    }

protected:
  YAFFImageIOFactory();
  ~YAFFImageIOFactory();

private:
  YAFFImageIOFactory(const Self&); //purposely not implemented
  void operator=(const Self&); //purposely not implemented

};
 
} // end namespace itk

#endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: itkYAFFImageIO.cxx
Type: text/x-c++src
Size: 5489 bytes
Desc: not available
Url : http://www.itk.org/mailman/private/insight-developers/attachments/20071031/3904ed5b/itkYAFFImageIO.cxx
-------------- next part --------------
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkYAFFImageIO.h,v $
  Language:  C++
  Date:      $Date: 2007/03/22 14:28:51 $
  Version:   $Revision: 1.31 $

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __itkYAFFImageIO_h
#define __itkYAFFImageIO_h

#ifdef _MSC_VER
#pragma warning ( disable : 4786 )
#endif

#include <fstream>
#include "itkImageIOBase.h"

namespace itk
{

/** \class YAFFImageIO
 *
 *  \brief Read YAFFImage file format.
 *
 *  The YAFF: Yet Another File Format is a fake fileformat introduced only
 *  for the purpose of testing the streaming capabilites of ITK.
 *
 * \warning DO NOT USE THIS FILEFORMAT FOR ANY SERIOUS PURPOSE. 
 *
 *  \ingroup IOFilters
 */
class ITK_EXPORT YAFFImageIO : public ImageIOBase
{
public:
  /** Standard class typedefs. */
  typedef YAFFImageIO        Self;
  typedef ImageIOBase        Superclass;
  typedef SmartPointer<Self> Pointer;
  
  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  /** Run-time type information (and related methods). */
  itkTypeMacro(YAFFImageIO, ImageIOBase);

 /*-------- This part of the interfaces deals with reading data. ----- */

  /** Determine the file type. Returns true if this ImageIO can read the
   * file specified. */
  virtual bool CanReadFile(const char*);

  /** Set the spacing and dimension information for the set filename. */
  virtual void ReadImageInformation();
  
  /** Reads the data from disk into the memory buffer provided. */
  virtual void Read(void* buffer);

  /*-------- This part of the interfaces deals with writing data. ----- */

  /** Determine the file type. Returns true if this ImageIO can write the
   * file specified. */
  virtual bool CanWriteFile(const char*);

  /** Set the spacing and dimension information for the set filename. */
  virtual void WriteImageInformation();
  
  /** Writes the data to disk from the memory buffer provided. Make sure
   * that the IORegions has been set properly. */
  virtual void Write(const void* buffer);

/** Method for supporting streaming.  Given a requested region, determine what
 * could be the region that we can read from the file. This is called the
 * streamable region, which will be smaller than the LargestPossibleRegion and
 * greater or equal to the RequestedRegion */
  virtual ImageIORegion 
  CalculateStreamableReadRegionFromRequestedRegion( const ImageIORegion & requested ) const;


protected:
  YAFFImageIO();
  ~YAFFImageIO();
  void PrintSelf(std::ostream& os, Indent indent) const;
  
private:
  YAFFImageIO(const Self&); //purposely not implemented
  void operator=(const Self&); //purposely not implemented
  
  std::ifstream     m_InputStream;
  std::ofstream     m_OutputStream;
  std::string       m_RawDataFilename;

};

} // end namespace itk

#endif // __itkYAFFImageIO_h
-------------- next part --------------
A non-text attachment was scrubbed...
Name: itkYAFFImageIOFactory.cxx
Type: text/x-c++src
Size: 1745 bytes
Desc: not available
Url : http://www.itk.org/mailman/private/insight-developers/attachments/20071031/3904ed5b/itkYAFFImageIOFactory.cxx


More information about the Insight-developers mailing list