Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- using EvilDicom;
- using System.Drawing.Imaging;
- namespace SevKavTomograf.Inner_Class
- {
- class DCMReader
- {
- string Name;
- ushort Rows,
- Collums,
- Samples_Per_Pixel,
- Bits_Allocated,
- Bits_Stored,
- Pixel_Representation;
- byte[] Pixel_Data;
- private void ReadDicomFile(string Path)
- {
- EvilDicom.Components.DICOMFile dcmf = new EvilDicom.Components.DICOMFile(Path);
- EvilDicom.Components.DICOMElement dICOMElement = new EvilDicom.Components.DICOMElement();
- var tName = dcmf.Find(EvilDicom.Helper.TagHelper.PATIENT_NAME) as EvilDicom.VR.PersonsName;
- Name = tName.Data;
- var tCollums = dcmf.Find(EvilDicom.Helper.TagHelper.COLUMNS) as EvilDicom.VR.UnsignedShort;
- Collums = tCollums.Data;
- var trows = dcmf.Find(EvilDicom.Helper.TagHelper.ROWS) as EvilDicom.VR.UnsignedShort;
- Rows = trows.Data;
- var tspp = dcmf.Find(EvilDicom.Helper.TagHelper.SAMPLES_PER_PIXEL) as EvilDicom.VR.UnsignedShort;
- Samples_Per_Pixel = tspp.Data;
- var tba = dcmf.Find(EvilDicom.Helper.TagHelper.BITS_ALLOCATED) as EvilDicom.VR.UnsignedShort;
- Bits_Allocated = tba.Data;
- var tbs = dcmf.Find(EvilDicom.Helper.TagHelper.BITS_STORED) as EvilDicom.VR.UnsignedShort;
- Bits_Stored = tbs.Data;
- var tpr = dcmf.Find(EvilDicom.Helper.TagHelper.PIXEL_REPRESENTATION) as EvilDicom.VR.UnsignedShort;
- Pixel_Representation = tpr.Data;
- var tpd = dcmf.Find(EvilDicom.Helper.TagHelper.PIXEL_DATA) as EvilDicom.VR.PixelData;
- Pixel_Data = tpd.ByteData;
- var tmin = dcmf.Find(EvilDicom.Helper.TagHelper.CHANNEL_MAXIMUM_VALUE) as EvilDicom.VR.IntegerString;
- }
- public Bitmap CreateBitmap(string Path)
- {
- ReadDicomFile(Path);
- byte[] AllBits = Pixel_Data;
- ushort[] mas1 = new ushort[Pixel_Data.Length / 2];
- int k = 0;
- int winMin = 65536;
- int winMax = 0;
- for (int i = 0; i < AllBits.Length - 1; i += 2)
- {
- mas1[k] = (ushort)(Convert.ToUInt16((AllBits[i + 1] << 8) + AllBits[i]));
- if (mas1[k] > winMax) winMax = mas1[k];
- if (mas1[k] < winMin) winMin = mas1[k];
- k++;
- }
- int range = winMax - winMin;
- if (range < 1) range = 1;
- double factor = 255.0 / range;
- Bitmap bmp = new Bitmap(Collums, Rows, PixelFormat.Format24bppRgb);
- BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, Collums, Rows), ImageLockMode.ReadOnly, bmp.PixelFormat);
- IntPtr xx;
- int bytes = mas1.Length * 3;
- byte[] rgbValues = new byte[bytes];
- int jk = 0;
- for (int i = 0; i < bmd.Height; i++)
- {
- xx = bmd.Scan0 + (i * bmd.Stride);
- for (int j = 0; j < bmd.Width; j++)
- {
- rgbValues[jk + 0] = (byte)((mas1[i * bmd.Width + j] - winMin) * factor);
- rgbValues[jk + 1] = (byte)((mas1[i * bmd.Width + j] - winMin) * factor);
- rgbValues[jk + 2] = (byte)((mas1[i * bmd.Width + j] - winMin) * factor);
- jk += 3;
- }
- System.Runtime.InteropServices.Marshal.Copy(rgbValues, i * bmd.Width * 3, xx, bmd.Width * 3);
- }
- bmp.UnlockBits(bmd);
- return bmp;
- }
- private int CheckCollums(string[] Paths)
- {
- int ChCollums = 0;
- bool flag = true;
- foreach (string Path in Paths)
- {
- EvilDicom.Components.DICOMFile dcmf = new EvilDicom.Components.DICOMFile(Path);
- EvilDicom.Components.DICOMElement dICOMElement = new EvilDicom.Components.DICOMElement();
- var tCollums = dcmf.Find(EvilDicom.Helper.TagHelper.COLUMNS) as EvilDicom.VR.UnsignedShort;
- var Collums = tCollums.Data;
- if (flag)
- {
- ChCollums = (int)Collums;
- flag = false;
- }
- if (ChCollums != (int)Collums) ;
- //Здесь выдавать ошибку
- //MessageBox.Show("Не совпадают разрешения выбранных Dicom файлов");
- }
- return ChCollums;
- }
- private int CheckRows(string[] Paths)
- {
- int ChRows = 0;
- bool flag = true;
- foreach (string Path in Paths)
- {
- EvilDicom.Components.DICOMFile dcmf = new EvilDicom.Components.DICOMFile(Path);
- EvilDicom.Components.DICOMElement dICOMElement = new EvilDicom.Components.DICOMElement();
- var trows = dcmf.Find(EvilDicom.Helper.TagHelper.ROWS) as EvilDicom.VR.UnsignedShort;
- var Rows = trows.Data;
- if (flag)
- {
- ChRows = (int)Rows;
- flag = false;
- }
- if (ChRows != (int)Rows) ;
- //Здесь выдавать ошибку
- //MessageBox.Show("Не совпадают разрешения выбранных Dicom файлов");
- }
- return ChRows;
- }
- public byte[] Buffer(string[] Paths)
- {
- int Collums = CheckCollums(Paths);
- int Rows = CheckRows(Paths);
- byte[] OneTime;
- byte[] OutPut = new byte[2 * Rows * Collums * Paths.Length];
- int k = 0;
- foreach (string Path in Paths)
- {
- OneTime = TreatmentDCM(Path);
- for (int i = 0; i < OneTime.Length - 1; i++)
- {
- OutPut[k] = OneTime[i];
- k++;
- }
- }
- return OutPut;
- }
- private byte[] TreatmentDCM(string Path)
- {
- EvilDicom.Components.DICOMFile dcmf = new EvilDicom.Components.DICOMFile(Path);
- EvilDicom.Components.DICOMElement dICOMElement = new EvilDicom.Components.DICOMElement();
- var tpd = dcmf.Find(EvilDicom.Helper.TagHelper.PIXEL_DATA) as EvilDicom.VR.PixelData;
- var Pixel_Data = tpd.ByteData;
- return Pixel_Data;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement