Exemple #1
0
 public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters)
 {
     if (oldPixelData.UncompressedFrameSize % 2 == 1)
     {
         using (var stream = new MemoryStream(oldPixelData.GetFrameFragmentData(frame)))
         {
             var data = new byte[oldPixelData.UncompressedFrameSize];
             stream.Read(data, 0, data.Length);
             newPixelData.AppendFrame(data);
         }
     }
     else
     {
         newPixelData.AppendFrame(oldPixelData.GetFrameFragmentData(frame));
     }
 }
		public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters)
		{
			if (oldPixelData.UncompressedFrameSize%2 == 1)
			{
				using (var stream = new MemoryStream(oldPixelData.GetFrameFragmentData(frame)))
				{
					var data = new byte[oldPixelData.UncompressedFrameSize];
					stream.Read(data, 0, data.Length);
					newPixelData.AppendFrame(data);
				}
			}
			else
			{
				newPixelData.AppendFrame(oldPixelData.GetFrameFragmentData(frame));
			}
		}
Exemple #3
0
        public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData,
                                DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters)
        {
            DicomRleCodecParameters rleParams = parameters as DicomRleCodecParameters ?? new DicomRleCodecParameters();

            int pixelCount       = oldPixelData.ImageWidth * oldPixelData.ImageHeight;
            int numberOfSegments = oldPixelData.BytesAllocated * oldPixelData.SamplesPerPixel;
            int segmentLength    = (pixelCount & 1) == 1 ? pixelCount + 1 : pixelCount;

            byte[] segment   = new byte[segmentLength];
            byte[] frameData = new byte[oldPixelData.UncompressedFrameSize];

            IList <DicomFragment> rleData = oldPixelData.GetFrameFragments(frame);
            RLEDecoder            decoder = new RLEDecoder(rleData);

            if (decoder.NumberOfSegments != numberOfSegments)
            {
                throw new DicomCodecException("Unexpected number of RLE segments!");
            }

            for (int s = 0; s < numberOfSegments; s++)
            {
                decoder.DecodeSegment(s, segment);

                int sample = s / oldPixelData.BytesAllocated;
                int sabyte = s % oldPixelData.BytesAllocated;

                int pos;
                int offset;

                if (newPixelData.PlanarConfiguration == 0)
                {
                    pos    = sample * oldPixelData.BytesAllocated;
                    offset = oldPixelData.SamplesPerPixel * oldPixelData.BytesAllocated;
                }
                else
                {
                    pos    = sample * oldPixelData.BytesAllocated * pixelCount;
                    offset = oldPixelData.BytesAllocated;
                }

                if (rleParams.ReverseByteOrder)
                {
                    pos += sabyte;
                }
                else
                {
                    pos += oldPixelData.BytesAllocated - sabyte - 1;
                }

                for (int p = 0; p < pixelCount; p++)
                {
                    frameData[pos] = segment[p];
                    pos           += offset;
                }
            }

            newPixelData.AppendFrame(frameData);
        }
