File: TestSCUFunctions.cxx

package info (click to toggle)
gdcm 3.0.24-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,856 kB
  • sloc: cpp: 203,722; ansic: 76,471; xml: 48,131; python: 3,473; cs: 2,308; java: 1,629; lex: 1,290; sh: 334; php: 128; makefile: 97
file content (312 lines) | stat: -rw-r--r-- 10,236 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

#include "gdcmCompositeNetworkFunctions.h"
#include "gdcmBaseRootQuery.h"

//#include "gdcmDirectory.h"
#include "gdcmScanner.h"
#include "gdcmTesting.h"
#include "gdcmReader.h"
#include "gdcmWriter.h"
#include "gdcmAttribute.h"
#include "gdcmGlobal.h"

#include <iostream>
#include <fstream>

//this should maybe override == ?
bool AreDataSetsEqual(const gdcm::DataSet& ds1, const gdcm::DataSet& ds2){
  gdcm::DataSet::ConstIterator it1 = ds1.Begin();
  gdcm::DataSet::ConstIterator it2 = ds2.Begin();

  const gdcm::DataElement &de1 = *it1;
  const gdcm::DataElement &de2 = *it2;
  if( de1 == de2 )
    {
    }
  while( it1 != ds1.End() && it2 != ds2.End() && *it1 == *it2 )
    {
    ++it1;
    ++it2;
    }

  if( it1 != ds1.End() || it2 != ds2.End() )
    {
    std::cerr << "Problem with:" << std::endl;
    if( it1 != ds1.End() )
      {
      std::cerr << "ds1: " << *it1 << std::endl;
      }
    if( it2 != ds2.End() )
      {
      std::cerr << "ds2: " << *it2 << std::endl;
      }
    return false;
    }

  return true;
}

bool checkbl( const char *filename )
{
  static const char *blacklist[] = {
    "PHILIPS_GDCM12xBug2.dcm", // #3196213
    // W: DIMSE Warning: (GDCMSCU,ANY-SCP): DIMSE receiveDataSetInMemory:
    // dset->read() Failed (Corrupted data)
    "MR_Philips_Intera_No_PrivateSequenceImplicitVR.dcm",
    "MR_Philips_Intera_PrivateSequenceImplicitVR.dcm",
    "GE_DLX-8-MONO2-PrivateSyntax.dcm", // Implicit VR Big Endian DLX (G.E Private)
    "PrivateGEImplicitVRBigEndianTransferSyntax16Bits.dcm", // Implicit VR Big Endian DLX (G.E Private)
    // W: DIMSE Warning: (STORESCU,ANY-SCP): sendMessage: unable to convert
    // dataset from 'JPEG Lossless, Non-hierarchical, Process 14' transfer
    // syntax to 'Big Endian Explicit'
    "SignedShortLosslessBug.dcm",
    "MR-MONO2-12-shoulder.dcm",
    // difficult for now:
    "SIEMENS_SOMATOM-12-ACR_NEMA-ZeroLengthUs.acr",
    "MR-MONO2-12-an2.acr",
    "ExplicitVRforPublicElementsImplicitVRforShadowElements.dcm",
    nullptr
  };
  for( const char **bl = blacklist; *bl; ++bl )
    {
    const char *res = strstr( filename, *bl );
    if( res )
      {
      return true;
      }
    }
  return false;
}

