예제 #1
0
    public static bool ConvertDcmToPng(DbStudy study)
    {
        try
        {
            string dcmPath   = ADCM.GetStoreString();
            string studyPath = Path.Combine(dcmPath, study.study_uid);
            if (!Directory.Exists(studyPath))
            {
                throw new Exception("Study path not found");
            }
            var allSeriesPaths = Directory.GetDirectories(studyPath);
            if (allSeriesPaths.Length < 1)
            {
                throw new Exception("No series subdirectories");
            }

            foreach (var s in allSeriesPaths)
            {
                var dcmFiles = Directory.GetFiles(s, "*.dcm");
                if (dcmFiles.Length < 1)
                {
                    throw new Exception("No DCM files inside series path: " + s);
                }
                DicomFile tempdcm = new DicomFile(dcmFiles[0]);
                tempdcm.Load();
                var seriesName = tempdcm.DataSet[DicomTags.SeriesDescription].GetString(0, null);
                var seriesUID  = s.Substring(s.LastIndexOf(Path.DirectorySeparatorChar) + 1);
                if (string.IsNullOrEmpty(seriesName))
                {
                    seriesName = "Unamed_Series";
                }

                APetaPoco.SetConnectionString("cn1");
                var      bm       = APetaPoco.PpRetrieveOne <DbSeries>("Series", "[case_id] = '" + study.case_id + "' AND [series_uid] = '" + seriesUID + "'");
                DbSeries dbseries = null;
                if (bm.Success)
                {
                    dbseries = (DbSeries)bm.Data;
                }

                string outputPath = Path.Combine(dcmPath, "OUTPUT", study.case_id, study.study_uid, seriesUID);
                if (!Directory.Exists(outputPath))
                {
                    Directory.CreateDirectory(outputPath);
                }

                //int fileCount = 0;

                for (int k = 0; k < dcmFiles.Length; k++)
                {
                    DicomFile dcmFile = new DicomFile(dcmFiles[k]);
                    dcmFile.Load();
                    int fileCount   = 0;
                    var windowWidth = dcmFile.DataSet[DicomTags.WindowWidth].ToString();
                    if (!string.IsNullOrEmpty(windowWidth))
                    {
                        var tempSplitString = windowWidth.Split('\\');
                        windowWidth = tempSplitString[0];
                    }
                    var windowCenter = dcmFile.DataSet[DicomTags.WindowCenter].ToString();
                    if (!string.IsNullOrEmpty(windowCenter))
                    {
                        var tempSplitString = windowCenter.Split('\\');
                        windowCenter = tempSplitString[0];
                    }
                    var ww = dcmFile.DataSet[DicomTags.WindowWidth].GetFloat32(0, 0);
                    var wc = dcmFile.DataSet[DicomTags.WindowCenter].GetFloat32(0, 0);

                    if (ww == 0 && !string.IsNullOrEmpty(windowWidth))
                    {
                        if (windowWidth.Contains("."))
                        {
                            var tempSplitString = windowWidth.Split('.');
                            ww = int.Parse(tempSplitString[0]);
                        }
                        else
                        {
                            ww = int.Parse(windowWidth);
                        }
                    }
                    if (wc == 0 && !string.IsNullOrEmpty(windowCenter))
                    {
                        if (windowCenter.Contains("."))
                        {
                            var tempSplitString = windowCenter.Split('.');
                            wc = int.Parse(tempSplitString[0]);
                        }
                        else
                        {
                            wc = int.Parse(windowCenter);
                        }
                    }

                    LocalSopDataSource localds = new LocalSopDataSource(dcmFile);
                    if (!localds.IsImage)
                    {
                        continue;
                    }
                    ImageSop sop        = new ImageSop(localds);
                    int      frameCount = sop.Frames.Count;
                    var      fileName   = dcmFile.DataSet[DicomTags.InstanceNumber].GetInt16(0, 0);
                    if (frameCount > 1)
                    {
                        for (int j = 1; j <= frameCount; j++)
                        {
                            GC.Collect();
                            Frame  f       = sop.Frames[j];
                            var    jpgPath = Path.Combine(outputPath, fileName + "." + j + ".png");
                            Bitmap bmp     = null;
                            if (string.IsNullOrEmpty(windowWidth) || string.IsNullOrEmpty(windowCenter))
                            {
                                bmp = DrawDefaultFrame(f);
                            }
                            else
                            {
                                bmp = DrawLutFrame(f, ww, wc);
                            }
                            if (bmp != null)
                            {
                                if (dbseries != null && dbseries.crop_h != null &&
                                    dbseries.crop_w != null &&
                                    dbseries.crop_x != null &&
                                    dbseries.crop_y != null)
                                {
                                    bmp = Crop(bmp, dbseries.crop_x.Value, dbseries.crop_y.Value, dbseries.crop_w.Value, dbseries.crop_h.Value);
                                }
                                SaveImage(bmp, jpgPath);
                            }
                            fileCount += 1;
                            GC.Collect();
                        }
                    }
                    else
                    {
                        GC.Collect();
                        var    jpgPath = Path.Combine(outputPath, fileName + ".png");
                        Frame  f       = sop.Frames[1];
                        Bitmap bmp     = null;
                        if (string.IsNullOrEmpty(windowWidth) || string.IsNullOrEmpty(windowCenter))
                        {
                            bmp = DrawDefaultFrame(f);
                        }
                        else
                        {
                            bmp = DrawLutFrame(f, ww, wc);
                        }
                        if (bmp != null)
                        {
                            if (dbseries != null && dbseries.crop_h != null &&
                                dbseries.crop_w != null &&
                                dbseries.crop_x != null &&
                                dbseries.crop_y != null)
                            {
                                bmp = Crop(bmp, dbseries.crop_x.Value, dbseries.crop_y.Value, dbseries.crop_w.Value, dbseries.crop_h.Value);
                            }
                            SaveImage(bmp, jpgPath);
                        }
                        fileCount += 1;
                        GC.Collect();
                    }
                }
            }
            LOG.InsertEvent("Successfully converted study from DCM to PNG", "IMG", null, study.case_id, study.study_uid);
            return(true);
        }
        catch (Exception ex)
        {
            string errorString = "Error at :" + System.Reflection.MethodBase.GetCurrentMethod().Name;
            LOG.Write(errorString);
            LOG.Write(ex.Message);
            LOG.InsertEvent(errorString, "IMG", ex.Message, study.case_id, study.study_uid);
            return(false);
        }
    }
예제 #2
0
 public static void InsertNewCase(HttpContext context)
 {
     string caseId = "";
     var bm = new BoolMessage();
     bm.Data = null;        
     try
     {
         var upCasePackage = AF.GetObjectFromJSON<UploadPackage>(context);
         if (upCasePackage == null || upCasePackage == default(UploadPackage))
         {
             throw new Exception("Unable to parse incoming package");
         }
         APetaPoco.SetConnectionString("cn1");            
         caseId = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + upCasePackage.username;
         var dCase = new DbCase();
         dCase.age = upCasePackage.case_details.age;
         dCase.body = upCasePackage.case_details.body;
         dCase.case_id = caseId;
         dCase.date = DateTime.Now;
         dCase.diagnostic_certainty_id = upCasePackage.case_details.diagnostic_certainty_id;
         dCase.presentation = upCasePackage.case_details.presentation;
         dCase.status = "PENDING";
         dCase.status_message = "Inserted via Uploader UI";
         dCase.suitable_for_quiz = upCasePackage.case_details.suitable_for_quiz;
         dCase.system_id = upCasePackage.case_details.system_id;
         dCase.title = upCasePackage.case_details.title;
         dCase.username = upCasePackage.username;
         bm = APetaPoco.PpInsert(dCase);
         if (!bm.Success) { throw new Exception("Unable to insert case"); }
         foreach (var st in upCasePackage.studies)
         {
             var dcmSt = ADCM.GetStudyFromStudyUid(st.study_uid);
             var dSt = new DbStudy();                
             dSt.caption = st.caption;
             dSt.case_id = caseId;
             DateTime dt;
             bool dtp = DateTime.TryParseExact(st.date, "dd/MM/yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt);
             if (dtp) { dSt.date = dt; }
             if(dcmSt != null && dcmSt != default(Study))
             {
                 dSt.patient_dob = dcmSt.PatientDob;
                 dSt.patient_id = dcmSt.PatientId;
                 dSt.patient_name = dcmSt.PatientSurname.ToUpper() + ", " + dcmSt.PatientFirstname;
             }
             dSt.description = st.description;
             dSt.findings = st.findings;
             dSt.images = st.images;
             dSt.modality = st.modality;
             dSt.position = st.position;
             dSt.study_uid = st.study_uid;                
             bm = APetaPoco.PpInsert(dSt);
             if (!bm.Success) { throw new Exception("Unable to insert study"); }
             foreach (var se in st.series)
             {
                 var dSe = new DbSeries();
                 dSe.case_id = caseId;
                 dSe.description = se.description;
                 dSe.images = se.images;
                 dSe.series_uid = se.series_uid;
                 dSe.study_uid = st.study_uid;
                 dSe.crop_h = se.crop_h;
                 dSe.crop_w = se.crop_w;
                 dSe.crop_x = se.crop_x;
                 dSe.crop_y = se.crop_y;
                 dSe.window_wc = se.window_wc;
                 dSe.window_ww = se.window_ww;
                 dSe.end_image = se.end_image;
                 dSe.every_image = se.every_image;
                 dSe.start_image = se.start_image;
                 bm = APetaPoco.PpInsert(dSe);
                 if (!bm.Success) { throw new Exception("Unable to insert series"); }
             }
         }
         InsertEvent("Successfully inserted case into database:\n" + caseId, "WEB", Newtonsoft.Json.JsonConvert.SerializeObject(dCase), dCase.case_id);
         bm.Success = true;
         bm.Data = null;
         bm.Message = "Successfully inserted case into database";
         AF.BoolMessageRespond(context, bm);
     }
     catch (System.Threading.ThreadAbortException) { return; }
     catch (Exception ex)
     {
         if (!string.IsNullOrEmpty(caseId)) { ClearAllInDbWithCaseId(caseId); }            
         AF.ExceptionRespond(context, ex);
     }                        
 }