void DICOMSegmentationIO::Write()
  {
    ValidateOutputLocation();

    mitk::LocaleSwitch localeSwitch("C");
    LocalFile localFile(this);
    const std::string path = localFile.GetFileName();

    auto input = dynamic_cast<const LabelSetImage *>(this->GetInput());
    if (input == nullptr)
      mitkThrow() << "Cannot write non-image data";

    // Get DICOM information from referenced image
    vector<DcmDataset *> dcmDatasets;
    DcmFileFormat *readFileFormat = new DcmFileFormat();
    try
    {
      // TODO: Generate dcmdataset witk DICOM tags from property list; ATM the source are the filepaths from the
      // property list
      mitk::StringLookupTableProperty::Pointer filesProp =
        dynamic_cast<mitk::StringLookupTableProperty *>(input->GetProperty("files").GetPointer());

      if (filesProp.IsNull())
      {
        mitkThrow() << "No property with dicom file path.";
        return;
      }

      StringLookupTable filesLut = filesProp->GetValue();
      const StringLookupTable::LookupTableType &lookUpTableMap = filesLut.GetLookupTable();

      for (auto it : lookUpTableMap)
      {
        const char *fileName = (it.second).c_str();
        if (readFileFormat->loadFile(fileName, EXS_Unknown).good())
          dcmDatasets.push_back(readFileFormat->getAndRemoveDataset());
      }
    }
    catch (const std::exception &e)
    {
      MITK_ERROR << "An error occurred while getting the dicom informations: " << e.what() << endl;
      return;
    }

    // Iterate over all layers. For each a dcm file will be generated
    for (unsigned int layer = 0; layer < input->GetNumberOfLayers(); ++layer)
    {
      vector<itkInternalImageType::Pointer> segmentations;

      try
      {
        // Cast mitk layer image to itk
        ImageToItk<itkInputImageType>::Pointer imageToItkFilter = ImageToItk<itkInputImageType>::New();
        // BUG: It must be the layer image, but there are some errors with it (dcmqi: generate the dcmSeg "No frame data
        // available") --> input->GetLayerImage(layer)
        imageToItkFilter->SetInput(input);
        imageToItkFilter->Update();

        // Cast from original itk type to dcmqi input itk image type
        typedef itk::CastImageFilter<itkInputImageType, itkInternalImageType> castItkImageFilterType;
        castItkImageFilterType::Pointer castFilter = castItkImageFilterType::New();
        castFilter->SetInput(imageToItkFilter->GetOutput());
        castFilter->Update();

        itkInternalImageType::Pointer itkLabelImage = castFilter->GetOutput();
        itkLabelImage->DisconnectPipeline();

        // Iterate over all labels. For each a segmentation image will be created
        const LabelSet *labelSet = input->GetLabelSet(layer);
        for (auto itLabel = labelSet->IteratorConstBegin(); itLabel != labelSet->IteratorConstEnd(); ++itLabel)
        {
          // Thresold over the image with the given label value
          itk::ThresholdImageFilter<itkInternalImageType>::Pointer thresholdFilter =
            itk::ThresholdImageFilter<itkInternalImageType>::New();
          thresholdFilter->SetInput(itkLabelImage);
          thresholdFilter->ThresholdOutside(itLabel->first, itLabel->first);
          thresholdFilter->SetOutsideValue(0);
          thresholdFilter->Update();
          itkInternalImageType::Pointer segmentImage = thresholdFilter->GetOutput();
          segmentImage->DisconnectPipeline();

          segmentations.push_back(segmentImage);
        }
      }
      catch (const itk::ExceptionObject &e)
      {
        MITK_ERROR << e.GetDescription() << endl;
        return;
      }

      // Create segmentation meta information
      const std::string &tmpMetaInfoFile = this->CreateMetaDataJsonFile(layer);

      MITK_INFO << "Writing image: " << path << std::endl;
      try
      {
        // Convert itk segmentation images to dicom image
        dcmqi::ImageSEGConverter *converter = new dcmqi::ImageSEGConverter();
        DcmDataset *result = converter->itkimage2dcmSegmentation(dcmDatasets, segmentations, tmpMetaInfoFile);

        // Write dicom file
        DcmFileFormat dcmFileFormat(result);

        std::string filePath = path.substr(0, path.find_last_of("."));
        // If there is more than one layer, we have to write more than 1 dicom file
        if (input->GetNumberOfLayers() != 1)
          filePath = filePath + std::to_string(layer) + ".dcm";
        else
          filePath = filePath + ".dcm";

        dcmFileFormat.saveFile(filePath.c_str(), EXS_LittleEndianExplicit);

        // Clean up
        if (converter != nullptr)
          delete converter;
        if (result != nullptr)
          delete result;
      }
      catch (const std::exception &e)
      {
        MITK_ERROR << "An error occurred during writing the DICOM Seg: " << e.what() << endl;
        return;
      }
    } // Write a dcm file for the next layer

    // End of image writing; clean up
    if (readFileFormat)
      delete readFileFormat;

    for (auto obj : dcmDatasets)
      delete obj;
    dcmDatasets.clear();
  }
