File: gdcmOverlay.cxx

package info (click to toggle)
gdcm 2.4.4-3%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 32,912 kB
  • ctags: 52,166
  • sloc: cpp: 188,527; ansic: 124,526; xml: 41,799; sh: 7,162; python: 3,667; cs: 2,128; java: 1,344; lex: 1,290; tcl: 677; php: 128; makefile: 116
file content (509 lines) | stat: -rw-r--r-- 17,350 bytes parent folder | download | duplicates (2)
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*=========================================================================

  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 "gdcmOverlay.h"
#include "gdcmTag.h"
#include "gdcmDataElement.h"
#include "gdcmDataSet.h"
#include "gdcmAttribute.h"

#include <vector>

namespace gdcm
{

class OverlayInternal
{
public:
  OverlayInternal():
  InPixelData(false),
  Group(0), // invalid default
  Rows(0),
  Columns(0),
  NumberOfFrames(0),
  Description(),
  Type(),
  //Origin[2],
  FrameOrigin(0),
  BitsAllocated(0),
  BitPosition(0),
  Data() { Origin[0] = Origin[1] = 0; }
  /*
  (6000,0010) US 484                                      #   2, 1 OverlayRows
  (6000,0011) US 484                                      #   2, 1 OverlayColumns
  (6000,0015) IS [1]                                      #   2, 1 NumberOfFramesInOverlay
  (6000,0022) LO [Siemens MedCom Object Graphics]         #  30, 1 OverlayDescription
  (6000,0040) CS [G]                                      #   2, 1 OverlayType
  (6000,0050) SS 1\1                                      #   4, 2 OverlayOrigin
  (6000,0051) US 1                                        #   2, 1 ImageFrameOrigin
  (6000,0100) US 1                                        #   2, 1 OverlayBitsAllocated
  (6000,0102) US 0                                        #   2, 1 OverlayBitPosition
  (6000,3000) OW 0000\0000\0000\0000\0000\0000\0000\0000\0000\0000\0000\0000\0000... # 29282, 1 OverlayData
  */

  bool InPixelData;
// Identifier need to be in the [6000,60FF] range (no odd number):
  unsigned short Group;
// Descriptor:
  unsigned short Rows;           // (6000,0010) US 484                                      #   2, 1 OverlayRows
  unsigned short Columns;        // (6000,0011) US 484                                      #   2, 1 OverlayColumns
  unsigned int   NumberOfFrames; // (6000,0015) IS [1]                                      #   2, 1 NumberOfFramesInOverlay
  std::string    Description;    // (6000,0022) LO [Siemens MedCom Object Graphics]         #  30, 1 OverlayDescription
  std::string    Type;           // (6000,0040) CS [G]                                      #   2, 1 OverlayType
  signed short   Origin[2];      // (6000,0050) SS 1\1                                      #   4, 2 OverlayOrigin
  unsigned short FrameOrigin;    // (6000,0051) US 1                                        #   2, 1 ImageFrameOrigin
  unsigned short BitsAllocated;  // (6000,0100) US 1                                        #   2, 1 OverlayBitsAllocated
  unsigned short BitPosition;    // (6000,0102) US 0                                        #   2, 1 OverlayBitPosition
  //std::vector<bool> Data;
  std::vector<char> Data; // hold the Overlay data, but not the trailing DICOM padding (\0)
  void Print(std::ostream &os) const {
    os << "Group           0x" <<  std::hex << Group << std::dec << std::endl;
    os << "Rows            " <<  Rows << std::endl;
    os << "Columns         " <<  Columns << std::endl;
    os << "NumberOfFrames  " <<  NumberOfFrames << std::endl;
    os << "Description     " <<  Description << std::endl;
    os << "Type            " <<  Type << std::endl;
    os << "Origin[2]       " <<  Origin[0] << "," << Origin[1] << std::endl;
    os << "FrameOrigin     " <<  FrameOrigin << std::endl;
    os << "BitsAllocated   " <<  BitsAllocated << std::endl;
    os << "BitPosition     " <<  BitPosition << std::endl;
    //std::vector<bool> Data;
  }
};

Overlay::Overlay()
{
  Internal = new OverlayInternal;
}

Overlay::~Overlay()
{
  delete Internal;
}

Overlay::Overlay(Overlay const &ov):Object(ov)
{
  //delete Internal;
  Internal = new OverlayInternal;
  // TODO: copy OverlayInternal into other...
  *Internal = *ov.Internal;
}

void Overlay::Update(const DataElement & de)
{
/*
  8.1.2 Overlay data encoding of related data elements
    Encoded Overlay Planes always have a bit depth of 1, and are encoded
    separately from the Pixel Data in Overlay Data (60xx,3000). The following two
    Data Elements shall define the Overlay Plane structure:
    - Overlay Bits Allocated (60xx,0100)
    - Overlay Bit Position (60xx,0102)
    Notes: 1. There is no Data Element analogous to Bits Stored (0028,0101)
    since Overlay Planes always have a bit depth of 1.
    2. Restrictions on the allowed values for these Data Elements are defined
    in PS 3.3. Formerly overlay data stored in unused bits of Pixel Data
    (7FE0,0010) was described, and these attributes had meaningful values but this
    usage has been retired. See PS 3.5 2004. For overlays encoded in Overlay Data
    Element (60xx,3000), Overlay Bits Allocated (60xx,0100) is always 1 and Overlay
    Bit Position (60xx,0102) is always 0.
*/

  assert( de.GetTag().IsPublic() );
  const ByteValue* bv = de.GetByteValue();
  if( !bv ) return; // Discard any empty element (will default to another value)
  assert( bv->GetPointer() && bv->GetLength() );
  std::string s( bv->GetPointer(), bv->GetLength() );
  // What if a \0 can be found before the end of string...
  //assert( strlen( s.c_str() ) == s.size() );

  // First thing check consistency:
  if( !GetGroup() )
    {
    SetGroup( de.GetTag().GetGroup() );
    }
  else // check consistency
    {
    assert( GetGroup() == de.GetTag().GetGroup() ); // programmer error
    }

  //std::cerr << "Tag: " << de.GetTag() << std::endl;
  if( de.GetTag().GetElement() == 0x0000 ) // OverlayGroupLength
    {
    // ??
    }
  else if( de.GetTag().GetElement() == 0x0010 ) // OverlayRows
    {
    Attribute<0x6000,0x0010> at;
    at.SetFromDataElement( de );
    SetRows( at.GetValue() );
    }
  else if( de.GetTag().GetElement() == 0x0011 ) // OverlayColumns
    {
    Attribute<0x6000,0x0011> at;
    at.SetFromDataElement( de );
    SetColumns( at.GetValue() );
    }
  else if( de.GetTag().GetElement() == 0x0015 ) // NumberOfFramesInOverlay
    {
    Attribute<0x6000,0x0015> at;
    at.SetFromDataElement( de );
    SetNumberOfFrames( at.GetValue() );
    }
  else if( de.GetTag().GetElement() == 0x0022 ) // OverlayDescription
    {
    SetDescription( s.c_str() );
    }
  else if( de.GetTag().GetElement() == 0x0040 ) // OverlayType
    {
    SetType( s.c_str() );
    }
  else if( de.GetTag().GetElement() == 0x0045 ) // OverlaySubtype
    {
    gdcmWarningMacro( "FIXME" );
    }
  else if( de.GetTag().GetElement() == 0x0050 ) // OverlayOrigin
    {
    Attribute<0x6000,0x0050> at;
    at.SetFromDataElement( de );
    SetOrigin( at.GetValues() );
    }
  else if( de.GetTag().GetElement() == 0x0051 ) // ImageFrameOrigin
    {
    Attribute<0x6000,0x0051> at;
    at.SetFromDataElement( de );
    SetFrameOrigin( at.GetValue() );
    }
  else if( de.GetTag().GetElement() == 0x0060 ) // OverlayCompressionCode (RET)
    {
    assert( s == "NONE" ); // FIXME ??
    }
  else if( de.GetTag().GetElement() == 0x0100 ) // OverlayBitsAllocated
    {
    Attribute<0x6000,0x0100> at;
    at.SetFromDataElement( de );
    // if OverlayBitsAllocated is 1 it imply OverlayData element
    // if OverlayBitsAllocated is 16 it imply Overlay in unused pixel bits
    if( at.GetValue() != 1 )
      {
      gdcmWarningMacro( "Unsuported OverlayBitsAllocated: " << at.GetValue() );
      }
    SetBitsAllocated( at.GetValue() );
    }
  else if( de.GetTag().GetElement() == 0x0102 ) // OverlayBitPosition
    {
    Attribute<0x6000,0x0102> at;
    at.SetFromDataElement( de );
    if( at.GetValue() != 0 ) // For old ACR when using unused bits...
      {
      gdcmDebugMacro( "Unsuported OverlayBitPosition: " << at.GetValue() );
      }
    SetBitPosition( at.GetValue() );
    }
  else if( de.GetTag().GetElement() == 0x0110 ) // OverlayFormat (RET)
    {
    assert( s == "RECT" );
    }
  else if( de.GetTag().GetElement() == 0x0200 ) // OverlayLocation (RET)
    {
    Attribute<0x6000,0x0200> at;
    at.SetFromDataElement( de );
    gdcmWarningMacro( "FIXME: " << at.GetValue() );
    }
  else if( de.GetTag().GetElement() == 0x1301 ) // ROIArea
    {
    gdcmWarningMacro( "FIXME" );
    }
  else if( de.GetTag().GetElement() == 0x1302 ) // ROIMean
    {
    gdcmWarningMacro( "FIXME" );
    }
  else if( de.GetTag().GetElement() == 0x1303 ) // ROIStandardDeviation
    {
    gdcmWarningMacro( "FIXME" );
    }
  else if( de.GetTag().GetElement() == 0x1500 ) // OverlayLabel
    {
    gdcmWarningMacro( "FIXME" );
    }
  else if( de.GetTag().GetElement() == 0x3000 ) // OverlayData
    {
    SetOverlay(bv->GetPointer(), bv->GetLength());
    }
  else
    {
    gdcmErrorMacro( "Tag is not supported: " << de.GetTag() << std::endl );
    assert(0);
    }
}

bool Overlay::GrabOverlayFromPixelData(DataSet const &ds)
{
  const unsigned int ovlength = Internal->Rows * Internal->Columns / 8;
  Internal->Data.resize( ovlength ); // set to 0
  if( Internal->BitsAllocated == 16 )
    {
    //assert( Internal->BitPosition >= 12 );
    if( ds.FindDataElement( Tag(0x7fe0,0x0010) ) )
      {
      gdcmErrorMacro("Could not find Pixel Data. Cannot extract Overlay." );
      return false;
      }
    const DataElement &pixeldata = ds.GetDataElement( Tag(0x7fe0,0x0010) );
    const ByteValue *bv = pixeldata.GetByteValue();
    if( !bv )
      {
      // XA_GE_JPEG_02_with_Overlays.dcm
      return false;
      }
    assert( bv );
    const char *array = bv->GetPointer();
    // SIEMENS_GBS_III-16-ACR_NEMA_1.acr is pain to support,
    // I cannot simply use the bv->GetLength I have to use the image dim:
    const unsigned int length = ovlength * 8 * 2; //bv->GetLength();
    const uint16_t *p = (uint16_t*)array;
    const uint16_t *end = (uint16_t*)(array + length);
    //const unsigned int ovlength = length / (8*2);
    assert( 8 * ovlength == (unsigned int)Internal->Rows * Internal->Columns );
    if( Internal->Data.empty() )
      {
      return false;
      }
    unsigned char * overlay = (unsigned char*)&Internal->Data[0];
    int c = 0;
    uint16_t pmask = (uint16_t)(1 << Internal->BitPosition);
    assert( length / 2 == ovlength * 8 );
    while( p != end )
      {
      const uint16_t val = *p & pmask;
      assert( val == 0x0 || val == pmask );
      // 128 -> 0x80
      if( val )
        {
        overlay[ c / 8 ] |= (unsigned char)(0x1 << c%8);
        }
      else
        {
        // else overlay[ c / 8 ] is already 0
        }
      ++p;
      ++c;
      }
    assert( (unsigned)c / 8 == ovlength );
    }
  else
    {
    gdcmErrorMacro( "Could not grab Overlay from image. Please report." );
    return false;
    }
  return true;
}

void Overlay::SetGroup(unsigned short group) { Internal->Group = group; }
unsigned short Overlay::GetGroup() const { return Internal->Group; }

void Overlay::SetRows(unsigned short rows) { Internal->Rows = rows; }
unsigned short Overlay::GetRows() const { return Internal->Rows; }
void Overlay::SetColumns(unsigned short columns) { Internal->Columns = columns; }
unsigned short Overlay::GetColumns() const { return Internal->Columns; }
void Overlay::SetNumberOfFrames(unsigned int numberofframes) { Internal->NumberOfFrames = numberofframes; }
void Overlay::SetDescription(const char* description) { if( description ) Internal->Description = description; }
const char *Overlay::GetDescription() const { return Internal->Description.c_str(); }
void Overlay::SetType(const char* type) { if( type ) Internal->Type = type; }
const char *Overlay::GetType() const { return Internal->Type.c_str(); }
static const char *OverlayTypeStrings[] = {
  "INVALID",
  "G ",
  "R ",
};
const char *Overlay::GetOverlayTypeAsString(OverlayType ot)
{
  return OverlayTypeStrings[ (int) ot ];
}
Overlay::OverlayType Overlay::GetOverlayTypeFromString(const char *s)
{
  static const int n = sizeof( OverlayTypeStrings ) / sizeof ( *OverlayTypeStrings );
  if( s )
    {
    for( int i = 0; i < n; ++i )
      {
      if( strcmp(s, OverlayTypeStrings[i] ) == 0 )
        {
        return (OverlayType)i;
        }
      }
    }
  // could not find the proper type, maybe padded with \0 ?
  if( strlen(s) == 1 )
    {
    for( int i = 0; i < n; ++i )
      {
      if( strncmp(s, OverlayTypeStrings[i], 1 ) == 0 )
        {
        gdcmDebugMacro( "Invalid Padding for OVerlay Type" );
        return (OverlayType)i;
        }
      }
    }
  return Overlay::Invalid;
}
Overlay::OverlayType Overlay::GetTypeAsEnum() const
{
  return GetOverlayTypeFromString( GetType() );
}

void Overlay::SetOrigin(const signed short origin[2])
{
  if( origin )
    {
    Internal->Origin[0] = origin[0];
    Internal->Origin[1] = origin[1];
    }
}
const signed short * Overlay::GetOrigin() const
{
  return &Internal->Origin[0];
}
void Overlay::SetFrameOrigin(unsigned short frameorigin) { Internal->FrameOrigin = frameorigin; }
void Overlay::SetBitsAllocated(unsigned short bitsallocated) { Internal->BitsAllocated = bitsallocated; }
unsigned short Overlay::GetBitsAllocated() const { return Internal->BitsAllocated; }
void Overlay::SetBitPosition(unsigned short bitposition) { Internal->BitPosition = bitposition; }
unsigned short Overlay::GetBitPosition() const { return Internal->BitPosition; }

bool Overlay::IsEmpty() const
{
  return Internal->Data.empty();
}
bool Overlay::IsZero() const
{
  if( IsEmpty() ) return false;

  std::vector<char>::const_iterator it = Internal->Data.begin();
  for(; it != Internal->Data.end(); ++it )
    {
    if( *it ) return true;
    }
  return false;
}
bool Overlay::IsInPixelData() const { return Internal->InPixelData; }
void Overlay::IsInPixelData(bool b) { Internal->InPixelData = b; }

/*
 * row,col = 400,400   => 20000
 * row,col = 1665,1453 => 302406
 * row,col = 20,198    => 495 words + 1 dicom \0 padding
 */
inline unsigned int compute_bit_and_dicom_padding(unsigned short rows, unsigned short columns)
{
  unsigned int word_padding = ( rows * columns + 7 ) / 8; // need to send full word (8bits at a time)
  return word_padding + word_padding%2; // Cannot have odd length
}

void Overlay::SetOverlay(const char *array, size_t length)
{
  if( !array || !length ) return;
  const size_t computed_length = (Internal->Rows * Internal->Columns + 7) / 8;
  Internal->Data.resize( computed_length ); // filled with 0 if length < computed_length
  if( length < computed_length )
    {
    gdcmWarningMacro( "Not enough data found in Overlay. Proceed with caution" );
    std::copy(array, array+length, Internal->Data.begin());
    }
  else
    {
    // do not try to copy more than allocated:
    // technically we may be missing the trailing DICOM padding (\0), but we have all the data needed anyway:
    std::copy(array, array+computed_length, Internal->Data.begin());
    }
  /* warning need to take into account padding to the next word (8bits) */
  //assert( length == compute_bit_and_dicom_padding(Internal->Rows, Internal->Columns) );
  assert( Internal->Data.size() == computed_length );
}

const ByteValue &Overlay::GetOverlayData() const
{
  static ByteValue bv;
  bv = ByteValue( Internal->Data );
  return bv;
}

size_t Overlay::GetUnpackBufferLength() const
{
  const size_t unpacklen = Internal->Rows * Internal->Columns;
  return unpacklen;
}

bool Overlay::GetUnpackBuffer(char *buffer, size_t len) const
{
  const size_t unpacklen = GetUnpackBufferLength();
  if( len < unpacklen ) return false;
  unsigned char *unpackedbytes = (unsigned char*)buffer;
  const unsigned char *begin = unpackedbytes;
  for( std::vector<char>::const_iterator it = Internal->Data.begin(); it != Internal->Data.end(); ++it )
    {
    assert( unpackedbytes <= begin + len ); // We never store more than actually required
    // const unsigned char &packedbytes = *it;
    // weird bug with gcc 3.3 (prerelease on SuSE) apparently:
    unsigned char packedbytes = static_cast<unsigned char>(*it);
    unsigned char mask = 1;
    for (unsigned int i = 0; i < 8 && unpackedbytes < begin + len; ++i)
      {
      if ( (packedbytes & mask) == 0)
        {
        *unpackedbytes = 0;
        }
      else
        {
        *unpackedbytes = 255;
        }
      ++unpackedbytes;
      mask <<= 1;
      }
    }
  assert(unpackedbytes <= begin + len);
  return true;
}

void Overlay::Decompress(std::ostream &os) const
{
  const size_t unpacklen = GetUnpackBufferLength();
  unsigned char unpackedbytes[8];
  //std::vector<char>::const_iterator beg = Internal->Data.begin();
  size_t curlen = 0;
  for( std::vector<char>::const_iterator it = Internal->Data.begin(); it != Internal->Data.end(); ++it )
    {
    unsigned char packedbytes = *it;
    unsigned char mask = 1;
    unsigned int i = 0;
    for (; i < 8 && curlen < unpacklen; ++i)
      {
      if ( (packedbytes & mask) == 0)
        {
        unpackedbytes[i] = 0;
        }
      else
        {
        unpackedbytes[i] = 255;
        }
      mask <<= 1;
      ++curlen;
      }
    os.write(reinterpret_cast<char*>(unpackedbytes), i);
    }
}

void Overlay::Print(std::ostream &os) const
{
  Internal->Print( os );
}

} // end namespace gdcm