コード例 #1
0
ファイル: DicomRleCodec.cs プロジェクト: scottshea/monodicom
        public void Encode(DicomUncompressedPixelData oldPixelData, DicomCompressedPixelData newPixelData, DicomCodecParameters parameters)
        {
            DicomRleCodecParameters rleParams = parameters as DicomRleCodecParameters;

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

			// Convert to RGB
			if (oldPixelData.HasPaletteColorLut && parameters.ConvertPaletteToRGB)
			{
				oldPixelData.ConvertPaletteColorToRgb();
				newPixelData.HasPaletteColorLut = false;
				newPixelData.SamplesPerPixel = oldPixelData.SamplesPerPixel;
				newPixelData.PlanarConfiguration = oldPixelData.PlanarConfiguration;
				newPixelData.PhotometricInterpretation = oldPixelData.PhotometricInterpretation;
			}

        	int pixelCount = oldPixelData.ImageWidth * oldPixelData.ImageHeight;
            int numberOfSegments = oldPixelData.BytesAllocated * oldPixelData.SamplesPerPixel;

            for (int i = 0; i < oldPixelData.NumberOfFrames; i++)
            {
                RLEEncoder encoder = new RLEEncoder();
                byte[] frameData = oldPixelData.GetFrame(i);

                for (int s = 0; s < numberOfSegments; s++)
                {
                    encoder.NextSegment();

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

                    int pos;
                    int offset;

                    if (newPixelData.PlanarConfiguration == 0)
                    {
                        pos = sample * oldPixelData.BytesAllocated;
                        offset = numberOfSegments;
                    }
                    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++)
                    {
                        if (pos >= frameData.Length)
                            throw new DicomCodecException("");
                        encoder.Encode(frameData[pos]);
                        pos += offset;
                    }
                    encoder.Flush();
                }

                encoder.MakeEvenLength();

                newPixelData.AddFrameFragment(encoder.GetBuffer());
            }
        }
コード例 #2
0
ファイル: DicomRleCodec.cs プロジェクト: 1059444127/XA
        public void Encode(DicomUncompressedPixelData oldPixelData, DicomCompressedPixelData newPixelData, DicomCodecParameters parameters)
        {
            DicomRleCodecParameters rleParams = parameters as DicomRleCodecParameters ?? new DicomRleCodecParameters();

            // Convert to RGB
            if (oldPixelData.HasPaletteColorLut && parameters.ConvertPaletteToRGB)
            {
                oldPixelData.ConvertPaletteColorToRgb();
                newPixelData.HasPaletteColorLut        = false;
                newPixelData.SamplesPerPixel           = oldPixelData.SamplesPerPixel;
                newPixelData.PlanarConfiguration       = oldPixelData.PlanarConfiguration;
                newPixelData.PhotometricInterpretation = oldPixelData.PhotometricInterpretation;
            }

            int pixelCount       = oldPixelData.ImageWidth * oldPixelData.ImageHeight;
            int numberOfSegments = oldPixelData.BytesAllocated * oldPixelData.SamplesPerPixel;

            for (int i = 0; i < oldPixelData.NumberOfFrames; i++)
            {
                RLEEncoder encoder   = new RLEEncoder();
                byte[]     frameData = oldPixelData.GetFrame(i);

                for (int s = 0; s < numberOfSegments; s++)
                {
                    encoder.NextSegment();

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

                    int pos;
                    int offset;

                    if (newPixelData.PlanarConfiguration == 0)
                    {
                        pos    = sample * oldPixelData.BytesAllocated;
                        offset = numberOfSegments;
                    }
                    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++)
                    {
                        if (pos >= frameData.Length)
                        {
                            throw new DicomCodecException("");
                        }
                        encoder.Encode(frameData[pos]);
                        pos += offset;
                    }
                    encoder.Flush();
                }

                encoder.MakeEvenLength();

                newPixelData.AddFrameFragment(encoder.GetBuffer());
            }
        }