Ejemplo n.º 2
0
AUI_ERRCODE ctp2_Button::InitCommonLdl(MBCHAR *ldlBlock, MBCHAR *ldlTemplate,
									   sint32 x, sint32 y,
									   sint32 width, sint32 height)
{
    ldl_datablock * theBlock = aui_Ldl::FindDataBlock(ldlBlock);

    if (!theBlock)
	{
        theBlock = aui_Ldl::FindDataBlock(ldlTemplate);
		if (!theBlock)
        {
            return AUI_ERRCODE_HACK;
		}
	}

	m_isToggle = theBlock->GetBool("togglebutton") ? true : false;
	if(m_isToggle) {
		m_toggleState = false;
	}

	for(sint32 layerIndex = 0; layerIndex < GetNumberOfLayers(); layerIndex++) {




		char layerIndexString[20];
		sprintf(layerIndexString, "%d", layerIndex);

		InitializeLayerFlag(theBlock, layerIndex, k_CTP2_BUTTON_LDL_LAYER_UP,
							k_CTP2_BUTTON_LAYER_FLAG_UP, layerIndexString);

		InitializeLayerFlag(theBlock, layerIndex, k_CTP2_BUTTON_LDL_LAYER_DOWN,
							k_CTP2_BUTTON_LAYER_FLAG_DOWN, layerIndexString);

		if(m_isToggle) {
			InitializeLayerFlag(theBlock, layerIndex, k_CTP2_BUTTON_LDL_LAYER_UP_ON,
								k_CTP2_BUTTON_LAYER_FLAG_UP_ON, layerIndexString);
		}
	}

	m_ignoreHighlight = theBlock->GetBool("nohighlight") ? true : false;
	if(m_ignoreHighlight) {
		m_drawMask &= ~(k_AUI_REGION_DRAWFLAG_MOUSEMOVEOVER | k_AUI_REGION_DRAWFLAG_MOUSEMOVEAWAY);
	}

	m_isDropdownButton = theBlock->GetBool("dropdownbutton") ? true : false;
	if(m_isDropdownButton) {

		m_drawMask = k_AUI_REGION_DRAWFLAG_UPDATE;
	}


	if(!IsDisabled()) {
		if(!m_isToggle || !m_toggleState) {
			AddRenderFlags(k_CTP2_BUTTON_LAYER_FLAG_UP);
			RemoveRenderFlags(k_CTP2_BUTTON_LAYER_FLAG_UP_ON);
		} else {
			AddRenderFlags(k_CTP2_BUTTON_LAYER_FLAG_UP_ON);
			RemoveRenderFlags(k_CTP2_BUTTON_LAYER_FLAG_UP);
		}
	}

	m_attract = false;

	return AUI_ERRCODE_OK;
}