//DICOMファイルの圧縮 public static bool DicomToComp(string file, string dicomFile, Dictionary <string, string> tags) { try { //モダリティ string Modality = tags[DcmTag.Modality.Name]; //圧縮タイプ string CompType; if (!AppUtil.ModalityCompType.TryGetValue(Modality, out CompType)) { CompType = AppUtil.CompType; } //圧縮オプション string CompOption; if (!AppUtil.ModalityCompOption.TryGetValue(Modality, out CompOption)) { CompOption = AppUtil.CompOption; } //ディレクトリ作成 string dicomDir = Path.GetDirectoryName(dicomFile); if (!Directory.Exists(dicomDir)) { Directory.CreateDirectory(dicomDir); } //ファイル削除 if (File.Exists(dicomFile)) { lock (fileLock) { File.Delete(dicomFile); } } //圧縮 if (CompType == "0") { //なし return(FileUtil.Copy(file, dicomFile)); } else if (CompType == "2") { //MEG var scale = double.Parse(CompOption); DicomToMeg(file, dicomFile, scale); return(true); } else { //転送構文UID string TransferSyntaxUID = tags[DcmTag.TransferSyntaxUID.Name]; if (TransferSyntaxUID != "1.2.840.10008.1.2" && TransferSyntaxUID != "1.2.840.10008.1.2.1") { LogUtil.Debug1("TransferSyntaxUID[{0}]", TransferSyntaxUID); } //画素表現 string PixelRepresentation = tags[DcmTag.PixelRepresentation.Name]; var dcmCtrl = new DicomFile(file); //圧縮対象 if (TransferSyntaxUID == "1.2.840.10008.1.2" || TransferSyntaxUID == "1.2.840.10008.1.2.1" || TransferSyntaxUID == "1.2.840.10008.1.2.2") { //JPEG-Lossy(8bit)圧縮 if (CompType == "50" && dcmCtrl.EncodeToJpeg("+eb +un", dicomFile)) { dcmCtrl = new DicomFile(dicomFile); } //JPEG-Lossy(12bit)圧縮 else if (CompType == "51" && dcmCtrl.EncodeToJpeg("+ee +un", dicomFile)) { dcmCtrl = new DicomFile(dicomFile); } //JPEG-Lossless圧縮 else if (CompType == "70" && dcmCtrl.EncodeToJpeg("+e1", dicomFile)) { dcmCtrl = new DicomFile(dicomFile); } //JPEG2000圧縮 else if (CompType == "90" || CompType == "91") { string tmpFile = ""; try { //Explicit変換 if (TransferSyntaxUID != "1.2.840.10008.1.2.1") { tmpFile = dcmCtrl.FileName + ".tmp"; if (dcmconv("+te", dcmCtrl.FileName, tmpFile)) { dcmCtrl = new DicomFile(tmpFile); } } //JPEG2000変換 if (!File.Exists(AppUtil.CompExe)) { throw new FileNotFoundException(AppUtil.CompExe + " NOT FOUND"); } //EXE引数 string compArg; //JPEG2000-Lossless圧縮 if (CompType == "90") { compArg = "\"" + dcmCtrl.FileName + "\" \"" + dicomFile + "\""; } //JPEG2000-Lossy圧縮 else { string ratio; if (!AppUtil.ModalityCompOption.TryGetValue(Modality, out ratio)) { ratio = AppUtil.CompOption; } string[] ratios = ratio.Split(':'); if (ratios.Length == 1 || (ratios.Length == 2 && ratios[0] == ratios[1])) { ratio = ratios[0]; } else { //画素表現により決定 if (PixelRepresentation == "1") { ratio = ratios[1]; } else { ratio = ratios[0]; } } compArg = "\"" + dcmCtrl.FileName + "\" \"" + dicomFile + "\" " + ratio; } bool moveFlag = true; using (Process p = new Process()) { p.StartInfo.FileName = AppUtil.CompExe; p.StartInfo.Arguments = compArg; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; if (p.Start()) { if (p.WaitForExit(AppUtil.ExeTimeout)) { if (File.Exists(dicomFile)) { dcmCtrl = new DicomFile(dicomFile); moveFlag = false; } else { LogUtil.Warn("CompExe:ExitCode={0}" + p.ExitCode); } } else { LogUtil.Warn("CompExe:Timeout"); try { p.Kill(); p.WaitForExit(); } catch { } } } else { LogUtil.Warn("CompExe:Process.Start()=false"); } } if (moveFlag) { for (int i = 0; i < AppUtil.RetryCount; i++) { try { if (File.Exists(dicomFile)) { lock (fileLock2) { File.Delete(dicomFile); } } return(FileUtil.Copy(file, dicomFile)); } catch (Exception ex) { LogUtil.Warn(ex); } //リトライ if (i < AppUtil.RetryCount - 1) { System.Threading.Thread.Sleep(AppUtil.SleepTime); LogUtil.Info1("RETRY:{0}", i + 1); } else { LogUtil.Error1("DicomFile.Move():失敗 [{0}]", dicomFile); } } } } finally { if (tmpFile != "") { FileUtil.Delete(tmpFile, true); } } } else { //無変換 return(FileUtil.Copy(file, dicomFile)); } } else { //無変換 return(FileUtil.Copy(file, dicomFile)); } } return(true); } catch (Exception ex) { LogUtil.Error(ex.ToString()); return(false); } }