Exemplo n.º 1
0
    // Create a 256 x 256 Secondary Capture Image Storage
    static private void CreateSmallDICOM(string fileName)
    {
        using (var writer = new gdcm.PixmapWriter())
        {
            gdcm.Pixmap img = writer.GetImage();
            img.SetNumberOfDimensions(3);
            img.SetDimension(0, 512);
            img.SetDimension(1, 512);
            img.SetDimension(2, 2); // fake a 3d volume
            PhotometricInterpretation pi = new PhotometricInterpretation(PhotometricInterpretation.PIType.MONOCHROME2);
            img.SetPhotometricInterpretation(pi);
            gdcm.DataElement pixeldata = new gdcm.DataElement(new gdcm.Tag(0x7fe0, 0x0010));
            byte[]           buffer    = new byte[512 * 512 * 2];
            pixeldata.SetByteValue(buffer, new gdcm.VL((uint)buffer.Length));
            img.SetDataElement(pixeldata);

            gdcm.File        file         = writer.GetFile();
            gdcm.DataSet     ds           = file.GetDataSet();
            gdcm.DataElement ms           = new gdcm.DataElement(new gdcm.Tag(0x0008, 0x0016));
            string           mediastorage = "1.2.840.10008.5.1.4.1.1.7.2"; // Multi-frame Grayscale Byte Secondary Capture Image Storage
            byte[]           val          = StrToByteArray(mediastorage);
            ms.SetByteValue(val, new gdcm.VL((uint)val.Length));
            ds.Insert(ms);

            writer.SetFileName(fileName);
            writer.Write();
        }
    }
Exemplo n.º 2
0
        static int Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("This program prints the patient name of a dicom file with gdcm");
                Console.WriteLine("Usage: [input.dcm]");
                return(1);
            }

            gdcm.Reader reader = new gdcm.Reader();
            reader.SetFileName(args[0]);
            bool ret = reader.Read();

            //TagSetType tst = new TagSetType();
            //tst.Add( new Tag(0x7fe0,0x10) );
            //bool ret = reader.ReadUpToTag( new Tag(0x88,0x200), tst );
            if (!ret)
            {
                return(1);
            }

            gdcm.File file = reader.GetFile();

            gdcm.StringFilter filter = new gdcm.StringFilter();
            filter.SetFile(file);
            string value = filter.ToString(new gdcm.Tag(0x0010, 0x0010));

            Console.WriteLine("Patient Name: " + value);
            return(0);
        }
Exemplo n.º 3
0
    public static int Main(string[] args)
    {
        string filename = args[0];

        gdcm.StreamImageReader reader = new gdcm.StreamImageReader();

        reader.SetFileName(filename);

        if (!reader.ReadImageInformation())
        {
            return(1);
        }
        // Get file infos
        gdcm.File f = reader.GetFile();

        // get some info about image
        UIntArrayType extent = ImageHelper.GetDimensionsValue(f);
        //System.Console.WriteLine( extent[0] );
        uint dimx = extent[0];
        //System.Console.WriteLine( extent[1] );
        uint dimy = extent[1];
        //System.Console.WriteLine( extent[2] );
        uint        dimz      = extent[2];
        PixelFormat pf        = ImageHelper.GetPixelFormatValue(f);
        int         pixelsize = pf.GetPixelSize();

        //System.Console.WriteLine( pixelsize );

        // buffer to get the pixels
        byte[] buffer = new byte[dimx * dimy * pixelsize];

        for (int i = 0; i < dimz; i++)
        {
            // Define that I want the image 0, full size (dimx x dimy pixels)
            reader.DefinePixelExtent(0, (ushort)dimx, 0, (ushort)dimy, (ushort)i, (ushort)(i + 1));
            uint buf_len = reader.DefineProperBufferLength(); // take into account pixel size
            //System.Console.WriteLine( buf_len );
            if (buf_len > buffer.Length)
            {
                throw new Exception("buffer is too small for target");
            }

            if (reader.Read(buffer, (uint)buffer.Length))
            {
                using (System.IO.Stream stream =
                           System.IO.File.Open(@"/tmp/frame.raw",
                                               System.IO.FileMode.Create))
                {
                    System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
                    writer.Write(buffer);
                }
            }
            else
            {
                throw new Exception("can't read pixels error");
            }
        }

        return(0);
    }
