Exemplo n.º 1
0
mitkIpPicDescriptor *_mitkIpPicCopySlice( mitkIpPicDescriptor *pic, mitkIpPicDescriptor *pic_in, mitkIpUInt4_t slice )
{
  mitkIpUInt4_t picsize;

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

  mitkIpPicClear( pic );

  pic->type = pic_in->type;
  pic->bpe = pic_in->bpe;
  pic->dim = 2;
  pic->n[0] = pic_in->n[0];
  pic->n[1] = pic_in->n[1];
  pic->data = NULL;

  picsize = _mitkIpPicSize(pic);

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

  pic->data = malloc( picsize );

  memmove( pic->data,
	  &((char *)pic_in->data)[(slice-1) * picsize],
	  picsize );

  return( pic );
}
Exemplo n.º 2
0
mitk::ImageDataItem::ImageDataItem(const ImageDataItem& aParent, unsigned int dimension, void *data, bool manageMemory, size_t offset) :
  m_Data(NULL), m_ManageMemory(false), m_PicDescriptor(NULL), m_VtkImageData(NULL), m_Offset(offset), m_IsComplete(false), m_Size(0),
  m_Parent(&aParent)
{
  m_PixelType = aParent.GetPixelType();
  m_PicDescriptor=mitkIpPicNew();
  m_PicDescriptor->bpe=m_PixelType.GetBpe();
  m_PicDescriptor->type=m_PixelType.GetType();
  m_PicDescriptor->dim=dimension;
  memcpy(m_PicDescriptor->n, aParent.GetPicDescriptor()->n, sizeof(mitkIpUInt4_t)*_mitkIpPicNDIM);
  m_PicDescriptor->data=m_Data=static_cast<unsigned char*>(aParent.GetData())+offset;
  mitkIpFuncCopyTags(m_PicDescriptor, aParent.GetPicDescriptor());

  m_Size = _mitkIpPicSize(m_PicDescriptor);
  if(data != NULL)
  {
    memcpy(m_Data, data, m_Size);
    if(manageMemory)
    {
      delete [] (unsigned char*) data;
    }
  }

  m_ReferenceCountLock.Lock();
  m_ReferenceCount = 0;
  m_ReferenceCountLock.Unlock();
}
Exemplo n.º 3
0
mitk::ImageDataItem::ImageDataItem(const mitk::PixelType& type, unsigned int dimension, unsigned int *dimensions, void *data, bool manageMemory) :
  m_Data((unsigned char*)data), m_ManageMemory(manageMemory), m_PicDescriptor(NULL), m_VtkImageData(NULL), m_Offset(0), m_IsComplete(false), m_Size(0),
  m_Parent(NULL)
{
  //const std::type_info & typeId=*type.GetTypeId();
  m_PixelType = type;
  m_PicDescriptor=mitkIpPicNew();
  m_PicDescriptor->bpe=m_PixelType.GetBpe();
  m_PicDescriptor->type=m_PixelType.GetType();
  m_PicDescriptor->dim=dimension;
  memcpy(m_PicDescriptor->n, dimensions, sizeof(mitkIpUInt4_t)*(dimension<=_mitkIpPicNDIM?dimension:_mitkIpPicNDIM));
  unsigned char i;
  for(i=dimension; i < _mitkIpPicNDIM; ++i)
    m_PicDescriptor->n[i] = 1;
  m_Size = _mitkIpPicSize(m_PicDescriptor);
  if(m_Data == NULL)
  {
    m_Data = mitk::MemoryUtilities::AllocateElements<unsigned char>( m_Size );
    m_ManageMemory = true;
  }
  m_PicDescriptor->data=m_Data;

  m_ReferenceCountLock.Lock();
  m_ReferenceCount = 0;
  m_ReferenceCountLock.Unlock();
}
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;
  }
}
Exemplo n.º 5
0
void
ipMITKSegmentationUndo (mitkIpPicDescriptor* segmentation)
{
    mitkIpPicTSV_t *undo, *data;

    assert (segmentation);
    undo = mitkIpPicQueryTag (segmentation, tagUNDO);
    if (!undo) {
        ipMITKSegmentationError (ipMITKSegmentationUNDO_DISABLED);
    }

    /* if any level is stored ... */
    data = mitkIpPicQuerySubTag (undo, tagUNDO_DATA);
    if (data->n[0]) {
        /* ... replace the image data and remove this level */
        _mitkIpPicTagsElement_t* head = (_mitkIpPicTagsElement_t *) data->value;
        _mitkIpPicTagsElement_t* current = head;
        mitkIpPicTSV_t* tag;

        while( current->next != NULL ) {
            current = current->next;
        }
        tag = _mitkIpPicRemoveTag (&head, current, current->tsv->tag);
        data->value = head;
        data->n[0]--;
        memmove (segmentation->data, tag->value, _mitkIpPicSize (segmentation));
        mitkIpPicFreeTag (tag);

        tag = mitkIpPicDelTag (segmentation, tagSEGMENTATION_EMPTY);
        if (tag) {
            mitkIpPicFreeTag (tag);
        }
    }
}
Exemplo n.º 6
0
void mitk::PicFileReader::ConvertHandedness(mitkIpPicDescriptor* pic)
{
  //left to right handed conversion
  if(pic->dim>=3)
  {
      mitkIpPicDescriptor* slice=mitkIpPicCopyHeader(pic, NULL);
      slice->dim=2;
      size_t size=_mitkIpPicSize(slice);
      slice->data=malloc(size);

      size_t v, volumes = (pic->dim>3? pic->n[3] : 1);
      size_t volume_size = size*pic->n[2];

      for(v=0; v<volumes; ++v)
      {
        unsigned char *p_first=(unsigned char *)pic->data;
        unsigned char *p_last=(unsigned char *)pic->data;
        p_first+=v*volume_size;
        p_last+=size*(pic->n[2]-1)+v*volume_size;

        size_t i, smid=pic->n[2]/2;
        for(i=0; i<smid;++i,p_last-=size, p_first+=size)
        {
            memcpy(slice->data, p_last, size);
            memcpy(p_last, p_first, size);
            memcpy(p_first, slice->data, size);
        }
      }
      mitkIpPicFree(slice);
  }
}
Exemplo n.º 7
0
void
ipMITKSegmentationUndoSave (mitkIpPicDescriptor* segmentation)
{
    mitkIpPicTSV_t *undo, *data, *level, *tag;

    assert (segmentation);
    undo = mitkIpPicQueryTag (segmentation, tagUNDO);
    if (!undo) {
        ipMITKSegmentationError (ipMITKSegmentationUNDO_DISABLED);
    }

    /* if no level is available ... */
    data = mitkIpPicQuerySubTag (undo, tagUNDO_DATA);
    level = mitkIpPicQuerySubTag (undo, tagUNDO_LEVEL);
    if (*((mitkIpUInt1_t *) level->value) > 0) {
        if (data->n[0] == *((mitkIpUInt1_t *) level->value)) {
            /* ... remove the first one. */
            _mitkIpPicTagsElement_t* head = (_mitkIpPicTagsElement_t *) data->value;
            mitkIpPicTSV_t* tag = _mitkIpPicRemoveTag (&head, head, head->tsv->tag);
      data->value = head;
      data->n[0]--;
            mitkIpPicFreeTag (tag);
        }
        /* build and store the level */
        tag = (mitkIpPicTSV_t *) malloc (sizeof (mitkIpPicTSV_t));
        if (!tag) {
            ipMITKSegmentationError (ipMITKSegmentationOUT_OF_MEMORY);
        }
        strcpy (tag->tag, "IMAGE");
        tag->type = segmentation->type;
        tag->bpe = segmentation->bpe;
        tag->dim = segmentation->dim;
        tag->n[0] = segmentation->n[0];
        tag->n[1] = segmentation->n[1];
        tag->value = malloc (_mitkIpPicSize (segmentation));
        memmove (tag->value, segmentation->data, _mitkIpPicSize (segmentation));
        mitkIpPicAddSubTag (data, tag);
    }
}
Exemplo n.º 8
0
mitkIpPicDescriptor *
MITKipPicGet( char *infile_name, mitkIpPicDescriptor *pic )
{
  if(pic != NULL)
  {
    mitkIpPicDescriptor * tmpPic;
    size_t sizePic, sizeTmpPic;
    tmpPic = mitkIpPicGet(infile_name, NULL);
    sizePic = _mitkIpPicSize(pic);
    sizeTmpPic = _mitkIpPicSize(tmpPic);
    if(sizePic != sizeTmpPic)
    {
      fprintf( stderr, "Critical: MITKipPicGet was given a pic with wrong size\n" );
      return tmpPic;
    }
    memcpy(pic->data, tmpPic->data, sizePic);
    pic->info->tags_head = tmpPic->info->tags_head;
    tmpPic->info->tags_head = NULL;
    mitkIpPicFree(tmpPic);
    return pic;
  }
  else
    return mitkIpPicGet(infile_name, pic);
}
Exemplo n.º 9
0
mitkIpPicDescriptor*
ipMITKSegmentationNew (mitkIpPicDescriptor* image)
{
    mitkIpPicDescriptor* s = NULL;

    if (image) {
        s = mitkIpPicNew ();
        s->type = ipMITKSegmentationTYPE_ID;
        s->bpe = ipMITKSegmentationBPE;
        s->dim = 2;
        s->n[0] = image->n[0];
        s->n[1] = image->n[1];
        s->data = malloc (_mitkIpPicSize (s));
    }
    return s;
}
void mitk::CylindricToCartesianFilter::GenerateData()
{
  mitk::Image::ConstPointer input = this->GetInput();
  mitk::Image::Pointer output = this->GetOutput();

  mitk::ImageTimeSelector::Pointer timeSelector=mitk::ImageTimeSelector::New();
  timeSelector->SetInput(input);

  mitkIpPicDescriptor* pic_transformed=NULL;
  pic_transformed = mitkIpPicNew();
  pic_transformed->dim=3;
  pic_transformed->bpe  = output->GetPixelType().GetBpe();
  //pic_transformed->type = output->GetPixelType().GetType();
  pic_transformed->n[0] = output->GetDimension(0);
  pic_transformed->n[1] = output->GetDimension(1);
  pic_transformed->n[2] = output->GetDimension(2);
  pic_transformed->data=malloc(_mitkIpPicSize(pic_transformed));

  int nstart, nmax;
  int tstart, tmax;

  tstart=output->GetRequestedRegion().GetIndex(3);
  nstart=output->GetRequestedRegion().GetIndex(4);

  tmax=tstart+output->GetRequestedRegion().GetSize(3);
  nmax=nstart+output->GetRequestedRegion().GetSize(4);

  if(zt==NULL)
  {
    timeSelector->SetChannelNr(nstart);
    timeSelector->SetTimeNr(tstart);

    buildTransformShortCuts(input->GetDimension(0),input->GetDimension(1), input->GetDimension(2), output->GetDimension(0), rt_pic, phit_pic, fr_pic, fphi_pic, zt, fz);

    // query the line limiting the sector
    a=b=0;
    mitk::FloatProperty::Pointer prop;

    prop = dynamic_cast<mitk::FloatProperty*>(input->GetProperty("SECTOR LIMITING LINE SLOPE").GetPointer());
    if (prop.IsNotNull() )
      a = prop->GetValue();
    prop = dynamic_cast<mitk::FloatProperty*>(input->GetProperty("SECTOR LIMITING LINE OFFSET").GetPointer());
    if (prop.IsNotNull() )
      b = prop->GetValue();

    buildConeCutOffShortCut(input->GetDimension(0),input->GetDimension(1), rt_pic, fr_pic, a, b, coneCutOff_pic);
    //    mitkIpPicPut("C:\\temp\\rt_90.pic",rt_pic);
    //mitkIpPicPut("C:\\temp\\coneCutOff.pic", coneCutOff_pic);
  }

  int n,t;
  for(n=nstart;n<nmax;++n)//output->GetNumberOfChannels();++n)
  {
    timeSelector->SetChannelNr(n);

    for(t=tstart;t<tmax;++t)
    {
      timeSelector->SetTimeNr(t);

      timeSelector->Update();

      // Cast to pic descriptor for the timeSelector image
      mitkIpPicDescriptor* timeSelectorPic = mitkIpPicNew();
      CastToIpPicDescriptor( timeSelector->GetOutput(), timeSelectorPic );

      _mitkIpPicFreeTags(pic_transformed->info->tags_head);
      pic_transformed->info->tags_head = _mitkIpPicCloneTags(timeSelectorPic->info->tags_head);

      if(input->GetDimension(2)>1)
      {
        mitkIpPicTypeMultiplex9(_transform, timeSelectorPic , pic_transformed, m_OutsideValue, (float*)fr_pic->data, (float*)fphi_pic->data, fz, (short *)rt_pic->data, (unsigned int *)phit_pic->data, zt, coneCutOff_pic);
        //  mitkIpPicPut("1trf.pic",pic_transformed);  
      }
      else
      {
        mitkIpPicDescriptor *doubleSlice = mitkIpPicCopyHeader( timeSelectorPic , NULL);
        doubleSlice->dim=3;
        doubleSlice->n[2]=2;
        doubleSlice->data=malloc(_mitkIpPicSize(doubleSlice));
        memcpy(doubleSlice->data, timeSelectorPic->data, _mitkIpPicSize(doubleSlice)/2);
        mitkIpPicTypeMultiplex9(_transform, doubleSlice, pic_transformed, m_OutsideValue, (float*)fr_pic->data, (float*)fphi_pic->data, fz, (short *)rt_pic->data, (unsigned int *)phit_pic->data, zt, coneCutOff_pic);
        mitkIpPicFree(doubleSlice);
      }
      output->SetVolume(pic_transformed->data, t, n);
    }
  }
  //mitkIpPicPut("outzzzzzzzz.pic",pic_transformed);  
  mitkIpPicFree(pic_transformed);

  m_TimeOfHeaderInitialization.Modified();
}
Exemplo n.º 11
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 );
}
void mitk::CylindricToCartesianFilter::buildTransformShortCuts(int orig_xsize, int orig_ysize, int orig_zsize, int new_xsize, mitkIpPicDescriptor * &rt_pic, mitkIpPicDescriptor * &phit_pic, mitkIpPicDescriptor * &fr_pic, mitkIpPicDescriptor * &fphi_pic, unsigned int * &zt, float * &fz)
{
  --orig_zsize;

  rt_pic=mitkIpPicNew();
  rt_pic->type=mitkIpPicInt;
  rt_pic->bpe=16;
  rt_pic->dim=2;
  rt_pic->n[0]=rt_pic->n[1]=new_xsize;
  rt_pic->data=malloc(_mitkIpPicSize(rt_pic));

  phit_pic=mitkIpPicNew();
  phit_pic->type=mitkIpPicUInt;
  phit_pic->bpe=32;
  phit_pic->dim=2;
  phit_pic->n[0]=phit_pic->n[1]=new_xsize;
  phit_pic->data=malloc(_mitkIpPicSize(phit_pic));

  fr_pic=mitkIpPicNew();
  fr_pic->type=mitkIpPicFloat;
  fr_pic->bpe=32;
  fr_pic->dim=2;
  fr_pic->n[0]=fr_pic->n[1]=new_xsize;
  fr_pic->data=malloc(_mitkIpPicSize(fr_pic));

  fphi_pic=mitkIpPicNew();
  fphi_pic->type=mitkIpPicFloat;
  fphi_pic->bpe=32;
  fphi_pic->dim=2;
  fphi_pic->n[0]=fphi_pic->n[1]=new_xsize;
  fphi_pic->data=malloc(_mitkIpPicSize(fphi_pic));

  mitkIpInt2_t  *rtp=(mitkIpInt2_t*)rt_pic->data, *rt_xzero, rt, phit;
  mitkIpUInt4_t  *phitp=(mitkIpUInt4_t*)phit_pic->data;
  mitkIpFloat4_t *fr=(mitkIpFloat4_t *)fr_pic->data;
  mitkIpFloat4_t *fphi=(mitkIpFloat4_t *)fphi_pic->data;

  mitkIpFloat4_t r, phi, scale=(double)orig_xsize/(double)new_xsize;

  int x,y,xy0,xy0_orig, oxy_size, new_zsize;
  oxy_size=orig_xsize*orig_ysize;
  xy0=(int)(((double)new_xsize)/2+0.5); 
  xy0_orig=(int)(((double)orig_xsize)/2+0.5); 

  new_zsize=(int)(orig_ysize/scale);
  // \bug y compared to x 
  for(y=0;y<new_xsize;++y)
  {
    rt_xzero=rtp; *rtp=0;
    for(x=0;x<new_xsize;++x,++fr,++fphi,++rtp, ++phitp)
    {
      int xq=x-xy0, yq=y-xy0;

      r=sqrt( (double) (xq*xq+yq*yq));

      //      float rtest=-(xy0-sqrt(xy0*xy0-yq*yq))-0.5;
      rt=(mitkIpInt2_t)(-(xy0-sqrt((double) (xy0*xy0-yq*yq)))-0.5);/*in rt steht der Index des Endes der zu �berspringenden Punkte=>anfangen bei -rt+1!*/
      //      if((x>=-rt) && (x<new_xsize+rt))
      {
        if(y!=xy0)
          r=r*(y>xy0?1.0:-1.0)*scale+xy0_orig;
        else
          r=r*(x>xy0?-1.0:1.0)*scale+xy0_orig;
        rt=(mitkIpInt2_t)r;
        int xtmp=x;
        if(x>xy0)
          xtmp=new_xsize-x;
        if(rt<0)
        {
          r=rt=0;
          if(xtmp>-*rt_xzero)
            *rt_xzero=-xtmp;
          *fr=0;
        }
        else
          if(rt>orig_xsize-1)
          {
            r=rt=orig_xsize-1;
            if(xtmp>-*rt_xzero)
              *rt_xzero=-xtmp;
            *fr=0;
          }
          else
            *fr=r-rt;
        if(*fr<0)
          *fr=0;
      }
      //      else
      //        *fr=0;

      phi=orig_zsize-(yq==0?1:-atan((float)xq/yq)/M_PI+0.5)*orig_zsize;
      phit=(mitkIpUInt4_t)phi;
      *fphi=phi-phit;

      *rtp=rt;
      *phitp=phit*oxy_size;
    }
  }

  zt=(unsigned int *)malloc(sizeof(unsigned int)*new_zsize);
  fz=(float *)malloc(sizeof(float)*new_zsize);

  float *fzp=fz;
  unsigned int *ztp=zt;

  int z;
  float z_step=orig_ysize/(orig_ysize*((float)new_xsize)/orig_xsize);
  for(z=0;z<new_zsize;++z,++fzp,++ztp)
  {    
    *fzp=z*z_step;
    *ztp=(unsigned int)*fzp;
    *fzp-=*ztp;
    *ztp*=orig_xsize;
  }
}
Exemplo n.º 13
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();*/
}
Exemplo n.º 14
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 );
}
Exemplo n.º 15
0
mitkIpPicDescriptor * _MITKipPicOldGet( FILE *infile, mitkIpPicDescriptor *pic )
{
  _mitkIpPicOldHeader old_pic;

  size_t elements;

  size_t size = 0;

  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;

  size_t ignored;

  mitkIpUInt1_t* data;

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

  /* read infile */
  ignored = mitkIpFReadLE( &(old_pic.dummy1), sizeof(mitkIpUInt4_t), 4, infile );
  if( old_pic.conv <= 0 || old_pic.conv > 6 )
    {
      old_pic.conv = 3;
      old_pic.rank = 2;
    }

  ignored = mitkIpFReadLE( &(old_pic.n1), sizeof(mitkIpUInt4_t), old_pic.rank, infile );
  if( old_pic.rank == 3 && old_pic.n3 == 1 )
    old_pic.rank = 2;

  ignored = mitkIpFReadLE( &(old_pic.type), sizeof(mitkIpUInt4_t), 3, infile );
  if( old_pic.ntxt )
    {
      fseek( infile, old_pic.ltxt, SEEK_CUR );
    }


  elements = old_pic.n1 * old_pic.n2;
  if( old_pic.rank == 3 )
    elements *= old_pic.n3;

  if((size == 0) || (size != _mitkIpPicSize(pic)))
    {
      if( pic->data != NULL )
        {
          free( pic->data );
          pic->data = NULL;
        }
      pic->data = malloc( _mitkIpPicSize(pic) );
    }

  /*
   * data is read in blocks of size 'block_size' to prevent from
   * errors due to large file sizes (>=2GB)
   */
  number_of_elements = elements;
  bytes_per_element = old_pic.type;
  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;

  data = (mitkIpUInt1_t*) pic->data;
  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 (ferror indicates %u), only %lu bytes were read! Eof indicator is %u.\n", ferror(infile), (long unsigned int)bytes_read, feof(infile) );

  /* convert to the new pic3 format */

  if( old_pic.type == 1 )
    pic->type = mitkIpPicUInt;
  else
    pic->type = (mitkIpPicType_t)old_pic.conv;
  pic->bpe = old_pic.type*8;
  pic->dim = old_pic.rank;
  pic->n[0] = old_pic.n1;
  pic->n[1] = old_pic.n2;
  pic->n[2] = old_pic.n3;
  pic->n[3] = old_pic.n4;
  pic->n[4] = old_pic.n5;
  pic->n[5] = old_pic.n6;
  pic->n[6] = old_pic.n7;
  pic->n[7] = old_pic.n8;

  return( pic );
}
void mitk::AngleCorrectByPointFilter::GenerateData()
{
  mitk::Image::ConstPointer input = this->GetInput();
  mitk::Image::Pointer output = this->GetOutput();


  if(m_PreferTransducerPositionFromProperty)
  {
    mitk::Point3iProperty::Pointer pointProp;
    pointProp = dynamic_cast<mitk::Point3iProperty*>(input->GetProperty("ORIGIN").GetPointer());
    if (pointProp.IsNotNull() )
    {
      const itk::Point<int, 3> & p = pointProp->GetValue();
      m_TransducerPosition[0] = p[0];
      m_TransducerPosition[1] = p[1];
      m_TransducerPosition[2] = p[2];
    }
  }

  itkDebugMacro( << "compute angle corrected image .... " );
  itkDebugMacro( << "  Center[0]=" << m_Center[0] << " Center[1]=" << m_Center[1] << " Center[2]=" << m_Center[2] );
  itkDebugMacro( << "  TransducerPosition[0]=" << m_TransducerPosition[0] << " TransducerPosition[1]=" << m_TransducerPosition[1] << " TransducerPosition[2]=" << m_TransducerPosition[2] );

  const Vector3D & spacing = input->GetSlicedGeometry()->GetSpacing();
  //  MITK_INFO << "   in: xres=" << spacing[0] << " yres=" << spacing[1] << " zres=" << spacing[2] << std::endl;
  
  if((spacing[0]!=spacing[1]) || (spacing[0]!=spacing[2]))
  {
    itkExceptionMacro("filter does not work for uninsotropic data: spacing: ("<< spacing[0] << "," << spacing[1] << "," << spacing[2] << ")");
  }

  Vector3D p;
  Vector3D tx_direction;
  Vector3D tx_position = m_TransducerPosition.GetVectorFromOrigin();
  Vector3D center = m_Center.GetVectorFromOrigin();
  Vector3D assumed_direction;
  ScalarType &x=p[0];
  ScalarType &y=p[1];
  ScalarType &z=p[2];
  Vector3D down;
  FillVector3D(down,0.0,0.0,-1.0);

  int xDim = input->GetDimension(0);
  int yDim = input->GetDimension(1);
  int zDim = input->GetDimension(2);

  mitkIpPicDescriptor* pic_out;
  pic_out = mitkIpPicNew();
  pic_out->dim = 3;
  pic_out->bpe  = output->GetPixelType().GetBpe();
  //pic_out->type = output->GetPixelType().GetType();
  pic_out->n[0] = xDim;
  pic_out->n[1] = yDim;
  pic_out->n[2] = zDim;
  pic_out->data = malloc(_mitkIpPicSize(pic_out));

  //go!
  mitk::ImageTimeSelector::Pointer timeSelector=mitk::ImageTimeSelector::New();
  timeSelector->SetInput(input);

  int nstart, nmax;
  int tstart, tmax;

  tstart=output->GetRequestedRegion().GetIndex(3);
  nstart=output->GetRequestedRegion().GetIndex(4);

  tmax=tstart+output->GetRequestedRegion().GetSize(3);
  nmax=nstart+output->GetRequestedRegion().GetSize(4);

  int n,t;
  for(n=nstart;n<nmax;++n)//output->GetNumberOfChannels();++n)
  {
    timeSelector->SetChannelNr(n);

    for(t=tstart;t<tmax;++t)
    {
      timeSelector->SetTimeNr(t);

      timeSelector->Update();

      typedef unsigned char InputImagePixelType;
      typedef ScalarType OutputImagePixelType;

      if(input->GetPixelType().GetTypeId()!=typeid(InputImagePixelType))
      {
        itkExceptionMacro("only implemented for " << typeid(PixelType).name() );
      }

      InputImagePixelType *in;
      OutputImagePixelType *out;

      in  = (InputImagePixelType *)timeSelector->GetOutput()->GetData();
      out = (OutputImagePixelType*)pic_out->data;

      for (z=0 ; z<zDim ; ++z) 
      {
        for (y=0; y<yDim; ++y) 
        {
          for (x=0; x<xDim; ++x, ++in, ++out) 
          {
            tx_direction = tx_position-p;
            tx_direction.Normalize();

            //are we within the acquisition cone?
//            if(-tx_direction*down>vnl_math::pi_over_4)
            {
              assumed_direction = center-p;
              assumed_direction.Normalize();
              ScalarType cos_factor = tx_direction*assumed_direction;

              if(fabs(cos_factor)>eps)
                *out=((ScalarType)(*in)-128.0)/cos_factor;
              else
                *out=((ScalarType)(*in)-128.0)/eps;
            }
            //else
            //  *out=0;
          }
        }
      }
      //output->SetPicVolume(pic_out, t, n);
    }
  }
}
Exemplo n.º 17
0
void mitk::DopplerToStrainRateFilter::GenerateData()
{
  mitk::Image::ConstPointer input = this->GetInput();
  mitk::Image::Pointer output = this->GetOutput();


  mitk::Point3iProperty::Pointer pointProp;
  pointProp = dynamic_cast<mitk::Point3iProperty*>(input->GetProperty("ORIGIN").GetPointer());
  if (pointProp.IsNotNull() )
  {
    m_Origin = pointProp->GetValue();
  }

  MITK_INFO << "compute Strain Rate Image .... " << std::endl
           << "  origin[0]=" << m_Origin[0] << " origin[1]=" << m_Origin[1] << " origin[2]=" << m_Origin[2] << std::endl
           << "  distance=" << m_Distance << std::endl
           << "  NoStrainIntervall=" << m_NoStrainInterval << std::endl;

  const Vector3D & spacing = input->GetSlicedGeometry()->GetSpacing();
  //  MITK_INFO << "   in: xres=" << spacing[0] << " yres=" << spacing[1] << " zres=" << spacing[2] << std::endl;


  mitk::ImageTimeSelector::Pointer timeSelector=mitk::ImageTimeSelector::New();
  timeSelector->SetInput(input);

  mitkIpPicDescriptor* picStrainRate;
  picStrainRate = mitkIpPicNew();
  picStrainRate->dim=3;
  picStrainRate->bpe  = output->GetPixelType().GetBpe();
  //picStrainRate->type = output->GetPixelType().GetType();
  picStrainRate->n[0] = output->GetDimension(0);
  picStrainRate->n[1] = output->GetDimension(1);
  picStrainRate->n[2] = output->GetDimension(2);
  picStrainRate->data=malloc(_mitkIpPicSize(picStrainRate));


  int xDim = picStrainRate->n[0];
  int yDim = picStrainRate->n[1];
  int zDim = picStrainRate->n[2];
  long slice_size = xDim*yDim;
  long vol_size = slice_size*zDim;


  mitkIpPicDescriptor *picDoppler;

  int x,y,z;//,time;        // loop-counter
  int strainRate;            // the computed Strain Rate
  int v1,v2;                // velocity and Point p1 and p2

  float alpha;                // the beam-angle, angle betwen current point and beam-point
  float dx=0, dy=0;                // projection of this->distance to x- and y-axis
  int x1;          // a square, where the velocity v1 lies in
  int y1;          // the points are used for interpolation


  int minStrainRate=128, maxStrainRate=128;

  int n, nmax;
  int t, tmax;

  t = output->GetRequestedRegion().GetIndex(3);
  n = output->GetRequestedRegion().GetIndex(4);
  MITK_INFO << "t = " <<t << " n = " << n << std::endl;

  tmax = t + output->GetRequestedRegion().GetSize(3);
  nmax = n + output->GetRequestedRegion().GetSize(4);
  MITK_INFO << "tmax = "<< tmax << " nmax = " << nmax << std::endl;

  if (m_Distance<1) m_Distance=1;

  for(;n<nmax;n++)//output->GetNumberOfChannels();++n)
  {
    timeSelector->SetChannelNr(n);
    MITK_INFO << "computing chanel n = " << n << std::endl;

    for(t=0;t<tmax;t++)
    {
      MITK_INFO << "computing time slot t = " << t << std::endl;
      timeSelector->SetTimeNr(t);
      timeSelector->Update();

      // Cast to pic descriptor for the timeSelector image
      mitkIpPicDescriptor* timeSelectorPic = mitkIpPicNew();
      CastToIpPicDescriptor( timeSelector->GetOutput(), timeSelectorPic );

      _mitkIpPicFreeTags(picStrainRate->info->tags_head);
      picStrainRate->info->tags_head = _mitkIpPicCloneTags(timeSelectorPic->info->tags_head);

//#define       WRITE_ANGLE_PIC
#ifdef WRITE_ANGLE_PIC
      mitkIpPicDescriptor *anglePic;
      mitkIpBool_t isAnglePicWritten = mitkIpFalse;

      anglePic = mitkIpPicNew();
      anglePic->type = mitkIpPicInt;
      anglePic->bpe = 8;
      anglePic->dim = 2;
      anglePic->n[0] = xDim;
      anglePic->n[1] = yDim;
      anglePic->data = (mitkIpInt1_t *)calloc(xDim*yDim,sizeof(mitkIpInt1_t));
#endif

      picDoppler = timeSelectorPic;
      picDoppler = mitkIpFuncGausF( picDoppler, 5,2 , mitkIpFuncBorderOld )  ;



      for (z=0 ; z<zDim ; z++) {


        for (y=1; y<yDim; y++) {

          if (y<m_Origin[1]) continue;  // cannot compute StrainRate above Transducer-Position

          for (x=0; x<xDim; x++) {

            if ((m_Origin[0] - x)==0) {  // winkelhalbierende

              int yDistanceInUnits = (int)( m_Distance/spacing[1]);
              int yTmp = ( (y-yDistanceInUnits)<0 )  ? 0 : (y-yDistanceInUnits);
              v1 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + yTmp*xDim + x] ;
              v2 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y*xDim + x] ;

            } else {

              // compute angle to transducer position
              // m_Origin is given in units, not in mm
              alpha = atan( (float) (m_Origin[0] - x)  / (float) (m_Origin[1] - y) );

              // m_Distance is given in mm
              dx = -sin(alpha) * m_Distance;
              dy = -cos(alpha) * m_Distance;


              // convert to units
              dx = dx/spacing[0];
              dy = dy/spacing[1];

              //#define WEIGTHED
#ifdef WEIGTHED
              weightX = dx - floor(dx);
              weightY = dy - floor(dy);

              dxFloor = (int) floor(dx);
              dyFloor = (int) floor(dy);

              x1 = x + dxFloor;          // lower left
              y1 = y + dyFloor;

              x2 = x + (dxFloor+1);      // lower right
              y2 = y + dyFloor;

              x3 = x + (dxFloor+1);      // upper right
              y3 = y + (dyFloor+1);

              x4 = x + dxFloor;          // upper left
              y4 = y + (dyFloor+1);

              vTmp1 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y1*xDim + x1];
              vTmp2 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y2*xDim + x2];
              vTmp3 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y3*xDim + x3];
              vTmp4 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y4*xDim + x4];

              v1 = (int) ((1-weightX)*(1-weightY)*vTmp1 + weightX*(1-weightY)*vTmp2 + weightX*weightY*vTmp3 + (1-weightX)*weightY*vTmp4);
              v2 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y*xDim + x] ;
