Exemple #1
0
 public static void DICOM2XML(DICOMFile df, string xmlFilePath)
 {
     using (XmlWriter writer = XmlWriter.Create(xmlFilePath, GenerateCleanSettings()))
     {
         writer.WriteStartElement(DICOM_FILE_WRAPPER);
         if (INCLUDE_NUM_OF_OBJECTS_IN_COLLECTIONS)
         {
             writer.WriteAttributeString(NUM_OF_OBJECTS_ATTR_NAME, df.DicomObjects.Count.ToString());
         }
         WriteObjects(writer, df.DicomObjects);
         writer.WriteEndElement();
     }
 }
Exemple #2
0
        /// <summary>
        /// This method retrieves the relevant image properties from a DICOM file for rendering
        /// the image
        /// </summary>
        /// <param name="df">the DICOM file to be processed</param>
        /// <returns>the image properties of the image</returns>
        public static ImageProperties PullProperties(DICOMFile df)
        {
            ImageProperties props = new ImageProperties();

            //Get Number of Rows
            try
            {
                UnsignedShort rowsObject = df.ROWS as UnsignedShort;
                props.Rows = rowsObject.Data;
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find number of rows");
            }

            //Get Number of Columns
            try
            {
                UnsignedShort columnsObject = df.COLUMNS as UnsignedShort;
                props.Columns = columnsObject.Data;
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find number of columns");
            }

            //Get Bit Depth
            try
            {
                UnsignedShort bitsAllocated = df.BITS_ALLOCATED as UnsignedShort;
                props.BitsAllocated = bitsAllocated.Data;
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find bits allocated");
            }

            //Get Pixel Height and Width
            try
            {
                DecimalString pixelSpacing = df.PIXEL_SPACING as DecimalString;
                props.PixelHeight = pixelSpacing.Data[0];
                props.PixelWidth = pixelSpacing.Data[1];
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find pixel spacing");
            }

            //Get SliceThickness
            try
            {
                DecimalString sliceThickness = df.SLICE_THICKNESS as DecimalString;
                props.SliceThickness = sliceThickness.Data[0];
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find slice thickness");
            }

            //Get Image Number
            try
            {
                IntegerString imageNumber = df.INSTANCE_NUMBER as IntegerString;
                props.ImageNumber = imageNumber.Data[0];
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find slice thickness");
            }

            //Get Window and Level
            try
            {
                DecimalString window = df.WINDOW_WIDTH as DecimalString;
                DecimalString level = df.WINDOW_CENTER as DecimalString;
                props.WindowAndLevel = new WindowLevel(window.Data[0], level.Data[0]);
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find window and level");
            }
            //Is This A Dose File
            try
            {
                if (df.DOSE_UNITS.Data != null) { props.IsDose = true; }
                else { props.IsDose = false; }
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not determine if file was a dose file");
            }

            //Read In Scaling Function
            try
            {
                DecimalString slope = props.IsDose ? df.DOSE_GRID_SCALING as DecimalString : df.RESCALE_SLOPE as DecimalString;
                DecimalString intercept = props.IsDose ? null : df.RESCALE_INTERCEPT as DecimalString;
                if (intercept == null) { intercept = new DecimalString(); intercept.Data = new double[] { 0 }; }
                props.Function = new ScalingFunction(slope.Data[0], intercept.Data[0]);
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find scaling function");
            }

            //Get Samples Per Pixel
            try
            {
                UnsignedShort samples = df.SAMPLES_PER_PIXEL as UnsignedShort;
                props.SamplesPerPixel = samples.Data;
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find samples per pixel");
            }

            //Get Transfer Syntax
            try
            {
                UniqueIdentifier syntax = df.TRANSFER_SYNTAX_UID as UniqueIdentifier;
                string stringSyntax = syntax.Data;

                switch (stringSyntax)
                {
                    case Constants.EXPLICIT_VR_BIG_ENDIAN: props.TransferSyntax = Constants.TransferSyntax.EXPLICIT_VR_BIG_ENDIAN; break;
                    case Constants.EXPLICIT_VR_LITTLE_ENDIAN: props.TransferSyntax = Constants.TransferSyntax.EXPLICIT_VR_LITTLE_ENDIAN; break;
                    case Constants.IMPLICIT_VR_LITTLE_ENDIAN: props.TransferSyntax = Constants.TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN; break;
                    case Constants.JPEG_2000: props.TransferSyntax = Constants.TransferSyntax.JPEG_2000; break;
                    case Constants.JPEG_2000_LOSSLESS: props.TransferSyntax = Constants.TransferSyntax.JPEG_2000_LOSSLESS; break;
                    case Constants.JPEG_BASELINE: props.TransferSyntax = Constants.TransferSyntax.JPEG_BASELINE; break;
                    case Constants.JPEG_EXTENDED: props.TransferSyntax = Constants.TransferSyntax.JPEG_EXTENDED; break;
                    case Constants.JPEG_LOSSLESS_14: props.TransferSyntax = Constants.TransferSyntax.JPEG_LOSSLESS_14; break;
                    case Constants.JPEG_LOSSLESS_14_S1: props.TransferSyntax = Constants.TransferSyntax.JPEG_LOSSLESS_14_S1; break;
                    case Constants.JPEG_LOSSLESS_15: props.TransferSyntax = Constants.TransferSyntax.JPEG_LOSSLESS_15; break;
                    case Constants.JPEG_LS_LOSSLESS: props.TransferSyntax = Constants.TransferSyntax.JPEG_LS_LOSSLESS; break;
                    case Constants.JPEG_LS_NEAR_LOSSLESS: props.TransferSyntax = Constants.TransferSyntax.JPEG_LS_NEAR_LOSSLESS; break;
                    case Constants.JPEG_PROGRESSIVE: props.TransferSyntax = Constants.TransferSyntax.JPEG_PROGRESSIVE; break;
                    case Constants.RLE_LOSSLESS: props.TransferSyntax = Constants.TransferSyntax.RLE_LOSSLESS; break;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find transfer syntax");
            }

            //Get Image Position
            try
            {
                DecimalString imagePosition = df.IMAGE_POSITION as DecimalString;
                props.ImagePosition = new Position(imagePosition.Data[0], imagePosition.Data[1], imagePosition.Data[3]);
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not find image position");
            }

            //Set Number of Frames
            try
            {
                IntegerString numberOfFrames = df.NUMBER_OF_FRAMES as IntegerString;
                props.NumberOfFrames = numberOfFrames.Data[0];
            }
            catch (Exception e) { Console.WriteLine("Could not find number of frames"); }

            //Set Grid Frame Offset Vector
            try
            {
                DecimalString offsetVector = df.GRID_FRAME_OFFSET_VECTOR as DecimalString;
                props.OffsetVector = offsetVector.Data;
            }
            catch (Exception e) { Console.WriteLine("Could not find grid frame offset vector"); }

            return props;
        }