Exemplo n.º 4
0
    public static int Main(string[] args)
    {
        string filename = args[0];

        // instantiate the reader:
        gdcm.ImageRegionReader reader = new gdcm.ImageRegionReader();
        reader.SetFileName(filename);

        // pull DICOM info:
        if (!reader.ReadInformation())
        {
            return(1);
        }
        // Get file infos
        gdcm.File f = reader.GetFile();

        // get some info about image
        UIntArrayType dims      = ImageHelper.GetDimensionsValue(f);
        PixelFormat   pf        = ImageHelper.GetPixelFormatValue(f);
        int           pixelsize = pf.GetPixelSize();

        // buffer to get the pixels
        byte[] buffer = new byte[dims[0] * dims[1] * pixelsize];

        // define a simple box region.
        BoxRegion box = new BoxRegion();

        for (uint z = 0; z < dims[2]; z++)
        {
            // Define that I want the image 0, full size (dimx x dimy pixels)
            // and do that for each z:
            box.SetDomain(0, dims[0] - 1, 0, dims[1] - 1, z, z);
            //System.Console.WriteLine( box.toString() );
            reader.SetRegion(box);

            // reader will try to load the uncompressed image region into buffer.
            // the call returns an error when buffer.Length is too small. For instance
            // one can call:
            // uint buf_len = reader.ComputeBufferLength(); // take into account pixel size
            // to get the exact size of minimum buffer
            if (reader.ReadIntoBuffer(buffer, (uint)buffer.Length))
            {
                using (System.IO.Stream stream =
                           System.IO.File.Open(@"/tmp/frame.raw",
                                               System.IO.FileMode.Create))
                {
                    System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
                    writer.Write(buffer);
                }
            }
            else
            {
                throw new Exception("can't read pixels error");
            }
        }

        return(0);
    }