Exemple #4
0
 public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters)
 {
     using (var input = new MemoryStream(oldPixelData.GetFrameFragmentData(frame)))
         using (var gzipStream = new GZipStream(input, CompressionMode.Decompress, false))
         {
             var data = new byte[oldPixelData.UncompressedFrameSize];
             gzipStream.Read(data, 0, data.Length);
             newPixelData.AppendFrame(data);
         }
 }
		public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData, DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters)
		{
			using (var input = new MemoryStream(oldPixelData.GetFrameFragmentData(frame)))
			using (var gzipStream = new GZipStream(input, CompressionMode.Decompress, false))
			{
				var data = new byte[oldPixelData.UncompressedFrameSize];
				gzipStream.Read(data, 0, data.Length);
				newPixelData.AppendFrame(data);
			}
		}
		protected static void CreateColorPixelData(DicomUncompressedPixelData pd)
		{
			int rows = pd.ImageHeight;
			int cols = pd.ImageWidth;

            int minValue = pd.IsSigned ? -(1 << (pd.BitsStored - 1)) : 0;
            int maxValue = (1 << pd.BitsStored) + minValue - 1;
            const byte noOpByteMask = 0xFF;
            var byteMask = (byte)(noOpByteMask >> (pd.BitsAllocated - pd.BitsStored));        

			// Create a small block of pixels in the test pattern in an integer,
			// then copy/tile into the full size frame data

			int smallRows = (rows * 3) / 8;
			int smallColumns = rows / 4;
			int stripSize = rows / 16;

			int[] smallPixels = new int[smallRows * smallColumns * 3];

			float slope = (float)(maxValue - minValue) / smallColumns;

			int pixelOffset = 0;
			for (int i = 0; i < smallRows; i++)
			{
				if (i < stripSize)
				{
					for (int j = 0; j < smallColumns; j++)
					{
						smallPixels[pixelOffset] = (int)((j * slope) + minValue);
						pixelOffset++;
						smallPixels[pixelOffset] = 0;
						pixelOffset++;
						smallPixels[pixelOffset] = 0;
						pixelOffset++;
					}
				}
				else if (i > (smallRows - stripSize))
				{
					for (int j = 0; j < smallColumns; j++)
					{
						smallPixels[pixelOffset] = 0;
						pixelOffset++;
						smallPixels[pixelOffset] = (int)(maxValue - (j * slope));
						pixelOffset++;
						smallPixels[pixelOffset] = 0;
						pixelOffset++;
					}
				}
				else
				{
					int pixel = minValue + (int)((i - stripSize) * slope);
					if (pixel < minValue) pixel = minValue + 1;
					if (pixel > maxValue) pixel = maxValue - 1;

					int start = (smallColumns / 2) - (i - stripSize) / 2;
					int end = (smallColumns / 2) + (i - stripSize) / 2;

					for (int j = 0; j < smallColumns; j++)
					{
						smallPixels[pixelOffset] = 0;
						pixelOffset++;
						smallPixels[pixelOffset] = 0;
						pixelOffset++;
						if (j < start)
							smallPixels[pixelOffset] = minValue;
						else if (j > end)
							smallPixels[pixelOffset] = maxValue;
						else
							smallPixels[pixelOffset] = pixel;

						pixelOffset++;
					}
				}
			}
			// Now create the actual frame
			for (int frame = 0; frame < pd.NumberOfFrames; frame++)
			{
				// Odd length frames are automatically dealt with by DicomUncompressedPixelData
				byte[] frameData = new byte[pd.UncompressedFrameSize];
				pixelOffset = 0;

				for (int i = 0; i < rows; i++)
				{
					int smallOffset = (i%smallRows)*smallColumns*3;

					for (int j = 0; j < cols*3; j++)
					{
						frameData[pixelOffset] = (byte) smallPixels[smallOffset + j%(smallColumns*3)];
					    frameData[pixelOffset] &= byteMask;
						pixelOffset++;
					}
				}

				pd.AppendFrame(frameData);
			}
		}
        protected static void CreateColorPixelData(DicomUncompressedPixelData pd)
        {
            int rows = pd.ImageHeight;
            int cols = pd.ImageWidth;

            int        minValue     = pd.IsSigned ? -(1 << (pd.BitsStored - 1)) : 0;
            int        maxValue     = (1 << pd.BitsStored) + minValue - 1;
            const byte noOpByteMask = 0xFF;
            var        byteMask     = (byte)(noOpByteMask >> (pd.BitsAllocated - pd.BitsStored));

            // Create a small block of pixels in the test pattern in an integer,
            // then copy/tile into the full size frame data

            int smallRows    = (rows * 3) / 8;
            int smallColumns = rows / 4;
            int stripSize    = rows / 16;

            int[] smallPixels = new int[smallRows * smallColumns * 3];

            float slope = (float)(maxValue - minValue) / smallColumns;

            int pixelOffset = 0;

            for (int i = 0; i < smallRows; i++)
            {
                if (i < stripSize)
                {
                    for (int j = 0; j < smallColumns; j++)
                    {
                        smallPixels[pixelOffset] = (int)((j * slope) + minValue);
                        pixelOffset++;
                        smallPixels[pixelOffset] = 0;
                        pixelOffset++;
                        smallPixels[pixelOffset] = 0;
                        pixelOffset++;
                    }
                }
                else if (i > (smallRows - stripSize))
                {
                    for (int j = 0; j < smallColumns; j++)
                    {
                        smallPixels[pixelOffset] = 0;
                        pixelOffset++;
                        smallPixels[pixelOffset] = (int)(maxValue - (j * slope));
                        pixelOffset++;
                        smallPixels[pixelOffset] = 0;
                        pixelOffset++;
                    }
                }
                else
                {
                    int pixel = minValue + (int)((i - stripSize) * slope);
                    if (pixel < minValue)
                    {
                        pixel = minValue + 1;
                    }
                    if (pixel > maxValue)
                    {
                        pixel = maxValue - 1;
                    }

                    int start = (smallColumns / 2) - (i - stripSize) / 2;
                    int end   = (smallColumns / 2) + (i - stripSize) / 2;

                    for (int j = 0; j < smallColumns; j++)
                    {
                        smallPixels[pixelOffset] = 0;
                        pixelOffset++;
                        smallPixels[pixelOffset] = 0;
                        pixelOffset++;
                        if (j < start)
                        {
                            smallPixels[pixelOffset] = minValue;
                        }
                        else if (j > end)
                        {
                            smallPixels[pixelOffset] = maxValue;
                        }
                        else
                        {
                            smallPixels[pixelOffset] = pixel;
                        }

                        pixelOffset++;
                    }
                }
            }
            // Now create the actual frame
            for (int frame = 0; frame < pd.NumberOfFrames; frame++)
            {
                // Odd length frames are automatically dealt with by DicomUncompressedPixelData
                byte[] frameData = new byte[pd.UncompressedFrameSize];
                pixelOffset = 0;

                for (int i = 0; i < rows; i++)
                {
                    int smallOffset = (i % smallRows) * smallColumns * 3;

                    for (int j = 0; j < cols * 3; j++)
                    {
                        frameData[pixelOffset]  = (byte)smallPixels[smallOffset + j % (smallColumns * 3)];
                        frameData[pixelOffset] &= byteMask;
                        pixelOffset++;
                    }
                }

                pd.AppendFrame(frameData);
            }
        }
