public void writeCoeffs(MEncoder encoder, BlockType blockType, int[] _out, int first, int num, int[] reorder) { for (int i = 0; i < num; i++) { tmp[i] = _out[reorder[first + i]]; } int numCoeff = 0; for (int i = 0; i < num; i++) { if (tmp[i] != 0) { numCoeff = i + 1; } } for (int i = 0; i < Math.Min(numCoeff, num - 1); i++) { if (tmp[i] != 0) { encoder.encodeBin(blockType.sigCoeffFlagCtxOff + i, 1); encoder.encodeBin(blockType.lastSigCoeffCtxOff + i, i == numCoeff - 1 ? 1 : 0); } else { encoder.encodeBin(blockType.sigCoeffFlagCtxOff + i, 0); } } int numGt1 = 0, numEq1 = 0; for (int j = numCoeff - 1; j >= 0; j--) { if (tmp[j] == 0) { continue; } int absLev = Math.Abs(tmp[j]) - 1; writeCoeffAbsLevel(encoder, blockType, numGt1, numEq1, absLev); if (absLev == 0) { ++numEq1; } else { ++numGt1; } encoder.encodeBinBypass(sign(tmp[j])); } }
private void writeCoeffAbsLevel(MEncoder encoder, BlockType blockType, int numDecodAbsLevelGt1, int numDecodAbsLevelEq1, int absLev) { int incB0 = ((numDecodAbsLevelGt1 != 0) ? 0 : Math.Min(4, 1 + numDecodAbsLevelEq1)); int incBN = 5 + Math.Min(4 - blockType.coeffAbsLevelAdjust, numDecodAbsLevelGt1); if (absLev == 0) { encoder.encodeBin(blockType.coeffAbsLevelCtxOff + incB0, 0); } else { encoder.encodeBin(blockType.coeffAbsLevelCtxOff + incB0, 1); if (absLev < 14) { for (int i = 1; i < absLev; i++) { encoder.encodeBin(blockType.coeffAbsLevelCtxOff + incBN, 1); } encoder.encodeBin(blockType.coeffAbsLevelCtxOff + incBN, 0); } else { for (int i = 1; i < 14; i++) { encoder.encodeBin(blockType.coeffAbsLevelCtxOff + incBN, 1); } absLev -= 14; int sufLen, pow; for (sufLen = 0, pow = 1; absLev >= pow; sufLen++, pow = (1 << sufLen)) { encoder.encodeBinBypass(1); absLev -= pow; } encoder.encodeBinBypass(0); for (sufLen--; sufLen >= 0; sufLen--) { encoder.encodeBinBypass((absLev >> sufLen) & 1); } } } }
/// <summary> /// Save DICOM file /// </summary> /// <param name="dataset"></param> /// <param name="baseTsn"></param> /// <param name="implClassUid"></param> /// <param name="versionName"></param> /// <param name="path"></param> /// <returns></returns> private bool SaveDICOMFile(MElementList dataset, ts_t baseTsn, string implClassUid, string versionName, string filePath) { try { // add the following group 2 element for writing pt10 files string str_tsn = "1.2.840.10008.1.2"; switch (baseTsn) { case ts_t.ImplicitVRLittleEndian: str_tsn = "1.2.840.10008.1.2"; break; case ts_t.ExplicitVRBigEndian: str_tsn = "1.2.840.10008.1.2.2"; break; case ts_t.ExplicitVRLittleEndian: str_tsn = "1.2.840.10008.1.2.1"; break; case ts_t.JPEGLosslessNonHierarchical_14: str_tsn = "1.2.840.10008.1.2.4.57"; break; case ts_t.JPEGLosslessNonHierarchicalFirstOrderPrediction: str_tsn = "1.2.840.10008.1.2.4.70"; break; default: str_tsn = "1.2.840.10008.1.2"; break; } MElement tsnUid = new MElement(tag_t.kTransferSyntaxUID, vr_t.UI); UID tsn = new UID(str_tsn); tsnUid.set_uid(0, tsn); dataset.addElement(tsnUid); MElement implUid = new MElement(tag_t.kImplementationClassUID, vr_t.UI); UID uid = new UID(implClassUid); implUid.set_uid(0, uid); dataset.addElement(implUid); MElement implVer = new MElement(tag_t.kImplementationVersionName, vr_t.SH); string version = versionName; implVer.set_string(0, version); dataset.addElement(implVer); // Note: in the current version,saving file will use a large size of memory,this is a limitation // of the managed class in KDT,in the later version,if the limitation is resolved,we will use // the MDecoder to save files,not the MEncoder.wirte_pt10_file if (MEncoder.write_pt10_file ( filePath, dataset, true, // item length is explicit false // do not check group 2 items )) { _logger.Info("DAPStoreService.SaveFile(): " + "save file:" + filePath + "OK."); return(true); } else { _logger.Info("DAPStoreService.SaveFile(): " + "save file:" + filePath + "failed."); return(false); } } catch (Exception e) { _logger.Info("DAPStoreService.SaveFile(): " + "error occurred while trying to write part-10 file " + e.Message); return(false); } }
public static bool ModifyAccNoAndPatientID(string filePath, string tempFilePath, string prefix, out string message) { Boolean ret = false; message = ""; //String strAccPrefix = @"-" + m_infoDAP.HospitalID.ToString(); String strAccPrefix = prefix; try { MElementList elmlist = new MElementList(); if (!MDecoder.read_pt10_file(filePath, ref elmlist, null, -1)) { _logger.Error("ImageSubmitService.ModifyAccNoAndPatientID(): " + "read dicom file " + filePath + " error."); message = "SendFTPError002"; return(ret); } //Modify AccessionNumber String accNoOrg = ""; String patIDOrg = ""; GetDICOMInfo(elmlist, ref accNoOrg, ref patIDOrg); #region process Num //PID,AccNo为空时拒绝接受该图像 if (patIDOrg.Length == 0 || accNoOrg.Length == 0) { IDEmptyProcess(patIDOrg); elmlist.Dispose(); message = "SendFTPError003"; return(false); } #endregion int PrefixLength = 32 - strAccPrefix.Length; if (accNoOrg.Length > PrefixLength) { accNoOrg = accNoOrg.Remove(PrefixLength); } patIDOrg = AddPrefix(strAccPrefix, elmlist, accNoOrg, patIDOrg, PrefixLength); //judge TransferSyntax SetTSN(elmlist); if (MEncoder.write_pt10_file ( tempFilePath, elmlist, true, // item length is explicit false // do not check group 2 items )) { elmlist.Dispose(); _logger.Debug("ImageSubmitService.ModifyAccNoAndPatientID(): " + "save dicom file " + tempFilePath + " ok."); return(true); } else { elmlist.Dispose(); _logger.Error("ImageSubmitService.ModifyAccNoAndPatientID(): " + "save dicom file " + tempFilePath + " error."); message = "SendFTPError002"; return(false); } } catch (Exception e) { _logger.Error("ImageSubmitService.ModifyAccNoAndPatientID(): " + "pop up an exception--" + e.Message); } return(ret); }