File: TestDCMTKMD5.py

package info (click to toggle)
gdcm 3.0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 26,880 kB
  • sloc: cpp: 203,477; ansic: 78,582; xml: 48,129; python: 3,459; cs: 2,308; java: 1,629; lex: 1,290; sh: 334; php: 128; makefile: 117
file content (190 lines) | stat: -rw-r--r-- 6,482 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
############################################################################
#
#  Program: GDCM (Grassroots DICOM). A DICOM library
#
#  Copyright (c) 2006-2011 Mathieu Malaterre
#  All rights reserved.
#  See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
#
#     This software is distributed WITHOUT ANY WARRANTY; without even
#     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#     PURPOSE.  See the above copyright notice for more information.
#
############################################################################

import gdcm
import os,sys,re

"""
You need to have dcmdump/dcmdrle/dcmdjpeg in your PATH
"""

def TestDCMTKMD5( filename, verbose = False ):
  blacklist = [
  # Get rid of DICOMDIR if any:
  'DICOMDIR',
  'DICOMDIR_MR_B_VA12A',
  'DICOMDIR-Philips-EasyVision-4200-Entries',
  'dicomdir_Acusson_WithPrivate_WithSR',
  'dicomdir_Pms_With_heavy_embedded_sequence',
  'dicomdir_Pms_WithVisit_WithPrivate_WithStudyComponents',
  'dicomdir_With_embedded_icons',
  # Unsupported file:
  'MR_Spectroscopy_SIEMENS_OF.dcm',
  'gdcm-CR-DCMTK-16-NonSamplePerPix.dcm', # this is not an image
  'ELSCINT1_PMSCT_RLE1.dcm',
  'SignedShortLosslessBug.dcm',
  'JPEGDefinedLengthSequenceOfFragments.dcm', # dcmtk 3.6.0 gives garbage
  'GE_DLX-8-MONO2-PrivateSyntax.dcm',
  'PrivateGEImplicitVRBigEndianTransferSyntax16Bits.dcm',
  #'DermaColorLossLess.dcm', # technically I could support this one...
  #'LEADTOOLS_FLOWERS-24-RGB-JpegLossy.dcm', # idem
  'ALOKA_SSD-8-MONO2-RLE-SQ.dcm'] # this one is not supported by dcmtk 3.5.4
  for f in blacklist:
    if f in filename:
      print("%s is on the black list, giving up"%filename)
      return 0
  #print filename
  #
  #dcmdump_exec = "dcmdump -dc -E +P 2,10 -s " + filename + " 2> /dev/null"
  # I had to remove the -dc for the following file:
  # GE_GENESIS-16-MONO2-Uncompressed-UnusualVR.dcm there is trailing space instead of \0
  dcmdump_exec = "dcmdump -E +P 2,10 -s " + filename + " 2> /dev/null"
  #print dcmdump_exec
  f = os.popen(dcmdump_exec)
  ret = f.read()
  #assert ret == 0
  #print ret
  jpegre = re.compile('^.*JPEGLossless.*$')
  jpegre2 = re.compile('^.*JPEGExtended.*$')
  jpegre3 = re.compile('^.*JPEGBaseline.*$')
  j2kre = re.compile('^.*JPEG2000.*$')
  jplsre = re.compile('^.*JPEGLS.*$')
  rlere = re.compile('^.*RLELossless.*$')
  lexre = re.compile('^.*LittleEndianExplicit.*$')
  leire = re.compile('^.*LittleEndianImplicit.*$')
  beire = re.compile('^.*BigEndianExplicit.*$')
  testing = gdcm.Testing()
  outputdir = testing.GetTempDirectory( "TestDCMTKMD5" )
  gdcm.System.MakeDirectory( outputdir )
  outputfilename = testing.GetTempFilename( filename, "TestDCMTKMD5" )
  executable_output_path = gdcm.GDCM_EXECUTABLE_OUTPUT_PATH
  gdcmraw = executable_output_path + '/gdcmraw -P'

  if not ret:
    #print "empty, problem with:", filename
    return 0
  elif type(ret) != type(''):
    print("problem of type with:", filename)
    return 0
  #print ret
  #print ret.__class__
  elif( jpegre.match( ret ) or jpegre2.match(ret) or jpegre3.match(ret) ):
    #print "jpeg: ",filename
    # +cn : conv-never
    # +px : color by pixel
    dcmdjpeg_exec = "dcmdjpeg +cn +px " + filename + " " + outputfilename
    ret = os.system( dcmdjpeg_exec )
    if ret:
      print("dcmdjpeg failed to decompress file. giving up")
      return 0

    gdcmraw_args = ' -i ' + outputfilename + ' -o ' + outputfilename + ".raw"
    gdcmraw += gdcmraw_args
    #print gdcmraw
    ret = os.system( gdcmraw )
    md5 = gdcm.Testing.ComputeFileMD5( outputfilename + ".raw" )
    ref = gdcm.Testing.GetMD5FromFile(filename)
    #print md5
    retval  = 0
    if ref != md5:
      print("md5 are different: %s should be: %s for file %s"%(md5,ref,filename))
      retval = 1
    #print outputfilename
    return retval
  elif( jplsre.match( ret ) ):
    #print "jpegls: ",filename
    dcmdjpls_exec = "dcmdjpls " + filename + " " + outputfilename
    ret = os.system( dcmdjpls_exec )
    if ret:
      print("failed with: ", dcmdjpls_exec)
      return 1

    gdcmraw_args = ' -i ' + outputfilename + ' -o ' + outputfilename + ".raw"
    gdcmraw += gdcmraw_args
    #print gdcmraw
    ret = os.system( gdcmraw )
    md5 = gdcm.Testing.ComputeFileMD5( outputfilename + ".raw" )
    ref = gdcm.Testing.GetMD5FromFile(filename)
    #print md5
    retval  = 0
    if ref != md5:
      print("md5 are different: %s should be: %s for file %s"%(md5,ref,filename))
      retval = 1
    #print outputfilename
    return retval
  elif( rlere.match( ret ) ):
    #print "rle: ",filename
    dcmdrle_exec = "dcmdrle " + filename + " " + outputfilename
    ret = os.system( dcmdrle_exec )
    if ret:
      print("failed with: ", dcmdrle_exec)
      return 1

    gdcmraw_args = ' -i ' + outputfilename + ' -o ' + outputfilename + ".raw"
    gdcmraw += gdcmraw_args
    #print gdcmraw
    ret = os.system( gdcmraw )
    md5 = gdcm.Testing.ComputeFileMD5( outputfilename + ".raw" )
    ref = gdcm.Testing.GetMD5FromFile(filename)
    #print md5
    retval  = 0
    if ref != md5:
      print("md5 are different: %s should be: %s for file %s"%(md5,ref,filename))
      retval = 1
    #print outputfilename
    return retval
  elif( j2kre.match( ret ) ):
    return 0
  elif( lexre.match( ret ) or leire.match(ret) or beire.match(ret) ):
    #print "rle: ",filename
    #dcmdrle_exec = "dcmdrle " + filename + " " + outputfilename
    #ret = os.system( dcmdrle_exec )

    gdcmraw_args = ' -i ' + filename + ' -o ' + outputfilename + ".raw"
    gdcmraw += gdcmraw_args
    #print gdcmraw
    ret = os.system( gdcmraw )
    if ret:
      print("failed with: ", gdcmraw)
      return 1
    md5 = gdcm.Testing.ComputeFileMD5( outputfilename + ".raw" )
    ref = gdcm.Testing.GetMD5FromFile(filename)
    #print md5
    retval  = 0
    if ref != md5:
      print("md5 are different: %s should be: %s for file %s"%(md5,ref,filename))
      retval = 1
    #print outputfilename
    return retval
  #else
  print("Unhandled:",filename,"with ret=",ret)
  return 1

if __name__ == "__main__":
  success = 0
  try:
    filename = os.sys.argv[1]
    success += TestDCMTKMD5( filename, True )
  except:
    # loop over all files:
    t = gdcm.Testing()
    gdcm.Trace.WarningOff()
    gdcm.Trace.DebugOff()
    nfiles = t.GetNumberOfFileNames()
    for i in range(0,nfiles):
      filename = t.GetFileName(i)
      success += TestDCMTKMD5( filename )

  # Test succeed ?
  sys.exit(success)