Ejemplo n.º 1
0
        public static unsafe void RotateFlip(ref ImageAttributes source, RotateFlipType rft)
        {
            Bitmap bitmap = OtherImageFormats.GetBitmap(source, null, null, false);

            bitmap.RotateFlip(rft);
            DataSet dicom = OtherImageFormats.GetImage(bitmap);

            source.height = bitmap.Height;
            source.width  = source.stride = bitmap.Width;
            Array.Copy((Array)dicom[t.PixelData].Value, source.buffer, source.buffer.Length);
        }
Ejemplo n.º 2
0
 public Viewer(FilmBox page)
     : this("")
 {
     try
     {
         this.dicom = OtherImageFormats.RenderPage(page, PictureBox.Size);
     }
     catch (Exception ex)
     {
         MessageBox.Show(Logging.Log(ex));
     }
 }
Ejemplo n.º 3
0
        private void GetPicture()
        {
            PictureBox.Location = new Point(0, 0);
            PictureBox.Size     = ClientRectangle.Size;

            ImageAttributes cropped = attributes; // OtherImageFormats.Crop(attributes);
            // if the application is minimized, PictureBox.Size has zero dimensions
            Size size = PictureBox.Size;

            if (size.Width == 0 || size.Height == 0)
            {
                // when the window is resized, we will render another image for display
                PictureBox.Image = null;
                return;
            }

            size = OtherImageFormats.Constrain(new Size(cropped.width, cropped.height), size);

            ImageAttributes resampled = (cropped.bitsperpixel == 8) ?
                                        cropped : Utilities.Resample(cropped, (double)attributes.width / (double)size.Width);

            ushort[] planes = null;
            if (overlays && overlay != null)
            {
                ImageAttributes source = new ImageAttributes();
                source.type   = TypeCode.UInt16;
                source.buffer = overlay;
                source.width  = attributes.width;
                source.height = attributes.height;
                source.stride = attributes.width;
                // attributes.spacing = ;
                source.bitsperpixel = 12;
                source.monochrome1  = true;

                ImageAttributes temp = Utilities.Resample(source, (double)attributes.width / (double)size.Width);
                planes = (ushort[])temp.buffer;
            }

            Bitmap bitmap = OtherImageFormats.GetBitmap(resampled, planes, voilut, invert);

            Bitmap final = (bitmap.Size != size) ? new Bitmap(bitmap, size) : bitmap;

            SurroundMask(final);

            PictureBox.Image = final;
        }
Ejemplo n.º 4
0
        private void Viewer_Load(object sender, EventArgs e)
        {
            try
            {
                // we should either have a filename or dataset
                if (filename != null && filename.Length > 0)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    try
                    {
                        dicom = OtherImageFormats.Read(filename);
                    }
                    finally
                    {
                        SetStatus("");
                        Cursor.Current = Cursors.Default;
                    }
                }
                if (dicom == null)
                {
                    throw new ArgumentException("No filename or dataset.");
                }

                SetIsCtImage();
                SetPixelRepresentation();
                voilut = GetVOILUT(dicom.Elements);

                attributes = Utilities.GetAttributes(dicom.Elements);
                // if the image is large, lets reduce memory consumption and decimate the source
                if (attributes.buffer.Length > 20000000)
                {
                    ReduceImage(dicom.Elements);
                }

                // extract overlays, if any
                overlay  = ExtractOverlays(dicom.Elements);
                overlays = (overlay != null);

                GetPicture();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Logging.Log(ex));
            }
        }
Ejemplo n.º 5
0
        private void CropToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double    scale = ScaleFactor;
            Rectangle shift = ImageInClient;

            rectangle = new Rectangle((int)((rectangle.X - shift.X) / scale), (int)((rectangle.Y - shift.Y) / scale), (int)(rectangle.Width / scale), (int)(rectangle.Height / scale));

            attributes.buffer = OtherImageFormats.Crop((short[])attributes.buffer, new Size(attributes.width, attributes.height), rectangle);

            attributes.width  = (ushort)rectangle.Width;
            attributes.height = (ushort)rectangle.Height;
            attributes.stride = attributes.width;

            start     = null;
            rectangle = previous = Rectangle.Empty;

            GetPicture();
            Invalidate();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Runs the conversion if configured.
        /// </summary>
        /// <param name="args">The command line arguments</param>
        /// <returns>Returns -1 if not run, 1 if run successfully and a return value of 0 indicates
        /// a problem.</returns>
        public static int Run(string[] args)
        {
            int  result   = -1;
            bool attached = false;

            try
            {
                if (Setup(args))
                {
                    if (verbose)
                    {
                        AttachConsole();
                        attached = true;
                    }

                    Viewer  viewer = new Viewer();
                    DataSet dicom  = OtherImageFormats.Read(input);
                    viewer.SaveAs(dicom, output, invert, rotation, flip);

                    result = 0;
                }
            }
            catch (Exception ex)
            {
                result = 1;
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (attached)
                {
                    FreeConsole();
                }
            }
            return(result);
        }
Ejemplo n.º 7
0
        public void SaveAs(DataSet dicom, string filename, bool invert, int rotation, bool flip)
        {
            try
            {
                if (rotation != 0 || flip)
                {
                    string         text = String.Format("Rotate{0}Flip{1}", rotation, (flip) ? "X" : "None");
                    RotateFlipType rft  = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), text);
                    RotateFlip(dicom, rft);
                }

                ImageAttributes attributes = Utilities.GetAttributes(dicom.Elements);
                ushort[]        voilut     = GetVOILUT(dicom.Elements);
                ushort[]        overlays   = ExtractOverlays(dicom.Elements);

                FileInfo info = new FileInfo(filename);

                ImageFormat format = ImageFormat.Bmp;
                switch (info.Extension.ToLower())
                {
                case ".dcm":
                    dicom.TransferSyntaxUID = Syntax.ExplicitVrLittleEndian;
                    dicom.Part10Header      = true;
                    dicom.Write(filename);
                    return;

                case ".raw":
                    filename = String.Format("{0}.{1}.{2}.raw", info.FullName.Replace(info.Extension, ""), attributes.width, attributes.height);
                    if (File.Exists(filename))
                    {
                        DialogResult result = MessageBox.Show("File Exists.\nDo you want to overwrite?", "Dicom Viewer", MessageBoxButtons.YesNo);
                        if (result == DialogResult.No)
                        {
                            return;
                        }
                    }
                    using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write))
                    {
                        EndianBinaryWriter writer = new EndianBinaryWriter(stream, Endian.Little);
                        writer.WriteWords((short[])dicom[t.PixelData].Value);
                    }
                    return;

                case ".bmp":
                    format = ImageFormat.Bmp;
                    break;

                case ".jpg":
                    format = ImageFormat.Jpeg;
                    break;

                case ".tif":
                case ".tiff":
                    format = ImageFormat.Tiff;
                    break;

                case ".gif":
                    format = ImageFormat.Gif;
                    break;

                case ".png":
                    format = ImageFormat.Png;
                    break;

                case ".emf":
                    format = ImageFormat.Emf;
                    break;

                case ".exif":
                    format = ImageFormat.Exif;
                    break;

                case ".wmf":
                    format = ImageFormat.Wmf;
                    break;

                default:
                    throw new ArgumentException("Unsupported extension");
                }
                Bitmap bitmap = OtherImageFormats.GetBitmap(attributes, overlays, voilut, invert);
                bitmap.Save(filename, format);
            }
            catch (Exception ex)
            {
                MessageBox.Show(Logging.Log(ex));
            }
        }