File: gdcmFileExplicitFilter.cxx

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 (216 lines) | stat: -rw-r--r-- 6,644 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
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
/*=========================================================================

  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 "gdcmFileExplicitFilter.h"
#include "gdcmExplicitDataElement.h"
#include "gdcmSequenceOfFragments.h"
#include "gdcmSequenceOfItems.h"
#include "gdcmFragment.h"
#include "gdcmGlobal.h"
#include "gdcmDict.h"
#include "gdcmDicts.h"
#include "gdcmGroupDict.h"
#include "gdcmVR.h"
#include "gdcmVM.h"
#include "gdcmDataSetHelper.h"

namespace gdcm
{

void FileExplicitFilter::SetRecomputeItemLength(bool b)
{
  RecomputeItemLength = b;
}

void FileExplicitFilter::SetRecomputeSequenceLength(bool b)
{
  RecomputeSequenceLength = b;
}

bool FileExplicitFilter::ChangeFMI()
{
/*
    FileMetaInformation &fmi = F->GetHeader();
    TransferSyntax ts = TransferSyntax::ImplicitVRLittleEndian;
      {
      ts = TransferSyntax::ExplicitVRLittleEndian;
      }
    const char *tsuid = TransferSyntax::GetTSString( ts );
    DataElement de( Tag(0x0002,0x0010) );
    de.SetByteValue( tsuid, strlen(tsuid) );
    de.SetVR( Attribute<0x0002, 0x0010>::GetVR() );
    fmi.Replace( de );
    //fmi.Remove( Tag(0x0002,0x0012) ); // will be regenerated
    //fmi.Remove( Tag(0x0002,0x0013) ); //  '   '    '
    fmi.SetDataSetTransferSyntax(ts);
*/

  return true;
}

bool FileExplicitFilter::ProcessDataSet(DataSet &ds, Dicts const & dicts)
{
  if( RecomputeSequenceLength || RecomputeItemLength )
    {
    gdcmWarningMacro( "Not implemented sorry" );
    return false;
    }
  DataSet::Iterator it = ds.Begin();
  for( ; it != ds.End(); )
    {
    DataElement de = *it;
    std::string strowner;
    const char *owner = nullptr;
    const Tag& t = de.GetTag();
    if( t.IsPrivate() && !ChangePrivateTags
    // As a special exception we convert to proper VR :
    // - Private Group Length
    // - Private Creator
    // This makes the output more readable (and this should be relative safe)
      && !t.IsGroupLength() && !t.IsPrivateCreator()
    )
      {
      // nothing to do ! just skip
      ++it;
      continue;
      }
    if( t.IsPrivate() && !t.IsPrivateCreator() )
      {
      strowner = ds.GetPrivateCreator(t);
      owner = strowner.c_str();
      }
    const DictEntry &entry = dicts.GetDictEntry(t,owner);
    const VR &vr = entry.GetVR();

    //assert( de.GetVR() == VR::INVALID );
    VR cvr = DataSetHelper::ComputeVR(*F,ds, t);
    VR oldvr = de.GetVR();
    if( cvr == VR::SQ ) { assert( oldvr == VR::SQ || oldvr == VR::UN || oldvr == VR::INVALID || oldvr == VR::OB ); }
    //SequenceOfItems *sqi = de.GetSequenceOfItems();
    //SequenceOfItems *sqi = dynamic_cast<SequenceOfItems*>(&de.GetValue());
    SmartPointer<SequenceOfItems> sqi = nullptr;
    if( vr == VR::SQ 
    || cvr == VR::SQ ) // CP-246 + Private SQ (not in dict)
      {
      sqi = de.GetValueAsSQ();
      if(!sqi)
        {
        if( !de.IsEmpty() )
          {
          gdcmWarningMacro("DICOM file written out may not be readable" );
          cvr = VR::UN;
          }
        }
      }
    if( de.GetByteValue() && !sqi )
      {
      // all set
      //assert( cvr != VR::SQ /*&& cvr != VR::UN*/ );
      assert( cvr != VR::INVALID );
      if( cvr != VR::UN )
        {
        // about to change , make some paranoid test:
        //assert( cvr.Compatible( oldvr ) ); // LT != LO but there are somewhat compatible
        if( cvr & VR::VRASCII )
          {
          //assert( oldvr & VR::VRASCII || oldvr == VR::INVALID || oldvr == VR::UN );
          // gdcm-JPEG-Extended.dcm has a couple of VR::OB private field
          // is this a good idea to change them to an ASCII when we know this might not work ?
          if( !(oldvr & VR::VRASCII || oldvr == VR::INVALID || oldvr == VR::UN || oldvr == VR::OB) )
            {
            gdcmErrorMacro( "Cannot convert VR for tag: " << t << " " << oldvr << " is incompatible with " << cvr << " as given by ref. dict." );
            return false;
            }
          }
        else if( cvr & VR::VRBINARY )
          {
          // PHILIPS_Gyroscan-12-MONO2-Jpeg_Lossless.dcm
          if( !( oldvr & VR::VRBINARY || oldvr == VR::INVALID || oldvr == VR::UN ) )
            {
            gdcmErrorMacro( "Cannot convert VR for tag: " << t << " " << oldvr << " is incompatible with " << cvr << " as given by ref. dict." );
            return false;
            }
          }
        else
          {
          assert( 0 ); // programmer error
          }

        // let's do one more check we are going to make this attribute explicit VR, there is
        // still a special case, when VL is > uint16_max then we must give up:
        if( !(cvr & VR::VL32) && de.GetVL() > UINT16_MAX )
          {
          cvr = VR::UN;
          }
        de.SetVR( cvr );
        }
      }
    else if( sqi )
      {
      assert( cvr == VR::SQ || cvr == VR::UN );
      de.SetVR( VR::SQ );
      if( de.GetByteValue() )
        {
        de.SetValue( *sqi );
        //de.SetVL( sqi->ComputeLength<ExplicitDataElement>() );
        }
      de.SetVLToUndefined();
      assert( sqi->GetLength().IsUndefined() );
      // recursive
      SequenceOfItems::ItemVector::iterator sit = sqi->Items.begin();
      for(; sit != sqi->Items.end(); ++sit)
        {
        //Item &item = const_cast<Item&>(*sit);
        Item &item = *sit;
        item.SetVLToUndefined();
        DataSet &nds = item.GetNestedDataSet();
        //const DataElement &deitem = item;
        ProcessDataSet(nds, dicts);
        item.SetVL( item.GetLength<ExplicitDataElement>() );
        }
      }
    else if( de.GetSequenceOfFragments() )
      {
      assert( cvr & VR::OB );
      }
    else
      {
      // Ok length is 0, it can be a 0 length explicit SQ (implicit) or a ByteValue...
      // we cannot make any error here, simply change the VR
      de.SetVR( cvr );
      }
    ++it;
    ds.Replace( de );
    }
  return true;
}

bool FileExplicitFilter::Change()
{
  //if( !UseVRUN)
  //  {
  //  gdcmErrorMacro( "Not implemented" );
  //  return false;
  //  }
  const Global& g = GlobalInstance;
  const Dicts &dicts = g.GetDicts();

  DataSet &ds = F->GetDataSet();

  bool b = ProcessDataSet(ds, dicts);

  return b;
}


} // end namespace gdcm