File: TestFileAnonymizer4.cxx

package info (click to toggle)
gdcm 3.0.24-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 27,560 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 (188 lines) | stat: -rw-r--r-- 5,201 bytes parent folder | download | duplicates (6)
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
/*=========================================================================

  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.

=========================================================================*/
#include "gdcmFileAnonymizer.h"

#include "gdcmTesting.h"
#include "gdcmSystem.h"
#include "gdcmReader.h"
#include "gdcmFilename.h"

#include <vector>

// in-place replace checking
static int TestFileAnonymize4(const char *filename, bool verbose = false)
{
  using namespace gdcm;
  if( verbose )
    std::cout << "Processing: " << filename << std::endl;

  // Create directory first:
  const char subdir[] = "TestFileAnonymize4";
  std::string tmpdir = Testing::GetTempDirectory( subdir );
  if( !System::FileIsDirectory( tmpdir.c_str() ) )
    {
    System::MakeDirectory( tmpdir.c_str() );
    //return 1;
    }
  std::string outfilename = Testing::GetTempFilename( filename, subdir );
  if( verbose )
    std::cout << "Generating: " << outfilename << std::endl;

  const gdcm::Tag t1(0x0018,0x5100);
  const gdcm::Tag t2(0x0018,0x1312);
  const gdcm::Tag t3(0x0018,0x1313);
  const gdcm::Tag t4(0x0018,0x1317);
  //const gdcm::Tag t5(0x0008,0x2112); // SQ
  //const gdcm::Tag t6(0x0008,0x9215); // SQ
  const gdcm::Tag t7(0x0018,0x1020);
  const gdcm::Tag t8(0x0004,0x1130); // Test DICOMDIR !
  const gdcm::Tag t9(0x0008,0x0000); // Earliest possible Data Element
  const gdcm::Tag t0(0xffff,0xffff); // Latest Possible Element

  std::vector<gdcm::Tag> tags;
  tags.push_back( t0 );
  tags.push_back( t1 );
  tags.push_back( t2 );
  tags.push_back( t3 );
  tags.push_back( t4 );
  tags.push_back( t7 );
  tags.push_back( t8 );
  tags.push_back( t9 );

  // precondition, remove the file if present:
  System::RemoveFile(outfilename.c_str());

    {
    gdcm::FileAnonymizer fa;
    fa.SetInputFileName( filename );
    fa.SetOutputFileName( outfilename.c_str() );
    for( std::vector<gdcm::Tag>::const_iterator it = tags.begin();
      it != tags.end(); ++it )
      {
      fa.Replace( *it, "TOTO" );
      }
    if( !fa.Write() )
      {
      std::cerr << "Failed to write (1): " << outfilename << std::endl;
      return 1;
      }
    }

    {
    // Read back and check:
    gdcm::Reader r;
    r.SetFileName( outfilename.c_str() );
    if( !r.Read() )
      {
      gdcm::Filename fn( filename );
      if( strcmp(fn.GetName(), "SIEMENS_MAGNETOM-12-MONO2-GDCM12-VRUN.dcm") == 0
        || strcmp(fn.GetName(), "DMCPACS_ExplicitImplicit_BogusIOP.dcm") == 0
        || strcmp(fn.GetName(), "ExplicitVRforPublicElementsImplicitVRforShadowElements.dcm") == 0 )
        {
        return 0;
        }
      std::cerr << "Failed to read: " << outfilename << std::endl;
      return 1;
      }
    }

    {
    gdcm::FileAnonymizer fa;
    fa.SetInputFileName( outfilename.c_str() );
    fa.SetOutputFileName( outfilename.c_str() );
    for( std::vector<gdcm::Tag>::const_iterator it = tags.begin();
      it != tags.end(); ++it )
      {
      fa.Replace( *it, "TATA" );
      }
    if( !fa.Write() )
      {
      gdcm::Filename fn( filename );
      if( strcmp(fn.GetName(), "JPEGInvalidSecondFrag.dcm") == 0 )
        {
        }
      else
        {
        std::cerr << "Failed to write (2): " << outfilename << std::endl;
        return 1;
        }
      }
    }

  // Read back and check:
  gdcm::Reader r;
  r.SetFileName( outfilename.c_str() );
  if( !r.Read() )
    {
    std::cerr << "Failed to read (2): " << outfilename << std::endl;
    return 1;
    }

  const File &f = r.GetFile();
  gdcm::MediaStorage ms;
  ms.SetFromFile( f );

  const DataSet &ds = f.GetDataSet();
  for( std::vector<gdcm::Tag>::const_iterator it = tags.begin();
    it != tags.end(); ++it )
    {
    const gdcm::Tag & t = *it;
    // Special handling of t8 (DICOMDIR only)
    const bool iserror = (ms == gdcm::MediaStorage::MediaStorageDirectoryStorage && t == t8) && false; // de-activate for now
    if( !ds.FindDataElement( t ) )
      {
      if( iserror )
        {
        std::cerr << "Not found element: " << t << " in " << outfilename << std::endl;
        return 1;
        }
      }
    const gdcm::DataElement & de = ds.GetDataElement( t );
    if( de.GetVL() != 4 )
      {
      if( iserror )
        {
        std::cerr << "Wrong VL for: " << t << " in " << outfilename << std::endl;
        return 1;
        }
      }
    }


  return 0;
}

int TestFileAnonymizer4(int argc, char *argv[])
{
  if( argc == 2 )
    {
    const char *filename = argv[1];
    return TestFileAnonymize4(filename, true);
    }

  // else
  gdcm::Trace::DebugOff();
  gdcm::Trace::WarningOff();
  gdcm::Trace::ErrorOff();
  int r = 0, i = 0;
  const char *filename;
  const char * const *filenames = gdcm::Testing::GetFileNames();
  while( (filename = filenames[i]) )
    {
    r += TestFileAnonymize4( filename );
    ++i;
    }

  return r;
}