#else
              x1 = (int)(x + dx);
              y1 = (int)(y + dy);
              if (y1<0) y1=0;

#endif
              v1 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y1*xDim + x1];
              v2 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y*xDim + x] ;
            }

            if (v1==128) {  // schaue in Gegenrichtung, falls keine SRI hier berechenbar
              x1 = (int)(x - dx);
              y1 = (int)(y - dy);
              if (y1>yDim) y1=yDim;
              //swap v1 and v2, otherwise StrainRate is calculate in false direction
              v1 = v2;
              v2 = ((mitkIpUInt1_t *)picDoppler->data)[z*slice_size + y1*xDim + x1];
            }

            if (   (v1==0 ) || (v2==0)  ||  // wenn keine Geschwindigkeit vorhanden
              // oder wenn nur ganz kleine Geschwindigkeit vorhanden
              (v1>=(128-m_NoStrainInterval) && v1<=(128+m_NoStrainInterval)) ||
              (v2>=(128-m_NoStrainInterval) && v2<=(128+m_NoStrainInterval))) {

                strainRate = 128;  // this means no deformation
                // this is neccessay due to the Cyl2Cart filter

              } else {
                strainRate = (int)( (v1 - v2)/2 + 128 );
                if (strainRate==128) strainRate=129;
              }

              if (strainRate < minStrainRate && strainRate > 0 )
                minStrainRate = strainRate;

              if (strainRate > maxStrainRate)
                maxStrainRate = strainRate;

              if (strainRate<0 ) {
                //strainRate = -strainRate;
                MITK_INFO << " error: neg. strainRate ... exit() " << std::endl
                         << " x=" << x << " y=" << y << " z=" << z << std::endl
                         << " v1=" << v1 << " v2=" << v2 << " dist=" << m_Distance << std::endl
                         << " sr=" << strainRate  << std::endl;
                exit(0);
              }


              ((mitkIpUInt1_t *)picStrainRate->data)[t*vol_size + z*slice_size + y*xDim + x]=strainRate;

              // cout << "z: " << z << " y: " << y << " x: " << x << " strainrate: " << strainRate << endl;