Exemple #8
0
        public void DecodeFrame(int frame, DicomCompressedPixelData oldPixelData,
                                DicomUncompressedPixelData newPixelData, DicomCodecParameters parameters)
        {
            DicomRleCodecParameters rleParams = parameters as DicomRleCodecParameters;

            if (rleParams == null)
                throw new DicomCodecException("Unexpected RLE Codec parameters");

            int pixelCount = oldPixelData.ImageWidth * oldPixelData.ImageHeight;
            int numberOfSegments = oldPixelData.BytesAllocated * oldPixelData.SamplesPerPixel;
            int segmentLength = (pixelCount & 1) == 1 ? pixelCount + 1 : pixelCount;

            byte[] segment = new byte[segmentLength];
            byte[] frameData = new byte[oldPixelData.UncompressedFrameSize];

            IList<DicomFragment> rleData = oldPixelData.GetFrameFragments(frame);
            RLEDecoder decoder = new RLEDecoder(rleData);

            if (decoder.NumberOfSegments != numberOfSegments)
                throw new DicomCodecException("Unexpected number of RLE segments!");

            for (int s = 0; s < numberOfSegments; s++)
            {
                decoder.DecodeSegment(s, segment);

                int sample = s / oldPixelData.BytesAllocated;
                int sabyte = s % oldPixelData.BytesAllocated;

                int pos;
                int offset;

                if (newPixelData.PlanarConfiguration == 0)
                {
                    pos = sample * oldPixelData.BytesAllocated;
                    offset = oldPixelData.SamplesPerPixel * oldPixelData.BytesAllocated;
                }
                else
                {
                    pos = sample * oldPixelData.BytesAllocated * pixelCount;
                    offset = oldPixelData.BytesAllocated;
                }

                if (rleParams.ReverseByteOrder)
                    pos += sabyte;
                else
                    pos += oldPixelData.BytesAllocated - sabyte - 1;

                for (int p = 0; p < pixelCount; p++)
                {
                    frameData[pos] = segment[p];
                    pos += offset;
                }
            }

            newPixelData.AppendFrame(frameData);
        }