DICOMAppHelper.cxx 46.6 KB
Newer Older
Matt Turek's avatar
Matt Turek committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*=========================================================================

  Program:   DICOMParser
  Module:    DICOMAppHelper.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2003 Matt Turek
  All rights reserved.
  See Copyright.txt 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.

=========================================================================*/
18

Matt Turek's avatar
Matt Turek committed
19
20
21
22
23
24
25
#ifdef _MSC_VER
#pragma warning ( disable : 4514 )
#pragma warning ( disable : 4786 )
#pragma warning ( disable : 4503 )
#pragma warning ( disable : 4710 )
#pragma warning ( disable : 4702 )
#pragma warning ( push, 3 )
Matt Turek's avatar
Matt Turek committed
26
#endif 
27

28
#include "DICOMConfig.h"
29
30
31
32
#include "DICOMAppHelper.h"
#include "DICOMCallback.h"

#include <stdlib.h>
Matt Turek's avatar
Matt Turek committed
33
34
#include <stdio.h>
#include <string>
35
#include <math.h>
Matt Turek's avatar
Matt Turek committed
36
#include <algorithm>
37
38
39
#if defined(__BORLANDC__)
#include <mem.h> // for memcpy
#endif
40
41
42

//#define DEBUG_DICOM_APP_HELPER

43
44
45
46
47
48
49
50
51
52
53
54
55
56
class DICOMAppHelperImplementation
{
public:
  // map from series UID to vector of files in the series 
  dicom_stl::map<dicom_stl::string, dicom_stl::vector<dicom_stl::string>, ltstdstr> SeriesUIDMap;

  // map from filename to intraseries sortable tags
  dicom_stl::map<dicom_stl::string, DICOMOrderingElements, ltstdstr> SliceOrderingMap;

  typedef dicom_stl::map<dicom_stl::pair<doublebyte, doublebyte>, DICOMTagInfo> TagMapType;
  TagMapType TagMap;

}; 

57
58
struct lt_pair_int_string
{
59
60
  bool operator()(const dicom_stl::pair<int, dicom_stl::string> s1, 
                  const dicom_stl::pair<int, dicom_stl::string> s2) const
61
62
63
64
65
  {
    return s1.first < s2.first;
  }
};

Matt Turek's avatar
Matt Turek committed
66
67
68

struct lt_pair_float_string
{
69
70
  bool operator()(const dicom_stl::pair<float, dicom_stl::string> s1, 
                  const dicom_stl::pair<float, dicom_stl::string> s2) const
Matt Turek's avatar
Matt Turek committed
71
72
73
74
75
76
  {
    return s1.first < s2.first;
  }
};


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
struct gt_pair_int_string
{
  bool operator()(const dicom_stl::pair<int, dicom_stl::string> s1, 
                  const dicom_stl::pair<int, dicom_stl::string> s2) const
  {
    return s1.first > s2.first;
  }
};


struct gt_pair_float_string
{
  bool operator()(const dicom_stl::pair<float, dicom_stl::string> s1, 
                  const dicom_stl::pair<float, dicom_stl::string> s2) const
  {
    return s1.first > s2.first;
  }
};


Matt Turek's avatar
Matt Turek committed
97
DICOMAppHelper::DICOMAppHelper()
98
99
100
{
  this->BitsAllocated = 8;
  this->ByteSwapData = false;
Ken Martin's avatar
Ken Martin committed
101
  this->PixelSpacing[0] = this->PixelSpacing[1] = this->PixelSpacing[2] = 1.0;
102
  this->Dimensions[0] = this->Dimensions[1] = 0;
103
104
  this->PhotometricInterpretation = new dicom_stl::string();
  this->TransferSyntaxUID = new dicom_stl::string();
105
106
107
108
  this->RescaleOffset = 0.0;
  this->RescaleSlope = 1.0;
  this->ImageData = NULL;
  this->ImageDataLengthInBytes = 0;
109
110
  this->PatientName = new dicom_stl::string();
  this->StudyUID = new dicom_stl::string();
Jim Miller's avatar
Jim Miller committed
111
  this->StudyID = new dicom_stl::string();
112
  this->GantryAngle = 0.0;
113
114
  this->Width = 0;
  this->Height = 0;
Ken Martin's avatar
Ken Martin committed
115
  this->PixelRepresentation = 0;
116
117
118

  this->SeriesUIDCB = new DICOMMemberCallback<DICOMAppHelper>;
  this->SliceNumberCB = new DICOMMemberCallback<DICOMAppHelper>;
Matt Turek's avatar
Matt Turek committed
119
120
121
  this->SliceLocationCB = new DICOMMemberCallback<DICOMAppHelper>;
  this->ImagePositionPatientCB = new DICOMMemberCallback<DICOMAppHelper>;
  this->ImageOrientationPatientCB = new DICOMMemberCallback<DICOMAppHelper>;
122
  this->TransferSyntaxCB = new DICOMMemberCallback<DICOMAppHelper>;
123
  this->ToggleSwapBytesCB = new DICOMMemberCallback<DICOMAppHelper>;
124
125
126
127
128
129
130
131
132
  this->BitsAllocatedCB = new DICOMMemberCallback<DICOMAppHelper>;
  this->PixelSpacingCB = new DICOMMemberCallback<DICOMAppHelper>;
  this->HeightCB = new DICOMMemberCallback<DICOMAppHelper>;
  this->WidthCB = new DICOMMemberCallback<DICOMAppHelper>;
  this->PixelRepresentationCB = new DICOMMemberCallback<DICOMAppHelper>;
  this->PhotometricInterpretationCB = new DICOMMemberCallback<DICOMAppHelper>;
  this->RescaleOffsetCB = new DICOMMemberCallback<DICOMAppHelper>;
  this->RescaleSlopeCB = new DICOMMemberCallback<DICOMAppHelper>;
  this->PixelDataCB = new DICOMMemberCallback<DICOMAppHelper>;
133
134
  this->PatientNameCB = new DICOMMemberCallback<DICOMAppHelper>;
  this->StudyUIDCB = new DICOMMemberCallback<DICOMAppHelper>;
Jim Miller's avatar
Jim Miller committed
135
  this->StudyIDCB = new DICOMMemberCallback<DICOMAppHelper>;
136
  this->GantryAngleCB = new DICOMMemberCallback<DICOMAppHelper>;
137

138
  this->Implementation = new DICOMAppHelperImplementation;
139
140
141
142
}

DICOMAppHelper::~DICOMAppHelper()
{
Matt Turek's avatar
Matt Turek committed
143
  this->Clear();
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

  this->HeaderFile.close();

  //
  // Fix warning here.
  //
  if (this->ImageData)
    {
    delete [] (static_cast<char*> (this->ImageData));
    }
  if (this->TransferSyntaxUID)
    {
    delete this->TransferSyntaxUID;
    }
  if (this->PhotometricInterpretation)
    {
    delete this->PhotometricInterpretation;
    }

163
164
165
166
167
168
169
170
171
172
  if (this->PatientName)
    {
    delete this->PatientName;
    }

  if (this->StudyUID)
    {
    delete this->StudyUID;
    }

Jim Miller's avatar
Jim Miller committed
173
174
175
176
177
  if (this->StudyID)
    {
    delete this->StudyID;
    }
  
178
179
  delete this->SeriesUIDCB;
  delete this->SliceNumberCB;
Matt Turek's avatar
Matt Turek committed
180
181
182
  delete this->SliceLocationCB;
  delete this->ImagePositionPatientCB;
  delete this->ImageOrientationPatientCB;
183
  delete this->TransferSyntaxCB;
184
  delete this->ToggleSwapBytesCB;
185
186
187
188
189
190
191
192
193
  delete this->BitsAllocatedCB;
  delete this->PixelSpacingCB;
  delete this->HeightCB;
  delete this->WidthCB;
  delete this->PixelRepresentationCB;
  delete this->PhotometricInterpretationCB;
  delete this->RescaleOffsetCB;
  delete this->RescaleSlopeCB;
  delete this->PixelDataCB;
194
195
  delete this->PatientNameCB;
  delete this->StudyUIDCB;
Jim Miller's avatar
Jim Miller committed
196
  delete this->StudyIDCB;
197
  delete this->GantryAngleCB;
198
199

  delete this->Implementation;
200
201
202
203
204
205
}

