Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes

mitk::CompressedImageContainer Class Reference

Holds one (compressed) mitk::Image. More...

#include <mitkCompressedImageContainer.h>

Collaboration diagram for mitk::CompressedImageContainer:
Collaboration graph
[legend]

List of all members.

Public Types

typedef CompressedImageContainer Self
typedef Object Superclass
typedef itk::SmartPointer< SelfPointer
typedef itk::SmartPointer
< const Self
ConstPointer

Public Member Functions

virtual const char * GetClassName () const
void SetImage (Image *)
 Creates a compressed version of the image.
Image::Pointer GetImage ()
 Creates a full mitk::Image from its compressed version.

Static Public Member Functions

static Pointer New ()

Protected Member Functions

 CompressedImageContainer ()
virtual ~CompressedImageContainer ()

Protected Attributes

PixelType m_PixelType
unsigned int m_ImageDimension
std::vector< unsigned int > m_ImageDimensions
unsigned long m_OneTimeStepImageSizeInBytes
unsigned int m_NumberOfTimeSteps
std::vector< std::pair
< unsigned char *, unsigned
long > > 
m_ByteBuffers
 one for each timestep. first = pointer to compressed data; second = size of buffer in bytes
Geometry3D::Pointer m_ImageGeometry

Detailed Description

Holds one (compressed) mitk::Image.

Uses zlib to compress the data of an mitk::Image.

$Author$

Definition at line 41 of file mitkCompressedImageContainer.h.


Member Typedef Documentation

typedef itk::SmartPointer<const Self> mitk::CompressedImageContainer::ConstPointer

Definition at line 45 of file mitkCompressedImageContainer.h.

typedef itk::SmartPointer<Self> mitk::CompressedImageContainer::Pointer

Definition at line 45 of file mitkCompressedImageContainer.h.

Definition at line 45 of file mitkCompressedImageContainer.h.

Definition at line 45 of file mitkCompressedImageContainer.h.


Constructor & Destructor Documentation

mitk::CompressedImageContainer::CompressedImageContainer (  ) [protected]

Definition at line 25 of file mitkCompressedImageContainer.cpp.

{
}
mitk::CompressedImageContainer::~CompressedImageContainer (  ) [protected, virtual]

Definition at line 29 of file mitkCompressedImageContainer.cpp.

{
  for (std::vector< std::pair<unsigned char*, unsigned long> >::iterator iter = m_ByteBuffers.begin();
       iter != m_ByteBuffers.end();
       ++iter)
  {
    free( iter->first );
  }
}

Member Function Documentation

virtual const char* mitk::CompressedImageContainer::GetClassName (  ) const [virtual]
mitk::Image::Pointer mitk::CompressedImageContainer::GetImage ( void   )

Creates a full mitk::Image from its compressed version.

This Method hold no buffer, so the uncompression algorithm will be executed every time you call this method. Don't overdo it.

Definition at line 124 of file mitkCompressedImageContainer.cpp.

References MITK_ERROR, MITK_INFO, and mitk::Image::New().

Referenced by mitkCompressedImageContainerTestClass::Test().

{
  if (m_ByteBuffers.empty()) return NULL;

  // uncompress image data, create an Image
  Image::Pointer image = Image::New();
  unsigned int dims[20]; // more than 20 dimensions and bang
  for (unsigned int dim = 0; dim < m_ImageDimension; ++dim)
    dims[dim] = m_ImageDimensions[dim];
  
  image->Initialize( m_PixelType, m_ImageDimension, dims ); // this IS needed, right ?? But it does allocate memory -> does create one big lump of memory (also in windows)

  unsigned int timeStep(0);
  for (std::vector< std::pair<unsigned char*, unsigned long> >::iterator iter = m_ByteBuffers.begin();
       iter != m_ByteBuffers.end();
       ++iter, ++timeStep)
  {
    ::Bytef* dest( static_cast<unsigned char*>(image->GetVolumeData(timeStep)->GetData()) );
    ::uLongf destLen(m_OneTimeStepImageSizeInBytes);
    ::Bytef* source( iter->first );
    ::uLongf sourceLen( iter->second );
    int zlibRetVal = ::uncompress(dest, &destLen, source, sourceLen);
    if (itk::Object::GetDebug())
    {
      if (zlibRetVal == Z_OK)
      {
        MITK_INFO << "Success, destLen now " << destLen << " bytes" << std::endl;
      }
      else
      {
        switch ( zlibRetVal )
        {
          case Z_DATA_ERROR:
            MITK_ERROR << "compressed data corrupted" << std::endl;
            break;
          case Z_MEM_ERROR:
            MITK_ERROR << "not enough memory" << std::endl;
            break;
          case Z_BUF_ERROR:
            MITK_ERROR << "output buffer too small" << std::endl;
            break;
          default:
            MITK_ERROR << "other, unspecified error" << std::endl;
            break;
        }
      }
    }
  }

  image->SetGeometry( m_ImageGeometry );
  image->Modified();

  return image;
}
static Pointer mitk::CompressedImageContainer::New (  ) [static]
void mitk::CompressedImageContainer::SetImage ( Image image )