#ifdef WRITE_ANGLE_PIC
              //            if (!isAnglePicWritten)
              //          ((mitkIpInt1_t *)anglePic->data)[y*xDim + x] = (int) ( (alpha/1.6)*128);
              if (!isAnglePicWritten)
                ((mitkIpInt1_t *)anglePic->data)[y*xDim + x] = (int) ( dx);

#endif

          }  // x

        } // y

        //isAnglePicWritten = mitkIpTrue;

      } // z

      //isStrainComputed = mitkIpTrue;
      std::string filenameD;
      filenameD ="doppler.pic";
      mitkIpPicPut(const_cast<char *>(filenameD.c_str()),picDoppler);

#define WRITE_STRAIN_PIC
#ifdef WRITE_STRAIN_PIC
      char tmpfilename[100];
      sprintf(tmpfilename,"strain%d.pic",t);;
      mitkIpPicPut(tmpfilename,picStrainRate);
#endif


#ifdef WRITE_ANGLE_PIC
      std::string filename2;
      filename2="angle.pic";
      mitkIpPicPut(const_cast<char *>(filename2.c_str()),anglePic);
#endif
      ((mitkIpUInt1_t *)picStrainRate->data)[0]=0;
      ((mitkIpUInt1_t *)picStrainRate->data)[1]=255;

      output->SetVolume(picStrainRate->data, t, n);
    }
  }

  mitkIpPicFree(picStrainRate);