void DICOMAppHelper::RegisterCallbacks(DICOMParser* parser)
{
  if (!parser)
    {
206
    dicom_stream::cerr << "Null parser!" << dicom_stream::endl;
207
208
209
210
211
212
213
214
    }

  SeriesUIDCB->SetCallbackFunction(this, &DICOMAppHelper::SeriesUIDCallback);
  parser->AddDICOMTagCallback(0x0020, 0x000e, DICOMParser::VR_UI, SeriesUIDCB);

  SliceNumberCB->SetCallbackFunction(this, &DICOMAppHelper::SliceNumberCallback);
  parser->AddDICOMTagCallback(0x0020, 0x0013, DICOMParser::VR_IS, SliceNumberCB);

Matt Turek's avatar
Matt Turek committed
215
216
217
218
219
220
221
222
223
  SliceLocationCB->SetCallbackFunction(this, &DICOMAppHelper::SliceLocationCallback);
  parser->AddDICOMTagCallback(0x0020, 0x1041, DICOMParser::VR_CS, SliceLocationCB);

  ImagePositionPatientCB->SetCallbackFunction(this, &DICOMAppHelper::ImagePositionPatientCallback);
  parser->AddDICOMTagCallback(0x0020, 0x0032, DICOMParser::VR_SH, ImagePositionPatientCB);

  ImageOrientationPatientCB->SetCallbackFunction(this, &DICOMAppHelper::ImageOrientationPatientCallback);
  parser->AddDICOMTagCallback(0x0020, 0x0037, DICOMParser::VR_SH, ImageOrientationPatientCB);
  
224
225
226
  TransferSyntaxCB->SetCallbackFunction(this, &DICOMAppHelper::TransferSyntaxCallback);
  parser->AddDICOMTagCallback(0x0002, 0x0010, DICOMParser::VR_UI, TransferSyntaxCB);

227
228
  ToggleSwapBytesCB->SetCallbackFunction(this, &DICOMAppHelper::ToggleSwapBytesCallback);

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
  BitsAllocatedCB->SetCallbackFunction(this, &DICOMAppHelper::BitsAllocatedCallback);
  parser->AddDICOMTagCallback(0x0028, 0x0100, DICOMParser::VR_US, BitsAllocatedCB);

  PixelSpacingCB->SetCallbackFunction(this, &DICOMAppHelper::PixelSpacingCallback);
  parser->AddDICOMTagCallback(0x0028, 0x0030, DICOMParser::VR_FL, PixelSpacingCB);
  parser->AddDICOMTagCallback(0x0018, 0x0050, DICOMParser::VR_FL, PixelSpacingCB);

  WidthCB->SetCallbackFunction(this, &DICOMAppHelper::WidthCallback);
  parser->AddDICOMTagCallback(0x0028, 0x0011, DICOMParser::VR_US, WidthCB);

  HeightCB->SetCallbackFunction(this, &DICOMAppHelper::HeightCallback);
  parser->AddDICOMTagCallback(0x0028, 0x0010, DICOMParser::VR_US, HeightCB);

  PixelRepresentationCB->SetCallbackFunction(this, &DICOMAppHelper::PixelRepresentationCallback);
  parser->AddDICOMTagCallback(0x0028, 0x0103, DICOMParser::VR_US, PixelRepresentationCB);

  PhotometricInterpretationCB->SetCallbackFunction(this, &DICOMAppHelper::PhotometricInterpretationCallback);
  parser->AddDICOMTagCallback(0x0028, 0x0004, DICOMParser::VR_CS, PhotometricInterpretationCB);

  RescaleOffsetCB->SetCallbackFunction(this, &DICOMAppHelper::RescaleOffsetCallback);
  parser->AddDICOMTagCallback(0x0028, 0x1052, DICOMParser::VR_CS, RescaleOffsetCB);

  RescaleSlopeCB->SetCallbackFunction(this, &DICOMAppHelper::RescaleSlopeCallback);
  parser->AddDICOMTagCallback(0x0028, 0x1053, DICOMParser::VR_FL, RescaleSlopeCB);

254
255
256
257
258
259
  PatientNameCB->SetCallbackFunction(this, &DICOMAppHelper::PatientNameCallback);
  parser->AddDICOMTagCallback(0x0010, 0x0010, DICOMParser::VR_PN, PatientNameCB);

  StudyUIDCB->SetCallbackFunction(this, &DICOMAppHelper::StudyUIDCallback);
  parser->AddDICOMTagCallback(0x0020, 0x000d, DICOMParser::VR_UI, StudyUIDCB);

Jim Miller's avatar
Jim Miller committed
260
261
262
  StudyIDCB->SetCallbackFunction(this, &DICOMAppHelper::StudyIDCallback);
  parser->AddDICOMTagCallback(0x0020, 0x0010, DICOMParser::VR_SH, StudyIDCB);
  
263
264
265
266
  GantryAngleCB->SetCallbackFunction(this, &DICOMAppHelper::GantryAngleCallback);
  parser->AddDICOMTagCallback(0x0018, 0x1120, DICOMParser::VR_FL, GantryAngleCB);


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
  DICOMTagInfo dicom_tags[] = {
    {0x0002, 0x0002, DICOMParser::VR_UI, "Media storage SOP class uid"},
    {0x0002, 0x0003, DICOMParser::VR_UI, "Media storage SOP inst uid"},
    {0x0002, 0x0010, DICOMParser::VR_UI, "Transfer syntax uid"},
    {0x0002, 0x0012, DICOMParser::VR_UI, "Implementation class uid"},
    {0x0008, 0x0018, DICOMParser::VR_UI, "Image UID"},
    {0x0008, 0x0020, DICOMParser::VR_DA, "Series date"},
    {0x0008, 0x0030, DICOMParser::VR_TM, "Series time"},
    {0x0008, 0x0060, DICOMParser::VR_SH, "Modality"},
    {0x0008, 0x0070, DICOMParser::VR_SH, "Manufacturer"},
    {0x0008, 0x1060, DICOMParser::VR_SH, "Physician"},
    {0x0018, 0x0050, DICOMParser::VR_FL, "slice thickness"},
    {0x0018, 0x0060, DICOMParser::VR_FL, "kV"},
    {0x0018, 0x0088, DICOMParser::VR_FL, "slice spacing"},
    {0x0018, 0x1100, DICOMParser::VR_SH, "Recon diameter"},
    {0x0018, 0x1151, DICOMParser::VR_FL, "mA"},
    {0x0018, 0x1210, DICOMParser::VR_SH, "Recon kernel"},
    {0x0020, 0x000d, DICOMParser::VR_UI, "Study UID"},
    {0x0020, 0x000e, DICOMParser::VR_UI, "Series UID"},
    {0x0020, 0x0013, DICOMParser::VR_IS, "Image number"},
    {0x0020, 0x0032, DICOMParser::VR_SH, "Patient position"},
    {0x0020, 0x0037, DICOMParser::VR_SH, "Patient position cosines"},
    {0x0020, 0x1041, DICOMParser::VR_CS, "Slice location"},
    {0x0028, 0x0010, DICOMParser::VR_FL, "Num rows"},
    {0x0028, 0x0011, DICOMParser::VR_FL, "Num cols"},
    {0x0028, 0x0030, DICOMParser::VR_FL, "pixel spacing"},
    {0x0028, 0x0100, DICOMParser::VR_US, "Bits allocated"},
    {0x0028, 0x0120, DICOMParser::VR_UL, "pixel padding"},
    {0x0028, 0x1052, DICOMParser::VR_FL, "pixel offset"}
  };

  int num_tags = sizeof(dicom_tags)/sizeof(DICOMTagInfo);

#ifdef DEBUG_DICOM_APP_HELPER
  DICOMMemberCallback<DICOMAppHelper>** callbackArray = new DICOMMemberCallback<DICOMAppHelper>*[num_tags];
#endif

  for (int j = 0; j < num_tags; j++)
    {
    //
    // Setup internal map.
    //
    DICOMTagInfo tagStruct = dicom_tags[j];
    doublebyte group = tagStruct.group;
    doublebyte element = tagStruct.element;

313
    dicom_stl::pair<doublebyte, doublebyte> gePair(group, element);
314
    dicom_stl::pair<const dicom_stl::pair<doublebyte, doublebyte>, DICOMTagInfo> mapPair(gePair, tagStruct);
315
    this->Implementation->TagMap.insert(mapPair);
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332

#ifdef DEBUG_DICOM_APP_HELPER
    //
    // Make callback
    //
    callbackArray[j] = new DICOMMemberCallback<DICOMAppHelper>;
    callbackArray[j]->SetCallbackFunction(this, &DICOMAppHelper::ArrayCallback);
    //
    // Set callback on parser.
    //
    parser->AddDICOMTagCallback(group, element,datatype, callbackArray[j]);
#endif

    }

}

