/// <summary>
        /// Run the decompressor.
        /// </summary>
        /// <param name="file"></param>
        public void Process(ClearCanvas.Dicom.DicomFile file)
        {
            var proposedTs = ClearCanvas.Dicom.TransferSyntax.GetTransferSyntax(OutputTransferSyntax);
            var currentTs  = file.TransferSyntax;

            ClearCanvas.Dicom.TransferSyntax finalTs = currentTs;

            if (proposedTs != TransferSyntax.ExplicitVrBigEndian &&
                proposedTs != TransferSyntax.ImplicitVrLittleEndian &&
                proposedTs != TransferSyntax.ExplicitVrLittleEndian)
            {
                log.Warn("Decompressor cannot supports target transfer syntax of EVBE, IVLE, EVLE only.  Using IVLE ..");
                finalTs = TransferSyntax.ImplicitVrLittleEndian;
            }
            else
            {
                finalTs = proposedTs;
            }

            log.Info(string.Format("Deompress: Proposed: {0}, current {1}, final {2}", proposedTs, currentTs, finalTs));

            if (currentTs != finalTs)
            {
                file.ChangeTransferSyntax(finalTs);
            }
        }
Exemplo n.º 2
0
        private void buttonCompress_Click(object sender, EventArgs e)
        {
            TransferSyntax syntax = this.comboBoxCompressionType.SelectedItem as TransferSyntax;
            if (syntax == null)
            {
                MessageBox.Show("Transfer syntax not selected");
                return;
            }

            DicomFile dicomFile = new DicomFile(textBoxSourceFile.Text);

            dicomFile.Load();
            if (dicomFile.TransferSyntax.Encapsulated)
            {
                MessageBox.Show(String.Format("Message encoded as {0}, cannot compress.", dicomFile.TransferSyntax));
                return;
            }

            dicomFile.Filename = textBoxDestinationFile.Text;

            dicomFile.ChangeTransferSyntax(syntax);

            dicomFile.Save();

        }
