void mitk::CylindricToCartesianFilter::buildConeCutOffShortCut(int orig_xsize, int orig_ysize, mitkIpPicDescriptor *rt_pic, mitkIpPicDescriptor *fr_pic, float a, float b, mitkIpPicDescriptor * &coneCutOff_pic)
{
  coneCutOff_pic=mitkIpPicNew();
  coneCutOff_pic->type=mitkIpPicInt;
  coneCutOff_pic->bpe=16;
  coneCutOff_pic->dim=2;
  coneCutOff_pic->n[0]=coneCutOff_pic->n[1]=rt_pic->n[0];
  coneCutOff_pic->data=malloc(_mitkIpPicSize(coneCutOff_pic));

  int i, size=_mitkIpPicElements(rt_pic);
  mitkIpInt2_t *rt, *ccop, ohx_size, nz_size;
  mitkIpFloat4_t *fr;

  a*=(float)rt_pic->n[0]/orig_xsize;
  b*=(float)rt_pic->n[0]/orig_xsize;

  ohx_size=orig_xsize/2;
  nz_size=orig_ysize*rt_pic->n[0]/orig_xsize;

  rt=(mitkIpInt2_t *)rt_pic->data; fr=(mitkIpFloat4_t*)fr_pic->data; ccop=(mitkIpInt2_t *)coneCutOff_pic->data;

  for(i=0; i<size; ++i, ++rt, ++ccop)
  {
    register mitkIpInt2_t cco;
    if(*rt<=ohx_size)
      cco=(mitkIpInt2_t)(a*(*rt+*fr)+b);
    else
      cco=(mitkIpInt2_t)(a*(orig_xsize-(*rt+*fr))+b);
    if(cco<0)
      cco=0;
    if(cco>=nz_size) 
      cco=nz_size;
    *ccop=cco;
  }
}
Ejemplo n.º 2
0
mitkIpPicDescriptor *mitkIpFuncRank ( mitkIpPicDescriptor *pic_old,
                              mitkIpUInt4_t       rank,
                              mitkIpUInt4_t       mask_dim,
                              mitkIpUInt4_t       mask_size,
                              mitkIpFuncFlagI_t   border )
{
  
  mitkIpPicDescriptor *pic_new;            /* pointer to transformed image            */
  mitkIpInt4_t       i;                    /* loop index                              */
  mitkIpInt4_t       offset;               /* offset of image                         */
  mitkIpInt4_t       ind[_mitkIpPicNDIM];      /* loop index vector                       */
  mitkIpInt4_t       *off_vekt;            /* pointer to offset vector                */
  mitkIpInt4_t       begin;                /* 0.5 * mask_size                         */
  mitkIpUInt4_t      size[_mitkIpPicNDIM];     /*                                         */
  mitkIpInt4_t       n[_mitkIpPicNDIM];        /* size of each dimension                  */
  mitkIpUInt4_t      no_elem;              /* number of elements in mask              */
  mitkIpUInt4_t      len;                  /* length of offset vector                 */

  /* calculate number of elements in mask                                         */

  no_elem = mask_size;
  for ( i = 1; i < mask_dim; i++ )
    no_elem = no_elem * mask_size;

  /* check whether data are correct                                               */

  if ( _mitkIpFuncError ( pic_old ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );
  if ( mask_dim < 1 || mask_dim > pic_old->dim )  
    { 
       _mitkIpFuncSetErrno ( mitkIpFuncDIMMASC_ERROR );
       return ( mitkIpFuncERROR );
    }
  if ( rank > no_elem ) 
    { 
       _mitkIpFuncSetErrno ( mitkIpFuncDATA_ERROR );
       return ( mitkIpFuncERROR );
    }
  if ( mask_size % 2 != 1 ) 
    { 
       _mitkIpFuncSetErrno ( mitkIpFuncSIZE_ERROR );
       return ( mitkIpFuncERROR );
    }

  /* initialize vectors and variables                                             */

  size[0] = 1;
  for ( i = 1; i < _mitkIpPicNDIM; i++ )
    size[i] = size[i-1] * pic_old->n[i-1];

  len = 0;
  begin = mask_size / 2;

  for ( i = 0; i < mask_dim; i++ )
    n[i] = begin + 1;
  for ( i = mask_dim; i < _mitkIpPicNDIM; i++ )
    n[i] = 1 - begin;
  
  /* allocate image structure                                                     */

  if ( border == mitkIpFuncBorderOld )
    pic_new = mitkIpPicClone ( pic_old );
  else if ( border == mitkIpFuncBorderZero )
    {
       pic_new = mitkIpPicCopyHeader ( pic_old, 0 );
       pic_new->data = calloc ( _mitkIpPicElements ( pic_new ), pic_new->bpe/8  );
    }
  else 
    {
       _mitkIpFuncSetErrno ( mitkIpFuncFLAG_ERROR );
       return ( mitkIpFuncERROR );
    }

  if ( pic_new == NULL )
    {
       _mitkIpFuncSetErrno ( mitkIpFuncPICNEW_ERROR );
       return ( mitkIpFuncERROR );
    }

  /* allocate offset vector                                                       */

  off_vekt = malloc ( no_elem * sizeof ( mitkIpUInt4_t ) );
  if ( off_vekt == NULL )
    {
       _mitkIpFuncSetErrno ( mitkIpFuncMALLOC_ERROR );
       return ( mitkIpFuncERROR );
    }

  /* calculate offset vector                                                      */

  for ( ind[0] = -begin; ind[0] < n[0]; ind[0]++ )
    for ( ind[7] = -begin; ind[7] < n[7]; ind[7]++ )
      for ( ind[6] = -begin; ind[6] < n[6]; ind[6]++ )
        for ( ind[5] = -begin; ind[5] < n[5]; ind[5]++ )
          for ( ind[4] = -begin; ind[4] < n[4]; ind[4]++ )
            for ( ind[3] = -begin; ind[3] < n[3]; ind[3]++ )
              for ( ind[2] = -begin; ind[2] < n[2]; ind[2]++ )
                for ( ind[1] = -begin; ind[1] < n[1]; ind[1]++ )
                  { 
                     offset = 0;
                     for ( i = 0; i < pic_old->dim; i++ )
                       offset = offset + ind[i] * size[i]; 
                     off_vekt[len] = offset;
                     len++;
                  }
  if ( rank == 0 ) rank = no_elem / 2 + 1;
  mitkIpPicFORALL_4 ( RANK, pic_old, begin, no_elem, size, rank );
      
  free ( off_vekt );

  /* Copy Tags */

  mitkIpFuncCopyTags(pic_new, pic_old);
  
  
                        
  return ( pic_new );
}
Ejemplo n.º 3
0
void mitkIpPicPutSlice( const char *outfile_name, mitkIpPicDescriptor *pic, mitkIpUInt4_t slice )
{
  mitkIpPicDescriptor *pic_in;

  FILE *outfile;
  
  size_t ignored;

  pic_in = mitkIpPicGetHeader( outfile_name,
                           NULL );

  if( pic_in == NULL )
    {
      if( slice == 1 )
        {
          mitkIpBool_t compression;

          pic->n[pic->dim] = 1;
          pic->dim += 1;

          compression = mitkIpPicSetWriteCompression( mitkIpFalse );
          mitkIpPicPut( outfile_name, pic );
          mitkIpPicSetWriteCompression( compression );

          pic->dim -= 1;
          pic->n[pic->dim] = 0;

          return;
        }
      else
        return;
    }

  pic_in = mitkIpPicGetTags( outfile_name,
                         pic_in );

  outfile = fopen( outfile_name, "r+b" );

  if( outfile == NULL )
    {
      /*ipPrintErr( "mitkIpPicPut: sorry, error opening outfile\n" );*/
      /*return();*/
    }

  if( pic->dim != pic_in->dim - 1 )
    {
      fclose( outfile );
      return;
    }
  else if( pic->n[0] != pic_in->n[0]  )
    {
      fclose( outfile );
      return;
    }
  else if( pic->n[1] != pic_in->n[1]  )
    {
      fclose( outfile );
      return;
    }

  if( slice > pic_in->n[pic_in->dim-1] )
    pic_in->n[pic_in->dim-1] += 1;

  /* write outfile */
  /*fseek( outfile, 0, SEEK_SET );*/
  rewind( outfile );
  ignored = fwrite( mitkIpPicVERSION, 1, sizeof(mitkIpPicTag_t), outfile );

  fseek( outfile, sizeof(mitkIpUInt4_t), SEEK_CUR ); /* skip tags_len */

  ignored = mitkIpFWriteLE( &(pic_in->type), sizeof(mitkIpUInt4_t), 1, outfile );
  ignored = mitkIpFWriteLE( &(pic_in->bpe), sizeof(mitkIpUInt4_t), 1, outfile );
  ignored = mitkIpFWriteLE( &(pic_in->dim), sizeof(mitkIpUInt4_t), 1, outfile );

  ignored = mitkIpFWriteLE( pic_in->n, sizeof(mitkIpUInt4_t), pic_in->dim, outfile );

  fseek( outfile, pic_in->info->pixel_start_in_file + _mitkIpPicSize(pic) * (slice - 1), SEEK_SET );

  ignored = mitkIpFWriteLE( pic->data, pic->bpe / 8, _mitkIpPicElements(pic), outfile );

  /*fseek( outfile, 0, SEEK_END );*/

  fclose( outfile );

  mitkIpPicFree(pic_in);

  /*return();*/
}
Ejemplo n.º 4
0
void mitk::ImageDataItem::ConstructVtkImageData() const
{
  vtkImageData *inData = vtkImageData::New();
  vtkDataArray *scalars = NULL;

  unsigned long size = 0;
  if ( m_PicDescriptor->dim == 1 )
  {
    inData->SetDimensions( m_PicDescriptor->n[0] -1, 1, 1);
    size = m_PicDescriptor->n[0];
    inData->SetOrigin( ((float) m_PicDescriptor->n[0]) / 2.0f, 0, 0 );
  }
  else
  if ( m_PicDescriptor->dim == 2 )
  {
    inData->SetDimensions( m_PicDescriptor->n[0] , m_PicDescriptor->n[1] , 1 );
    size = m_PicDescriptor->n[0] * m_PicDescriptor->n[1];
    inData->SetOrigin( ((float) m_PicDescriptor->n[0]) / 2.0f, ((float) m_PicDescriptor->n[1]) / 2.0f, 0 );
  }
  else
  if ( m_PicDescriptor->dim >= 3 )
  {
    inData->SetDimensions( m_PicDescriptor->n[0], m_PicDescriptor->n[1], m_PicDescriptor->n[2] );
    size = m_PicDescriptor->n[0] * m_PicDescriptor->n[1] * m_PicDescriptor->n[2];
    // Test
    //inData->SetOrigin( (float) m_PicDescriptor->n[0] / 2.0f, (float) m_PicDescriptor->n[1] / 2.0f, (float) m_PicDescriptor->n[2] / 2.0f );
    inData->SetOrigin( 0, 0, 0 );
  }
  else
  {
    inData->Delete () ;
    return;
  }

  inData->SetNumberOfScalarComponents(m_PixelType.GetNumberOfComponents());

  if ( ( m_PixelType.GetType() == mitkIpPicInt || m_PixelType.GetType() == mitkIpPicUInt ) && m_PixelType.GetBitsPerComponent() == 1 )
  {
    inData->SetScalarType( VTK_BIT );
    scalars = vtkBitArray::New();
  }
  else if ( m_PixelType.GetType() == mitkIpPicInt && m_PixelType.GetBitsPerComponent() == 8 )
  {
    inData->SetScalarType( VTK_CHAR );
    scalars = vtkCharArray::New();
  }
  else if ( m_PixelType.GetType() == mitkIpPicUInt && m_PixelType.GetBitsPerComponent() == 8 )
  {
    inData->SetScalarType( VTK_UNSIGNED_CHAR );
    scalars = vtkUnsignedCharArray::New();
  }
  else if ( m_PixelType.GetType() == mitkIpPicInt && m_PixelType.GetBitsPerComponent() == 16 )
  {
    inData->SetScalarType( VTK_SHORT );
    scalars = vtkShortArray::New();
  }
  else if ( m_PixelType.GetType() == mitkIpPicUInt && m_PixelType.GetBitsPerComponent() == 16 )
  {
    inData->SetScalarType( VTK_UNSIGNED_SHORT );
    scalars = vtkUnsignedShortArray::New();
  }
  else if ( m_PixelType.GetType() == mitkIpPicInt && m_PixelType.GetBitsPerComponent() == 32 )
  {
    inData->SetScalarType( VTK_INT );
    scalars = vtkIntArray::New();
  }
  else if ( m_PixelType.GetType() == mitkIpPicUInt && m_PixelType.GetBitsPerComponent() == 32 )
  {
    inData->SetScalarType( VTK_UNSIGNED_INT );
    scalars = vtkUnsignedIntArray::New();
  }
  else if ( m_PixelType.GetType() == mitkIpPicInt && m_PixelType.GetBitsPerComponent() == 64 )
  {
    inData->SetScalarType( VTK_LONG );
    scalars = vtkLongArray::New();
  }
  else if ( m_PixelType.GetType() == mitkIpPicUInt && m_PixelType.GetBitsPerComponent() == 64 )
  {
    inData->SetScalarType( VTK_UNSIGNED_LONG );
    scalars = vtkUnsignedLongArray::New();
  }
  else if ( m_PixelType.GetType() == mitkIpPicFloat && m_PixelType.GetBitsPerComponent() == 32 )
  {
    inData->SetScalarType( VTK_FLOAT );
    scalars = vtkFloatArray::New();
  }
  else if ( m_PixelType.GetType() == mitkIpPicFloat && m_PixelType.GetBitsPerComponent() == 64 )
  {
    inData->SetScalarType( VTK_DOUBLE );
    scalars = vtkDoubleArray::New();
  }
  else
  {
    inData->Delete();
    return;
  }

  m_VtkImageData = inData;

  // allocate the new scalars
  scalars->SetNumberOfComponents(m_VtkImageData->GetNumberOfScalarComponents());

  scalars->SetVoidArray(m_PicDescriptor->data, _mitkIpPicElements(m_PicDescriptor)*m_VtkImageData->GetNumberOfScalarComponents(), 1);

  m_VtkImageData->GetPointData()->SetScalars(scalars);
  scalars->Delete();

}
Ejemplo n.º 5
0
mitkIpPicDescriptor *
MITKipPicGet( char *infile_name, mitkIpPicDescriptor *pic )
{
  mitkIpPicFile_t infile;

  mitkIpPicTag_t tag_name;
  mitkIpUInt4_t len;

  mitkIpUInt4_t to_read;
  size_t size;

  size_t number_of_elements;
  size_t bytes_per_element;
  size_t number_of_bytes;
  size_t block_size;
  size_t number_of_blocks;
  size_t remaining_bytes;
  size_t bytes_read;
  size_t block_nr;

  mitkIpUInt1_t* data;

  size_t ignored;

  infile = _mitkIpPicOpenPicFileIn( infile_name );

  if( !infile )
    {
      /*ipPrintErr( "mitkIpPicGet: sorry, error opening infile\n" );*/
      return( NULL );
    }

  /* read infile */
  ignored = mitkIpPicFRead( tag_name, 1, 4, infile );

  if( strncmp( "\037\213", tag_name, 2 ) == 0 )
    {
      fprintf( stderr, "mitkIpPicGetHeader: sorry, can't read compressed file\n" );
      return( NULL );
    }
  else if( strncmp( mitkIpPicVERSION, tag_name, 4 ) != 0 )
    {
#ifndef CHILIPLUGIN
      if( pic == NULL )
        pic = _MITKipPicOldGet( infile,
                            NULL );
      else
        _MITKipPicOldGet( infile,
                      pic );
      if( infile != stdin )
        mitkIpPicFClose( infile );
#else
      return NULL;
#endif
      return( pic );
    }

  size = 0;
  if( pic == NULL )
    pic = mitkIpPicNew();
  else
  {
    size= _mitkIpPicSize(pic);
    if(pic->data == NULL)
      size = 0;
  }

  if( pic->info != NULL )
  {
    _mitkIpPicFreeTags( pic->info->tags_head );
    pic->info->tags_head = NULL;
  }

  ignored = mitkIpPicFRead( &(tag_name[4]), 1, sizeof(mitkIpPicTag_t)-4, infile );
  strncpy( pic->info->version, tag_name, _mitkIpPicTAGLEN );

  ignored = mitkIpPicFReadLE( &len, sizeof(mitkIpUInt4_t), 1, infile );

  ignored = mitkIpPicFReadLE( &(pic->type), sizeof(mitkIpUInt4_t), 1, infile );
  ignored = mitkIpPicFReadLE( &(pic->bpe), sizeof(mitkIpUInt4_t), 1, infile );
  ignored = mitkIpPicFReadLE( &(pic->dim), sizeof(mitkIpUInt4_t), 1, infile );

  ignored = mitkIpPicFReadLE( &(pic->n), sizeof(mitkIpUInt4_t), pic->dim, infile );

  (void *)ignored;

  to_read = len -        3 * sizeof(mitkIpUInt4_t)
                - pic->dim * sizeof(mitkIpUInt4_t);
#if 0
  mitkIpPicFSeek( infile, to_read, SEEK_CUR );
#else
  pic->info->tags_head = _MITKipPicReadTags( pic->info->tags_head, to_read, infile, mitkIpPicEncryptionType(pic) );
#endif

  pic->info->write_protect = mitkIpFalse;

  if((size == 0) || (size != _mitkIpPicSize(pic)))
    {
      if( pic->data != NULL )
        {
          free( pic->data );
          pic->data = NULL;
        }
#ifdef WIN
      if ((pic->hdata = GlobalAlloc( GMEM_MOVEABLE, _mitkIpPicSize(pic) )) != 0)
        pic->data = GlobalLock( pic->hdata );
#else
      pic->data = malloc( _mitkIpPicSize(pic) );
#endif
    }

  pic->info->pixel_start_in_file = mitkIpPicFTell( infile );
  /*
   * data is read in blocks of size 'block_size' to prevent from
   * errors due to large file sizes (>=2GB)
   */
  number_of_elements = _mitkIpPicElements(pic);
  bytes_per_element = pic->bpe / 8;
  number_of_bytes = number_of_elements * bytes_per_element;
  block_size = 1024*1024; /* Use 1 MB blocks. Make sure that block size is smaller than 2^31 */
  number_of_blocks = number_of_bytes / block_size;
  remaining_bytes = number_of_bytes % block_size;
  bytes_read = 0;
  block_nr = 0;
  /*printf( "mitkIpPicGet: number of blocks to read is %ul.\n", number_of_blocks ); */

  data = (mitkIpUInt1_t*) pic->data;
  if( pic->type == mitkIpPicNonUniform )
    {
      for ( block_nr = 0 ; block_nr < number_of_blocks ; ++block_nr )
        bytes_read += mitkIpPicFRead( data + ( block_nr * block_size ), 1, (unsigned int) block_size, infile );
      bytes_read += mitkIpPicFRead( data + ( number_of_blocks * block_size ), 1, (unsigned int) remaining_bytes, infile );
    }
  else
    {
      for ( block_nr = 0 ; block_nr < number_of_blocks ; ++block_nr )
        bytes_read += mitkIpPicFReadLE( data + ( block_nr * block_size ), 1, (unsigned int) block_size, infile );
      bytes_read += mitkIpPicFReadLE( data + ( number_of_blocks * block_size ), 1, (unsigned int) remaining_bytes, infile );
    }

  if ( bytes_read != number_of_bytes )
  {
    fprintf( stderr, "Error while reading, only %lu bytes were read! Eof indicator is %u.\n", (long unsigned int)bytes_read, mitkIpPicFEOF(infile) );
#ifndef USE_ZLIB
    fprintf( stderr, "(ferror indicates %u).\n", ferror(infile));
#endif
  }

  if( infile != stdin )
    mitkIpPicFClose( infile );

#ifdef WIN
  GlobalUnlock( pic->hdata );
#endif

  return( pic );
}
Ejemplo n.º 6
0
mitkIpPicDescriptor *
mitkIpPicGet( const char *infile_name, mitkIpPicDescriptor *pic )
{
  mitkIpPicFile_t infile;

  mitkIpPicTag_t tag_name;
  mitkIpUInt4_t len;

  mitkIpUInt4_t to_read;

  infile = _mitkIpPicOpenPicFileIn( infile_name );

  if( !infile )
    {
      /*ipPrintErr( "mitkIpPicGet: sorry, error opening infile\n" );*/
      return( NULL );
    }

  /* read infile */
  mitkIpPicFRead( tag_name, 1, 4, infile );

  if( strncmp( "\037\213", tag_name, 2 ) == 0 )
    {
      fprintf( stderr, "mitkIpPicGetHeader: sorry, can't read compressed file\n" );
      return( NULL );
    }
  else if( strncmp( mitkIpPicVERSION, tag_name, 4 ) != 0 )
    {
      if( pic == NULL )
        pic = _mitkIpPicOldGet( infile,
                            NULL );
      else
        _mitkIpPicOldGet( infile,
                      pic );
      if( infile != stdin )
        mitkIpPicFClose( infile );
      return( pic );
    }

  if( pic == NULL )
    pic = mitkIpPicNew();

  mitkIpPicClear( pic );

  mitkIpPicFRead( &(tag_name[4]), 1, sizeof(mitkIpPicTag_t)-4, infile );
  strncpy( pic->info->version, tag_name, _mitkIpPicTAGLEN );

  mitkIpPicFReadLE( &len, sizeof(mitkIpUInt4_t), 1, infile );

  mitkIpPicFReadLE( &(pic->type), sizeof(mitkIpUInt4_t), 1, infile );
  mitkIpPicFReadLE( &(pic->bpe), sizeof(mitkIpUInt4_t), 1, infile );
  mitkIpPicFReadLE( &(pic->dim), sizeof(mitkIpUInt4_t), 1, infile );

  mitkIpPicFReadLE( &(pic->n), sizeof(mitkIpUInt4_t), pic->dim, infile );


  to_read = len -        3 * sizeof(mitkIpUInt4_t)
                - pic->dim * sizeof(mitkIpUInt4_t);
#if 0
  mitkIpPicFSeek( infile, to_read, SEEK_CUR );
#else
  pic->info->tags_head = _mitkIpPicReadTags( pic->info->tags_head, to_read, infile, mitkIpPicEncryptionType(pic) );
#endif

  pic->info->write_protect = mitkIpFalse;

#ifdef WIN
  if ((pic->hdata = GlobalAlloc( GMEM_MOVEABLE, _mitkIpPicSize(pic) )) != 0)
    pic->data = GlobalLock( pic->hdata );
#else
  pic->data = malloc( _mitkIpPicSize(pic) );
#endif

  pic->info->pixel_start_in_file = mitkIpPicFTell( infile );
  if( pic->type == mitkIpPicNonUniform )
    mitkIpPicFRead( pic->data, pic->bpe / 8, _mitkIpPicElements(pic), infile );
  else
    mitkIpPicFReadLE( pic->data, pic->bpe / 8, _mitkIpPicElements(pic), infile );

  if( infile != stdin )
    mitkIpPicFClose( infile );

#ifdef WIN
  GlobalUnlock( pic->hdata );
#endif

  return( pic );
}
Ejemplo n.º 7
0
mitkIpPicDescriptor *mitkIpPicGetSlice( const char *infile_name, mitkIpPicDescriptor *pic, mitkIpUInt4_t slice )
{
  mitkIpPicFile_t infile;

  mitkIpPicTag_t tag_name;
  mitkIpUInt4_t len;
  mitkIpUInt4_t skip;

  int number = 1;

  unsigned long int picsize;

  if( infile_name == NULL )
    infile = stdin;
  else if( strcmp(infile_name, "stdin") == 0 )
    infile = stdin;
  else
    infile = _mitkIpPicOpenPicFileIn( infile_name );

  if( infile == NULL )
    {
      /*ipPrintErr( "mitkIpPicGetSlice: sorry, error opening infile\n" );*/
      return( NULL );
    }

  /* read infile */
  mitkIpPicFRead( &(tag_name[0]), 1, 4, infile );

  if( strncmp( mitkIpPicVERSION, tag_name, 4 ) != 0 )
    {
      if( pic == NULL )
        pic = _mitkIpPicOldGetSlice( infile,
                                 NULL,
                                 slice );
      else
        _mitkIpPicOldGetSlice( infile,
                           pic,
                           slice );
      if( infile != stdin )
        mitkIpPicFClose( infile );
      return( pic );
    }


  if( (void*)pic == (void*)3 )
    {
      pic = NULL;
      number = 3;
    }

  if( pic == NULL )
    pic = mitkIpPicNew();

  mitkIpPicClear( pic );

  mitkIpPicFRead( &(tag_name[4]), 1, sizeof(mitkIpPicTag_t)-4, infile );
  strncpy( pic->info->version, tag_name, _mitkIpPicTAGLEN );

  mitkIpPicFReadLE( &len, sizeof(mitkIpUInt4_t), 1, infile );

  mitkIpPicFReadLE( &(pic->type), sizeof(mitkIpUInt4_t), 1, infile );
  mitkIpPicFReadLE( &(pic->bpe), sizeof(mitkIpUInt4_t), 1, infile );
  mitkIpPicFReadLE( &(pic->dim), sizeof(mitkIpUInt4_t), 1, infile );

  mitkIpPicFReadLE( &(pic->n), sizeof(mitkIpUInt4_t), pic->dim, infile );

  skip = len -        3 * sizeof(mitkIpUInt4_t)
             - pic->dim * sizeof(mitkIpUInt4_t);
  mitkIpPicFSeek( infile, skip, SEEK_CUR );

  picsize = _mitkIpPicSize(pic);

  pic->dim = 2;

  if( slice < 1
      || slice > picsize / _mitkIpPicSize(pic) )
    {
      mitkIpPicClear( pic );
      return( pic );
    }

  if( number < 1 )
    number = 1;

  if( slice + number - 1 > pic->n[2] )
    number = pic->n[2] - slice + 1;


  pic->info->write_protect = mitkIpTrue;

  picsize = _mitkIpPicSize(pic);

  mitkIpPicFSeek( infile, picsize * (slice - 1), SEEK_CUR );

  if( number > 1 )
    {
      pic->dim = 3;
      pic->n[2] = number;
    }

  picsize = _mitkIpPicSize(pic);

  pic->data = malloc( picsize );

  mitkIpPicFReadLE( pic->data, pic->bpe / 8, _mitkIpPicElements(pic), infile );

  if( infile != stdin )
    mitkIpPicFClose( infile );

  return( pic );
}
Ejemplo n.º 8
0
mitkIpPicDescriptor *mitkIpFuncGausF   ( mitkIpPicDescriptor *pic_old,
                                 mitkIpUInt4_t       len_mask,
                                 mitkIpUInt4_t       dim_mask,  
                                 mitkIpFuncFlagI_t   border )
{
  mitkIpPicDescriptor *pic_new;          /* pointer to new image structure   */
  mitkIpPicDescriptor *pic_mask;         /* pointer to mask                  */
  mitkIpUInt4_t       n[_mitkIpPicNDIM];     /* size of each dimension           */
  mitkIpUInt4_t       ind[_mitkIpPicNDIM];   /* loop index vector                */
  mitkIpUInt4_t       i, k;              /* loop index                       */
  mitkIpUInt4_t       no_elem;           /* number of mask elements          */
  mitkIpUInt4_t       offset;            /* offset of pixels                 */
  mitkIpUInt4_t       element;           /* used to calculate mask elements  */
  mitkIpUInt4_t       sum;               /* sum of all mask elements         */
  mitkIpUInt4_t       nn, nfac, kfac;    /* used to calculate bin. coeff     */
  mitkIpUInt4_t       *bin;              /* binomial coeffizients            */

  /* check data                                                          */

  if ( _mitkIpFuncError ( pic_old ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );
  if ( pic_old->dim < dim_mask || dim_mask < 1 )
    {
       _mitkIpFuncSetErrno ( mitkIpFuncDIMMASC_ERROR );
       return ( mitkIpFuncERROR );
    }
  if ( len_mask % 2 != 1 )
    {
       _mitkIpFuncSetErrno ( mitkIpFuncDIM_ERROR );
       return ( mitkIpFuncERROR );
    }

  /* calculate binomial coefficient                                      */

  bin       = malloc ( len_mask * sizeof ( mitkIpUInt4_t ) );
  if ( bin == NULL ) 
    {
       _mitkIpFuncSetErrno ( mitkIpFuncMALLOC_ERROR );
       return ( mitkIpFuncERROR );
    }

  nn        = len_mask;
  bin[0]    = 1;
  bin[nn-1] = 1;
  nfac      = 1;
  kfac      = 1;

  for ( k = 1; k < nn-1; k++ )
    {
       kfac   = k * kfac;
       nfac   = nfac * ( nn - k );
       bin[k] = nfac / kfac;
    }

  /* initialize mask                                                     */
  
  pic_mask       = mitkIpPicNew();
  
  if ( pic_mask == NULL )
    {
       free ( bin );
       _mitkIpFuncSetErrno ( mitkIpFuncPICNEW_ERROR );
       return ( mitkIpFuncERROR );
    }

  pic_mask->type = mitkIpPicFloat;
  pic_mask->bpe  = 64;
  pic_mask->dim  = dim_mask;

  for ( i = 0; i < dim_mask; i++ ) pic_mask->n[i] = len_mask;

  pic_mask->data = malloc ( _mitkIpPicSize ( pic_mask ) );

  if ( pic_mask->data == NULL )
    {
       free ( bin );
       mitkIpPicFree ( pic_mask );
       _mitkIpFuncSetErrno ( mitkIpFuncPICNEW_ERROR );
       return ( mitkIpFuncERROR );
    }

  /* initialize vectors                                                  */

  for ( i = 0; i < pic_mask->dim; i++ )
    n[i] = len_mask;
  for ( i = pic_mask->dim; i < _mitkIpPicNDIM; i++ )
    n[i] = 1;

  /* calculate mask                                                      */

  offset = 0;
  sum    = 0;
  for ( ind[7] = 0; ind[7] < n[7]; ind[7]++ )
    for ( ind[6] = 0; ind[6] < n[6]; ind[6]++ )
      for ( ind[5] = 0; ind[5] < n[5]; ind[5]++ )
        for ( ind[4] = 0; ind[4] < n[4]; ind[4]++ )
          for ( ind[3] = 0; ind[3] < n[3]; ind[3]++ )
            for ( ind[2] = 0; ind[2] < n[2]; ind[2]++ )
              for ( ind[1] = 0; ind[1] < n[1]; ind[1]++ )
                for ( ind[0] = 0; ind[0] < n[0]; ind[0]++ )
                  {
                    element = 1;
                    for ( i = 0; i < pic_mask->dim; i++ )
                      element = element * bin[ind[i]];

                    (( mitkIpFloat8_t * )pic_mask->data)[offset] = 
                       ( mitkIpFloat8_t ) element;
                    sum = sum + element;
                    offset++;
                  }

  no_elem = _mitkIpPicElements ( pic_mask );
  for ( i = 0; i < no_elem; i++ )                          
    (( mitkIpFloat8_t * ) pic_mask->data ) [i] =  
       (( mitkIpFloat8_t * ) pic_mask->data ) [i] / ( mitkIpFloat8_t ) sum;

  /* convolve image with Gausian mask                                  */

  pic_new = mitkIpFuncConv ( pic_old, pic_mask, border );

  mitkIpPicFree ( pic_mask );
  free ( bin );
  /* Copy Tags */

  mitkIpFuncCopyTags(pic_new, pic_old);
  
  

  return ( pic_new );
}