Matt Turek's avatar
Matt Turek committed
333
334
void DICOMAppHelper::SeriesUIDCallback(DICOMParser *parser,
                                       doublebyte,
335
336
337
338
339
340
                                       doublebyte,
                                       DICOMParser::VRTypes,
                                       unsigned char* val,
                                       quadbyte) 
{
  char* newString = (char*) val;
341
342
343
  dicom_stl::string newStdString(newString);
  dicom_stl::map<dicom_stl::string, dicom_stl::vector<dicom_stl::string>, ltstdstr>::iterator iter = this->Implementation->SeriesUIDMap.find(newStdString);
  if ( iter == this->Implementation->SeriesUIDMap.end())
344
    {
345
    dicom_stl::vector<dicom_stl::string> newVector;
346

Matt Turek's avatar
Matt Turek committed
347
    newVector.push_back(parser->GetFileName());
348
    this->Implementation->SeriesUIDMap.insert(dicom_stl::pair<const dicom_stl::string, dicom_stl::vector<dicom_stl::string> > (newStdString, newVector));
349
350
351
    }
  else
    {
Matt Turek's avatar
Matt Turek committed
352
    (*iter).second.push_back(parser->GetFileName());
353
354
355
356
357
    }
} 

void DICOMAppHelper::OutputSeries()
{
358
  dicom_stream::cout << dicom_stream::endl << dicom_stream::endl;
359
        
360
361
  for (dicom_stl::map<dicom_stl::string, dicom_stl::vector<dicom_stl::string>, ltstdstr >::iterator iter = this->Implementation->SeriesUIDMap.begin();
       iter != this->Implementation->SeriesUIDMap.end();
362
363
       iter++)
    {
364
365
    dicom_stream::cout << "SERIES: " << (*iter).first.c_str() << dicom_stream::endl;
    dicom_stl::vector<dicom_stl::string>& v_ref = (*iter).second;
366
             
367
    for (dicom_stl::vector<dicom_stl::string>::iterator v_iter = v_ref.begin();
368
369
370
         v_iter != v_ref.end();
         v_iter++)
      {
371
      dicom_stl::map<dicom_stl::string, DICOMOrderingElements, ltstdstr>::iterator sn_iter = Implementation->SliceOrderingMap.find(*v_iter);
372
373

      int slice = -1;
374
      if (sn_iter != Implementation->SliceOrderingMap.end())
375
        {
Matt Turek's avatar
Matt Turek committed
376
        slice = (*sn_iter).second.SliceNumber;
377
        }
378
      dicom_stream::cout << "\t" << (*v_iter).c_str() << " [" << slice << "]" <<  dicom_stream::endl;
379
380
381
382
383
384
      }
                
    }
}


Matt Turek's avatar
Matt Turek committed
385
386
void DICOMAppHelper::ArrayCallback(DICOMParser *parser,
                                   doublebyte group,
387
388
389
390
391
                                   doublebyte element,
                                   DICOMParser::VRTypes datatype,
                                   unsigned char* val,
                                   quadbyte len) 
{
392
  const char* desc = "No description";
393
  
394
395
  TagMapType::iterator iter = this->Implementation->TagMap.find(dicom_stl::pair<doublebyte, doublebyte> (group, element));
  if (iter != this->Implementation->TagMap.end())
396
397
398
399
400
401
402
    {
    desc = (*iter).second.description;
    }

  int t2 = int((0x0000FF00 & datatype) >> 8);
  int t1 = int((0x000000FF & datatype));

Matt Turek's avatar
Matt Turek committed
403
404
  char ct2(t2);
  char ct1(t1);
405
406
407
408
409
410
411

    
  HeaderFile << "(0x";

  HeaderFile.width(4);
  char prev = HeaderFile.fill('0');

412
  HeaderFile << dicom_stream::hex << group;
413
414
415
416
417
  HeaderFile << ",0x";

  HeaderFile.width(4);
  HeaderFile.fill('0');
    
418
  HeaderFile << dicom_stream::hex << element;
419
420
421
  HeaderFile << ") ";

  HeaderFile.fill(prev);
422
  HeaderFile << dicom_stream::dec;
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
  HeaderFile << " " << ct1 << ct2 << " ";
  HeaderFile << "[" << len << " bytes] ";
  
  HeaderFile << desc << " : ";
  
  unsigned int uival = 0;
  float fval = 0;
  double dval = 0;
  int ival = 0;

  if (val)
    {
    switch (datatype)
      {
      case DICOMParser::VR_AE:
      case DICOMParser::VR_AS:
      case DICOMParser::VR_CS:
      case DICOMParser::VR_UI:
      case DICOMParser::VR_DA:
      case DICOMParser::VR_DS:
      case DICOMParser::VR_DT:
      case DICOMParser::VR_LO:
      case DICOMParser::VR_LT:
      case DICOMParser::VR_OB: // ordered bytes
      case DICOMParser::VR_OW: // ordered words
      case DICOMParser::VR_PN:
      case DICOMParser::VR_ST:
      case DICOMParser::VR_TM:
      case DICOMParser::VR_UN:
      case DICOMParser::VR_UT:
      case DICOMParser::VR_SQ: // sequence
      case DICOMParser::VR_SH: // strings
      case DICOMParser::VR_IS:
        HeaderFile << val;
        break;
      case DICOMParser::VR_FL: // float
        fval = static_cast<float> (atof((char*) val));
        HeaderFile << fval;
        break;
      case DICOMParser::VR_FD: // float double
        fval = static_cast<float> (atof((char*) val));
        HeaderFile << dval;
        break;
      case DICOMParser::VR_UL: // unsigned long
      case DICOMParser::VR_SL: // signed long
      case DICOMParser::VR_AT:
        HeaderFile << uival;
        break;
      //case DICOMParser::VR_IS:
Matt Turek's avatar
Matt Turek committed
472
      //  ival = DICOMFile::ReturnAsSignedLong(val, parser->GetDICOMFile()->GetPlatformIsBigEndian()); 
473
474
475
      //  HeaderFile << ival;
      //  break;
      case DICOMParser::VR_SS:
Matt Turek's avatar
Matt Turek committed
476
        ival = DICOMFile::ReturnAsSignedShort(val, parser->GetDICOMFile()->GetPlatformIsBigEndian()); 
477
478
479
        HeaderFile << ival;
        break;
      case DICOMParser::VR_US: // unsigned short
Matt Turek's avatar
Matt Turek committed
480
        uival = DICOMFile::ReturnAsUnsignedShort(val, parser->GetDICOMFile()->GetPlatformIsBigEndian()); 
481
482
483
        HeaderFile << uival;
        break;
      default:
484
        HeaderFile << val << dicom_stream::endl;
485
486
487
488
489
490
491
492
        break;
      }
    }
  else
    {
    HeaderFile << "NULL";
    }
  
493
  HeaderFile << dicom_stream::dec << dicom_stream::endl;
494
495
496
497
498
  HeaderFile.fill(prev);

  delete [] val;
}
    