Creates a compressed version of the image.

Will not hold any further SmartPointers to the image.

Definition at line 39 of file mitkCompressedImageContainer.cpp.

References mitk::PixelType::GetBpe(), mitk::Image::GetDimension(), mitk::BaseData::GetGeometry(), mitk::Image::GetPixelType(), mitk::Image::GetVolumeData(), MITK_ERROR, and MITK_INFO.

Referenced by mitkCompressedImageContainerTestClass::Test().

{
  for (std::vector< std::pair<unsigned char*, unsigned long> >::iterator iter = m_ByteBuffers.begin();
       iter != m_ByteBuffers.end();
       ++iter)
  {
    free( iter->first );
  }

  m_ByteBuffers.clear();

  // Compress diff image using zlib (will be restored on demand)
  // determine memory size occupied by voxel data
  m_ImageDimension = image->GetDimension();
  m_ImageDimensions.clear();
  m_PixelType = image->GetPixelType();
  m_OneTimeStepImageSizeInBytes = m_PixelType.GetBpe() >> 3; // bits per element divided by 8
  for (unsigned int i = 0; i < m_ImageDimension; ++i)
  {
    unsigned int currentImageDimension = image->GetDimension(i);
    m_ImageDimensions.push_back( currentImageDimension );
    if (i < 3)
    {
      m_OneTimeStepImageSizeInBytes *= currentImageDimension; // only the 3D memory size
    }
  }

  m_ImageGeometry = image->GetGeometry();

  m_NumberOfTimeSteps = 1;
  if (m_ImageDimension > 3)
  {
    m_NumberOfTimeSteps = image->GetDimension(3);
  }

  for (unsigned int timestep = 0; timestep < m_NumberOfTimeSteps; ++timestep)
  {
    // allocate a buffer as specified by zlib
    unsigned long bufferSize = m_OneTimeStepImageSizeInBytes + static_cast<unsigned long>(m_OneTimeStepImageSizeInBytes * 0.2) + 12;
    unsigned char* byteBuffer = (unsigned char*) malloc(bufferSize);
    
    if (itk::Object::GetDebug())
    {
    // compress image here into a buffer
      MITK_INFO << "Using ZLib version: '" << zlibVersion() << "'" << std::endl
               << "Attempting to compress " << m_OneTimeStepImageSizeInBytes << " image bytes into a buffer of size " << bufferSize << std::endl;
    }

    ::Bytef* dest(byteBuffer);
    ::uLongf destLen(bufferSize);
    ::Bytef* source( static_cast<unsigned char*>(image->GetVolumeData(timestep)->GetData()) );
    ::uLongf sourceLen( m_OneTimeStepImageSizeInBytes );
    int zlibRetVal = ::compress(dest, &destLen, source, sourceLen);
    if (itk::Object::GetDebug())
    {
      if (zlibRetVal == Z_OK)
      {
        MITK_INFO << "Success, using " << destLen << " bytes of the buffer (ratio " << ((double)destLen / (double)sourceLen) << ")" << std::endl;
      }
      else
      {
        switch ( zlibRetVal )
        {
          case Z_MEM_ERROR:
            MITK_ERROR << "not enough memory" << std::endl;
            break;
          case Z_BUF_ERROR:
            MITK_ERROR << "output buffer too small" << std::endl;
            break;
          default:
            MITK_ERROR << "other, unspecified error" << std::endl;
            break;
        }
      }
    }

    // only use the neccessary amount of memory, realloc the buffer!
    byteBuffer = (unsigned char*) realloc( byteBuffer, destLen );
    bufferSize = destLen;
    //MITK_INFO << "Using " << bufferSize << " bytes to store compressed image (" << destLen << " needed)" << std::endl;
  
    m_ByteBuffers.push_back( std::pair<unsigned char*, unsigned long>( byteBuffer, bufferSize ) );
  }
}

Member Data Documentation

std::vector< std::pair<unsigned char*, unsigned long> > mitk::CompressedImageContainer::m_ByteBuffers [protected]

one for each timestep. first = pointer to compressed data; second = size of buffer in bytes

Definition at line 80 of file mitkCompressedImageContainer.h.

Definition at line 72 of file mitkCompressedImageContainer.h.

std::vector<unsigned int> mitk::CompressedImageContainer::m_ImageDimensions [protected]

Definition at line 73 of file mitkCompressedImageContainer.h.

Definition at line 82 of file mitkCompressedImageContainer.h.

Definition at line 77 of file mitkCompressedImageContainer.h.

Definition at line 75 of file mitkCompressedImageContainer.h.

Definition at line 70 of file mitkCompressedImageContainer.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines