File: gdcmDataSetHelper.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 (370 lines) | stat: -rw-r--r-- 12,280 bytes parent folder | download
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*=========================================================================

  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 "gdcmDataSetHelper.h"
#include "gdcmFile.h"
#include "gdcmDataSet.h"
#include "gdcmSequenceOfItems.h"
#include "gdcmImplicitDataElement.h"
#include "gdcmGlobal.h"
#include "gdcmDicts.h"
#include "gdcmDict.h"
#include "gdcmAttribute.h"

namespace gdcm
{
/*
    See PS 3.5 - 2008
    Annex A (Normative) Transfer Syntax Specifications
*/

VR ComputeVRImplicitLittleEndian(DataSet const &ds, const Tag& tag)
{
  (void)ds;
  (void)tag;
    /*
    A.1 DICOM IMPLICIT VR LITTLE ENDIAN TRANSFER SYNTAX
    a) The Data Elements contained in the Data Set structure shall be encoded with Implicit VR
    (without a VR Field) as specified in Section 7.1.3.
    b) The encoding of the overall Data Set structure (Data Element Tags, Value Length, and Value)
    shall be in Little Endian as specified in Section 7.3.
    c) The encoding of the Data Elements of the Data Set shall be as follows according to their Value
    Representations:
      - For all Value Representations defined in this part, except for the Value Representations
      OB and OW, the encoding shall be in Little Endian as specified in Section 7.3
      - For the Value Representations OB and OW, the encoding shall meet the following
      specification depending on the Data Element Tag:
        - Data Element (7FE0,0010) Pixel Data has the Value Representation OW and shall
        be encoded in Little Endian.
        - Data Element (60xx,3000) Overlay Data has the Value Representation OW and shall
        be encoded in Little Endian.
        - Data Element (5400,1010) Waveform Data shall have Value Representation OW
        and shall be encoded in Little Endian.
        - Data Elements (0028,1201), (0028,1202),(0028,1203) Red, Green, Blue Palette
        Lookup Table Data have the Value Representation OW and shall be encoded in
        Little Endian.
        Note: Previous versions of the Standard either did not specify the encoding of these Data
        Elements in this Part, but specified a VR of US or SS in PS 3.6 (1993), or specified
        OW in this Part but a VR of US, SS or OW in PS 3.6 (1996). The actual encoding
        of the values and their byte order would be identical in each case.
        - Data Elements (0028,1101), (0028,1102),(0028,1103) Red, Green, Blue Palette
        Lookup Table Descriptor have the Value Representation SS or US (depending on
        rules specified in the IOD in PS 3.3), and shall be encoded in Little Endian. The first
        and third values are always interpreted as unsigned, regardless of the Value
        Representation.
        - Data Elements (0028,1221),(0028,1222),(0028,1223) Segmented Red, Green, Blue
        Palette Color Lookup table Data have the Value Representation OW and shall be
        encoded in Little Endian.
        - Data Element (0028,3006) Lookup Table Data has the Value Representation US, SS
        or OW and shall be encoded in Little Endian.
        - Data Element (0028,3002) Lookup Table Descriptor has the Value Representation
        SS or US (depending on rules specified in the IOD in PS 3.3), and shall be encoded
        in Little Endian. The first and third values are always interpreted as unsigned,
        regardless of the Value Representation.
    */
  VR vr = VR::INVALID;
  return vr;
}

VR DataSetHelper::ComputeVR(File const &file, DataSet const &ds, const Tag& tag)
{
  const Global& g = GlobalInstance;
  const Dicts &dicts = g.GetDicts();
  //const Dict &d = dicts.GetPublicDict();

  std::string strowner;
  const char *owner = nullptr;
  const Tag& t = tag;
  if( t.IsPrivate() && !t.IsPrivateCreator() )
    {
    strowner = ds.GetPrivateCreator(t);
    owner = strowner.c_str();
    }
  const DictEntry &entry = dicts.GetDictEntry(t,owner);
  const VR &refvr = entry.GetVR();
  //const VM &vm = entry.GetVM();

  // not much we can do...
  if( refvr == VR::INVALID || refvr == VR::UN )
    {
    // New public/private tag, let's forward what is found in the file (hopefully correct)
    if( ds.FindDataElement( t ) )
      {
      const DataElement &de = ds.GetDataElement( t );
      const VR &devr = de.GetVR();
      if( devr != VR::INVALID && devr != VR::UN )
        {
        gdcmWarningMacro("Please report. Missing dict entry for: [" << (owner ? owner : "<null>") << "]" << " de is: "  << de );
        return devr;
        }
      // CP-246
      if( devr == VR::UN && de.IsUndefinedLength() )
        {
        return VR::SQ;
        }
      }
    // postcondition says it cannot be VR::INVALID, so return VR::UN
    return VR::UN;
    }
  else
  {
    if( ds.FindDataElement( t ) )
      {
      const DataElement &de = ds.GetDataElement( t );
      const VR &devr = de.GetVR();
      if( devr != refvr )
        {
        if(!refvr.Compatible(devr)) {
          gdcmWarningMacro("Inconsistent VR: " << devr << " should be " << refvr << " for: [" << (owner ? owner : "<null>") << "]" << " de is: "  << de );
	}
        }
      }

  }

  VR vr = refvr;

  // Special handling of US or SS vr:
  if( vr == VR::US_SS )
    {
    // I believe all US_SS VR derived from the value from 0028,0103 ... except 0028,0071
    if( t != Tag(0x0028,0x0071) )
      {
      // In case of SAX parser, we would have had to process Pixel Representation already:
      Attribute<0x0028,0x0103> at;
      const Tag &pixelrep = at.GetTag();
      assert( pixelrep < t );
      const DataSet &rootds = file.GetDataSet();
      // FIXME
      // PhilipsWith15Overlays.dcm has a Private SQ with public elements such as
      // 0028,3002, so we cannot look up element in current dataset, but have to get the root dataset
      // to loop up...

      // FIXME:
      // gdcmDataExtra/gdcmSampleData/ImagesPapyrus/TestImages/wristb.pap
      // It's the contrary: root dataset does not have a Pixel Representation, but each SQ do...
      //assert( rootds.FindDataElement( pixelrep ) || ds.FindDataElement( pixelrep ) );
      if( ds.FindDataElement( pixelrep ) )
        {
        at.SetFromDataElement( ds.GetDataElement( pixelrep ) );
        }
      else if( rootds.FindDataElement( pixelrep ) )
        {
        at.SetFromDataElement( rootds.GetDataElement( pixelrep ) );
        }
      else
        {
        //throw Exception( "Unhandled" );
        gdcmWarningMacro( "Unhandled" ); // Typical to PDF encapsulated with some illegal attribute
        vr = VR::INVALID;
        }
      //assert( at.GetValue() == 0 || at.GetValue() == 1 );
      if( at.GetValue() == 1 )
        {
        vr = VR::SS;
        }
      else /*if( at.GetValue() == 0 )*/
        {
        // default to VR:US always (even when attribute is missing, or impossible to compute). This is much better than VR:UN anyway
        vr = VR::US;
        }
      }
    else
      {
      // FIXME ???
      vr = VR::US;
      }
    }
  else if( vr == VR::OB_OW )
    {
    Tag pixeldata(0x7fe0,0x0010);
    Tag waveformpaddingvalue(0x5400,0x100a);
    Tag waveformdata(0x5400,0x1010);
    Tag overlaydata(0x6000,0x3000);
    Tag curvedata(0x5000,0x3000);
    Tag audiodata(0x5000,0x200c);
    Tag variablepixeldata(0x7f00,0x0010);
    Tag bitsallocated(0x0028,0x0100);
    Tag channelminval(0x5400,0x0110);
    Tag channelmaxval(0x5400,0x0112);
    //assert( ds.FindDataElement( pixeldata ) );
    int v = -1;
    if( waveformdata == t || waveformpaddingvalue == t )
      {
      Tag waveformbitsallocated(0x5400,0x1004);
      // For Waveform Data:
      // (5400,1004) US 16                                             # 2,1 Waveform Bits Allocated
      assert( ds.FindDataElement( waveformbitsallocated ) );
      Attribute<0x5400,0x1004> at;
      at.SetFromDataElement( ds.GetDataElement( waveformbitsallocated ) );
      v = at.GetValue();
      }
    else // ( pixeldata == t  )
      {
      // For Pixel Data:
      if( !ds.FindDataElement( bitsallocated ) )
        return VR::UN;
      Attribute<0x0028,0x0100> at;
      at.SetFromDataElement( ds.GetDataElement( bitsallocated ) );
      }
    (void)v;

    if( pixeldata == t || t.IsGroupXX(overlaydata) )
      {
      vr = VR::OW;
      if( ds.FindDataElement( t ) )
        {
        const DataElement &de = ds.GetDataElement( t );
        const gdcm::SequenceOfFragments * sqf = de.GetSequenceOfFragments();
        if( sqf ) vr = VR::OB;
        }
      }
    else if( waveformdata == t || waveformpaddingvalue == t )
      {
      //assert( v == 8 || v == 16 );
      vr = VR::OW;
      }
    else if ( t.IsGroupXX(audiodata) )
      {
      vr = VR::OB;
      }
    else if ( t.IsGroupXX(curvedata) )
      {
      vr = VR::OB;
      }
    else if ( t.IsGroupXX(variablepixeldata) )
      {
      vr = VR::OB;
      }
    else if ( t == channelminval || t == channelmaxval )
      {
      vr = VR::OB;
      }
    else
      {
      // We may reach here coming from a private attribute
      if( t.IsPrivate() && !t.IsPrivateCreator() )
        {
        // ?HitachiMedical.Dream.Cabinet.ApplicationObjects.ImageAppData?
        vr = VR::UN;
        // no clue which VR to pick, try to re-use the one from the file:
        if( ds.FindDataElement( t ) )
          {
          const DataElement &de = ds.GetDataElement( t );
          const VR &devr = de.GetVR();
          if( devr != VR::INVALID && devr != VR::UN )
            {
            gdcmWarningMacro("Possibly incorrect VR for: " << de );
            vr = devr;
            }
          }
        }
      else
        {
        // Do we need to update this logic ?
        gdcmAssertAlwaysMacro( 0 && "Should not happen" );
        }
      }
    }
  else if( vr == VR::US_SS_OW || vr == VR::US_OW )
    {
    vr = VR::OW;
    }
  // TODO need to treat US_SS_OW too

  // \postcondition:
  assert( vr.IsVRFile() );
  assert( vr != VR::INVALID );

  if( tag.IsGroupLength() )
    {
    assert( vr == VR::UL );
    }
  if( tag.IsPrivateCreator() )
    {
    assert( vr == VR::LO );
    }
  return vr;
}


/*
SequenceOfItems* DataSetHelper::ComputeSQFromByteValue(File const & file, DataSet const &ds, const Tag &tag)
{
  const TransferSyntax &ts = file.GetHeader().GetDataSetTransferSyntax();
  assert( ts != TransferSyntax::DeflatedExplicitVRLittleEndian );
  const DataElement &de = ds.GetDataElement( tag );
  if( de.IsEmpty() )
    {
    return 0;
    }
  Value &v = const_cast<Value&>(de.GetValue());
  SequenceOfItems *sq = dynamic_cast<SequenceOfItems*>(&v);
  if( sq ) // all set !
    {
    SmartPointer<SequenceOfItems> sqi = sq;
    return sqi;
    }

  try
    {
    if( ts.GetSwapCode() == SwapCode::BigEndian )
      {
      assert(0);
      }
    else
      {
      if( ts.GetNegociatedType() == TransferSyntax::Implicit )
        {
        assert( de.GetVR() == VR::INVALID );
        const ByteValue *bv = de.GetByteValue();
        assert( bv );
        SequenceOfItems *sqi = new SequenceOfItems;
        sqi->SetLength( bv->GetLength() );
        std::stringstream ss;
        ss.str( std::string( bv->GetPointer(), bv->GetLength() ) );
        sqi->Read<ImplicitDataElement,SwapperNoOp>( ss );
        return sqi;
        }
      else
        {
        assert( de.GetVR() == VR::UN ); // cp 246, IVRLE SQ
        const ByteValue *bv = de.GetByteValue();
        assert( bv );
        SequenceOfItems *sqi = new SequenceOfItems;
        sqi->SetLength( bv->GetLength() );
        std::stringstream ss;
        ss.str( std::string( bv->GetPointer(), bv->GetLength() ) );
        sqi->Read<ImplicitDataElement,SwapperNoOp>( ss );
        return sqi;
        }
      }
    }
  catch( ParseException &pex )
    {
    gdcmDebugMacro( pex.what() );
    }
  catch( Exception &ex )
    {
    gdcmDebugMacro( ex.what() );
    }
  catch( ... )
    {
    gdcmWarningMacro( "Unknown exception" );
    }

  return 0;
}
*/

}