Matt Turek's avatar
Matt Turek committed
499
500
void DICOMAppHelper::SliceNumberCallback(DICOMParser *parser,
                                         doublebyte,
501
502
503
504
505
                                         doublebyte,
                                         DICOMParser::VRTypes,
                                         unsigned char* val,
                                         quadbyte) 
{
Matt Turek's avatar
Matt Turek committed
506
  // Look for the current file in the map of slice ordering data
507
508
509
  dicom_stl::map<dicom_stl::string, DICOMOrderingElements, ltstdstr>::iterator it;
  it = this->Implementation->SliceOrderingMap.find(parser->GetFileName());
  if (it == Implementation->SliceOrderingMap.end())
Matt Turek's avatar
Matt Turek committed
510
511
512
513
    {
    // file not found, create a new entry
    DICOMOrderingElements ord;
    ord.SliceNumber = atoi( (char *) val);
514

Matt Turek's avatar
Matt Turek committed
515
    // insert into the map
516
    this->Implementation->SliceOrderingMap.insert(dicom_stl::pair<const dicom_stl::string, DICOMOrderingElements>(parser->GetFileName(), ord));
Matt Turek's avatar
Matt Turek committed
517
518
519
520
521
522
    }
  else
    {
    // file found, add new values
    (*it).second.SliceNumber = atoi( (char *)val );
    }
523

Matt Turek's avatar
Matt Turek committed
524
525
  // cache the slice number
  this->SliceNumber = atoi( (char *) val);
526
527
}

Matt Turek's avatar
Matt Turek committed
528
529
530
531
532
533
534
535
536

void DICOMAppHelper::SliceLocationCallback(DICOMParser *parser,
                                           doublebyte,
                                           doublebyte,
                                           DICOMParser::VRTypes,
                                           unsigned char* val,
                                           quadbyte) 
{
  // Look for the current file in the map of slice ordering data
537
538
539
  dicom_stl::map<dicom_stl::string, DICOMOrderingElements, ltstdstr>::iterator it;
  it = this->Implementation->SliceOrderingMap.find(parser->GetFileName());
  if (it == Implementation->SliceOrderingMap.end())
Matt Turek's avatar
Matt Turek committed
540
541
542
543
544
545
    {
    // file not found, create a new entry
    DICOMOrderingElements ord;
    ord.SliceLocation = (float)atof( (char *) val);

    // insert into the map
546
547
    this->Implementation->SliceOrderingMap.insert(dicom_stl::pair<const dicom_stl::string,
      DICOMOrderingElements>(parser->GetFileName(), ord));
Matt Turek's avatar
Matt Turek committed
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
    }
  else
    {
    // file found, add new values
    (*it).second.SliceLocation = (float)atof( (char *)val );
    }
}

void DICOMAppHelper::ImagePositionPatientCallback(DICOMParser *parser,
                                                  doublebyte,
                                                  doublebyte,
                                                  DICOMParser::VRTypes,
                                                  unsigned char* val,
                                                  quadbyte) 
{
  // Look for the current file in the map of slice ordering data
564
565
566
  dicom_stl::map<dicom_stl::string, DICOMOrderingElements, ltstdstr>::iterator it;
  it = this->Implementation->SliceOrderingMap.find(parser->GetFileName());
  if (it == Implementation->SliceOrderingMap.end())
Matt Turek's avatar
Matt Turek committed
567
568
569
    {
    // file not found, create a new entry
    DICOMOrderingElements ord;
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584

    if (val)
      {
      sscanf( (char*)(val), "%f\\%f\\%f",
              &ord.ImagePositionPatient[0],
              &ord.ImagePositionPatient[1],
              &ord.ImagePositionPatient[2] );
      }
    else
      {
      // no actual position specified, default to the origin
      ord.ImagePositionPatient[0] = 0.0;
      ord.ImagePositionPatient[1] = 0.0;
      ord.ImagePositionPatient[2] = 0.0;
      }
Matt Turek's avatar
Matt Turek committed
585
586
    
    // insert into the map
587
588
    this->Implementation->SliceOrderingMap.insert(dicom_stl::pair<const dicom_stl::string, 
      DICOMOrderingElements>(parser->GetFileName(), ord));
Matt Turek's avatar
Matt Turek committed
589
590
591
592
593
594
595

    // cache the value
    memcpy( this->ImagePositionPatient, ord.ImagePositionPatient,
            3*sizeof(float) );
    }
  else
    {
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
    if (val)
      {
      // file found, add new values
      sscanf( (char*)(val), "%f\\%f\\%f",
              &(*it).second.ImagePositionPatient[0],
              &(*it).second.ImagePositionPatient[1],
              &(*it).second.ImagePositionPatient[2] );
      }
    else
      {
      // no actual position specified, default to the origin
      (*it).second.ImagePositionPatient[0] = 0.0;
      (*it).second.ImagePositionPatient[1] = 0.0;
      (*it).second.ImagePositionPatient[2] = 0.0;
      }
    
Matt Turek's avatar
Matt Turek committed
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
    // cache the value
    memcpy( this->ImagePositionPatient, (*it).second.ImagePositionPatient,
            3*sizeof(float) );
    }
}