Exemplo n.º 3
0
        private void buttonDecompress_Click(object sender, EventArgs e)
        {
            if (this.textBoxSourceFile.Text.Length == 0 || this.textBoxDestinationFile.Text.Length == 0)
            {
                MessageBox.Show("Invalid source or destination filename");
                return;
            }

            DicomFile dicomFile = new DicomFile(textBoxSourceFile.Text);

            dicomFile.Load();

            dicomFile.Filename = textBoxDestinationFile.Text;

            dicomFile.ChangeTransferSyntax(TransferSyntax.ExplicitVrLittleEndian);

            dicomFile.Save();
        }
		private ProcessDuplicateResult ProcessDuplicate(DicomFile dupFile, WorkQueueUid uid, StudyXml studyXml)
		{
			var result = new ProcessDuplicateResult();

			var data = uid.SerializeWorkQueueUidData;

			string duplicateSopPath = ServerHelper.GetDuplicateUidPath(StorageLocation, uid);
			string basePath = StorageLocation.GetSopInstancePath(uid.SeriesInstanceUid, uid.SopInstanceUid);
			if (!File.Exists(basePath))
			{
				// NOTE: This is special case. The file which caused dicom service to think this sop is a duplicate
				// no longer exists in the study folder. Perhaps it has been moved to another folder during auto reconciliation.
				// We have nothing to compare against so let's just throw it into the SIQ queue.
				CreateDuplicateSIQEntry(uid, dupFile, null);
				result.ActionTaken = DuplicateProcessResultAction.Reconcile;
			}
			else
			{
				var duplicateEnum = data.DuplicateProcessing.HasValue ? data.DuplicateProcessing.Value : DuplicateProcessingEnum.Compare;

				// Check if system is configured to override the rule for this study
				if (duplicateEnum == DuplicateProcessingEnum.OverwriteSop)
				{
					return OverwriteDuplicate(dupFile, uid, studyXml);
				}

				// Check if system is configured to override the rule for this study
				if (duplicateEnum == DuplicateProcessingEnum.OverwriteSopAndUpdateDatabase)
				{
					return OverwriteAndUpdateDuplicate(dupFile, uid, studyXml);
				}

				var baseFile = new DicomFile(basePath);
				baseFile.Load();

				if (duplicateEnum == DuplicateProcessingEnum.OverwriteReport)
				{
					return ProcessDuplicateReport(dupFile, baseFile, uid, studyXml);
				}

				// DuplicateProcessingEnum.Compare
				if (!dupFile.TransferSyntax.Equals(baseFile.TransferSyntax))
				{
					// If they're compressed, and we have a codec, lets decompress and still do the comparison
					if (dupFile.TransferSyntax.Encapsulated
					    && !dupFile.TransferSyntax.LossyCompressed
					    && DicomCodecRegistry.GetCodec(dupFile.TransferSyntax) != null)
					{
						dupFile.ChangeTransferSyntax(TransferSyntax.ExplicitVrLittleEndian);
					}

					if (baseFile.TransferSyntax.Encapsulated
					    && !baseFile.TransferSyntax.LossyCompressed
					    && DicomCodecRegistry.GetCodec(baseFile.TransferSyntax) != null)
					{
						baseFile.ChangeTransferSyntax(TransferSyntax.ExplicitVrLittleEndian);
					}

					if (dupFile.TransferSyntax.Encapsulated || baseFile.TransferSyntax.Encapsulated)
					{
						string failure = String.Format("Base file transfer syntax is '{0}' while duplicate file has '{1}'",
						                               baseFile.TransferSyntax, dupFile.TransferSyntax);

						var list = new List<DicomAttributeComparisonResult>();
						var compareResult = new DicomAttributeComparisonResult
							{
								ResultType = ComparisonResultType.DifferentValues,
								TagName = DicomTagDictionary.GetDicomTag(DicomTags.TransferSyntaxUid).Name,
								Details = failure
							};
						list.Add(compareResult);
						CreateDuplicateSIQEntry(uid, dupFile, list);
						result.ActionTaken = DuplicateProcessResultAction.Reconcile;
						return result;
					}
				}

				var failureReason = new List<DicomAttributeComparisonResult>();
				if (baseFile.DataSet.Equals(dupFile.DataSet, ref failureReason))
				{
					Platform.Log(LogLevel.Info,
					             "Duplicate SOP being processed is identical.  Removing SOP: {0}",
					             baseFile.MediaStorageSopInstanceUid);


					RemoveWorkQueueUid(uid, duplicateSopPath);
					result.ActionTaken = DuplicateProcessResultAction.Delete;

				}
				else
				{
					CreateDuplicateSIQEntry(uid, dupFile, failureReason);
					result.ActionTaken = DuplicateProcessResultAction.Reconcile;
				}
			}

            return result;
        }