Exemplo n.º 5
0
    public static int Main(string[] args)
    {
        string filename = args[0];

        gdcm.Reader reader = new gdcm.Reader();
        reader.SetFileName(filename);
        if (!reader.Read())
        {
            return(1);
        }

        gdcm.File    f  = reader.GetFile();
        gdcm.DataSet ds = f.GetDataSet();

        string[] expectedSiemensTags = new string[] { "B_value", "AcquisitionMatrixText" };
        using (PrivateTag gtag = CSAHeader.GetCSAImageHeaderInfoTag())
        {
            if (ds.FindDataElement(gtag))
            {
                using (DataElement de = ds.GetDataElement(gtag))
                {
                    if (de != null && !de.IsEmpty())
                    {
                        using (CSAHeader csa = new CSAHeader())
                        {
                            if (csa.LoadFromDataElement(de))
                            {
                                foreach (string str in expectedSiemensTags)
                                {
                                    if (csa.FindCSAElementByName(str))
                                    {
                                        using (CSAElement elem = csa.GetCSAElementByName(str))
                                        {
                                            if (elem != null)
                                            {
                                                System.Console.WriteLine(elem.toString());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


        return(0);
    }
Exemplo n.º 6
0
        private void FramesQuery(string PatientId)
        {
            gdcm.ERootType typ = gdcm.ERootType.ePatientRootType;

            gdcm.EQueryLevel poziom = gdcm.EQueryLevel.ePatient; // zobacz inne

            gdcm.KeyValuePairArrayType klucze = new gdcm.KeyValuePairArrayType();
            gdcm.KeyValuePairType      klucz1 = new gdcm.KeyValuePairType(new gdcm.Tag(0x0010, 0x0020), PatientId); // NIE WOLNO TU STOSOWAC *; tutaj PatientID="01"
            klucze.Add(klucz1);

            // skonstruuj zapytanie
            gdcm.BaseRootQuery zapytanie = gdcm.CompositeNetworkFunctions.ConstructQuery(typ, poziom, klucze, true);

            // sprawdź, czy zapytanie spełnia kryteria
            if (!zapytanie.ValidateQuery())
            {
                MessageBox.Show("Wrong frames query.", "Error");
            }

            // przygotuj katalog na wyniki
            String odebrane = System.IO.Path.Combine(".", "temporary");

            if (!System.IO.Directory.Exists(odebrane))
            {
                System.IO.Directory.CreateDirectory(odebrane);
            }

            dane = System.IO.Path.Combine(odebrane, System.IO.Path.GetRandomFileName());  //katalog
            System.IO.Directory.CreateDirectory(dane);

            // wykonaj zapytanie - pobierz do katalogu _dane_
            bool stan = gdcm.CompositeNetworkFunctions.CMove(ip, port, zapytanie, port, aet, call, dane);

            // sprawdź stan
            if (!stan)
            {
                MessageBox.Show("PACS server doesn't work.", "Error");
            }
            // skasowanie listy zdjęć
            Images.Clear();
            ImageNames.Clear();
            Datas.Clear();
            List <string> pliki = new List <string>(System.IO.Directory.EnumerateFiles(dane));  //nazwy plikow

            foreach (String plik in pliki)
            {
                gdcm.Reader dataReader = new gdcm.Reader();
                dataReader.SetFileName(plik);

                if (!dataReader.Read())
                {
                    continue;
                }
                gdcm.File    file      = dataReader.GetFile();
                gdcm.DataSet dataSet   = file.GetDataSet();
                string       data      = dataSet.toString();
                gdcm.Global  g         = gdcm.Global.GetInstance();
                gdcm.Dicts   dicts     = g.GetDicts();
                gdcm.Dict    dict      = dicts.GetPublicDict();
                string[]     dataArray = dataSet.toString().Split('\n');
                Dictionary <string, string> dataValues = new Dictionary <string, string>();
                String[] id = plik.Split('\\');

                foreach (string s in dataArray)
                {
                    string[] dataArrayRow = s.Split('\t');
                    if (dataArrayRow.Length > 1)
                    {
                        string[] tags = dataArrayRow[0].Remove(0, 1).Remove(dataArrayRow[0].Length - 2, 1).Split(',');

                        //Pobranie nazwy Tagu
                        //gdcm.Tag tag = new gdcm.Tag(Convert.ToUInt16(tags[0]),Convert.ToUInt16(tags[1]));
                        //string dictDorTag = dict.GetKeywordFromTag(tag);
                        //if (dictDorTag != null)
                        //   dataValues.Add(dictDorTag, dataArrayRow[dataArrayRow.Length - 1]);

                        dataValues.Add(dataArrayRow[0], dataArrayRow[dataArrayRow.Length - 1]);
                    }
                }

                // przeczytaj pixele
                gdcm.PixmapReader reader = new gdcm.PixmapReader();
                reader.SetFileName(plik);
                if (!reader.Read())
                {
                    continue;
                }


                // przekonwertuj na "znany format"
                gdcm.Bitmap bmjpeg2000 = PACS.ImageConverter.pxmap2jpeg2000(reader.GetPixmap());
                // przekonwertuj na .NET bitmapy
                System.Drawing.Bitmap[] X = PACS.ImageConverter.gdcmBitmap2Bitmap(bmjpeg2000);

                // zapisz
                for (int i = 0; i < X.Length; i++)
                {
                    String name = "";
                    if (X.Length > 1)
                    {
                        name = String.Format("{0}_slice{1}.jpg", plik, i);
                        Images.Add(String.Format("{0}_slice{1}", id[id.Length - 1], i), X[i]);
                        ImageNames.Add(String.Format("{0}_slice{1}", id[id.Length - 1], i));
                        Datas.Add(String.Format("{0}_slice{1}", id[id.Length - 1], i), dataValues);
                    }
                    else
                    {
                        name = String.Format("{0}.jpg", plik);
                        Images.Add(String.Format("{0}", id[id.Length - 1]), X[i]);
                        ImageNames.Add(String.Format("{0}", id[id.Length - 1]));
                        Datas.Add(String.Format("{0}", id[id.Length - 1]), dataValues);
                    }

                    X[i].Save(name);
                }
            }
        }
Exemplo n.º 7
0
 gdcm.DataSet GetDatasetFromFile(string path)
 {
     gdcm.Reader reader = CreateReader(path);
     gdcm.File   file   = reader.GetFile();
     return(file.GetDataSet());
 }
Exemplo n.º 8
0
        private void FramesQuery(string PatientId, out string dane, out string name, out Dictionary <string, Dictionary <string, string> > Datas, out string segmentation_name)
        {
            gdcm.ERootType typ = gdcm.ERootType.ePatientRootType;

            gdcm.EQueryLevel poziom = gdcm.EQueryLevel.ePatient; // zobacz inne

            gdcm.KeyValuePairArrayType klucze = new gdcm.KeyValuePairArrayType();
            gdcm.KeyValuePairType      klucz1 = new gdcm.KeyValuePairType(new gdcm.Tag(0x0010, 0x0020), PatientId); // NIE WOLNO TU STOSOWAC *; tutaj PatientID="01"
            klucze.Add(klucz1);

            // skonstruuj zapytanie
            gdcm.BaseRootQuery zapytanie = gdcm.CompositeNetworkFunctions.ConstructQuery(typ, poziom, klucze, true);

            // sprawdź, czy zapytanie spełnia kryteria
            if (!zapytanie.ValidateQuery())
            {
                MessageBox.Show("Wrong frames query.", "Error");
            }

            // przygotuj katalog na wyniki
            String odebrane = System.IO.Path.Combine(".", "temporary");

            if (!System.IO.Directory.Exists(odebrane))
            {
                System.IO.Directory.CreateDirectory(odebrane);
            }

            dane = System.IO.Path.Combine(odebrane, System.IO.Path.GetRandomFileName());  //katalog
            System.IO.Directory.CreateDirectory(dane);

            // wykonaj zapytanie - pobierz do katalogu _dane_
            bool stan = gdcm.CompositeNetworkFunctions.CMove(ip, port, zapytanie, 10104, aet, call, dane);

            // sprawdź stan
            if (!stan)
            {
                MessageBox.Show("PACS server doesn't work.", "Error");
            }
            // skasowanie listy zdjęć
            name = "";
            segmentation_name = "";
            Datas             = new Dictionary <string, Dictionary <string, string> >();

            List <string> pliki = new List <string>(System.IO.Directory.EnumerateFiles(dane));  //nazwy plikow

            //OBRAZ ORYGINALNY
            gdcm.Reader dataReader = new gdcm.Reader();
            dataReader.SetFileName(pliki[0]);

            if (!dataReader.Read())
            {
            }

            gdcm.File    file    = dataReader.GetFile();
            gdcm.DataSet dataSet = file.GetDataSet();
            string       data    = dataSet.toString();

            gdcm.Global g         = gdcm.Global.GetInstance();
            gdcm.Dicts  dicts     = g.GetDicts();
            gdcm.Dict   dict      = dicts.GetPublicDict();
            string[]    dataArray = dataSet.toString().Split('\n');
            Dictionary <string, string> dataValues = new Dictionary <string, string>();

            String[] id = pliki[0].Split('\\');

            bool first_exp      = true;
            bool first_original = true;

            foreach (string plik in pliki)
            {
                dataValues.Clear();
                foreach (string s in dataArray)
                {
                    string[] dataArrayRow = s.Split('\t');
                    if (dataArrayRow.Length > 1)
                    {
                        string[] tags = dataArrayRow[0].Remove(0, 1).Remove(dataArrayRow[0].Length - 2, 1).Split(',');

                        //Pobranie nazwy Tagu
                        //gdcm.Tag tag = new gdcm.Tag(Convert.ToUInt16(tags[0]),Convert.ToUInt16(tags[1]));
                        //string dictDorTag = dict.GetKeywordFromTag(tag);
                        //if (dictDorTag != null)
                        //   dataValues.Add(dictDorTag, dataArrayRow[dataArrayRow.Length - 1]);

                        dataValues.Add(dataArrayRow[0], dataArrayRow[dataArrayRow.Length - 1]);
                    }
                }
                if (dataValues.Count > 0)
                {
                    if (dataValues["(0020,000d)"].Substring(0, 1) == "E" && first_exp == true)
                    {//OBRAZ SEGMENTACJI
                        gdcm.Reader dataReader2 = new gdcm.Reader();
                        dataReader2.SetFileName(plik);

                        if (!dataReader2.Read())
                        {
                        }

                        gdcm.File file2 = dataReader2.GetFile();

                        // przeczytaj pixele
                        gdcm.PixmapReader reader2 = new gdcm.PixmapReader();
                        string            temp    = plik;
                        if (plik.Substring(plik.Length - 4, 4) != ".dcm")
                        {
                            temp = pliki[1] + ".dcm";
                        }

                        reader2.SetFileName(plik);
                        if (!reader2.Read())
                        {
                        }

                        segmentation_name = String.Format("{0}", plik.Substring(0, plik.Length - 4));
                        first_exp         = false;
                    }
                    else
                    {
                        if (first_original == true)
                        {
                            // przeczytaj pixele
                            gdcm.PixmapReader reader = new gdcm.PixmapReader();
                            string            temp   = plik;
                            if (plik.Substring(plik.Length - 4, 4) != ".dcm")
                            {
                                temp = plik + ".dcm";
                            }
                            reader.SetFileName(plik);
                            if (!reader.Read())
                            {
                            }
                            name = String.Format("{0}", plik.Substring(0, plik.Length - 4));
                            Datas.Add(String.Format("{0}", plik.Substring(0, plik.Length - 4)).Replace("\\", "\\\\"), dataValues);
                            first_original = false;
                        }
                        else
                        {
                            gdcm.Reader dataReader2 = new gdcm.Reader();
                            dataReader2.SetFileName(plik);

                            if (!dataReader2.Read())
                            {
                            }

                            gdcm.File file2 = dataReader2.GetFile();

                            // przeczytaj pixele
                            gdcm.PixmapReader reader2 = new gdcm.PixmapReader();
                            string            temp    = plik;
                            if (plik.Substring(plik.Length - 4, 4) != ".dcm")
                            {
                                temp = pliki[1] + ".dcm";
                            }

                            reader2.SetFileName(plik);
                            if (!reader2.Read())
                            {
                            }

                            segmentation_name = String.Format("{0}", plik.Substring(0, plik.Length - 4));
                            first_exp         = false;
                        }
                    }
                }
            }
        }
Exemplo n.º 9
0
    public static int Main(string[] args)
    {
        string         file1 = args[0];
        Mpeg2VideoInfo info  = new Mpeg2VideoInfo(file1);

        System.Console.WriteLine(info.StartTime);
        System.Console.WriteLine(info.EndTime);
        System.Console.WriteLine(info.Duration);
        System.Console.WriteLine(info.AspectRatio);
        System.Console.WriteLine(info.FrameRate);
        System.Console.WriteLine(info.PictureWidth);
        System.Console.WriteLine(info.PictureHeight);

        ImageReader r = new ImageReader();
        //Image image = new Image();
        Image image = r.GetImage();

        image.SetNumberOfDimensions(3);
        DataElement pixeldata = new DataElement(new gdcm.Tag(0x7fe0, 0x0010));

        System.IO.FileStream infile =
            new System.IO.FileStream(file1, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        uint fsize = gdcm.PosixEmulation.FileSize(file1);

        byte[] jstream = new byte[fsize];
        infile.Read(jstream, 0, jstream.Length);

        SmartPtrFrag sq   = SequenceOfFragments.New();
        Fragment     frag = new Fragment();

        frag.SetByteValue(jstream, new gdcm.VL((uint)jstream.Length));
        sq.AddFragment(frag);
        pixeldata.SetValue(sq.__ref__());

        // insert:
        image.SetDataElement(pixeldata);

        PhotometricInterpretation pi = new PhotometricInterpretation(PhotometricInterpretation.PIType.YBR_PARTIAL_420);

        image.SetPhotometricInterpretation(pi);
        // FIXME hardcoded:
        PixelFormat pixeltype = new PixelFormat(3, 8, 8, 7);

        image.SetPixelFormat(pixeltype);

        // FIXME hardcoded:
        TransferSyntax ts = new TransferSyntax(TransferSyntax.TSType.MPEG2MainProfile);

        image.SetTransferSyntax(ts);

        image.SetDimension(0, (uint)info.PictureWidth);
        image.SetDimension(1, (uint)info.PictureHeight);
        image.SetDimension(2, 721);

        ImageWriter writer = new ImageWriter();

        gdcm.File file = writer.GetFile();
        file.GetHeader().SetDataSetTransferSyntax(ts);
        Anonymizer anon = new Anonymizer();

        anon.SetFile(file);

        MediaStorage ms = new MediaStorage(MediaStorage.MSType.VideoEndoscopicImageStorage);

        UIDGenerator gen = new UIDGenerator();

        anon.Replace(new Tag(0x0008, 0x16), ms.GetString());
        anon.Replace(new Tag(0x0018, 0x40), "25");
        anon.Replace(new Tag(0x0018, 0x1063), "40.000000");
        anon.Replace(new Tag(0x0028, 0x34), "4\\3");
        anon.Replace(new Tag(0x0028, 0x2110), "01");

        writer.SetImage(image);
        writer.SetFileName("dummy.dcm");
        if (!writer.Write())
        {
            System.Console.WriteLine("Could not write");
            return(1);
        }

        return(0);
    }