void DICOMAppHelper::ImageOrientationPatientCallback(DICOMParser *parser,
                                                     doublebyte,
                                                     doublebyte,
                                                     DICOMParser::VRTypes,
                                                     unsigned char* val,
                                                     quadbyte) 
{
  // Look for the current file in the map of slice ordering data
627
628
629
  dicom_stl::map<dicom_stl::string, DICOMOrderingElements, ltstdstr>::iterator it;
  it = this->Implementation->SliceOrderingMap.find(parser->GetFileName());
  if (it == Implementation->SliceOrderingMap.end())
Matt Turek's avatar
Matt Turek committed
630
631
632
    {
    // file not found, create a new entry
    DICOMOrderingElements ord;
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
    if (val)
      {
      sscanf( (char*)(val), "%f\\%f\\%f\\%f\\%f\\%f",
              &ord.ImageOrientationPatient[0],
              &ord.ImageOrientationPatient[1],
              &ord.ImageOrientationPatient[2],
              &ord.ImageOrientationPatient[3],
              &ord.ImageOrientationPatient[4],
              &ord.ImageOrientationPatient[5] );
      }
    else
      {
      // no orientation defined, default to an standard axial orientation
      ord.ImageOrientationPatient[0] = 1.0;
      ord.ImageOrientationPatient[1] = 0.0;
      ord.ImageOrientationPatient[2] = 0.0;
      ord.ImageOrientationPatient[3] = 0.0;
      ord.ImageOrientationPatient[4] = 1.0;
      ord.ImageOrientationPatient[5] = 0.0;
      }
Matt Turek's avatar
Matt Turek committed
653
654
    
    // insert into the map
655
656
657
658
659
660
    this->Implementation->SliceOrderingMap.insert(dicom_stl::pair<const dicom_stl::string,
      DICOMOrderingElements>(parser->GetFileName(), ord));

    // cache the value
    memcpy( this->ImageOrientationPatient, ord.ImageOrientationPatient,
            6*sizeof(float) );
Matt Turek's avatar
Matt Turek committed
661
662
663
664
    }
  else
    {
    // file found, add new values
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
    if (val)
      {
      sscanf( (char*)(val), "%f\\%f\\%f\\%f\\%f\\%f",
              &(*it).second.ImageOrientationPatient[0],
              &(*it).second.ImageOrientationPatient[1],
              &(*it).second.ImageOrientationPatient[2],
              &(*it).second.ImageOrientationPatient[3],
              &(*it).second.ImageOrientationPatient[4],
              &(*it).second.ImageOrientationPatient[5] );
      }
    else
      {
      // no orientation defined, default to an standard axial orientation
      (*it).second.ImageOrientationPatient[0] = 1.0;
      (*it).second.ImageOrientationPatient[1] = 0.0;
      (*it).second.ImageOrientationPatient[2] = 0.0;
      (*it).second.ImageOrientationPatient[3] = 0.0;
      (*it).second.ImageOrientationPatient[4] = 1.0;
      (*it).second.ImageOrientationPatient[5] = 0.0;
      }
685
686
687
688

    // cache the value
    memcpy( this->ImageOrientationPatient, (*it).second.ImageOrientationPatient,
            6*sizeof(float) );
Matt Turek's avatar
Matt Turek committed
689
690
691
692
693
694
    }
}


void DICOMAppHelper::TransferSyntaxCallback(DICOMParser *parser,
                                            doublebyte,
695
696
697
698
699
700
701
702
703
704
705
706
                                            doublebyte,
                                            DICOMParser::VRTypes,
                                            unsigned char* val,
                                            quadbyte) 
{

#ifdef DEBUG_DICOM_APP_HELPER
#ifdef WIN32
  char platformByteOrder = 'L';
#else
  char platformByteOrder = 'B';
#endif
707
  dicom_stream::cout << "Platform byte order: " << platformByteOrder << dicom_stream::endl;
708
709
#endif

710
  static const char* TRANSFER_UID_EXPLICIT_BIG_ENDIAN = "1.2.840.10008.1.2.2";
711

712
  // Only add the ToggleSwapBytes callback when we need it.
713
714
715
  if (strcmp(TRANSFER_UID_EXPLICIT_BIG_ENDIAN, (char*) val) == 0)
    {
    this->ByteSwapData = true;
716
    parser->AddDICOMTagCallback(0x0800, 0x0000, DICOMParser::VR_UNKNOWN, ToggleSwapBytesCB);
717
#ifdef DEBUG_DICOM_APP_HELPER
718
    dicom_stream::cerr <<"Registering callback for swapping bytes." << dicom_stream::endl;
719
720
721
722
723
724
725
#endif
    }
  
  if (this->TransferSyntaxUID)
    {
    delete this->TransferSyntaxUID;
    }
726
  this->TransferSyntaxUID = new dicom_stl::string((char*) val);
727
728

#ifdef DEBUG_DICOM_APP_HELPER
729
730
  dicom_stream::cout << "Transfer Syntax UID: " << *this->TransferSyntaxUID;
  dicom_stream::cout << " " << this->TransferSyntaxUIDDescription(this->TransferSyntaxUID->c_str()) << dicom_stream::endl;
731
732
733
#endif
}

Matt Turek's avatar
Matt Turek committed
734
735
void DICOMAppHelper::BitsAllocatedCallback(DICOMParser *parser,
                                           doublebyte,
736
737
738
739
740
                                           doublebyte,
                                           DICOMParser::VRTypes,
                                           unsigned char* val,
                                           quadbyte) 
{
Matt Turek's avatar
Matt Turek committed
741
  this->BitsAllocated = parser->GetDICOMFile()->ReturnAsUnsignedShort(val, parser->GetDICOMFile()->GetPlatformIsBigEndian());
742
#ifdef DEBUG_DICOM_APP_HELPER
743
  dicom_stream::cout << "Bits allocated: " << this->BitsAllocated << dicom_stream::endl;
744
745
746
747
#endif
}


Matt Turek's avatar
Matt Turek committed
748
749
void DICOMAppHelper::ToggleSwapBytesCallback(DICOMParser *parser,
                                             doublebyte,
750
751
752
753
754
755
                                             doublebyte,
                                             DICOMParser::VRTypes,
                                             unsigned char* ,
                                             quadbyte len) 
{
#ifdef DEBUG_DICOM_APP_HELPER
756
  dicom_stream::cout << "ToggleSwapBytesCallback" << dicom_stream::endl;
757
#endif
Matt Turek's avatar
Matt Turek committed
758
759
  bool bs = parser->GetDICOMFile()->GetPlatformIsBigEndian();
  parser->GetDICOMFile()->SetPlatformIsBigEndian(!bs);
760
761

#ifdef DEBUG_DICOM_APP_HELPER
762
  dicom_stream::cout << "Set byte swap to: " << parser->GetDICOMFile()->GetPlatformIsBigEndian() << dicom_stream::endl;
763
764
#endif

Matt Turek's avatar
Matt Turek committed
765
  long pos = parser->GetDICOMFile()->Tell();
766
767
768
769

  //
  // The +4 is probably a hack, but it's a guess at the length of the previous field.
  //
Matt Turek's avatar
Matt Turek committed
770
  parser->GetDICOMFile()->SkipToPos(pos - len + 4);
771
772
773
}


Matt Turek's avatar
Matt Turek committed
774
775
void DICOMAppHelper::PixelSpacingCallback(DICOMParser *parser,
                                          doublebyte group,
776
777
778
779
780
781
782
                                          doublebyte element,
                                          DICOMParser::VRTypes,
                                          unsigned char* val,
                                          quadbyte) 
{
  if (group == 0x0028 && element == 0x0030)
    {
783
784
785
786
787
788
    if (!val || sscanf((char*)(val), "%f\\%f",
                       &this->PixelSpacing[0],
                       &this->PixelSpacing[1]) != 2)
      {
      this->PixelSpacing[0] = this->PixelSpacing[1] = 0.0;
      }
789
790
791
    }
  else if (group == 0x0018 && element == 0x0050)
    {
792
793
794
    this->PixelSpacing[2] = 
      DICOMFile::ReturnAsFloat(
        val, parser->GetDICOMFile()->GetPlatformIsBigEndian());
795
796
797
    }
}

Matt Turek's avatar
Matt Turek committed
798
799
void DICOMAppHelper::WidthCallback(DICOMParser *parser,
                                   doublebyte,
800
801
802
803
804
                                   doublebyte,
                                   DICOMParser::VRTypes,
                                   unsigned char* val,
                                   quadbyte)
{
Matt Turek's avatar
Matt Turek committed
805
  unsigned short uival = DICOMFile::ReturnAsUnsignedShort(val, parser->GetDICOMFile()->GetPlatformIsBigEndian()); 
806
#ifdef DEBUG_DICOM_APP_HELPER
807
  dicom_stream::cout << "Width: " << uival << dicom_stream::endl;
808
809
810
811
812
813
#endif

  this->Width = uival;
  this->Dimensions[0] = this->Width;
}

