继续手搓DICOM文件解析 (2)
书接上回,我们使用了简单的函数进行了DICOM文件的解析,那么我们就继续吧。
// to be continued (part 2)
std::cout<<std::string(25,'%')<<" Part 2 continued "<<std::string(25,'%')<<std::endl;
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));```
以上,就是重复使用读取TAG函数,读取VR函数,然后使用getShort函数来读取数据的长度。还好很多数据都是字符串形式,可以使用readString函数来读取,下面是读取的结果。
```cpp
%%%%%%%%%%%%%%%%%%%%%%%%% Part 2 continued %%%%%%%%%%%%%%%%%%%%%%%%%
[0008,0005] : CS 10 # ISO_IR 100
[0008,0008] : CS 24 # DERIVED\SECONDARY\AXIAL
[0008,0012] : DA 8 # 20180828
[0008,0013] : TM 14 # 145109.969000
[0008,0016] : UI 26 # 1.2.840.10008.5.1.4.1.1.2
[0008,0018] : UI 54 # 1.2.276.0.20.1.4.5.964244510648.1152.1535439069.968750
[0008,0020] : DA 8 # 20160113
[0008,0023] : DA 8 # 20180828
[0008,0030] : TM 0 #
[0008,0033] : TM 14 # 145109.969000
[0008,0050] : SH 0 #
[0008,0060] : CS 2 # CT
[0008,0070] : LO 8 # BrainLAB
[0008,ff90] : PN 0 #
[0008,1010] : SH 14 # RTWORKSTATION2
[0008,1030] : LO 20 # RT^1_RT_Head (Adult)
[0009,0010] : LO 20 # BrainLAB_Conversion
[0009,1001] : LO 24 # ARIA/Varis/OBI-Platform
[0010,0010] : PN 14 # Zheng^Mingtang
[0010,0020] : LO 10 # 0000246783
[0010,0030] : DA 8 # 19410113
[0010,0040] : CS 2 # F
[0018,0050] : DS 16 # 2.9925389620816
[0018,0060] : DS 0 #
[0018,1020] : LO 6 # 4.1.4
[0018,5100] : CS 4 # HFS
[0020,000d] : UI 54 # 1.2.276.0.20.1.2.5.964244510648.3588.1451978243.765625
[0020,000e] : UI 54 # 1.2.276.0.20.1.3.5.964244510648.1152.1535439069.765625
[0020,0010] : SH 2 # 1
[0020,0011] : IS 2 # 2
[0020,0012] : IS 0 #
[0020,0013] : IS 2 # 50
[0020,0032] : DS 50 # -259.15500957601\-184.74776528391\87.274973824935
[0020,0037] : DS 16 # 1\-0\-0\-0\1\-0
[0020,0052] : UI 56 # 1.2.276.0.20.1.30.5.964244510648.1152.1535439069.781250
[0020,1040] : LO 0 #
接下来我们会发现VR的数值变成US类型,此时就需要使用getShort函数来读取数值了,因为该数值是unsigned short 类型。需要注意到是小端字节序,读取之后自己进行拼接。代码如下:
std::cout<<"-------------[0028,0002]------------ "<<std::endl;
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
unsigned short usvar=getShort(file);
std::cout<<usvar<<std::endl;
//std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
usvar=getShort(file);
std::cout<<usvar<<std::endl;
//std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
usvar=getShort(file);
std::cout<<usvar<<std::endl;
//std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
std::cout<<"-----------[0028,0100]----------"<<std::endl;
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
usvar=getShort(file);
std::cout<<usvar<<std::endl;
//std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
usvar=getShort(file);
std::cout<<usvar<<std::endl;
//std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
usvar=getShort(file);
std::cout<<usvar<<std::endl;
//std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
usvar=getShort(file);
std::cout<<usvar<<std::endl;
//std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
length=getShort(file);
std::cout<<length<<" # ";
readString(file,str,length);
std::cout<<std::string(str)<<std::endl;
std::memset(str,0,sizeof(str));
readTag(file);
read2Byte(file);
long len=getLong(file);
std::cout<<len<<" # ";
然后输出的结果如下:
-------------[0028,0002]------------
[0028,0002] : US 2 # 1
[0028,0004] : CS 12 # MONOCHROME2
[0028,0010] : US 2 # 512
[0028,0011] : US 2 # 512
[0028,0030] : DS 20 # 0.9765625\0.9765625
-----------[0028,0100]----------
[0028,0100] : US 2 # 16
[0028,0101] : US 2 # 16
[0028,0102] : US 2 # 15
[0028,0103] : US 2 # 0
[0028,1050] : DS 2 # 35
[0028,1051] : DS 2 # 80
[0028,1052] : DS 6 # -1024
[0028,1053] : DS 2 # 1
[ffe0,0010] : OW 8 #
是不是差不多了,我们可以看到数据是的行和列都是512,数据是16位数据,但是大家看到没有,最后的图像数据,TAG是【ffe0,0010】,实际上,使用DCMTK解析应该是7fe0,0010我猜测,可能是越界了,而且我也没有解析出数据的长度。OW是对的,到nema主页去查询,这个与传输与法有关联。我还在摸索中,等待下一期的结果吧,谢谢大家!如果有哪位专业人士指导原因,也可以评论区或者私信指点一下,非常感谢!