#define WRITE_STRAIN_PIC
#ifdef WRITE_STRAIN_PIC
      // Get the StrainRate ipPic descriptor
      picStrainRate = mitkIpPicNew();
      CastToIpPicDescriptor( output, picStrainRate );

      std::string filename;
      filename ="strain.pic";
      mitkIpPicPut(const_cast<char *>(filename.c_str()),picStrainRate);
#endif

  MITK_INFO << "Strain Rate Image computed.... " << std::endl
           << "  minStrainRate: " << minStrainRate << std::endl
           << "  maxStrainRate: " << maxStrainRate << std::endl;
}
Exemplo n.º 18
0
mitkIpPicDescriptor*
ipMITKSegmentationCreateGrowerHistory( mitkIpPicDescriptor *seg, int startOfs, mitkIpPicDescriptor *histBuffer )
{
  std::queue<int> ofsQueue;

  if (!seg) return 0;
  if (!histBuffer) {
    histBuffer = mitkIpPicCopyHeader( seg, histBuffer );
    histBuffer->type = mitkIpPicUInt;
    histBuffer->bpe = 16; 
    mitkIpUInt4_t size = _mitkIpPicSize( histBuffer );
    histBuffer->data = malloc( size );
    memset( histBuffer->data, 0, size );  // clear buffer
  }
  else {
    // check if dimension of histBuffer is valid, if not: change it!
    if (histBuffer->n[0] != seg->n[0] || histBuffer->n[1] != seg->n[1]) {
      histBuffer->n[0] = seg->n[0];
      histBuffer->n[1] = seg->n[1];
      mitkIpUInt4_t size = _mitkIpPicSize( histBuffer );
      histBuffer->data = realloc( histBuffer->data, size );
      if (histBuffer->data == 0) return 0;
      memset( histBuffer->data, 0, size );  // clear buffer
    }
  }

  // create a clear buffer to check wether a point has already been visited
  // (seg cannot be modifier and histBuffer can contain any value)
  mitkIpPicDescriptor *flagBuffer = mitkIpPicCopyHeader( seg, 0 );
  mitkIpUInt4_t size = _mitkIpPicSize( flagBuffer );
  flagBuffer->data = malloc( size );
  memset( flagBuffer->data, 0, size );
  *((mitkIpUInt1_t*)flagBuffer->data+startOfs) = 1;    // flag pixel as visited
    
  int line = seg->n[0];
  int maxOfs = (int)(line * seg->n[1]);
  int testOfs;
  mitkIpUInt1_t segVal, flagVal;
  int iteration = 0;
  int currentWave = 1;
  int nextWave = 0;
  
  ofsQueue.push( startOfs );

  while (!ofsQueue.empty()) {
    int nextOfs = ofsQueue.front();
    ofsQueue.pop();
    currentWave--;
    *((mitkIpUInt2_t*)histBuffer->data+nextOfs) = (mitkIpUInt2_t)(iteration+1);  // seed point should get history = 1

    // check right:
    testOfs = nextOfs+1;
    if (testOfs%line!=0) {
      segVal = *((mitkIpUInt1_t*)seg->data+testOfs);
      flagVal = *((mitkIpUInt1_t*)flagBuffer->data+testOfs);
      if ( segVal != 0 && flagVal == 0) {
        ofsQueue.push( testOfs );
        *((mitkIpUInt1_t*)flagBuffer->data+testOfs) = 1;    // flag pixel as visited
        nextWave++;
      }
    }
    // check top:
    testOfs = nextOfs-line;
    if (testOfs > 0) {
      segVal = *((mitkIpUInt1_t*)seg->data+testOfs);
      flagVal = *((mitkIpUInt1_t*)flagBuffer->data+testOfs);
      if ( segVal != 0 && flagVal == 0) {
        ofsQueue.push( testOfs );
        *((mitkIpUInt1_t*)flagBuffer->data+testOfs) = 1;    // flag pixel as visited
        nextWave++;
      }
    }
    // check left:
    testOfs = nextOfs-1;
    if (nextOfs%line!=0) {
      segVal = *((mitkIpUInt1_t*)seg->data+testOfs);
      flagVal = *((mitkIpUInt1_t*)flagBuffer->data+testOfs);
      if ( segVal != 0 && flagVal == 0) {
        ofsQueue.push( testOfs );
        *((mitkIpUInt1_t*)flagBuffer->data+testOfs) = 1;    // flag pixel as visited
        nextWave++;
      }
    }
    // check bottom:
    testOfs = nextOfs+line;
    if (testOfs < maxOfs) {
      segVal = *((mitkIpUInt1_t*)seg->data+testOfs);
      flagVal = *((mitkIpUInt1_t*)flagBuffer->data+testOfs);
      if ( segVal != 0 && flagVal == 0) {
        ofsQueue.push( testOfs );
        *((mitkIpUInt1_t*)flagBuffer->data+testOfs) = 1;    // flag pixel as visited
        nextWave++;
      }
    }
    // check for number of iterations:
    if (currentWave == 0) {
      currentWave = nextWave;
      nextWave = 0;
      iteration++;
    }
  }

  mitkIpPicFree( flagBuffer );
  return histBuffer;
}
Exemplo n.º 19
0
mitkIpPicDescriptor*
tmGrowRegion4N( mitkIpPicDescriptor *src, int startOfs, bool relativeBounds, float lowerBoundFlt, float upperBoundFlt, int maxIterations, mitkIpPicDescriptor *segBuffer, int &contourOfs, float &startCol, mitkIpPicDescriptor *histBuffer )
{
  PicType lowerBound = static_cast<PicType>(lowerBoundFlt);
  PicType upperBound = static_cast<PicType>(upperBoundFlt);
  std::queue<int> ofsQueue;

  if (maxIterations <= 0) maxIterations = 32000;
  if (!src) return 0;
  if (!segBuffer) {
    segBuffer = mitkIpPicCopyHeader( src, segBuffer );
    segBuffer->type = mitkIpPicUInt;
    segBuffer->bpe = 8; 
    mitkIpUInt4_t size = _mitkIpPicSize( segBuffer );
    segBuffer->data = malloc( size );
  }
  else {
    // check if dimension of segBuffer is valid, if not: change it!
    if (segBuffer->n[0] != src->n[0] || segBuffer->n[1] != src->n[1]) {
      segBuffer->n[0] = src->n[0];
      segBuffer->n[1] = src->n[1];
      mitkIpUInt4_t size = _mitkIpPicSize( segBuffer );
      segBuffer->data = realloc( segBuffer->data, size );
      if (segBuffer->data == 0) return 0;
    }
  }
  if (histBuffer) {
    // check if dimension of histBuffer is valid, if not: change it!
    if (histBuffer->n[0] != src->n[0] || histBuffer->n[1] != src->n[1]) {
      histBuffer->n[0] = src->n[0];
      histBuffer->n[1] = src->n[1];
      mitkIpUInt4_t size = _mitkIpPicSize( histBuffer );
      histBuffer->data = realloc( histBuffer->data, size );
      if (histBuffer->data == 0) return 0;
      memset( histBuffer->data, 0, size );  // clear buffer
    }
  }
  
  int line = segBuffer->n[0];
  int maxOfs = (int)(line * segBuffer->n[1]);
  //PicType *start = ((PicType*)src->data) + startOfs;
  // init borders:
  PicType lowest, highest;
  if (relativeBounds) {
    
    // average base color from 3x3 block:
    // check for edges of image
    int offset;
    int numberOfValidOffsets = 0;
    int baseCol = 0;
    offset = startOfs;           if ( (offset >= 0) && (offset < (int)(src->n[0] * src->n[1])) ) { baseCol += *((PicType*)(src->data)+offset); ++numberOfValidOffsets; }
    offset = startOfs+1;         if ( (offset >= 0) && (offset < (int)(src->n[0] * src->n[1])) ) { baseCol += *((PicType*)(src->data)+offset); ++numberOfValidOffsets; }
    offset = startOfs+1-line;    if ( (offset >= 0) && (offset < (int)(src->n[0] * src->n[1])) ) { baseCol += *((PicType*)(src->data)+offset); ++numberOfValidOffsets; }
    offset = startOfs-line;      if ( (offset >= 0) && (offset < (int)(src->n[0] * src->n[1])) ) { baseCol += *((PicType*)(src->data)+offset); ++numberOfValidOffsets; }
    offset = startOfs-1-line;    if ( (offset >= 0) && (offset < (int)(src->n[0] * src->n[1])) ) { baseCol += *((PicType*)(src->data)+offset); ++numberOfValidOffsets; }
    offset = startOfs-1;         if ( (offset >= 0) && (offset < (int)(src->n[0] * src->n[1])) ) { baseCol += *((PicType*)(src->data)+offset); ++numberOfValidOffsets; }
    offset = startOfs-1+line;    if ( (offset >= 0) && (offset < (int)(src->n[0] * src->n[1])) ) { baseCol += *((PicType*)(src->data)+offset); ++numberOfValidOffsets; }
    offset = startOfs+line;      if ( (offset >= 0) && (offset < (int)(src->n[0] * src->n[1])) ) { baseCol += *((PicType*)(src->data)+offset); ++numberOfValidOffsets; }
    offset = startOfs+1+line;    if ( (offset >= 0) && (offset < (int)(src->n[0] * src->n[1])) ) { baseCol += *((PicType*)(src->data)+offset); ++numberOfValidOffsets; }

    if ( numberOfValidOffsets > 0 )
      baseCol = (PicType)( (float)baseCol / (float)numberOfValidOffsets );

    lowest = baseCol - lowerBound;
    highest = baseCol + upperBound;
    startCol = (float)baseCol;
  }
  else {
    lowest = lowerBound;
    highest = upperBound;
    startCol = 0.0f;
  }

  memset( segBuffer->data, 0, _mitkIpPicSize(segBuffer) );  // clear buffer
    
  PicType value = *((PicType*)src->data+startOfs);
  if ( value >=lowest && value <=highest ) {
    ofsQueue.push( startOfs );
  }

  contourOfs = -1;
  int testOfs;
  mitkIpUInt1_t segVal;
  int iteration = 0;
  int currentWave = 1;
  int nextWave = 0;

  while (!ofsQueue.empty() && iteration<=maxIterations) {
    int nextOfs = ofsQueue.front();
    ofsQueue.pop();
    currentWave--;
    *((mitkIpUInt1_t*)segBuffer->data+nextOfs) = 1;
    if (histBuffer) {
      *((mitkIpUInt2_t*)histBuffer->data+nextOfs) = (mitkIpUInt2_t)(iteration+1);  // seed point should get history = 1
    }
    if (nextOfs > contourOfs) contourOfs = nextOfs;
    // check right:
    testOfs = nextOfs+1;
    if (testOfs%line!=0) {
      segVal = *((mitkIpUInt1_t*)segBuffer->data+testOfs);
      if ( segVal == 0 ) {
        value = *((PicType*)src->data+testOfs);
        if ( value >=lowest && value <=highest ) {
          ofsQueue.push( testOfs );
          nextWave++;
          *((mitkIpUInt1_t*)segBuffer->data+testOfs) = 2;
        }
      }
    }
    // check top:
    testOfs = nextOfs-line;
    if (testOfs > 0) {
      segVal = *((mitkIpUInt1_t*)segBuffer->data+testOfs);
      if ( segVal == 0 ) {
        value = *((PicType*)src->data+testOfs);
        if ( value >=lowest && value <=highest ) {
          ofsQueue.push( testOfs );
          nextWave++;
          *((mitkIpUInt1_t*)segBuffer->data+testOfs) = 2;
        }
      }
    }
    // check left:
    testOfs = nextOfs-1;
    if (nextOfs%line!=0) {
      segVal = *((mitkIpUInt1_t*)segBuffer->data+testOfs);
      if ( segVal == 0 ) {
        value = *((PicType*)src->data+testOfs);
        if ( value >=lowest && value <=highest ) {
          ofsQueue.push( testOfs );
          nextWave++;
          *((mitkIpUInt1_t*)segBuffer->data+testOfs) = 2;
        }
      }
    }
    // check bottom:
    testOfs = nextOfs+line;
    if (testOfs < maxOfs) {
      segVal = *((mitkIpUInt1_t*)segBuffer->data+testOfs);
      if ( segVal == 0 ) {
        value = *((PicType*)src->data+testOfs);
        if ( value >=lowest && value <=highest ) {
          ofsQueue.push( testOfs );
          nextWave++;
          *((mitkIpUInt1_t*)segBuffer->data+testOfs) = 2;
        }
      }
    }
    // check for number of iterations:
    if (currentWave == 0) {
      currentWave = nextWave;
      nextWave = 0;
      iteration++;
    }
  }

  return segBuffer;
}
Exemplo n.º 20
0
int mitkImageSliceSelectorTest(int argc, char* argv[])
{
  int slice_nr = 1;
  std::cout << "Loading file: ";
  if(argc==0)
  {
    std::cout<<"no file specified [FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  mitk::Image::Pointer image = NULL;
  mitk::DataNodeFactory::Pointer factory = mitk::DataNodeFactory::New();
  try
  {
    std::cout<<argv[1]<<std::endl;
    factory->SetFileName( argv[1] );
    factory->Update();

    if(factory->GetNumberOfOutputs()<1)
    {
      std::cout<<"file could not be loaded [FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
    mitk::DataNode::Pointer node = factory->GetOutput( 0 );
    image = dynamic_cast<mitk::Image*>(node->GetData());
    if(image.IsNull())
    {
      std::cout<<"file not an image - test will not be applied [PASSED]"<<std::endl;
      std::cout<<"[TEST DONE]"<<std::endl;
      return EXIT_SUCCESS;
    }
  }
  catch ( itk::ExceptionObject & ex )
  {
    std::cout << "Exception: " << ex << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
  }

  if(image->GetDimension(2)<2)
    slice_nr = 0;

  //Take a slice
  mitk::ImageSliceSelector::Pointer slice = mitk::ImageSliceSelector::New();
  slice->SetInput(image);
  slice->SetSliceNr(slice_nr);
  slice->Update();

  std::cout << "Testing IsInitialized(): ";
  if(slice->GetOutput()->IsInitialized()==false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing IsSliceSet(): ";
  if(slice->GetOutput()->IsSliceSet(0)==false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  if(itksys::SystemTools::LowerCase(itksys::SystemTools::GetFilenameExtension(argv[1])).find(".pic")!=std::string::npos)
  {
    std::cout << "Testing whether the slice is identical with a slice loaded by mitkIpPicGetSlice:";
    mitkIpPicDescriptor *picslice = mitkIpPicGetSlice(argv[1], NULL, (image->GetDimension(2)-1-slice_nr)+1);
    int i, size = _mitkIpPicSize(picslice);
    char * p1 = (char*)slice->GetPic()->data;
    char * p2 = (char*)picslice->data;
    //picslice->info->write_protect=mitkIpFalse;
    //mitkIpPicPut("C:\\1aaaaIPPIC.pic", picslice);
    //mitkIpPicPut("C:\\1aaaaSEL.pic", slice->GetPic());
    for(i=0; i<size; ++i, ++p1, ++p2)
    {
      if((*p1) != (*p2))
      {
        std::cout<<"[FAILED]"<<std::endl;
        return EXIT_FAILURE;
      }
    }
    std::cout<<"[PASSED]"<<std::endl;
    mitkIpPicFree(picslice);
  }

  try
  {
    std::cout << "Testing another, smaller (!!) input with the same slice-selector(): ";
    //Use CylindricToCartesianFilter
    mitk::CylindricToCartesianFilter::Pointer cyl2cart = mitk::CylindricToCartesianFilter::New();
    cyl2cart->SetInput(image);
    //the output size of this filter is smaller than the of the input!!
    cyl2cart->SetTargetXSize( 64 );

    //Use the same slice-selector again, this time to take a slice of the filtered image
    //which is smaller than the one of the old input!!
    slice->SetInput(cyl2cart->GetOutput());
    slice->SetSliceNr(1);

    //The requested region is still the old one,
    //therefore the following results in most ITK versions
    //in an exception!
    slice->Update();

    //If no exception occured, check that the requested region is now
    //the one of the smaller image
    if(cyl2cart->GetOutput()->GetLargestPossibleRegion().GetSize()[0]!=64)
    {
      std::cout<<"Part 1 [FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
    std::cout<<"Part 1 (without exception) [PASSED] ";

    //Check that the size of the output is now the one of the smaller image
    if((cyl2cart->GetOutput()->GetDimensions()[0]!=64) || (cyl2cart->GetOutput()->GetDimensions()[1]!=64))
    {
      std::cout<<"Part 1b [FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
    std::cout<<"Part 1b [PASSED] ";
  }
  catch ( itk::ExceptionObject &err)
  {
    std::cout<<"Part 1(with expected exception) ... seems to be not ITK 2.0.0 [PASSED]"<<std::endl;
    std::cout<<err<<std::endl;
    //after such an exception, we need to call ResetPipeline.
    slice->ResetPipeline();
  }

  try
  {
    slice->UpdateLargestPossibleRegion();
  }
  catch ( itk::ExceptionObject )
  {
    std::cout<<"Part 2 [FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"Part 2 [PASSED]"<<std::endl;

  std::cout << "Testing IsInitialized(): ";
  if(slice->GetOutput()->IsInitialized()==false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  std::cout << "Testing IsSliceSet(): ";
  if(slice->GetOutput()->IsSliceSet(0)==false)
  {
    std::cout<<"[FAILED]"<<std::endl;
    return EXIT_FAILURE;
  }
  std::cout<<"[PASSED]"<<std::endl;

  if(image->GetDimension(3) > 1)
  {
    int time=image->GetDimension(3)-1;

    std::cout << "Testing 3D+t: Setting time to " << time << ": ";
    slice->SetTimeNr(time);
    if(slice->GetTimeNr()!=time)
    {
      std::cout<<"[FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
    std::cout<<"[PASSED]"<<std::endl;

    std::cout << "Testing 3D+t: Updating slice: ";
    slice->Update();
    if(slice->GetOutput()->IsInitialized()==false)
    {
      std::cout<<"[FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
    std::cout<<"[PASSED]"<<std::endl;

    std::cout << "Testing 3D+t: IsSliceSet(): ";
    if(slice->GetOutput()->IsSliceSet(0)==false)
    {
      std::cout<<"[FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
    std::cout<<"[PASSED]"<<std::endl;

    std::cout << "Testing 3D+t: First slice in reader available: ";
    if(image->IsSliceSet(0, time)==false)
    {
      std::cout<<"[FAILED]"<<std::endl;
      return EXIT_FAILURE;
    }
    std::cout<<"[PASSED]"<<std::endl;
  }

  std::cout<<"[TEST DONE]"<<std::endl;
  return EXIT_SUCCESS;
}
Exemplo n.º 21
0
mitkIpPicDescriptor *mitkIpFuncSubC ( mitkIpPicDescriptor *pic_old,
                              mitkIpFloat8_t      value, 
                              mitkIpFuncFlagI_t   keep,
                              mitkIpPicDescriptor *pic_return )
{

  mitkIpPicDescriptor *pic_new;         /* pointer to new image             */
  mitkIpFloat8_t      max_gv;           /* max. possible greyvalue          */
  mitkIpFloat8_t      min_gv;           /* min. possible greyvalue          */
  mitkIpFloat8_t      min1, max1;       /* extreme greyvalues of 1. image   */ 
  mitkIpFloat8_t      smin, smax;       /* product of extreme greyvalues    */


  /* check image data                                                   */

  if ( _mitkIpFuncError ( pic_old ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );

  /* check value                                                        */

  if ( value == 0. ) return ( pic_old );
  /*
  else if ( value == 0. )
    {
       _mitkIpFuncSetErrno ( mitkIpFuncDATA_ERROR );
       return ( mitkIpFuncERROR );
    }
  */
  /* calculate max. and min. possible greyvalues for data type of images*/

  if ( _mitkIpFuncExtT ( pic_old->type, pic_old->bpe, &min_gv, &max_gv ) != mitkIpFuncOK )
    return ( mitkIpFuncERROR );

  /* find out data type of new iamge                                    */

  if ( keep == mitkIpFuncKeep )
    {
       pic_new = _mitkIpFuncMalloc ( pic_old, pic_return, mitkIpOVERWRITE );
       if ( pic_new == NULL ) return ( mitkIpFuncERROR );
    }
  else if ( keep == mitkIpFuncNoKeep )
    {
       /* calculate max. and min. greyvalues of both images             */

       if ( mitkIpFuncExtr ( pic_old, &min1, &max1 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );

       smax      = max1 - value;
       smin      = min1 - value;

       /* change image type of images of type mitkIpPicInt                  */

       if ( pic_old->type == mitkIpPicInt )
         {
           if ( smax < max_gv && smin > min_gv ) 
             {
                pic_new = mitkIpPicCopyHeader ( pic_old, NULL );
             }
           else
             {
                pic_new       = mitkIpPicCopyHeader ( pic_old, NULL );
                pic_new->type = mitkIpPicFloat;
                pic_new->bpe  = 64;
                _mitkIpFuncExtT ( pic_new->type, pic_new->bpe, &min_gv, &max_gv );
             }
         }

       /* change image type of images of type mitkIpPicUInt                 */

       else if ( pic_old->type == mitkIpPicUInt )
         {
           if ( smax < max_gv && smin > min_gv )
             {
                pic_new = mitkIpPicCopyHeader ( pic_old, NULL );
             }
           else
             {
                pic_new = mitkIpPicCopyHeader ( pic_old, NULL );
                pic_new->type = mitkIpPicInt;
                pic_new->bpe  = 16;
                _mitkIpFuncExtT ( pic_new->type, pic_new->bpe, &min_gv, &max_gv );
                if ( smax > max_gv || smin < min_gv )
                  {
                     pic_new->type = mitkIpPicFloat;
                     pic_new->bpe  = 64;
                     _mitkIpFuncExtT ( pic_new->type, pic_new->bpe, &min_gv, &max_gv );
                  }
             }
         } 

       /* change image type of images of type mitkIpPicUInt                 */
 
       else if ( pic_old->type == mitkIpPicFloat )
         {
            pic_new = mitkIpPicCopyHeader ( pic_old, NULL );
         }
       else 
         {     
            _mitkIpFuncSetErrno ( mitkIpFuncTYPE_ERROR );
            return ( mitkIpFuncERROR );
         }
       
    }
  else 
    {
       _mitkIpFuncSetErrno ( mitkIpFuncFLAG_ERROR );
       return ( mitkIpFuncERROR );
    }

  if ( pic_new == NULL )
    {
       _mitkIpFuncSetErrno ( mitkIpFuncPICNEW_ERROR );
       return ( mitkIpFuncERROR );
    }
    
  if ( keep == mitkIpFuncNoKeep )
    pic_new->data = malloc ( _mitkIpPicSize  ( pic_new ) );
  if ( pic_new->data == NULL )
    {
       mitkIpPicFree ( pic_new );
       _mitkIpFuncSetErrno ( mitkIpFuncMALLOC_ERROR );
       return ( mitkIpFuncERROR );
    }

  /* macro to invert the picture (for all data types)                   */

  if ( keep == mitkIpFuncNoKeep )
    mitkIpPicFORALL_2 ( SUBC, pic_old, pic_new, value )
  if ( keep == mitkIpFuncKeep )
    mitkIpPicFORALL_2 ( SUBC3, pic_old, pic_new, value )

  /* Copy Tags */

  mitkIpFuncCopyTags(pic_new, pic_old);
  
  
                        
  return pic_new;
}
Exemplo n.º 22
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 );
}
Exemplo n.º 23
0
mitkIpPicDescriptor *mitkIpFuncSqrt ( mitkIpPicDescriptor *pic_1,
                              mitkIpFuncFlagI_t    keep,
                              mitkIpPicDescriptor *pic_return )
{

  mitkIpPicDescriptor *pic_new;         /* pointer to new image             */
  mitkIpUInt4_t       i;                /* loop index                       */
  mitkIpFloat8_t      min1, max1;       /* extreme greyvalues of 1. image   */ 


  /* check image data                                                   */

  if ( _mitkIpFuncError ( pic_1 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );

  /* calculate max. and min. greyvalues of both images                  */

  if ( mitkIpFuncExtr ( pic_1, &min1, &max1 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );

  /* calculate max. and min. possible greyvalues for data type of images*/

  /* find out data type of new iamge                                    */

  if ( keep == mitkIpFuncKeep )
    {
       pic_new = _mitkIpFuncMalloc ( pic_1, pic_return, mitkIpOVERWRITE );
       if ( pic_new == NULL ) return ( mitkIpFuncERROR );
    }
  else if ( keep == mitkIpFuncNoKeep )
    {
       pic_new = mitkIpPicNew ();
       pic_new->dim  = pic_1->dim;
       pic_new->bpe  = 64;
       pic_new->type = mitkIpPicFloat;
       for ( i = 0; i < pic_new->dim; i++ ) 
          pic_new->n[i] = pic_1->n[i];
    }
  else 
    {
       _mitkIpFuncSetErrno ( mitkIpFuncFLAG_ERROR );
       return ( mitkIpFuncERROR );
    }

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

  if ( keep == mitkIpFuncNoKeep )
    pic_new->data = malloc ( _mitkIpPicSize  ( pic_new ) );
  if ( pic_new->data == NULL )
    {
       mitkIpPicFree ( pic_new );
       _mitkIpFuncSetErrno ( mitkIpFuncMALLOC_ERROR );
       return ( mitkIpFuncERROR );
    }

  /* macro to invert the picture (for all data types)                   */

  mitkIpPicFORALL_1 (  SQRT1, pic_1, pic_new )

  /* Copy Tags */

  mitkIpFuncCopyTags(pic_new, pic_1);
                        
  return pic_new;
}
Exemplo n.º 24
0
mitkIpPicDescriptor *mitkIpFuncSubI ( mitkIpPicDescriptor *pic_1,
                              mitkIpPicDescriptor *pic_2,
                              mitkIpFuncFlagI_t   keep,
                              mitkIpPicDescriptor *pic_return )
{

  mitkIpPicDescriptor *pic_new;         /* pointer to new image             */
  mitkIpUInt4_t       i;                /* loop index                       */
  mitkIpFloat8_t      max_gv;           /* max. possible greyvalue          */
  mitkIpFloat8_t      min_gv;           /* min. possible greyvalue          */
  mitkIpFloat8_t      min1, max1;       /* extreme greyvalues of 1. image   */ 
  mitkIpFloat8_t      min2, max2;       /* extreme greyvalues of 2. image   */
  mitkIpFloat8_t      smin, smax;       /* product of extreme greyvalues    */


  /* ckeck whether data are correct                                     */

  if ( _mitkIpFuncError ( pic_1 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );
  if ( _mitkIpFuncError ( pic_2 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );

  /* check whether images have the same size                            */

  if ( ( pic_1->type != pic_2->type ) || ( pic_1->bpe != pic_2->bpe ) )
    {
      _mitkIpFuncSetErrno ( mitkIpFuncUNFIT_ERROR );
      return NULL;
    }
  if ( pic_1->dim == pic_2->dim )
    for ( i = 0; i < _mitkIpPicNDIM; i++ )
      {
        if ( pic_1->n[i] != pic_2->n[i] )
          {
             _mitkIpFuncSetErrno ( mitkIpFuncUNFIT_ERROR );
             return NULL;
          }
      }
  else
    {
       _mitkIpFuncSetErrno ( mitkIpFuncUNFIT_ERROR );
       return NULL;
    }

  /* calculate max. and min. possible greyvalues for data type of images*/

  if ( _mitkIpFuncExtT ( pic_1->type, pic_1->bpe, &min_gv, &max_gv ) != mitkIpFuncOK )
    return ( mitkIpFuncERROR );

  /* find out data type of new iamge                                    */

  if ( keep == mitkIpFuncKeep )
    {
       pic_new = _mitkIpFuncMalloc ( pic_1, pic_return, mitkIpOVERWRITE );
       if ( pic_new == NULL ) return ( mitkIpFuncERROR );
    }
  else if ( keep == mitkIpFuncNoKeep )
    {
       /* calculate max. and min. greyvalues of both images                  */

       if ( mitkIpFuncExtr ( pic_1, &min1, &max1 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );
       if ( mitkIpFuncExtr ( pic_2, &min2, &max2 ) != mitkIpFuncOK ) return ( mitkIpFuncERROR );

       smax      = max1 - max2;
       smin      = min1 - min2;

       /* change image type of images of type mitkIpPicInt                  */

       if ( pic_1->type == mitkIpPicInt )
         {
           if ( smax < max_gv && smin > min_gv ) 
             {
                pic_new = mitkIpPicCopyHeader ( pic_1, NULL );
             }
           else
             {
                pic_new       = mitkIpPicCopyHeader ( pic_1, NULL );
                pic_new->type = mitkIpPicFloat;
                pic_new->bpe  = 64;
                _mitkIpFuncExtT ( pic_new->type, pic_new->bpe, &min_gv, &max_gv );
             }
         }

       /* change image type of images of type mitkIpPicUInt                 */

       else if ( pic_1->type == mitkIpPicUInt )
         {
           if ( smax < max_gv && smin > min_gv )
             {
                pic_new = mitkIpPicCopyHeader ( pic_1, NULL );
             }
           else
             {
                pic_new = mitkIpPicCopyHeader ( pic_1, NULL );
                pic_new->type = mitkIpPicInt;
                pic_new->bpe  = 16;
                _mitkIpFuncExtT ( pic_new->type, pic_new->bpe, &min_gv, &max_gv );
                if ( smax > max_gv || smin < min_gv )
                  {
                     pic_new->type = mitkIpPicFloat;
                     pic_new->bpe  = 64;
                     _mitkIpFuncExtT ( pic_new->type, pic_new->bpe, &min_gv, &max_gv );
                  }
             }
         } 

       /* change image type of images of type mitkIpPicUInt                 */
 
       else if ( pic_1->type == mitkIpPicFloat )
         {
            pic_new = mitkIpPicCopyHeader ( pic_1, NULL );
         }
       else 
         {
           _mitkIpFuncSetErrno ( mitkIpFuncTYPE_ERROR );
           return ( mitkIpFuncERROR );
         }
    }
  else 
    {
       _mitkIpFuncSetErrno ( mitkIpFuncFLAG_ERROR );
       return ( mitkIpFuncERROR ); 
    }

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

  if ( keep == mitkIpFuncNoKeep )
    pic_new->data = malloc ( _mitkIpPicSize  ( pic_new ) );
  if ( pic_new->data == NULL )
    {  
       mitkIpPicFree ( pic_new );
       _mitkIpFuncSetErrno ( mitkIpFuncMALLOC_ERROR );
       return ( mitkIpFuncERROR );
    }

  /* macro to invert the picture (for all data types)                   */

  if ( keep == mitkIpFuncNoKeep )
    mitkIpPicFORALL_2 ( SUBI, pic_1, pic_2, pic_new )
  else if ( keep == mitkIpFuncKeep )
    mitkIpPicFORALL_2 ( SUBI3, pic_1, pic_2, pic_new )

  /* Copy Tags */

  mitkIpFuncCopyTags(pic_new, pic_1);
                        
  return pic_new;
}
Exemplo n.º 25
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 );
}