File: TestCleaner2.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 (174 lines) | stat: -rw-r--r-- 5,363 bytes parent folder | download | duplicates (3)
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
/*=========================================================================

  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 "gdcmCleaner.h"
#include "gdcmFilename.h"
#include "gdcmReader.h"
#include "gdcmSmartPointer.h"
#include "gdcmSystem.h"
#include "gdcmTesting.h"
#include "gdcmWriter.h"

namespace gdcm {
static const DataSet &GetNestedDataSet(const DataSet &ds,
                                       const PrivateTag &path) {
  const DataElement &de = ds.GetDataElement(path);
  SmartPointer<SequenceOfItems> sqi = de.GetValueAsSQ();
  assert(sqi && sqi->GetNumberOfItems() == 1);
  const Item &item = sqi->GetItem(1);
  const DataSet &subds = item.GetNestedDataSet();
  return subds;
}
static bool IsTagIn(const DataSet &ds, const PrivateTag &path,
                    const PrivateTag &pt) {
  const DataSet &subds = GetNestedDataSet(ds, path);
  return subds.FindDataElement(pt);
}
static bool IsTagEmpty(const DataSet &ds, const PrivateTag &path,
                       const PrivateTag &pt) {
  const DataSet &subds = GetNestedDataSet(ds, path);
  if (!subds.FindDataElement(pt)) return false;
  const DataElement &de = subds.GetDataElement(pt);
  return de.IsEmpty();
}
}  // namespace gdcm

int TestCleaner2Impl(std::string const &filename,
                     std::string const &outfilename, bool useDPath) {
  using namespace gdcm;

  // Setup DPath
  DPath dpath1, dpath2;
  PrivateTag pt1, pt2;
  // This attribute is a SQ. make sure we can Empty a SQ:
  static const char ref1[] = "0029,e0,SPI-P-Private_ICS Release 1;4";
  static const char ref2[] = "0029,00,SPI-P-Private_ICS Release 1;1";
  if (useDPath) {
    if (!dpath1.ConstructFromString(ref1)) {
      return 1;
    }
    if (!dpath2.ConstructFromString(ref2)) {
      return 1;
    }
  }
  if (!pt1.ReadFromCommaSeparatedString(ref1)) {
    return 1;
  }
  if (!pt2.ReadFromCommaSeparatedString(ref2)) {
    return 1;
  }
  PrivateTag path1(0x0029, 0xf, "SPI-P-Private_ICS Release 1");
  PrivateTag path2(0x0029, 0xcd, "SPI-P-Private_ICS Release 1;1");
  PrivateTag path3(0x0029, 0x1b, "SPI-P-Private_ICS Release 1");
  PrivateTag path4(0x0029, 0x4c, "SPI-P-Private_ICS Release 1");
  // first pass
  {
    Reader reader;
    reader.SetFileName(filename.c_str());
    if (!reader.Read()) {
      std::cerr << "Could not read: " << filename << std::endl;
      return 1;
    }

    File &file = reader.GetFile();
    DataSet &ds = file.GetDataSet();

    const bool b0 = IsTagIn(ds, path4, pt1);

    const bool b1 = IsTagIn(ds, path1, pt2);
    const DataSet &ds1 = GetNestedDataSet(ds, path1);
    const bool b2 = IsTagIn(ds1, path2, pt2);
    const bool b3 = IsTagIn(ds, path3, pt2);
    const bool b4 = IsTagIn(ds, path4, pt2);
    if (!b0 || !b1 || !b2 || !b3 || !b4) {
      std::cerr << "Test not setup correctly" << std::endl;
      return 1;
    }

    Cleaner cleaner;
    cleaner.SetFile(file);
    cleaner.RemoveAllGroupLength(false);
    if (useDPath) {
      if (!cleaner.Empty(dpath1)) {
        return 1;
      }
      if (!cleaner.Empty(dpath2)) {
        return 1;
      }
    } else {
      if (!cleaner.Empty(pt1)) {
        return 1;
      }
      if (!cleaner.Empty(pt2)) {
        return 1;
      }
    }
    if (!cleaner.Clean()) {
      return 1;
    }

    Writer writer;
    writer.SetFileName(outfilename.c_str());
    writer.SetFile(cleaner.GetFile());
    if (!writer.Write()) {
      std::cerr << "Failed to write: " << outfilename << std::endl;
      return 1;
    }
  }

  // re-read, written out:
  {
    Reader reader;
    reader.SetFileName(outfilename.c_str());
    if (!reader.Read()) {
      std::cerr << "Could not read: " << outfilename << std::endl;
      return 1;
    }

    File &file = reader.GetFile();
    DataSet &ds = file.GetDataSet();

    const bool b0 = IsTagEmpty(ds, path4, pt1);
    const bool b1 = IsTagEmpty(ds, path1, pt2);
    const DataSet &ds1 = GetNestedDataSet(ds, path1);
    const bool b2 = IsTagEmpty(ds1, path2, pt2);
    const bool b3 = IsTagEmpty(ds, path3, pt2);
    const bool b4 = IsTagEmpty(ds, path4, pt2);
    if (!b0 || !b1 || !b2 || !b3 || !b4) {
      std::cerr << "At least one tag was not empty" << std::endl;
      return 1;
    }
  }
  return 0;
}

int TestCleaner2(int, char *[]) {
  using namespace gdcm;
  const char *directory = gdcm::Testing::GetDataRoot();
  std::string sfilename =
      std::string(directory) + "/PhilipsInteraSeqTermInvLen.dcm";
  const char *filename = sfilename.c_str();

  // Create directory first:
  const char subdir[] = "TestCleaner2";
  std::string tmpdir = Testing::GetTempDirectory(subdir);
  if (!System::FileIsDirectory(tmpdir.c_str())) {
    System::MakeDirectory(tmpdir.c_str());
  }
  std::string outfilename = Testing::GetTempFilename(filename, subdir);

  int res1 = 0;  // TestCleaner2Impl(filename, outfilename, true);
  int res2 = TestCleaner2Impl(filename, outfilename, false);

  return res1 + res2;
}