Matt Turek's avatar
Matt Turek committed
814
815
void DICOMAppHelper::HeightCallback(DICOMParser *parser,
                                    doublebyte,
816
817
818
819
820
                                    doublebyte,
                                    DICOMParser::VRTypes,
                                    unsigned char* val,
                                    quadbyte) 
{
Matt Turek's avatar
Matt Turek committed
821
  unsigned short uival = DICOMFile::ReturnAsUnsignedShort(val, parser->GetDICOMFile()->GetPlatformIsBigEndian()); 
822
#ifdef DEBUG_DICOM_APP_HELPER
823
  dicom_stream::cout << "Height: " << uival << dicom_stream::endl;
824
825
826
827
828
829
#endif
  this->Height = uival;
  this->Dimensions[1] = this->Height;
}


Matt Turek's avatar
Matt Turek committed
830
831
void DICOMAppHelper::PixelRepresentationCallback( DICOMParser *parser,
                                                  doublebyte,
832
833
834
835
836
                                                  doublebyte,
                                                  DICOMParser::VRTypes,
                                                  unsigned char* val,
                                                  quadbyte)
{
Matt Turek's avatar
Matt Turek committed
837
  unsigned short uival = DICOMFile::ReturnAsUnsignedShort(val, parser->GetDICOMFile()->GetPlatformIsBigEndian());
838
#ifdef DEBUG_DICOM_APP_HELPER
839
  dicom_stream::cout << "Pixel Representation: " << (uival ? "Signed" : "Unsigned") << dicom_stream::endl;
840
841
842
843
#endif
  this->PixelRepresentation = uival;
}

Matt Turek's avatar
Matt Turek committed
844
void DICOMAppHelper::PhotometricInterpretationCallback( DICOMParser *,
Matt Turek's avatar
Matt Turek committed
845
                                                        doublebyte,
846
847
848
849
850
851
                                                        doublebyte,
                                                        DICOMParser::VRTypes,
                                                        unsigned char* val,
                                                        quadbyte)
{
#ifdef DEBUG_DICOM_APP_HELPER
852
  dicom_stream::cout << "Photometric Interpretation: " << (char*) val << dicom_stream::endl;
853
854
855
856
857
858
#endif
  if (this->PhotometricInterpretation)
    {
    delete this->PhotometricInterpretation;
    }

859
  this->PhotometricInterpretation = new dicom_stl::string((char*) val);
860
861
}

Matt Turek's avatar
Matt Turek committed
862
void DICOMAppHelper::PixelDataCallback( DICOMParser *,
Matt Turek's avatar
Matt Turek committed
863
                                        doublebyte,
864
865
866
867
868
                                        doublebyte,
                                        DICOMParser::VRTypes,
                                        unsigned char* data,
                                        quadbyte len)
{
869
  int numPixels = this->Dimensions[0] * this->Dimensions[1] * this->GetNumberOfComponents();
870
871
872
873
874
875
876
877
878
879
  if (len < numPixels)
    {
    numPixels = len;
    }
  if (numPixels < 0)
    {
    numPixels = 0;
    }

#ifdef DEBUG_DICOM_APP_HELPER
880
  dicom_stream::cout << "numPixels : " << numPixels << dicom_stream::endl;
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
#endif

  int ptrIncr = int(this->BitsAllocated/8.0);

  unsigned short* ushortInputData = reinterpret_cast<unsigned short*>(data);
  unsigned char* ucharInputData = data;
  short* shortInputData = reinterpret_cast<short*> (data);

  float* floatOutputData; // = NULL;
  
  bool isFloat = this->RescaledImageDataIsFloat();

  if (isFloat)
    {
#ifdef DEBUG_DICOM_APP_HELPER
896
897
    dicom_stream::cout << "Slope and offset are not integer valued : ";
    dicom_stream::cout << this->RescaleSlope << ", " << this->RescaleOffset << dicom_stream::endl;
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
#endif
    if (this->ImageData)
      {
      delete [] (static_cast<char*> (this->ImageData));
      }
    this->ImageData = new float[numPixels];
    floatOutputData = static_cast<float*> (this->ImageData);

    this->ImageDataType = DICOMParser::VR_FL;
    this->ImageDataLengthInBytes = numPixels * sizeof(float);
    float newFloatPixel;

    if (ptrIncr == 1)
      {
      for (int i = 0; i < numPixels; i++)
        {
        newFloatPixel = float(this->RescaleSlope * ucharInputData[i] + this->RescaleOffset);
        floatOutputData[i] = newFloatPixel;
        }
#ifdef DEBUG_DICOM_APP_HELPER
918
919
      dicom_stream::cout << "Did rescale, offset to float from char." << dicom_stream::endl;
      dicom_stream::cout << numPixels << " pixels." << dicom_stream::endl;
920
921
922
923
924
925
926
927
928
929
#endif
      }
    else if (ptrIncr == 2)
      {
      for (int i = 0; i < numPixels; i++)
        {
        newFloatPixel = float(this->RescaleSlope * ushortInputData[i] + this->RescaleOffset);
        floatOutputData[i] = newFloatPixel;
        }
#ifdef DEBUG_DICOM_APP_HELPER
930
931
      dicom_stream::cout << "Did rescale, offset to float from short." << dicom_stream::endl;
      dicom_stream::cout << numPixels << " pixels." << dicom_stream::endl;
932
933
934
935
936
937
#endif
      }
    }
  else
    {
#ifdef DEBUG_DICOM_APP_HELPER
938
939
    dicom_stream::cout << "Slope and offset are integer valued : ";
    dicom_stream::cout << this->RescaleSlope << ", " << this->RescaleOffset << dicom_stream::endl;
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
#endif

    if (ptrIncr == 1)
      {
      if (this->ImageData)
        {
        delete [] (static_cast<char*> (this->ImageData));
        }
      this->ImageData = new char[numPixels];
  
      char*  charOutputData =  static_cast<char*>  (this->ImageData);

      this->ImageDataType = DICOMParser::VR_OB;
      this->ImageDataLengthInBytes = numPixels * sizeof(char);
      char newCharPixel;

      for (int i = 0; i < numPixels; i++)
        {
        newCharPixel = char(this->RescaleSlope * ucharInputData[i] + this->RescaleOffset);
        charOutputData[i] = newCharPixel;
        }
#ifdef DEBUG_DICOM_APP_HELPER
962
963
      dicom_stream::cout << "Did rescale, offset to char from char." << dicom_stream::endl;
      dicom_stream::cout << numPixels << " pixels." << dicom_stream::endl;
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
#endif
      }
    else if (ptrIncr == 2)
      {
      if (this->ImageData)
        {
        delete [] (static_cast<char*> (this->ImageData));
        }
      this->ImageData = new short[numPixels];
      short* shortOutputData = static_cast<short*> (this->ImageData);

      this->ImageDataType = DICOMParser::VR_OW;
      this->ImageDataLengthInBytes = numPixels * sizeof(short);
      short newShortPixel;
      for (int i = 0; i < numPixels; i++)
        {
        newShortPixel = short(this->RescaleSlope * shortInputData[i] + this->RescaleOffset);
        shortOutputData[i] = newShortPixel;
        }
#ifdef DEBUG_DICOM_APP_HELPER
984
985
      dicom_stream::cout << "Did rescale, offset to short from short." << dicom_stream::endl;
      dicom_stream::cout << numPixels << " pixels." << dicom_stream::endl;
986
987
988
989
990
#endif
      }
    }
}

Matt Turek's avatar
Matt Turek committed
991
void DICOMAppHelper::RegisterPixelDataCallback(DICOMParser* parser)
992
993
{
  this->PixelDataCB->SetCallbackFunction(this, &DICOMAppHelper::PixelDataCallback);
Matt Turek's avatar
Matt Turek committed
994
  parser->AddDICOMTagCallback(0x7FE0, 0x0010, DICOMParser::VR_OW, this->PixelDataCB);
995
996
}

