Example #1
0
        public CTSliceInfo(XDocument theXDocument, string theFileName)
        {
            _XDocument = theXDocument;
            _FileName  = theFileName;

            _ColumnCount = DICOMParserUtility.GetDICOMAttributeAsInt(theXDocument, DICOMTAG.COLUMNS);
            _RowCount    = DICOMParserUtility.GetDICOMAttributeAsInt(theXDocument, DICOMTAG.ROWS);

            _SliceLoc = DICOMParserUtility.GetDICOMAttributeAsInt(theXDocument, DICOMTAG.SLICE_LOCATION);

            string imagePositionPatientString = DICOMParserUtility.GetDICOMAttributeAsString(theXDocument, DICOMTAG.IMAGE_POSITION_PATIENT);

            string[] imagePositionPatientArray = imagePositionPatientString.Split('\\');
            _UpperLeft_X = Convert.ToDouble(imagePositionPatientArray[0], CultureInfo.InvariantCulture);
            _UpperLeft_Y = Convert.ToDouble(imagePositionPatientArray[1], CultureInfo.InvariantCulture);
            _UpperLeft_Z = Convert.ToDouble(imagePositionPatientArray[2], CultureInfo.InvariantCulture);

            string aPixelSpacingString = DICOMParserUtility.GetDICOMAttributeAsString(theXDocument, DICOMTAG.PIXEL_SPACING);

            string[] aPixelSpacingValueArray = aPixelSpacingString.Split('\\');
            _PixelSpacing_X = Convert.ToDouble(aPixelSpacingValueArray[0], CultureInfo.InvariantCulture);
            _PixelSpacing_Y = Convert.ToDouble(aPixelSpacingValueArray[1], CultureInfo.InvariantCulture);

            _windowCenter = DICOMParserUtility.GetDICOMAttributeAsInt(_XDocument, DICOMTAG.WINDOW_CENTER);
            _windowWidth  = DICOMParserUtility.GetDICOMAttributeAsInt(_XDocument, DICOMTAG.WINDOW_WIDTH);
        }
Example #2
0
        private short[,] BuildHounsfieldPixelBuffer()
        {
            // Allocate the Hounsfield Pixel Buffer
            short[,] HounsfieldPixelBuffer = new short[_RowCount, _ColumnCount];

            if (File.Exists(_FileName + HOUNSFIELD_EXT))
            {
                BinaryReader br = new BinaryReader(File.Open(_FileName + HOUNSFIELD_EXT, FileMode.Open, FileAccess.Read, FileShare.Read));

                for (int r = 0; r != _RowCount; ++r)
                {
                    for (int c = 0; c != _ColumnCount; ++c)
                    {
                        HounsfieldPixelBuffer[r, c] = br.ReadInt16();
                    }
                }
                br.Dispose();

                return(HounsfieldPixelBuffer);
            }

            int aRescaleIntercept = DICOMParserUtility.GetDICOMAttributeAsInt(_XDocument, DICOMTAG.RESCALE_INTERCEPT);
            int aRescaleSlope     = DICOMParserUtility.GetDICOMAttributeAsInt(_XDocument, DICOMTAG.RESCALE_SLOPE);

            bool aPixelPaddingValueExist = DICOMParserUtility.DoesDICOMAttributeExist(_XDocument, "(0028,0120)");
            int  aPixelPaddingValue      = DICOMParserUtility.GetDICOMAttributeAsInt(_XDocument, "(0028,0120)");

            // Find the pixel data DICOM attribute (7FE0,0010)
            var aPixelDataQuery = from Element in _XDocument.Descendants("DataElement")
                                  where Element.Attribute("Tag").Value.Equals("(7FE0,0010)")
                                  select Element;

            // Get the start position of the stream for the pixel data attribute
            long aStreamPosition = Convert.ToInt64(aPixelDataQuery.Last().Attribute("StreamPosition").Value);

            // Open the binary reader
            BinaryReader aBinaryReader = new BinaryReader(File.Open(_FileName, FileMode.Open, FileAccess.Read, FileShare.Read));

            // Set the stream position of the binary reader to first pixel
            aBinaryReader.BaseStream.Position = aStreamPosition;

            // Loop over all pixel data values
            for (int r = 0; r != _RowCount; ++r)
            {
                for (int c = 0; c != _ColumnCount; ++c)
                {
                    // For some images, the pixel buffer is smaller than '2Byte * RowCount * ColumnCount'
                    // That's why we need the check...
                    if (aBinaryReader.BaseStream.Position - 2 < aBinaryReader.BaseStream.Length) /// @#@ test is wrong?
                    {
                        byte aByte0 = aBinaryReader.ReadByte();
                        byte aByte1 = aBinaryReader.ReadByte();

                        short aPixelValue = Convert.ToInt16((aByte1 << 8) + aByte0);

                        // Check for Pixel Padding Value
                        if (aPixelPaddingValueExist)
                        {
                            if (aPixelValue == aPixelPaddingValue)
                            {
                                aPixelValue = Int16.MinValue;
                            }
                        }

                        // Rescale handling
                        aPixelValue = (short)((int)aPixelValue * aRescaleSlope + aRescaleIntercept);

                        // Value of the voxel is stored in Hounsfield Units
                        HounsfieldPixelBuffer[r, c] = aPixelValue;
                    }
                }
            }

            // cache HounsfieldPixelBuffer
            {
                if (File.Exists(_FileName + HOUNSFIELD_EXT))
                {
                    File.Delete(_FileName + HOUNSFIELD_EXT);
                }

                FileStream   f  = File.Open(_FileName + HOUNSFIELD_EXT, FileMode.OpenOrCreate, FileAccess.Write);
                BinaryWriter bw = new BinaryWriter(f);

                for (int r = 0; r != _RowCount; ++r)
                {
                    for (int c = 0; c != _ColumnCount; ++c)
                    {
                        bw.Write(HounsfieldPixelBuffer[r, c]);
                    }
                }
                bw.Flush();

                f.Close();
            }

            return(HounsfieldPixelBuffer);
        }