Esempio n. 1
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);
        }
    }
}
Esempio n. 2
0
void
ipMITKSegmentationUndoDisable (mitkIpPicDescriptor* segmentation)
{
    mitkIpPicTSV_t *undo;

    assert (segmentation);
    undo = mitkIpPicDelTag (segmentation, tagUNDO);
    mitkIpPicFreeTag (undo);
}
void mitk::PicFileReader::FillImage(Image::Pointer output)
{
  mitkIpPicDescriptor *outputPic = mitkIpPicNew();
  outputPic = CastToIpPicDescriptor(output, nullptr, outputPic);
  mitkIpPicDescriptor *pic = mitkIpPicGet(const_cast<char *>(this->GetLocalFileName().c_str()), outputPic);
  // comes upside-down (in MITK coordinates) from PIC file
  ConvertHandedness(pic);

  mitkIpPicTSV_t *tsv;
  if ((tsv = mitkIpPicQueryTag(pic, "SOURCE HEADER")) != NULL)
  {
    if (tsv->n[0] > 1e+06)
    {
      mitkIpPicTSV_t *tsvSH;
      tsvSH = mitkIpPicDelTag(pic, "SOURCE HEADER");
      mitkIpPicFreeTag(tsvSH);
    }
  }
  if ((tsv = mitkIpPicQueryTag(pic, "ICON80x80")) != NULL)
  {
    mitkIpPicTSV_t *tsvSH;
    tsvSH = mitkIpPicDelTag(pic, "ICON80x80");
    mitkIpPicFreeTag(tsvSH);
  }
  if ((tsv = mitkIpPicQueryTag(pic, "VELOCITY")) != NULL)
  {
    mitkIpPicDescriptor *header = mitkIpPicCopyHeader(pic, NULL);
    header->data = tsv->value;
    ConvertHandedness(header);
    output->SetChannel(header->data, 1);
    header->data = NULL;
    mitkIpPicFree(header);
    mitkIpPicDelTag(pic, "VELOCITY");
  }

  // Copy the memory to avoid mismatches of malloc() and delete[].
  // mitkIpPicGet will always allocate a new memory block with malloc(),
  // but MITK Images delete the data via delete[].
  output->SetImportChannel(pic->data, 0, Image::CopyMemory);
  pic->data = nullptr;
  mitkIpPicFree(pic);
}
Esempio n. 4
0
void mitk::PicFileReader::GenerateOutputInformation()
{
    Image::Pointer output = this->GetOutput();

    if ((output->IsInitialized()) && (this->GetMTime() <= m_ReadHeaderTime.GetMTime()))
        return;

    itkDebugMacro(<<"Reading file for GenerateOutputInformation()" << m_FileName);

    // Check to see if we can read the file given the name or prefix
    //
    if ( m_FileName == "" && m_FilePrefix == "" )
    {
        throw itk::ImageFileReaderException(__FILE__, __LINE__, "One of FileName or FilePrefix must be non-empty");
    }

    if( m_FileName != "")
    {
        mitkIpPicDescriptor* header=mitkIpPicGetHeader(const_cast<char *>(m_FileName.c_str()), NULL);

        if ( !header )
        {
            throw itk::ImageFileReaderException(__FILE__, __LINE__, "File could not be read.");
        }

        header=MITKipPicGetTags(const_cast<char *>(m_FileName.c_str()), header);

        int channels = 1;

        mitkIpPicTSV_t *tsv;
        if ( (tsv = mitkIpPicQueryTag( header, "SOURCE HEADER" )) != NULL)
        {
          if(tsv->n[0]>1e+06)
          {
            mitkIpPicTSV_t *tsvSH;
            tsvSH = mitkIpPicDelTag( header, "SOURCE HEADER" );
            mitkIpPicFreeTag(tsvSH);
          }
        }
        if ( (tsv = mitkIpPicQueryTag( header, "ICON80x80" )) != NULL)
        {
          mitkIpPicTSV_t *tsvSH;
          tsvSH = mitkIpPicDelTag( header, "ICON80x80" );
          mitkIpPicFreeTag(tsvSH);
        }
        if ( (tsv = mitkIpPicQueryTag( header, "VELOCITY" )) != NULL)
        {
          ++channels;
          mitkIpPicDelTag( header, "VELOCITY" );
        }

        if( header == NULL || header->bpe == 0)
        {
            itk::ImageFileReaderException e(__FILE__, __LINE__);
            itk::OStringStream msg;
            msg << " Could not read file "
                << m_FileName.c_str();
            e.SetDescription(msg.str().c_str());
            throw e;
            return;
        }

        // First initialize the geometry of the output image by the pic-header
        SlicedGeometry3D::Pointer slicedGeometry = mitk::SlicedGeometry3D::New();
        PicHelper::InitializeEvenlySpaced(header, header->n[2], slicedGeometry);

        // if pic image only 3D, the n[3] value is not initialized
        unsigned int timesteps = 1;
        if( header->dim > 3 )
            timesteps = header->n[3];

        TimeSlicedGeometry::Pointer timeSliceGeometry = TimeSlicedGeometry::New();
        timeSliceGeometry->InitializeEvenlyTimed(slicedGeometry, timesteps);
        timeSliceGeometry->ImageGeometryOn();

        // Cast the pic descriptor to ImageDescriptor and initialize the output

        output->Initialize( CastToImageDescriptor(header));
        output->SetGeometry( timeSliceGeometry );
        mitkIpPicFree ( header );
    }
    else
    {
        int numberOfImages=0;
        m_StartFileIndex=0;

        mitkIpPicDescriptor* header=NULL;

        char fullName[1024];

        while(m_StartFileIndex<10)
        {
            sprintf(fullName, m_FilePattern.c_str(), m_FilePrefix.c_str(), m_StartFileIndex+numberOfImages);
            FILE * f=fopen(fullName,"r");
            if(f==NULL) 
            {
                //already found an image?
                if(numberOfImages>0)
                    break;
                //no? let's increase start
                ++m_StartFileIndex;
            }
            else
            {
                fclose(f);
                //only open the header of the first file found,
                //@warning what to do when images do not have the same size??
                if(header==NULL) 
                {
                    header=mitkIpPicGetHeader(fullName, NULL);
                    header=MITKipPicGetTags(fullName, header);
                }
                ++numberOfImages;
            }
        }

        printf("\n numberofimages %d\n",numberOfImages);

        if(numberOfImages==0)
        {
            itk::ImageFileReaderException e(__FILE__, __LINE__);
            itk::OStringStream msg;
            msg << "no images found";
            e.SetDescription(msg.str().c_str());
            throw e;
            return;
        }

        //@FIXME: was ist, wenn die Bilder nicht alle gleich gross sind?
        if(numberOfImages>1)
        {  
            printf("\n numberofimages %d > 1\n",numberOfImages);
            header->dim=3;
            header->n[2]=numberOfImages;
        }

        printf(" \ninitialisize output\n");
        output->Initialize( CastToImageDescriptor(header) );
        mitkIpPicFree ( header );
    }

    m_ReadHeaderTime.Modified();
}
Esempio n. 5
0
void mitk::PicFileReader::GenerateData()
{
    Image::Pointer output = this->GetOutput();

    // Check to see if we can read the file given the name or prefix
    //
    if ( m_FileName == "" && m_FilePrefix == "" )
    {
        throw itk::ImageFileReaderException(__FILE__, __LINE__, "One of FileName or FilePrefix must be non-empty");
    }

    if( m_FileName != "")
    {
        mitkIpPicDescriptor* outputPic = mitkIpPicNew();
        outputPic = CastToIpPicDescriptor(output, outputPic);
        mitkIpPicDescriptor* pic=MITKipPicGet(const_cast<char *>(m_FileName.c_str()),
                                              outputPic);
        // comes upside-down (in MITK coordinates) from PIC file
        ConvertHandedness(pic);

        mitkIpPicTSV_t *tsv;
        if ( (tsv = mitkIpPicQueryTag( pic, "SOURCE HEADER" )) != NULL)
        {
          if(tsv->n[0]>1e+06)
          {
            mitkIpPicTSV_t *tsvSH;
            tsvSH = mitkIpPicDelTag( pic, "SOURCE HEADER" );
            mitkIpPicFreeTag(tsvSH);
          }
        }
        if ( (tsv = mitkIpPicQueryTag( pic, "ICON80x80" )) != NULL)
        {
          mitkIpPicTSV_t *tsvSH;
          tsvSH = mitkIpPicDelTag( pic, "ICON80x80" );
          mitkIpPicFreeTag(tsvSH);
        }
        if ( (tsv = mitkIpPicQueryTag( pic, "VELOCITY" )) != NULL)
        {
          mitkIpPicDescriptor* header = mitkIpPicCopyHeader(pic, NULL);
          header->data = tsv->value;
          ConvertHandedness(header);
          output->SetChannel(header->data, 1);
          header->data = NULL;
          mitkIpPicFree(header);
          mitkIpPicDelTag( pic, "VELOCITY" );
        }

        //slice-wise reading
        //currently much too slow.
        //else
        //{
        //  int sstart, smax;
        //  int tstart, tmax;

        //  sstart=output->GetRequestedRegion().GetIndex(2);
        //  smax=sstart+output->GetRequestedRegion().GetSize(2);

        //  tstart=output->GetRequestedRegion().GetIndex(3);
        //  tmax=tstart+output->GetRequestedRegion().GetSize(3);

        //  int s,t;
        //  for(s=sstart; s<smax; ++s)
        //  {
        //    for(t=tstart; t<tmax; ++t)
        //    {
        //      mitkIpPicDescriptor* pic=mitkIpPicGetSlice(const_cast<char *>(m_FileName.c_str()), NULL, t*smax+s+1);
        //      output->SetPicSlice(pic,s,t);
        //    }
        //  }
        //}
    }
    else
    {
        int position;
        mitkIpPicDescriptor*  pic=NULL;

        int zDim=(output->GetDimension()>2?output->GetDimensions()[2]:1);
        printf("\n zdim is %u \n",zDim);

        for (position = 0; position < zDim; ++position) 
        {
            char fullName[1024];

            sprintf(fullName, m_FilePattern.c_str(), m_FilePrefix.c_str(), m_StartFileIndex+position);

            pic=MITKipPicGet(fullName, pic);
            if(pic==NULL)
            {
                itkDebugMacro("Pic file '" << fullName << "' does not exist."); 
            }
            /* FIXME else
            if(output->SetPicSlice(pic, position)==false)
            {
                itkDebugMacro("Image '" << fullName << "' could not be added to Image."); 
            }*/
       }
       if(pic!=NULL)
         mitkIpPicFree(pic);
    }
}
Esempio n. 6
0
int
ipMITKSegmentationReplaceRegion4N( mitkIpPicDescriptor *seg, int startOfs, mitkIpInt1_t newValue )
{
  mitkIpPicTSV_t* tag;

  std::queue<int> ofsQueue;

  if (ipMITKSegmentationUndoIsEnabled (seg)) {
    ipMITKSegmentationUndoSave (seg);
  }
  tag = mitkIpPicDelTag (seg, tagSEGMENTATION_EMPTY);
  if (tag) {
    mitkIpPicFreeTag (tag);
  }

  if (seg->bpe != 8) return 0;

  int line = seg->n[0];
  int maxOfs = (int)(line * seg->n[1]);
  int testOfs;
  mitkIpInt1_t replaceMe = *((mitkIpInt1_t*)seg->data + startOfs);
  if (replaceMe == newValue) return 0;
  
  mitkIpInt1_t segVal;
  ofsQueue.push( startOfs );
  *((mitkIpInt1_t*)seg->data+startOfs) = newValue;
  int regionSize = 0;

  while (!ofsQueue.empty()) {
    int nextOfs = ofsQueue.front();
    ofsQueue.pop();
    regionSize++;
    // check right:
    testOfs = nextOfs+1;
    if (testOfs%line!=0) {
      segVal = *((mitkIpInt1_t*)seg->data+testOfs);
      if ( segVal == replaceMe ) {
        ofsQueue.push( testOfs );
        *((mitkIpInt1_t*)seg->data+testOfs) = newValue;
      }
    }
    // check top:
    testOfs = nextOfs-line;
    if (testOfs >= 0) {
      segVal = *((mitkIpInt1_t*)seg->data+testOfs);
      if ( segVal == replaceMe ) {
        ofsQueue.push( testOfs );
        *((mitkIpInt1_t*)seg->data+testOfs) = newValue;
      }
    }
    // check left:
    testOfs = nextOfs-1;
    if (nextOfs%line!=0) {
      segVal = *((mitkIpInt1_t*)seg->data+testOfs);
      if ( segVal == replaceMe ) {
        ofsQueue.push( testOfs );
        *((mitkIpInt1_t*)seg->data+testOfs) = newValue;
      }
    }
    // check bottom:
    testOfs = nextOfs+line;
    if (testOfs < maxOfs) {
      segVal = *((mitkIpInt1_t*)seg->data+testOfs);
      if ( segVal == replaceMe ) {
        ofsQueue.push( testOfs );
        *((mitkIpInt1_t*)seg->data+testOfs) = newValue;
      }
    }
  }
  return regionSize;
}
mitk::Image::Pointer mitk::PicFileReader::CreateImage()
{
  Image::Pointer output = Image::New();

  std::string fileName = this->GetLocalFileName();

  mitkIpPicDescriptor *header = mitkIpPicGetHeader(const_cast<char *>(fileName.c_str()), NULL);

  if (!header)
  {
    mitkThrow() << "File could not be read.";
  }

  header = mitkIpPicGetTags(const_cast<char *>(fileName.c_str()), header);

  int channels = 1;

  mitkIpPicTSV_t *tsv;
  if ((tsv = mitkIpPicQueryTag(header, "SOURCE HEADER")) != NULL)
  {
    if (tsv->n[0] > 1e+06)
    {
      mitkIpPicTSV_t *tsvSH;
      tsvSH = mitkIpPicDelTag(header, "SOURCE HEADER");
      mitkIpPicFreeTag(tsvSH);
    }
  }
  if ((tsv = mitkIpPicQueryTag(header, "ICON80x80")) != NULL)
  {
    mitkIpPicTSV_t *tsvSH;
    tsvSH = mitkIpPicDelTag(header, "ICON80x80");
    mitkIpPicFreeTag(tsvSH);
  }
  if ((tsv = mitkIpPicQueryTag(header, "VELOCITY")) != NULL)
  {
    ++channels;
    mitkIpPicDelTag(header, "VELOCITY");
  }

  if (header == NULL || header->bpe == 0)
  {
    mitkThrow() << " Could not read file " << fileName;
  }

  // if pic image only 2D, the n[2] value is not initialized
  unsigned int slices = 1;
  if (header->dim == 2)
  {
    header->n[2] = slices;
  }

  // First initialize the geometry of the output image by the pic-header
  SlicedGeometry3D::Pointer slicedGeometry = mitk::SlicedGeometry3D::New();
  PicHelper::InitializeEvenlySpaced(header, header->n[2], slicedGeometry);

  // if pic image only 3D, the n[3] value is not initialized
  unsigned int timesteps = 1;
  if (header->dim > 3)
  {
    timesteps = header->n[3];
  }

  slicedGeometry->ImageGeometryOn();
  ProportionalTimeGeometry::Pointer timeGeometry = ProportionalTimeGeometry::New();
  timeGeometry->Initialize(slicedGeometry, timesteps);

  // Cast the pic descriptor to ImageDescriptor and initialize the output

  output->Initialize(CastToImageDescriptor(header));
  output->SetTimeGeometry(timeGeometry);
  mitkIpPicFree(header);

  return output;
}