Esempio n. 1
0
uint ImageInstance::getPixelValue(long x, long y, EP_Representation &r) const
{
    DicomImage *image;
    if (rawType) image = rawImage;
    else image = dcmImage;

    if (image) {
        const DiPixel* pixel = image->getInterData();
        if (pixel && (x < (long)image->getWidth()) && (x >= 0)
                && (y < (long)image->getHeight()) && (y >= 0)) {
            r = pixel->getRepresentation();
            switch (r) {
            case EPR_Sint8:
            case EPR_Uint8:
                return *((char*)(pixel->getData()) + (y * image->getWidth() + x));
            case EPR_Sint16:
            case EPR_Uint16:
                return *((short*)(pixel->getData()) + (y * image->getWidth() + x));
            case EPR_Sint32:
            case EPR_Uint32:
                return *((int*)(pixel->getData()) + (y * image->getWidth() + x));
            }
        }
    }

    r = (EP_Representation)-1;
    return 0;
}
Esempio n. 2
0
Dose::Dose( RTDcmtkDicomInterface* rtDoseDicom, Plan* rtPlan)
{
	d = new DoseData();
	d->m_dcmGeometry = vtkSmartPointer<DCMGeometry>::New();
	d->m_resampledDoseCuboid = NULL;
	d->m_resampled_dcm_geometry = NULL;
	DcmItem *ditem = NULL;
	const char * refPlanUID = NULL;
	this->frameRefUID = string(rtDoseDicom->Get_FRAME_OF_REFER_UID());
	this->sopUID = string(rtDoseDicom->Get_SOP_INSTANCE_UID());
	this->studyUID = string(rtDoseDicom->Get_STUDY_INSTANCE_UID());
	this->seriesUID = string(rtDoseDicom->Get_SERIES_INSTANCE_UID());
	
	this->height = rtDoseDicom->Get_ROW();
	this->width = rtDoseDicom->Get_COLOUMN();

	double image_orientation[6] = { 0, 0, 0, 0, 0, 0 };
	string imageOrientation = string(rtDoseDicom->Get_IMAGE_ORIENTATION());
	vector<string> _imageOrientation;
	tokenize(imageOrientation, _imageOrientation, "\\", true);
	for (int i = 0; i<_imageOrientation.size(); i++)
	{
		image_orientation[i] = convert_to_double(_imageOrientation.at(i).c_str());
	}

	double xorient[3] = { image_orientation[0], image_orientation[1], image_orientation [2]};
	double yorient[3] = { image_orientation[3], image_orientation[4], image_orientation [5]};


	double firstImagePosition[3] = { 0, 0, 0};
	double lastImagePosition[3] = { 0, 0, 0};
	string imagePosition = string(rtDoseDicom->Get_IMAGE_POSITION());
	vector<string> _imagePosition;
	tokenize(imagePosition, _imagePosition, "\\", true);
	for (int i = 0; i<_imagePosition.size(); i++)
	{
		firstImagePosition[i] = convert_to_double(_imagePosition.at(i).c_str());
		lastImagePosition[i] = convert_to_double(_imagePosition.at(i).c_str());
	}

	rtDoseDicom->getMinMaxPixelValue(this->minDosePixelValue, this->maxDosePixelValue);// MinMax pixel value

	this->doseGridScaling = convert_to_double(rtDoseDicom->Get_DOSEGRID_SCALING());

	OFString _gridFrameOffsetVector;
	vector<string> temp;
	if (rtDoseDicom->dataset->findAndGetOFStringArray(DCM_GridFrameOffsetVector, _gridFrameOffsetVector).bad())
	{
		isMultiframe = false;
		//		return;
	}
	else
	{
		isMultiframe = true;
		tokenize(_gridFrameOffsetVector.c_str(), temp, "\\", true);
		// Convert offset vector points from string to float and store permanently.
		for (int i = 0; i<temp.size(); i++)
		{
			this->doseGridOffsetVector.push_back(atof(temp.at(i).c_str()));
		}
	}

	if (isMultiframe)
		lastImagePosition[2] += doseGridOffsetVector.at(doseGridOffsetVector.size() - 1);
	else
		lastImagePosition[2] += 0;

	double spacing[3] = { 0, 0, 0 };
	const char* _pixelSpacing = rtDoseDicom->Get_PIXEL_SPACING();
	vector<string> temp1;
	tokenize(_pixelSpacing, temp1, "\\", true);
	spacing[0] = convert_to_double(temp1.at(0).c_str());
	spacing[1] = convert_to_double(temp1.at(1).c_str());
	if (isMultiframe)
	{
		spacing[2] = this->doseGridOffsetVector.at(1) - this->doseGridOffsetVector.at(0);// hardcoded.
	}
	else
	{
		//this->pixelSpacing[0] = 0;
		//this->pixelSpacing[1] = 0;
		spacing[2] = 0;
	}

	int numberOfFrames = convert_to_int(rtDoseDicom->Get_NO_OF_FRAMES());
	int dim[3] = { this->width, this->height, numberOfFrames };

	
	d->m_dcmGeometry->SetImageGeometry(firstImagePosition, spacing, dim, xorient, yorient);

	//Populate frame wise pixel data in map<int,void*> framePixelData;
	int noOfBytes = rtDoseDicom->Get_BITS_ALLOCATED() / 8;
	DJDecoderRegistration::registerCodecs();
	DcmRLEDecoderRegistration::registerCodecs();

	DicomImage * dicomImage = new DicomImage(&rtDoseDicom->file_format, rtDoseDicom->file_format.getDataset()->getOriginalXfer(),
		CIF_UsePartialAccessToPixelData, 0, 1 /* fcount */);

	DJDecoderRegistration::cleanup();
	DcmRLEDecoderRegistration::cleanup();

	

	do{
		const DiPixel* diPixel = dicomImage->getInterData();
		doseFrameData* dfData = new doseFrameData();
		dfData->frameNumber = dicomImage->getFirstFrame();
		dfData->cols = rtDoseDicom->Get_COLOUMN();
		dfData->rows = rtDoseDicom->Get_ROW();

		// create a vtkImage
		vtkImageData* image = vtkImageData::New();

		image->SetDimensions(dfData->cols, dfData->rows, 1);
		image->SetSpacing(spacing[0], spacing[1], 1);
		image->Initialize();

		vtkDataArray* scalars = 0;

		switch (diPixel->getRepresentation())
		{
			case EPR_Uint8:
				/*dfData->ORG_pixelData = calloc(diPixel->getCount(),sizeof(U8DataType));
				memcpy(dfData->ORG_pixelData,diPixel->getData(),diPixel->getCount()*sizeof(U8DataType));
				dfData->ORG_DicomDataType = TYPE_U8Data;*/

				scalars = vtkUnsignedCharArray::New();
				((vtkUnsignedCharArray*)(scalars))->SetArray((unsigned char*)diPixel->getData(), diPixel->getCount(), 1);
				break;

			case EPR_Sint8:

				/*dfData->ORG_pixelData = calloc(diPixel->getCount(),sizeof(S8DataType));
				memcpy(dfData->ORG_pixelData,diPixel->getData(),diPixel->getCount()*sizeof(S8DataType));
				dfData->ORG_DicomDataType = TYPE_S8Data;*/

				scalars = vtkSignedCharArray::New();
				((vtkSignedCharArray*)(scalars))->SetArray((signed char*)diPixel->getData(), diPixel->getCount(), 1);
				break;

			case EPR_Uint16:
				/*dfData->ORG_pixelData = calloc(diPixel->getCount(),sizeof(U16DataType));
				memcpy(dfData->ORG_pixelData,diPixel->getData(),diPixel->getCount()*sizeof(U16DataType));
				dfData->ORG_DicomDataType = TYPE_U16Data;*/

				scalars = vtkUnsignedShortArray::New();
				((vtkUnsignedShortArray*)(scalars))->SetArray((unsigned short*)diPixel->getData(), diPixel->getCount(), 1);
				break;

			case EPR_Sint16:

				/*dfData->ORG_pixelData = calloc(diPixel->getCount(),sizeof(S16DataType));
				memcpy(dfData->ORG_pixelData,diPixel->getData(),diPixel->getCount()*sizeof(S16DataType));
				dfData->ORG_DicomDataType = TYPE_S16Data;*/

				scalars = vtkShortArray::New();
				((vtkShortArray*)(scalars))->SetArray((short*)diPixel->getData(), diPixel->getCount(), 1);
				break;

			case EPR_Uint32:
				/*dfData->ORG_pixelData = calloc(diPixel->getCount(),sizeof(U32DataType));
				memcpy(dfData->ORG_pixelData,diPixel->getData(),diPixel->getCount()*sizeof(U32DataType));
				dfData->ORG_DicomDataType = TYPE_U32Data;*/

				scalars = vtkUnsignedIntArray::New();
				((vtkUnsignedIntArray*)(scalars))->SetArray((unsigned int*)diPixel->getData(), diPixel->getCount(), 1);

				break;

			case EPR_Sint32:
				/*dfData->ORG_pixelData = calloc(diPixel->getCount(),sizeof(S32DataType));
				memcpy(dfData->ORG_pixelData,diPixel->getData(),diPixel->getCount()*sizeof(S32DataType));
				dfData->ORG_DicomDataType = TYPE_S32Data;*/

				scalars = vtkIntArray::New();
				((vtkIntArray*)(scalars))->SetArray((int*)diPixel->getData(), diPixel->getCount(), 1);
				break;
			default:
				RAD_LOG_CRITICAL("DCMTK EP_Representation type:" << diPixel->getRepresentation() << " not supported");
		}

		scalars->SetNumberOfComponents(1);
		image->SetDimensions(dfData->cols, dfData->rows, 1);

		image->SetSpacing(spacing[0], spacing[1], 1.0);

		image->GetPointData()->SetScalars(scalars);
		image->GetPointData()->GetScalars()->SetName("DICOMImage");
		//image->Update();
		double bounds[6];
		image->GetBounds(bounds);

		// type cast all images to unsigned int32
		vtkImageCast* imageCast = vtkImageCast::New();
		imageCast->SetInputData(image);
		imageCast->SetOutputScalarTypeToFloat();
		imageCast->Update();

		dfData->pixelData = calloc(diPixel->getCount(), sizeof(U32DataType));
		dfData->dicomDataType = TYPE_U32Data;
		RAD_LOG_INFO("MEMCPY pixel data for frame:" << dicomImage->getFirstFrame())

			memcpy(dfData->pixelData, imageCast->GetOutput()->GetPointData()->GetScalars()->GetVoidPointer(0),
			imageCast->GetOutput()->GetPointData()->GetScalars()->GetSize()*sizeof(float));

		imageCast->Delete();
		scalars->Delete();
		image->Delete();

		this->allFrameDoseData.push_back(dfData);

	} while (dicomImage->processNextFrames());

	/// compute cuboid

	// get first dose frame data
	doseFrameData* dfData = allFrameDoseData.at(0);

	// create VTK Cuboid
	int dimension[3] = { dfData->cols, dfData->rows, allFrameDoseData.size() };
	long dicomSliceSize = dimension[0] * dimension[1];
	long dicomDataSize = dimension[0] * dimension[1] * dimension[2];
	long dicomDataIdx = dicomSliceSize;

	void* dicomData = rad_get_memory(dicomDataSize * sizeof(float));
#ifdef ULTA
	for (int i = allFrameDoseData.size() - 1; i >= 0; i--)
#else
	for (int i = 0;i< allFrameDoseData.size();i++)
#endif
	{
		float* dicomData2 = static_cast<float*>(dicomData);
		dicomData2 += dicomDataSize;
		memcpy(dicomData2 - dicomDataIdx, allFrameDoseData.at(i)->pixelData, dicomSliceSize*sizeof(float));
		dicomDataIdx += dicomSliceSize;
	}

	vtkDataArray* scalars = 0;
	scalars = vtkFloatArray::New();
	((vtkFloatArray*)(scalars))->SetArray((float*)dicomData, dicomDataSize, 1);
	scalars->SetNumberOfComponents(1);

	d->m_doseCuboid = vtkImageData::New();
	d->m_doseCuboid->SetDimensions(dimension);
	d->m_doseCuboid->SetSpacing(spacing);

	//double lip[3] = { 0, 0, 0 };
	//d->m_dcmGeometry->GetLastImagePosition(lip);
	//d->m_doseCuboid->SetOrigin(firstImagePosition);

	d->m_doseCuboid->GetPointData()->SetScalars(scalars);
	d->m_doseCuboid->GetPointData()->GetScalars()->SetName("Dose Cuboid");
	d->m_doseCuboid->Modified();



	rxDose = rtPlan->getTargetPrescribedDose();
	int dosemax = int(this->maxDosePixelValue * this->doseGridScaling * 10000 / rxDose);
	// ISODOSE LEVEL(s) READY
	this->isodoses.push_back(new isodose(dosemax, 120, 0, 0, "", false, 0, 0, "Built-In"));
	this->isodoses.push_back(new isodose(102, 170, 0, 0, "", false, 0, 0, "Built-In"));
	this->isodoses.push_back(new isodose(100, 238, 69, 0, "", false, 0, 0, "Built-In"));
	this->isodoses.push_back(new isodose(98, 255, 65, 0, "", false, 0, 0, "Built-In"));
	this->isodoses.push_back(new isodose(95, 255, 255, 0, "", false, 0, 0, "Built-In"));
	this->isodoses.push_back(new isodose(90, 0, 255, 0, "", false, 0, 0, "Built-In"));
	this->isodoses.push_back(new isodose(80, 0, 139, 0, "", false, 0, 0, "Built-In"));
	this->isodoses.push_back(new isodose(70, 0, 255, 255, "", false, 0, 0, "Built-In"));
	this->isodoses.push_back(new isodose(50, 0, 0, 255, "", false, 0, 0, "Built-In"));
	this->isodoses.push_back(new isodose(30, 0, 0, 128, "", false, 0, 0, "Built-In"));
	// Calculate cGy Value and append to names.
	for (int i = 0; i<this->isodoses.size(); i++)
	{
		isodose* iso = this->isodoses.at(i);
		iso->cGyValue = (int)(iso->level * rxDose / 100);
		iso->name.append(convert_to_string(iso->level));
		iso->name.append("% - ");
		iso->name.append(convert_to_string(iso->cGyValue));
		iso->name.append(" cGy");
		iso->pixelIntensity = iso->level * rxDose / (this->doseGridScaling * 10000);
	}

	// load DV data
	DcmItem *dvhItem = NULL;
	signed long dvhCount = 0;
	while (rtDoseDicom->dataset->findAndGetSequenceItem(DCM_DVHSequence, dvhItem, dvhCount++).good())
	{
		DVH* dh = new DVH();

		DcmItem *dvhRefROISequenceItem = NULL;

		// ROI Number
		const char * RoiNumber = NULL;
		if (dvhItem->findAndGetSequenceItem(DCM_DVHReferencedROISequence, dvhRefROISequenceItem, 0).good())
		{
			OFCondition status = dvhItem->findAndGetString(DCM_ReferencedROINumber, RoiNumber, true);
			dh->setROINumber(convert_to_uint(RoiNumber));
		}

		// DVH Data
		string fNm = "D:\\DVH_DICOM_";
		fNm.append(convert_to_string(dvhCount));
		fNm.append(".csv");
		//ofstream Morison_File(fNm.c_str());

		const char * dvhdata = NULL;
		dvhItem->findAndGetString(DCM_DVHData, dvhdata);

		vector<string> dvhdataListTemp;
		tokenize(dvhdata, dvhdataListTemp, "\\");
		for (int i = 0; i < dvhdataListTemp.size(); i++)
		{
			i++;
			string volume = dvhdataListTemp.at(i);
			if (0 > int(atof(volume.c_str())))
				volume = "0";
			dh->dvhDataList.push_back(convert_to_double(volume.c_str()));
	//		Morison_File << volume.c_str() << endl;
		}
		//Morison_File.close();

		//convert volume information in dvhDataList from abosulte to percentage.
		dh->setMaxDVHValue(*std::max_element(dh->dvhDataList.begin(), dh->dvhDataList.end()));

		/*std::transform(dh->dvhDataList.begin(), dh->dvhDataList.end(),
			dh->dvhDataList.begin(), std::bind2nd(std::divides<double>(), dh->getMaxDVHValue()));

		std::transform(dh->dvhDataList.begin(), dh->dvhDataList.end(),
			dh->dvhDataList.begin(), std::bind2nd(std::multiplies<double>(), 100));*/


		// DVH Mini Dose
		const char * dvhMinDose = NULL;
		if (dvhItem->findAndGetString(DCM_DVHMinimumDose, dvhMinDose).good())
			dh->setDVHMiniDose(convert_to_double(dvhMinDose));

		//DVH Max Dose
		const char * dvhMaxDose = NULL;
		if (dvhItem->findAndGetString(DCM_DVHMaximumDose, dvhMaxDose).good())
			dh->setDVHMaxDose(convert_to_double(dvhMaxDose));

		//DVH Mean Dose
		const char * dvhMeanDose = NULL;
		if (dvhItem->findAndGetString(DCM_DVHMeanDose, dvhMeanDose).good())
			dh->setDVHMeanDose(convert_to_double(dvhMeanDose));

		// Structureset ref SOPUID
		DcmItem *dvhRefStructureSet = NULL;
		const char * structSetRefSOPUID = NULL;
		if (rtDoseDicom->dataset->findAndGetSequenceItem(DCM_ReferencedStructureSetSequence, dvhRefStructureSet).good())
		{
			if (dvhRefStructureSet->findAndGetString(DCM_ReferencedSOPInstanceUID, structSetRefSOPUID, true).good())
			dh->setStrucSetRefSopUID(string(structSetRefSOPUID));
		}
		this->dh.push_back(dh);
	}
}