public virtual DCMAbstractType Decode(byte[] data, ref uint idx) { MemoryStream ms = new MemoryStream();//创建流 ms.Write(data, 0, data.Length); ms.Position = idx; BinaryReader reader = new BinaryReader(ms); DCMDataElement element = new DCMDataElement(); element.gtag = reader.ReadUInt16(); //组号 element.etag = reader.ReadUInt16(); //元素号 LookupDictionary(element); element.length = reader.ReadUInt32(); //值长度 if (element.length == 0xffffffff) //undefined length { element.value = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position)); } else { element.value = reader.ReadBytes((int)element.length);//获取值的内容 } idx = (uint)ms.Position; reader.Close(); //关闭内存流 ms.Close(); //关闭二进制读写 return(element); }
public override List <DCMAbstractType> Decode(byte[] data, ref uint idx) { //打开filename文件,将文件内容读到缓冲区byte[] data using (FileStream fs = File.OpenRead(filename)) { data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); } //跳过128字节前导符(idx=128),读取4字节的”DICM” idx = 128; //*** idx += 4; //用ExplicitVRLittleEndian对象实例化filemeta对象,通过其Decode方法从data中读取头元素 syntax = new ExplicitVRLittleEndian(); filemeta = new DCMFileMeta(syntax); filemeta.Decode(data, ref idx); //读取(0002,0010)头元素即filemeta[DicomTags.TransferSyntaxUid]的值, //在tsFactory中找到对应的数据集传输语法对象赋给基类的syntax字段 DCMDataElement synelem = (DCMDataElement)filemeta[DicomTags.TransferSyntaxUid]; syntax = tsFactory[synelem.ReadValue <String>()[0].Replace("\0", "")]; //调用base.Decode方法解码数据集 base.Decode(data, ref idx); return(items); }
public virtual DCMAbstractType Decode(byte[] data, ref uint idx) { DCMDataElement element = new DCMDataElement(); //读取TAG element.gtag = vrdecoder.GetGroupTag(data, ref idx); element.etag = vrdecoder.GetElementTag(data, ref idx); //处理SQ if (element.gtag == 0xfffe && element.etag == 0xe000) //处理SQ的三个特殊标记 { DCMDataItem sqitem = new DCMDataItem(this); uint length = vrdecoder.GetUInt(data, ref idx); //不能用GetLength uint offset = idx; while (idx - offset < length) { DCMAbstractType sqelem = Decode(data, ref idx); //递归 if (length == 0xffffffff && sqelem.gtag == 0xfffe && sqelem.etag == 0xe00d) //条目结束标记 { break; } sqitem.items.Add(sqelem); } return(sqitem); } else if (element.gtag == 0xfffe && element.etag == 0xe0dd) //序列结束标记 { element.vr = "UL"; element.length = vrdecoder.GetUInt(data, ref idx); //不能用GetLength return(element); } //查数据字典得到VR,Name,VM element.vr = vrdecoder.GetVR(data, ref idx); DicomDictonaryEntry entry = dicomDictionary.GetEntry(element.gtag, element.etag); if (entry != null) { if (element.vr == "") { element.vr = entry.Vr; } element.name = entry.Keyword; element.vm = entry.Vm; } else if (element.vr == "" && element.etag == 0) { element.vr = "US"; } //根据得到的值表示法构造VR子类对象 element.vrparser = vrfactory.GetVR(element.vr); //读取值长度 element.length = element.vrparser.GetLength(data, ref idx); //读取值 element.value = element.vrparser.GetValue(data, ref idx, element.length); element.idx = idx; return(element); }
protected void LookupDictionary(DCMDataElement element) { //查数据字典得到VR,Name,VM DicomDictionaryEntry entry = dictionary.GetEntry(element.gtag, element.etag); if (entry != null) { if (element.vr == "" || element.vr == null) { element.vr = entry.VR; } element.name = entry.Name; element.vm = entry.VM; } else if (element.vr == "" && element.etag == 0) { element.vr = "UL"; } //得到VR对象实例 element.vrparser = vrfactory.GetVR(element.vr); }
public override DCMAbstractType Decode(byte[] data, ref uint idx) { MemoryStream ms = new MemoryStream();//创建流 ms.Write(data, 0, data.Length); ms.Position = idx; BinaryReaderBE reader = new BinaryReaderBE(ms); DCMDataElement element = new DCMDataElement(); element.gtag = reader.ReadUInt16(); //组号 element.etag = reader.ReadUInt16(); //元素号 LookupDictionary(element); element.vr = Encoding.Default.GetString(reader.ReadBytes(2)); //判断OB, OW, OF, SQ, UT, UN if (element.vr == "OB" || element.vr == "OW" || element.vr == "OF" || element.vr == "SQ" || element.vr == "UT" || element.vr == "UN") { ms.Position += 2; //跳过两字节0000H element.length = reader.ReadUInt32(); //值长度 } else { element.length = reader.ReadUInt16();//值长度 } //element.value = reader.ReadBytes((int)element.length);//值的内容 if (element.length == 0xffffffff) //undefined length { element.value = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position)); } else { element.value = reader.ReadBytes((int)element.length); } idx = (uint)ms.Position; reader.Close(); //关闭内存流 ms.Close(); //关闭二进制读写 return(element); }
public virtual DCMAbstractType Decode(byte[] data, ref uint idx) { DCMDataElement element = new DCMDataElement(); //读取TAG element.gtag = vrdecoder.GetGroupTag(data, ref idx); element.etag = vrdecoder.GetElementTag(data, ref idx); //这一项仅仅只用于测试容易看出,Reference==0; string tag = "(" + element.gtag.ToString("X4") + "," + element.etag.ToString("X4") + ")"; if (element.gtag == 0xfffe && element.etag == 0xe000) { DCMDataItem sqitem = new DCMDataItem(this); sqitem.gtag = element.gtag; sqitem.etag = element.etag; uint length = vrdecoder.GetUInt32(data, ref idx); sqitem.length = length; uint offset = idx; //idx小于value长度时,不断地调用Decode方法来解锁; while (idx - offset < length) { //递归,不断地解码 //解出Tag不为DataElemt类型时,重复的递归 DCMAbstractType sqelem = Decode(data, ref idx); if (sqelem.gtag == 0xfffe && sqelem.etag == 0xe00d) { break; } sqitem[0] = sqelem; } return(sqitem); } else if (element.gtag == 0xfffe && element.etag == 0xe00d) //序列结束标记 { element.vr = "UL"; //回归初始状态 element.length = vrdecoder.GetUInt32(data, ref idx); //不能用GetLength //且不用解出Value return(element); } else if (element.gtag == 0xfffe && element.etag == 0xe0dd) { element.vr = "UL";//回归初始状态 element.length = vrdecoder.GetUInt32(data, ref idx); //不能用GetLength //不用解出Value类了; return(element); } //查数据字典得到VR,Name,VM element.vr = vrdecoder.GetVR(data, ref idx); //0002系类文件头并没有在DicomDirationay内有定义 DicomDictonaryEntry entry = dictionary.GetEntry(element.gtag, element.etag); if (entry != null) { element.vr = entry.VR; element.name = entry.Keyword; element.vm = entry.VM; } else if (element.vr == "" && element.etag == 0) { element.vr = "US"; } if (element.vr == "OB or OW") { //在上一层找items[DicomTags.BitsAllocated];但是上层对下层是完全不透明的;怎么办 element.vr = "OW"; } try { vrdecoder = vrfactory.GetVR(element.vr); element.vrparser = vrdecoder; //读取值长度 element.length = vrdecoder.GetLength(data, ref idx, element.vr); //读取值 element.value = vrdecoder.GetValue(data, ref idx, element.length); return(element); } catch { throw; } }