コード例 #1
0
        public static IPixelData Create(DcmPixelData pixelData, int frame)
        {
            PhotometricInterpretation pi = PhotometricInterpretation.Lookup(pixelData.PhotometricInterpretation);

            if (pi == PhotometricInterpretation.Monochrome1 || pi == PhotometricInterpretation.Monochrome2 || pi == PhotometricInterpretation.PaletteColor)
            {
                if (pixelData.BitsStored <= 8)
                {
                    return(new GrayscalePixelDataU8(pixelData.ImageWidth, pixelData.ImageHeight, pixelData.GetFrameDataU8(frame)));
                }
                else if (pixelData.BitsStored <= 16)
                {
                    if (pixelData.IsSigned)
                    {
                        return(new GrayscalePixelDataS16(pixelData.ImageWidth, pixelData.ImageHeight, pixelData.GetFrameDataS16(frame)));
                    }
                    else
                    {
                        return(new GrayscalePixelDataU16(pixelData.ImageWidth, pixelData.ImageHeight, pixelData.GetFrameDataU16(frame)));
                    }
                }
                else
                {
                    throw new DicomImagingException("Unsupported pixel data value for bits stored: {0}", pixelData.BitsStored);
                }
            }
            else if (pi == PhotometricInterpretation.Rgb || pi == PhotometricInterpretation.YbrFull)
            {
                return(new ColorPixelData24(pixelData.ImageWidth, pixelData.ImageHeight, pixelData.GetFrameDataU8(frame)));
            }
            else
            {
                throw new DicomImagingException("Unsupported pixel data photometric interpretation: {0}", pi.Value);
            }
        }
コード例 #2
0
        private void DrawImageBox(DcmImageBox imageBox, Graphics graphics, Point position, int width, int height, int dpiX, int dpiY)
        {
            DcmDataset dataset = imageBox.ImageSequence;

            if (!dataset.Contains(DicomTags.PixelData))
            {
                return;
            }

            DcmPixelData pixelData = new DcmPixelData(dataset);

            PinnedIntArray pixelBuffer = null;
            Bitmap         bitmap      = null;

            if (pixelData.SamplesPerPixel == 3)
            {
                pixelBuffer = new PinnedIntArray(pixelData.GetFrameDataS32(0));
                bitmap      = new Bitmap(pixelData.ImageWidth, pixelData.ImageHeight,
                                         pixelData.ImageWidth * sizeof(int), PixelFormat.Format32bppRgb, pixelBuffer.Pointer);
            }
            else
            {
                bool invert = (pixelData.PhotometricInterpretation == "MONOCHROME1");
                if (imageBox.Polarity == "REVERSE")
                {
                    invert = !invert;
                }

                byte[] pixelsOut = null;

                if (pixelData.BitsAllocated == 8)
                {
                    pixelsOut = pixelData.GetFrameDataU8(0);
                }
                else
                {
                    ushort[] pixels = pixelData.GetFrameDataU16(0);
                    pixelsOut = new byte[pixels.Length];
                    double scale = 256.0 / 4096.0;

                    int pixel = 0;
                    for (int y = 0; y < pixelData.ImageHeight; y++)
                    {
                        for (int x = 0; x < pixelData.ImageWidth; x++)
                        {
                            pixelsOut[pixel] = (byte)(pixels[pixel] * scale);
                            pixel++;
                        }
                    }

                    pixels = null;
                }

                bitmap = new Bitmap(pixelData.ImageWidth, pixelData.ImageHeight, PixelFormat.Format8bppIndexed);

                if (invert)
                {
                    ColorTable.Apply(bitmap, ColorTable.Monochrome1);
                }
                else
                {
                    ColorTable.Apply(bitmap, ColorTable.Monochrome2);
                }

                BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, pixelData.ImageWidth, pixelData.ImageHeight),
                                                    ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

                IntPtr pos = bmData.Scan0;
                for (int i = 0, c = pixelsOut.Length; i < c; i += bmData.Width)
                {
                    Marshal.Copy(pixelsOut, i, pos, bmData.Width);
                    pos = new IntPtr(pos.ToInt64() + bmData.Stride);
                }

                bitmap.UnlockBits(bmData);
            }

            //bitmap.SetResolution(dpiX, dpiY);

            int border = 3;

            double factor = Math.Min((double)height / (double)bitmap.Height,
                                     (double)width / (double)bitmap.Width);

            int drawWidth  = (int)(bitmap.Width * factor) - (border * 2);
            int drawHeight = (int)(bitmap.Height * factor) - (border * 2);

            int drawX = position.X + ((width - drawWidth) / 2);
            int drawY = position.Y + ((height - drawHeight) / 2);

            graphics.DrawImage(bitmap, drawX, drawY, drawWidth, drawHeight);
        }