Matt Turek's avatar
Matt Turek committed
997
998
999
1000
1001
1002
void DICOMAppHelper::RescaleOffsetCallback( DICOMParser *parser,
                                            doublebyte,
                                            doublebyte,
                                            DICOMParser::VRTypes,
                                            unsigned char* val,
                                            quadbyte)
1003
{
Matt Turek's avatar
Matt Turek committed
1004
  float fval = DICOMFile::ReturnAsFloat(val, parser->GetDICOMFile()->GetPlatformIsBigEndian());
1005
1006
  this->RescaleOffset = fval;
#ifdef DEBUG_DICOM_APP_HELPER
1007
  dicom_stream::cout << "Pixel offset: " << this->RescaleOffset << dicom_stream::endl;
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
#endif
}

const char* DICOMAppHelper::TransferSyntaxUIDDescription(const char* uid)
{
  static const char* DICOM_IMPLICIT_VR_LITTLE_ENDIAN = "1.2.840.10008.1.2";
  static const char* DICOM_LOSSLESS_JPEG = "1.2.840.10008.1.2.4.70";
  static const char* DICOM_LOSSY_JPEG_8BIT = "1.2.840.10008.1.2.4.50";
  static const char* DICOM_LOSSY_JPEG_16BIT = "1.2.840.10008.1.2.4.51";
  static const char* DICOM_EXPLICIT_VR_LITTLE_ENDIAN = "1.2.840.10008.1.2.1";
  static const char* DICOM_EXPLICIT_VR_BIG_ENDIAN = "1.2.840.10008.1.2.2";
  static const char* DICOM_GE_PRIVATE_IMPLICIT_BIG_ENDIAN = "1.2.840.113619.5.2";

  if (!strcmp(DICOM_IMPLICIT_VR_LITTLE_ENDIAN, uid))
    {
    return "Implicit VR, Little Endian";
    }
  else if (!strcmp(DICOM_LOSSLESS_JPEG, uid))
    {
    return "Lossless JPEG";
    }
  else if (!strcmp(DICOM_LOSSY_JPEG_8BIT, uid))
    {
    return "Lossy JPEG 8 bit";
    }
  else if (!strcmp(DICOM_LOSSY_JPEG_16BIT, uid))
    {
    return "Lossy JPEG 16 bit.";
    }
  else if (!strcmp(DICOM_EXPLICIT_VR_LITTLE_ENDIAN, uid))
    {
    return "Explicit VR, Little Endian.";
    }
  else if (!strcmp(DICOM_EXPLICIT_VR_BIG_ENDIAN, uid))
    {
    return "Explicit VR, Big Endian.";
    }
  else if (!strcmp(DICOM_GE_PRIVATE_IMPLICIT_BIG_ENDIAN, uid))
    {
    return "GE Private, Implicit VR, Big Endian Image Data.";
    }
  else
    {
    return "Unknown.";
    }

}


Matt Turek's avatar
Matt Turek committed
1057
1058
1059
1060
1061
1062
void DICOMAppHelper::RescaleSlopeCallback(DICOMParser *parser,
                                          doublebyte,
                                          doublebyte ,
                                          DICOMParser::VRTypes ,
                                          unsigned char* val,
                                          quadbyte )
1063
{
Matt Turek's avatar
Matt Turek committed
1064
1065
  float fval = DICOMFile::ReturnAsFloat(val,
                                        parser->GetDICOMFile()->GetPlatformIsBigEndian ());
1066
#ifdef DEBUG_DICOM_APP_HELPER
1067
  dicom_stream::cout << "Rescale slope: " << fval << dicom_stream::endl;
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
#endif
  this->RescaleSlope = fval;
}

bool DICOMAppHelper::RescaledImageDataIsFloat()
{
  int s = int(this->RescaleSlope);
  int o = int(this->RescaleOffset);

  float sf = float(s);
  float of = float(o);

  double d1 = fabs(sf - this->RescaleSlope);
  double d2 = fabs(of - this->RescaleOffset);

  if (d1 > 0.0 || d2 > 0.0)
    {
    return true;
    }
  else
    {
    return false;
    }
}

void DICOMAppHelper::GetImageData(void*& data, DICOMParser::VRTypes& dataType, unsigned long& len)
{
  data = this->ImageData;
  dataType = this->ImageDataType;
  len = this->ImageDataLengthInBytes;
}

bool DICOMAppHelper::RescaledImageDataIsSigned()
{
  bool rescaleSigned = (this->RescaleSlope < 0.0);
  bool pixelRepSigned = (this->PixelRepresentation == 1);
  bool offsetSigned = (this->RescaleOffset < 0.0);
 
  return (rescaleSigned || pixelRepSigned || offsetSigned);
}


1110
void DICOMAppHelper::GetSliceNumberFilenamePairs(const dicom_stl::string &seriesUID,
1111
                                                 dicom_stl::vector<dicom_stl::pair<int, dicom_stl::string> >& v, bool ascending)
1112
1113
1114
{
  v.clear();

1115
  dicom_stl::map<dicom_stl::string, dicom_stl::vector<dicom_stl::string>, ltstdstr >::iterator miter  = this->Implementation->SeriesUIDMap.find(seriesUID);
1116

1117
  if (miter == this->Implementation->SeriesUIDMap.end() )
Matt Turek's avatar
Matt Turek committed
1118
1119
1120
1121
1122
    {
    return;
    }

  // grab the filenames for the specified series
1123
  dicom_stl::vector<dicom_stl::string> files = (*miter).second;
1124

1125
  for (dicom_stl::vector<dicom_stl::string>::iterator fileIter = files.begin();
1126
1127
1128
       fileIter != files.end();
       fileIter++)
       {
1129
1130
1131
1132
       dicom_stl::pair<int, dicom_stl::string> p;
       p.second = dicom_stl::string(*fileIter);
       int slice_number;
       dicom_stl::map<dicom_stl::string, DICOMOrderingElements, ltstdstr>::iterator sn_iter = Implementation->SliceOrderingMap.find(*fileIter);
Matt Turek's avatar
Matt Turek committed
1133
       // Only store files that have a valid slice number
1134
       if (sn_iter != Implementation->SliceOrderingMap.end())
1135
        {
Matt Turek's avatar
Matt Turek committed
1136
1137
1138
        slice_number = (*sn_iter).second.SliceNumber;
        p.first = slice_number;
        v.push_back(p);
1139
1140
        }
       }
1141
1142
1143
1144
1145
1146
1147
1148
  if (ascending)
    {    
    dicom_stl::sort(v.begin(), v.end(), lt_pair_int_string());
    }
  else
    {
    dicom_stl::sort(v.begin(), v.end(), gt_pair_int_string());
    }
1149
1150
}

1151
void DICOMAppHelper::GetSliceNumberFilenamePairs(dicom_stl::vector<dicom_stl::pair<int, dicom_stl::string> >& v, bool ascending)
1152
{
Matt Turek's avatar
Matt Turek committed
1153
  // Default to using the first series
1154
  if (this->Implementation->SeriesUIDMap.size() > 0)
Matt Turek's avatar
Matt Turek committed
1155
    {
1156
    this->GetSliceNumberFilenamePairs( (*this->Implementation->SeriesUIDMap.begin()).first, v, ascending );
Matt Turek's avatar
Matt Turek committed
1157
1158
1159
1160
1161
    }
  else
    {
    v.clear();
    }
1162
1163
}

1164
void DICOMAppHelper::GetSliceLocationFilenamePairs(const dicom_stl::string &seriesUID,
1165
                                                   dicom_stl::vector<dicom_stl::pair<float, dicom_stl::string> >& v, bool ascending)
