@@ -108,7 +108,7 @@ namespace gdcm
108
108
return false ;
109
109
}
110
110
111
- void dataElementToJSONArray (const VR::VRType vr, const DataElement &de, rapidjson::Value &jsonArray, rapidjson::Document::AllocatorType &allocator)
111
+ void dataElementToJSONArray (const VR::VRType vr, const DataElement &de, rapidjson::Value &jsonArray, const CharStringToUTF8Converter toUtf8, rapidjson::Document::AllocatorType &allocator)
112
112
{
113
113
jsonArray.SetArray ();
114
114
if (de.IsEmpty ())
@@ -169,7 +169,7 @@ namespace gdcm
169
169
assert (str2 && (size_t )(str2 - component.c_str ()) <= len2);
170
170
const char *sep2 = strchr (str2, ' =' );
171
171
const size_t llen2 = (sep2 != NULL ) ? (sep2 - str2) : (component.c_str () + len2 - str2);
172
- const std::string group (str2, llen2);
172
+ const std::string group = toUtf8. convertCharStringToUTF8 (str2, llen2);
173
173
// const char *thekey = Keys[idx++];
174
174
175
175
// rapidjson::Value nameType(thekey, allocator);
@@ -235,9 +235,9 @@ namespace gdcm
235
235
assert (str1 && (size_t )(str1 - value) <= len);
236
236
const char *sep = strchr (str1, ' \\ ' );
237
237
const size_t llen = (sep != NULL ) ? (sep - str1) : (value + len - str1);
238
- // json_object_array_add(my_array, json_object_new_string_len (str1, llen) );
238
+ const std::string valueUtf8 = toUtf8. convertCharStringToUTF8 (str1, llen);
239
239
rapidjson::Value valueString;
240
- valueString.SetString (str1, llen , allocator);
240
+ valueString.SetString (valueUtf8. c_str (), valueUtf8. size () , allocator);
241
241
jsonArray.PushBack (valueString, allocator);
242
242
if (sep == NULL )
243
243
break ;
@@ -246,14 +246,14 @@ namespace gdcm
246
246
}
247
247
else // default
248
248
{
249
+ const std::string valueUtf8 = toUtf8.convertCharStringToUTF8 (value, len);
249
250
rapidjson::Value valueString;
250
- valueString.SetString (value, len , allocator);
251
+ valueString.SetString (valueUtf8. c_str (), valueUtf8. size () , allocator);
251
252
jsonArray.PushBack (valueString, allocator);
252
253
}
253
254
}
254
255
255
- const gdcm::Tag PIXEL_DATA_TAG = gdcm::Tag(0x7fe0 , 0x0010 );
256
- rapidjson::Value *toJson (const gdcm::DataSet &dataSet, const Tags pickTags, const Tags skipTags, rapidjson::Value &dicomTagsObject, rapidjson::Document::AllocatorType &allocator)
256
+ rapidjson::Value *toJson (const gdcm::DataSet &dataSet, const Tags &pickTags, const Tags &skipTags, const CharStringToUTF8Converter &toUtf8, rapidjson::Value &dicomTagsObject, rapidjson::Document::AllocatorType &allocator)
257
257
{
258
258
for (gdcm::DataSet::ConstIterator it = dataSet.Begin (); it != dataSet.End (); ++it)
259
259
{
@@ -293,7 +293,7 @@ namespace gdcm
293
293
const DataSet &nested = item.GetNestedDataSet ();
294
294
rapidjson::Value sequenceObject (rapidjson::kObjectType );
295
295
// grab all nested tags, empty pick and skip tag sets
296
- toJson (nested, {}, {} , sequenceObject, allocator);
296
+ toJson (nested, EMPTY_TAGS, EMPTY_TAGS, toUtf8 , sequenceObject, allocator);
297
297
tagValue.PushBack (sequenceObject, allocator);
298
298
}
299
299
}
@@ -312,7 +312,7 @@ namespace gdcm
312
312
}
313
313
else if (VR::IsASCII (vr))
314
314
{
315
- dataElementToJSONArray (vr, de, tagValue, allocator);
315
+ dataElementToJSONArray (vr, de, tagValue, toUtf8, allocator);
316
316
}
317
317
else
318
318
{
@@ -466,6 +466,14 @@ namespace gdcm
466
466
}
467
467
}
468
468
469
+ rapidjson::Value *toJson (const gdcm::DataSet &dataSet, const Tags &pickTags, const Tags &skipTags, rapidjson::Value &dicomTagsObject, rapidjson::Document::AllocatorType &allocator)
470
+ {
471
+ const auto specificCharacterSet = getTagBuffer (dataSet, SPECIFIC_CHARACTER_SET);
472
+ const std::string charSet = specificCharacterSet.first == nullptr ? " " : std::string (specificCharacterSet.first , specificCharacterSet.second );
473
+ const CharStringToUTF8Converter decoder = CharStringToUTF8Converter (charSet);
474
+ return toJson (dataSet, pickTags, skipTags, decoder, dicomTagsObject, allocator);
475
+ }
476
+
469
477
using FileName = std::string;
470
478
471
479
struct DicomFile
@@ -476,13 +484,6 @@ struct DicomFile
476
484
DicomFile (const FileName &fileName)
477
485
: fileName(fileName)
478
486
{
479
- itk::DICOMTagReader tagReader;
480
- if (!tagReader.CanReadFile (fileName))
481
- {
482
- throw std::runtime_error (" Can not read the input DICOM file: " + fileName);
483
- }
484
- tagReader.SetFileName (fileName);
485
-
486
487
gdcm::ImageReader reader;
487
488
reader.SetFileName (fileName.c_str ());
488
489
if (!reader.Read ())
@@ -520,22 +521,9 @@ DicomFiles loadFiles(const std::vector<FileName> &fileNames)
520
521
}
521
522
522
523
using Volume = std::vector<DicomFile>;
523
- using Volumes = std::vector<Volume>; // aka ImageSet
524
+ using Volumes = std::vector<Volume>; // Aka ImageSet. A set of volumes/series that share Study and Patient.
524
525
using ImageSets = std::vector<Volumes>;
525
526
526
- std::pair<const char *, size_t > getTagBuffer (const gdcm::DataSet &ds, const gdcm::Tag &tag)
527
- {
528
- if (!ds.FindDataElement (tag) || ds.GetDataElement (tag).IsEmpty ())
529
- {
530
- return std::make_pair (nullptr , 0 );
531
- }
532
- const gdcm::DataElement de = ds.GetDataElement (tag);
533
- const gdcm::ByteValue *bv = de.GetByteValue ();
534
- const char *tagValue = bv->GetPointer ();
535
- size_t len = bv->GetLength ();
536
- return std::make_pair (tagValue, len);
537
- }
538
-
539
527
bool compareTags (const gdcm::DataSet &tagsA, const gdcm::DataSet &tagsB, const Tags &tagKeys)
540
528
{
541
529
for (const auto &tagKey : tagKeys)
@@ -649,13 +637,13 @@ rapidjson::Document toJson(const ImageSets &imageSets)
649
637
{
650
638
rapidjson::Document imageSetsJson (rapidjson::kArrayType );
651
639
rapidjson::Document::AllocatorType &allocator = imageSetsJson.GetAllocator ();
652
- gdcm::DataSet dataSet;
653
640
Tags instanceSkipTags; // filter out patient, study, series tags from instance object
654
641
instanceSkipTags.insert (PATIENT_TAGS.begin (), PATIENT_TAGS.end ());
655
642
instanceSkipTags.insert (STUDY_TAGS.begin (), STUDY_TAGS.end ());
656
643
instanceSkipTags.insert (SERIES_TAGS.begin (), SERIES_TAGS.end ());
657
644
for (const Volumes &volumes : imageSets)
658
645
{
646
+ gdcm::DataSet dataSet;
659
647
rapidjson::Value seriesById (rapidjson::kObjectType );
660
648
for (const Volume &volume : volumes)
661
649
{
@@ -665,7 +653,8 @@ rapidjson::Document toJson(const ImageSets &imageSets)
665
653
FileName file = dicomFile.fileName ;
666
654
dataSet = dicomFile.dataSet ;
667
655
rapidjson::Value instanceTagsJson (rapidjson::kObjectType );
668
- toJson (dataSet, {}, instanceSkipTags, instanceTagsJson, allocator);
656
+
657
+ toJson (dataSet, EMPTY_TAGS, instanceSkipTags, instanceTagsJson, allocator);
669
658
rapidjson::Value instance (rapidjson::kObjectType );
670
659
instance.AddMember (" DICOM" , instanceTagsJson, allocator);
671
660
@@ -686,7 +675,7 @@ rapidjson::Document toJson(const ImageSets &imageSets)
686
675
687
676
// Series
688
677
rapidjson::Value seriesTags (rapidjson::kObjectType );
689
- toJson (dataSet, SERIES_TAGS, {} , seriesTags, allocator);
678
+ toJson (dataSet, SERIES_TAGS, EMPTY_TAGS , seriesTags, allocator);
690
679
rapidjson::Value series (rapidjson::kObjectType );
691
680
series.AddMember (" DICOM" , seriesTags, allocator);
692
681
series.AddMember (" Instances" , instances, allocator);
@@ -703,14 +692,14 @@ rapidjson::Document toJson(const ImageSets &imageSets)
703
692
704
693
// Patient
705
694
rapidjson::Value patientTags (rapidjson::kObjectType );
706
- toJson (dataSet, PATIENT_TAGS, {} , patientTags, allocator);
695
+ toJson (dataSet, PATIENT_TAGS, EMPTY_TAGS , patientTags, allocator);
707
696
rapidjson::Value patient (rapidjson::kObjectType );
708
697
patient.AddMember (" DICOM" , patientTags, allocator);
709
698
imageSet.AddMember (" Patient" , patient, allocator);
710
699
711
700
// Study
712
701
rapidjson::Value studyTags (rapidjson::kObjectType );
713
- toJson (dataSet, STUDY_TAGS, {} , studyTags, allocator);
702
+ toJson (dataSet, STUDY_TAGS, EMPTY_TAGS , studyTags, allocator);
714
703
rapidjson::Value study (rapidjson::kObjectType );
715
704
study.AddMember (" DICOM" , studyTags, allocator);
716
705
study.AddMember (" Series" , seriesById, allocator);
@@ -739,7 +728,6 @@ int main(int argc, char *argv[])
739
728
const ImageSets imageSets = groupByImageSet (volumes);
740
729
741
730
rapidjson::Document imageSetsJson = toJson (imageSets);
742
-
743
731
rapidjson::StringBuffer stringBuffer;
744
732
rapidjson::Writer<rapidjson::StringBuffer> writer (stringBuffer);
745
733
imageSetsJson.Accept (writer);
0 commit comments