Exemplo n.º 5
0
		public void ChangeAspectRatio(DicomFile file)
		{
			if (NewAspectRatio.IsNull || FloatComparer.AreEqual(NewAspectRatio.Value, 1))
				throw new InvalidOperationException("Invalid new aspect ratio");

			if (file.TransferSyntax.Encapsulated)
				file.ChangeTransferSyntax(TransferSyntax.ExplicitVrLittleEndian);

			OldInfo = new PixelDataInfo(file.DataSet);
			if (!OldInfo.IsSquare)
				throw new ArgumentException("Pixels are already non-square.");

			NewInfo = OldInfo.Clone();
			NewInfo.AspectRatio = NewAspectRatio;

			if (IncreasePixelDimensions)
			{
				if (NewAspectRatio.Value < 1)
					NewInfo.Rows = (int)(OldInfo.Rows / NewAspectRatio.Value + 0.5);
				else
					NewInfo.Columns = (int)(OldInfo.Columns * NewAspectRatio.Value + 0.5);
			}
			else
			{
				if (NewAspectRatio.Value < 1)
					NewInfo.Columns = (int)(OldInfo.Columns * NewAspectRatio.Value + 0.5);
				else
					NewInfo.Rows = (int)(OldInfo.Rows / NewAspectRatio.Value + 0.5);
			}

			float rowScale = OldInfo.Rows / (float)NewInfo.Rows;
			float colScale = OldInfo.Columns / (float)NewInfo.Columns;

			if (RemoveCalibration)
			{
				NewInfo.PixelSpacing = new PixelSpacing(0, 0);
				NewInfo.ImagerPixelSpacing = new PixelSpacing(0, 0);
			}
			else
			{
				NewInfo.PixelSpacing = new PixelSpacing(NewInfo.PixelSpacing.Row * rowScale, NewInfo.PixelSpacing.Column * colScale);
				NewInfo.ImagerPixelSpacing = new PixelSpacing(NewInfo.ImagerPixelSpacing.Row * rowScale, NewInfo.ImagerPixelSpacing.Column * colScale);
			}

			ValidateNewInfo();

			NewInfo.SeriesDescription = (OldInfo.SeriesDescription ?? "") + String.Format(" ({0}:{1}, dim/cal={2}/{3})",
						NewAspectRatio.Row, NewAspectRatio.Column,
						IncreasePixelDimensions ? "y" : "n",
						NewInfo.IsCalibrated ? "y" : "n");

			NewInfo.PlanarConfiguration = 0;

			PixelData oldPixelData = OldInfo.GetPixelData();
			PixelData newPixelData = NewInfo.GetPixelData();

			for (int row = 0; row < NewInfo.Rows; ++row)
			{
				for (int column = 0; column < NewInfo.Columns; ++column)
				{
					var sourcePoint = new PointF(column * colScale, row * rowScale);
					int interpolated = PerformBilinearInterpolationAt(oldPixelData, sourcePoint);
					newPixelData.SetPixel(column, row, interpolated);
				}
			}

			NewInfo.SetPixelData(newPixelData);
			NewInfo.UpdateDataSet(file.DataSet);
		}
		/// <summary>
		/// Compares received duplicate with the existing copy in the filesystem and throw it into the SIQ if they differ.
		/// Otherwise, simply delete the duplicate and keep everything as it.
		/// </summary>
		/// <param name="dupFile"></param>
		/// <param name="baseFile"></param>
		/// <param name="uid"></param>
		/// <returns></returns>
		private ProcessDuplicateResult CompareDuplicates(DicomFile dupFile, DicomFile baseFile, WorkQueueUid uid)
    	{
			var result = new ProcessDuplicateResult();
			string duplicateSopPath = ServerHelper.GetDuplicateUidPath(StorageLocation, uid);
			
			if (!dupFile.TransferSyntax.Equals(baseFile.TransferSyntax))
    		{
    			// If they're compressed, and we have a codec, lets decompress and still do the comparison
    			if (dupFile.TransferSyntax.Encapsulated
    			    && !dupFile.TransferSyntax.LossyCompressed
    			    && DicomCodecRegistry.GetCodec(dupFile.TransferSyntax) != null)
    			{
    				dupFile.ChangeTransferSyntax(TransferSyntax.ExplicitVrLittleEndian);
    			}

    			if (baseFile.TransferSyntax.Encapsulated
    			    && !baseFile.TransferSyntax.LossyCompressed
    			    && DicomCodecRegistry.GetCodec(baseFile.TransferSyntax) != null)
    			{
    				baseFile.ChangeTransferSyntax(TransferSyntax.ExplicitVrLittleEndian);
    			}

    			if (dupFile.TransferSyntax.Encapsulated || baseFile.TransferSyntax.Encapsulated)
    			{
    				string failure = String.Format("Base file transfer syntax is '{0}' while duplicate file has '{1}'",
    				                               baseFile.TransferSyntax, dupFile.TransferSyntax);

    				var list = new List<DicomAttributeComparisonResult>();
    				var compareResult = new DicomAttributeComparisonResult
    				                    	{
    				                    		ResultType = ComparisonResultType.DifferentValues,
    				                    		TagName = DicomTagDictionary.GetDicomTag(DicomTags.TransferSyntaxUid).Name,
    				                    		Details = failure
    				                    	};
    				list.Add(compareResult);
    				CreateDuplicateSIQEntry(uid, dupFile, list);
    				result.ActionTaken = DuplicateProcessResultAction.Reconcile;
    				return result;
    			}
    		}

    		var failureReason = new List<DicomAttributeComparisonResult>();
    		if (baseFile.DataSet.Equals(dupFile.DataSet, ref failureReason))
    		{
    			Platform.Log(LogLevel.Info,
    			             "Duplicate SOP being processed is identical.  Removing SOP: {0}",
    			             baseFile.MediaStorageSopInstanceUid);


    			RemoveWorkQueueUid(uid, duplicateSopPath);
    			result.ActionTaken = DuplicateProcessResultAction.Delete;
    		}
    		else
    		{
    			CreateDuplicateSIQEntry(uid, dupFile, failureReason);
    			result.ActionTaken = DuplicateProcessResultAction.Reconcile;
    		}

			return result;
    	}