1166
{
Matt Turek's avatar
Matt Turek committed
1167
  v.clear();
1168

1169
  dicom_stl::map<dicom_stl::string, dicom_stl::vector<dicom_stl::string>, ltstdstr >::iterator miter  = this->Implementation->SeriesUIDMap.find(seriesUID);
1170

1171
  if (miter == this->Implementation->SeriesUIDMap.end() )
Matt Turek's avatar
Matt Turek committed
1172
1173
1174
1175
1176
    {
    return;
    }

  // grab the filenames for the specified series
1177
  dicom_stl::vector<dicom_stl::string> files = (*miter).second;
1178

1179
  for (dicom_stl::vector<dicom_stl::string>::iterator fileIter = files.begin();
1180
1181
       fileIter != files.end();
       fileIter++)
1182
       {
1183
1184
1185
1186
       dicom_stl::pair<float, dicom_stl::string> p;
       p.second = dicom_stl::string(*fileIter);
       float slice_location;
       dicom_stl::map<dicom_stl::string, DICOMOrderingElements, ltstdstr>::iterator sn_iter = Implementation->SliceOrderingMap.find(*fileIter);
Matt Turek's avatar
Matt Turek committed
1187

1188
       if (sn_iter != Implementation->SliceOrderingMap.end())
1189
        {
Matt Turek's avatar
Matt Turek committed
1190
1191
1192
        slice_location = (*sn_iter).second.SliceLocation;
        p.first = slice_location;
        v.push_back(p);
1193
        }
1194
       }
1195
1196
1197
1198
1199
1200
1201
1202
  if (ascending)
    {
    dicom_stl::sort(v.begin(), v.end(), lt_pair_float_string());
    }
  else
    {
    dicom_stl::sort(v.begin(), v.end(), gt_pair_float_string());
    }
1203
1204
}

1205
void DICOMAppHelper::GetSliceLocationFilenamePairs(dicom_stl::vector<dicom_stl::pair<float, dicom_stl::string> >& v, bool ascending)
1206
{
Matt Turek's avatar
Matt Turek committed
1207
  // Default to using the first series
1208
  if (this->Implementation->SeriesUIDMap.size() > 0)
Matt Turek's avatar
Matt Turek committed
1209
    {
1210
    this->GetSliceLocationFilenamePairs( (*this->Implementation->SeriesUIDMap.begin()).first,
1211
                                         v , ascending);
Matt Turek's avatar
Matt Turek committed
1212
1213
1214
1215
1216
    }
  else
    {
    v.clear();
    }
1217
1218
}

1219
void DICOMAppHelper::GetImagePositionPatientFilenamePairs(const dicom_stl::string &seriesUID, dicom_stl::vector<dicom_stl::pair<float, dicom_stl::string> >& v, bool ascending)
1220
{
Matt Turek's avatar
Matt Turek committed
1221
1222
  v.clear();

1223
  dicom_stl::map<dicom_stl::string, dicom_stl::vector<dicom_stl::string>, ltstdstr >::iterator miter  = this->Implementation->SeriesUIDMap.find(seriesUID);
Matt Turek's avatar
Matt Turek committed
1224

1225
  if (miter == this->Implementation->SeriesUIDMap.end() )
1226
    {
Matt Turek's avatar
Matt Turek committed
1227
    return;
1228
    }
Matt Turek's avatar
Matt Turek committed
1229
1230

  // grab the filenames for the specified series
1231
  dicom_stl::vector<dicom_stl::string> files = (*miter).second;
Matt Turek's avatar
Matt Turek committed
1232

1233
  for (dicom_stl::vector<dicom_stl::string>::iterator fileIter = files.begin();
Matt Turek's avatar
Matt Turek committed
1234
1235
1236
       fileIter != files.end();
       fileIter++)
       {
1237
1238
       dicom_stl::pair<float, dicom_stl::string> p;
       p.second = dicom_stl::string(*fileIter);
Matt Turek's avatar
Matt Turek committed
1239

1240
       float image_position;
Matt Turek's avatar
Matt Turek committed
1241
1242
       float normal[3];
       
1243
1244
       dicom_stl::map<dicom_stl::string, DICOMOrderingElements, ltstdstr>::iterator sn_iter = 
         Implementation->SliceOrderingMap.find(*fileIter);
Matt Turek's avatar
Matt Turek committed
1245

1246
       if (sn_iter != Implementation->SliceOrderingMap.end())
Matt Turek's avatar
Matt Turek committed
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
        {
        // compute the image patient position wrt to the slice image
        // plane normal

        normal[0] = ((*sn_iter).second.ImageOrientationPatient[1]
                     * (*sn_iter).second.ImageOrientationPatient[5])
          - ((*sn_iter).second.ImageOrientationPatient[2]
             * (*sn_iter).second.ImageOrientationPatient[4]);
        normal[1] = ((*sn_iter).second.ImageOrientationPatient[0]
                     *(*sn_iter).second.ImageOrientationPatient[5])
          - ((*sn_iter).second.ImageOrientationPatient[2]
             * (*sn_iter).second.ImageOrientationPatient[3]);
        normal[2] = ((*sn_iter).second.ImageOrientationPatient[0]
                     * (*sn_iter).second.ImageOrientationPatient[4])
          - ((*sn_iter).second.ImageOrientationPatient[1]
             * (*sn_iter).second.ImageOrientationPatient[3]);
        
        image_position = (normal[0]*(*sn_iter).second.ImagePositionPatient[0])
          + (normal[1]*(*sn_iter).second.ImagePositionPatient[1])
          + (normal[2]*(*sn_iter).second.ImagePositionPatient[2]);
        p.first = image_position;
        v.push_back(p);
        }
       }
1271
1272
1273
1274
1275
1276
1277
1278
  if (ascending)
    {
    dicom_stl::sort(v.begin(), v.end(), lt_pair_float_string());
    }
  else
    {
    dicom_stl::sort(v.begin(), v.end(), gt_pair_float_string());
    }
1279
}
1280

Matt Turek's avatar
Matt Turek committed
1281

1282
void DICOMAppHelper::GetImagePositionPatientFilenamePairs(dicom_stl::vector<dicom_stl::pair<float, dicom_stl::string> >& v, bool ascending)
1283
{
Matt Turek's avatar
Matt Turek committed
1284
  // Default to using the first series
1285
  if (this->Implementation->SeriesUIDMap.size() > 0)
Matt Turek's avatar
Matt Turek committed
1286
    {
1287
1288
    this->GetImagePositionPatientFilenamePairs(
      (*this->Implementation->SeriesUIDMap.begin()).first, v, ascending);
Matt Turek's avatar
Matt Turek committed
1289
1290
1291
1292
1293
    }
  else
    {
    v.clear();
    }
1294
1295
}

1296
void DICOMAppHelper::GetSeriesUIDs(dicom_stl::vector<dicom_stl::string> &v)
1297
{
Matt Turek's avatar
Matt Turek committed
1298
1299
  v.clear();

1300
  dicom_stl::map<dicom_stl::string, dicom_stl::vector<dicom_stl::string>, ltstdstr >::iterator miter;
Matt Turek's avatar
Matt Turek committed
1301

1302
  for (miter = this->Implementation->SeriesUIDMap.begin(); miter != this->Implementation->SeriesUIDMap.end();
Matt Turek's avatar
Matt Turek committed
1303
1304
1305
1306
1307
1308
1309
1310
       ++miter)
    {
    v.push_back( (*miter).first );
    }
}

void DICOMAppHelper::Clear()
{ 
1311
1312
  this->Implementation->SliceOrderingMap.clear();
  this->Implementation->SeriesUIDMap.clear();
1313
}
Matt Turek's avatar
Matt Turek committed
1314

1315
1316
1317
1318
1319
1320
1321