int TestSCUFunctions(int argc, char *argv[])
{
  if( argc < 6 )
    {
    std::cerr << argv[0] << " aetitle call portno moveReturnPort remote" << std::endl;
    return 1;
    }
  gdcm::Trace::DebugOff();
  gdcm::Trace::WarningOff();
  std::string aetitle = argv[1]; // the ae title of this computer
  std::string call = argv[2]; // the ae title of the server
  uint16_t portno = (uint16_t)atoi(argv[3]); // the port of the server
  uint16_t moveReturnPort = (uint16_t)atoi(argv[4]); // the port over which return cstore scps are done for cmove
  std::string remote = argv[5]; //the ip address of the remote server
  std::string tmpdir = gdcm::Testing::GetTempDirectory( "TestSCUFunctions" );
  std::string outputDir = tmpdir; //place to where data is returned by cmove
  //std::string inputDir = gdcm::Testing::GetDataRoot(); //input collection of data to transfer

  bool didItWork = gdcm::CompositeNetworkFunctions::CEcho( remote.c_str(), portno,
    aetitle.c_str(), call.c_str() );

  if (!didItWork)
    {
    std::cerr << "Echo failed." << std::endl;
    return 1;
    }

  std::vector<std::string> theFilenames;
  const char *filename;
  int i = 0;
  const char * const *filenames = gdcm::Testing::GetFileNames();
  while( (filename = filenames[i]) )
    {
    theFilenames.emplace_back(filename );
    ++i;
    }

  // Fow now lets eliminate invalid candidates:
  // - no SOP Class UID
  // - no SOP Instance UID
  gdcm::Scanner sc;
  gdcm::Tag tts(0x0002,0x0010);
  gdcm::Tag sopclass(0x8,0x16);
  gdcm::Tag sopinstance(0x8,0x18);
  sc.AddTag( tts );
  sc.AddTag( sopclass );
  sc.AddTag( sopinstance );
  if( !sc.Scan( theFilenames ) )
    {
    return 1;
    }

  std::vector< gdcm::UIComp > validuids;
  validuids.emplace_back("1.2.840.10008.1.2" );
  validuids.emplace_back("1.2.840.10008.1.2.1" );
  //validuids.push_back( "1.2.840.10008.1.2.2" );
  // remove any file without SOP Instance UID
  for(
    gdcm::Directory::FilenamesType::iterator it = theFilenames.begin();
    it != theFilenames.end(); )
    {
    const char *file = it->c_str();
    const char* v1 = sc.GetValue(file, sopclass );
    const char* v2 = sc.GetValue(file, sopinstance );
    const char* v3 = sc.GetValue(file, tts );
    gdcm::UIComp v3uid;
    if ( v3 ) v3uid = v3;
    std::vector< gdcm::UIComp >::const_iterator it2 = std::find( validuids.begin(),
      validuids.end(), v3uid );
    if( !v1 || !v2 || !*v1 || !*v2 ) it = theFilenames.erase( it );
    else if( checkbl( file ) ) it = theFilenames.erase( it );
    else if( v3 && it2 == validuids.end() )
      {
      //std::cerr << "erase: " << *it << " [" << v3uid << "]" << std::endl;
      it = theFilenames.erase( it );
      }
    else ++it;
    }

  //store the datasets remotely
  didItWork = gdcm::CompositeNetworkFunctions::CStore(remote.c_str(), portno, theFilenames,
    aetitle.c_str(), call.c_str());

  if (!didItWork)
    {
    std::cerr << "Store failed." << std::endl;
    return 1;
    }

  std::vector<std::string>::iterator fitor;
  for (fitor = theFilenames.begin(); fitor < theFilenames.end(); ++fitor)
    {
    //read in the file
    gdcm::Reader theReader;
    theReader.SetFileName(fitor->c_str());
    if (!theReader.Read())
      {
      std::cerr << "Test failed, dicom file " << *fitor << " failed to load." <<std::endl;
      return 1;
      }
    gdcm::File theFile = theReader.GetFile();
    gdcm::DataSet ds = theFile.GetDataSet();

    //have to construct a query from the dataset.
    //grab tag 10,10, use that to get 10,20
    //set the query just to be the first character and * after that.
    std::vector<std::pair<gdcm::Tag, std::string> > keys;
    gdcm::Tag theTag(0x0010, 0x0010);
    gdcm::Tag theIDTag(0x0010, 0x0020);
    if (ds.FindDataElement(theTag))
      {
      gdcm::DataElement de = ds.GetDataElement(theTag);
      const gdcm::ByteValue* bv = de.GetByteValue();
      int theBufferLen = bv->GetLength();
      if (theBufferLen < 2) continue;
      char* theBuf = new char[theBufferLen];
      bv->GetBuffer(theBuf, theBufferLen);
      gdcm::UIComp theSearchStringRaw(theBuf, theBufferLen/2);
      delete [] theBuf;
      // HACK:
      std::string theSearchString = theSearchStringRaw.Trim();
      std::replace( theSearchString.begin(), theSearchString.end(), ' ', '?');
      theSearchString += "*";
      std::cerr << "search for: [" << theSearchString << "]" << std::endl;
      if (theSearchString.size() %2 == 1)
        {
        theSearchString += " "; //to make sure everything is double spaced
        }
      keys.emplace_back(theTag, theSearchString);
      }
    else
      {
      continue;
      }
    std::string theEmptyString;
    keys.emplace_back(theIDTag, theEmptyString);

    gdcm::BaseRootQuery *theQuery =
      gdcm::CompositeNetworkFunctions::ConstructQuery(gdcm::ePatientRootType, gdcm::ePatient, keys);

    std::vector<gdcm::DataSet> theDataSets;
    bool b = gdcm::CompositeNetworkFunctions::CFind(remote.c_str(), portno, theQuery, theDataSets, aetitle.c_str(), call.c_str());

    //std::cout << theQuery->GetQueryDataSet() << std::endl;
    delete theQuery;
    if( !b )
      {
      std::cerr << "Problem in CFind" << std::endl;
      return 1;
      }

    if (theDataSets.empty())
      {
      std::cerr << "Unable to find the dataset that was just sent to the server, " << *fitor << std::endl;
      return 1;
      }

    keys.clear();
    //if it's not empty, then pull it.
    std::vector<gdcm::DataSet>::iterator itor;
    for (itor = theDataSets.begin(); itor != theDataSets.end(); itor++)
      {
      if (itor->FindDataElement(theIDTag))
        {
        gdcm::DataElement de = itor->GetDataElement(theIDTag);
        const gdcm::ByteValue *bv = de.GetByteValue();
        int theBufferLen = bv->GetLength();
        char* theBuf = new char[theBufferLen];
        bv->GetBuffer(theBuf, theBufferLen);
        std::string theSearchString(theBuf, theBuf + theBufferLen);
        delete [] theBuf;
        keys.emplace_back(theIDTag, theSearchString);

        gdcm::DataElement de2 = ds.GetDataElement(theIDTag);
        de2.SetVR( gdcm::VR::INVALID );
        de.SetVR(  gdcm::VR::INVALID );
        if (!(de == de2))
          {
          std::cerr << "Sent dataset does not match returned dataset ID. " << std::endl;
          std::cerr << de << std::endl;
          std::cerr << " vs " << std::endl;
          std::cerr << de2 << std::endl;
          std::cerr << "File: " << *fitor << std::endl;
          return 1;
          }
        break;
        }
      else
        {
        continue;
        }
      }

    if (keys.empty())
      {
      std::cerr << "Sent dataset " << *fitor << " was not found by resulting CFind query. " << std::endl;
      return 1;
      }

    theQuery = gdcm::CompositeNetworkFunctions::ConstructQuery(gdcm::ePatientRootType, gdcm::ePatient, keys, gdcm::eMove );
    didItWork = gdcm::CompositeNetworkFunctions::CMove(remote.c_str(), portno, theQuery,
      moveReturnPort, aetitle.c_str(), call.c_str(), outputDir.c_str() );
    if ( !didItWork )
      {
      std::cerr << "CMove failed for file " << *fitor << std::endl;
      return 1;
      }
    delete theQuery;
    

    std::cout << "File " << *fitor << " moved back to server." << std::endl;
    }

  return 0;
}