Exemplo n.º 7
0
        /// <summary>
        /// Process the rule
        /// </summary>
        /// <param name="file"></param>
        public void Process(ClearCanvas.Dicom.DicomFile file)
        {
            var proposedTs = ClearCanvas.Dicom.TransferSyntax.GetTransferSyntax(OutputTransferSyntax);
            var currentTs  = file.TransferSyntax;

            var bitsStored                = file.DataSet[DicomTags.BitsStored].GetInt16(0, 0);
            var bitsAllocated             = file.DataSet[DicomTags.BitsAllocated].GetInt16(0, 0);
            var samplesPerPixel           = file.DataSet[DicomTags.SamplesPerPixel].GetInt16(0, 0);
            var photometricInterpretation = file.DataSet[DicomTags.PhotometricInterpretation].GetString(0, "");

            log.Info(string.Format(
                         "compress: bits stored {0}, bits allocated {1}, samples per pixel {2}, photometric {3}",
                         bitsStored, bitsAllocated, samplesPerPixel, photometricInterpretation));

            log.Info(string.Format("Compress: Proposed: {0}, current {1}", proposedTs, currentTs));
            log.Info("Checking that proposed transfer syntax is consistent with the dataset");

            ClearCanvas.Dicom.TransferSyntax finalTs = currentTs;

            try
            {
                if (proposedTs == TransferSyntax.JpegBaselineProcess1)
                {
                    ValidateJPEGBaselineProcess1(bitsAllocated, bitsStored, samplesPerPixel, photometricInterpretation);
                }
                else if (proposedTs == TransferSyntax.JpegExtendedProcess24)
                {
                    ValidateJPEGExtendedProcess2And4(bitsAllocated, bitsStored, samplesPerPixel, photometricInterpretation);
                }
                else if (proposedTs == TransferSyntax.JpegLosslessNonHierarchicalFirstOrderPredictionProcess14SelectionValue1)
                {
                    ValidateJpegLosslessNonHierarchicalFirstOrderPredictionProcess14SelectionValue1(bitsAllocated, bitsStored, samplesPerPixel, photometricInterpretation);
                }
                else if (proposedTs == TransferSyntax.Jpeg2000ImageCompression)
                {
                    ValidateJpeg2000Lossy(bitsAllocated, bitsStored, samplesPerPixel, photometricInterpretation);
                }
                else if (proposedTs == TransferSyntax.Jpeg2000ImageCompressionLosslessOnly)
                {
                    ValidateJpeg2000Lossless(bitsAllocated, bitsStored, samplesPerPixel, photometricInterpretation);
                }

                // Compression from non-compressed transfer syntaxes
                if (currentTs == TransferSyntax.ImplicitVrLittleEndian ||
                    currentTs == TransferSyntax.ExplicitVrLittleEndian ||
                    currentTs == TransferSyntax.ExplicitVrBigEndian)
                {
                    // This is fine ... we're compressing something that isn't compressed.
                    finalTs = proposedTs;
                }
                else
                {
                    // Potentially a problem.  We could be moving from a compressed syntax to
                    // another compress syntax.  We know the target syntax is legal for this dataset
                    // but the toolkit pukes when moving encapsulated frames.

                    // TO DO: More work needed here.
                    finalTs = proposedTs;
                }

                log.Info(string.Format("Final ts {0}", finalTs));
            }
            catch (NotSupportedException nse)
            {
                log.Error("Compression would be illegal and will NOT be applied. " + nse.ToString());
            }

            if (currentTs != finalTs)
            {
                file.ChangeTransferSyntax(finalTs);
            }
        }