comparison OrthancServer/FromDcmtkBridge.cpp @ 956:2fd5a163776d

primitives for proper encoding handling
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Jun 2014 16:08:47 +0200
parents 766a57997121
children 63973b76a51f
comparison
equal deleted inserted replaced
950:8811abd6aec9 956:2fd5a163776d
113 (GetCharValue(c[1]) << 8) + 113 (GetCharValue(c[1]) << 8) +
114 (GetCharValue(c[2]) << 4) + 114 (GetCharValue(c[2]) << 4) +
115 GetCharValue(c[3])); 115 GetCharValue(c[3]));
116 } 116 }
117 117
118
119 Encoding FromDcmtkBridge::DetectEncoding(DcmDataset& dataset)
120 {
121 // TODO Implement this!
122 return Encoding_Latin1;
123 }
124
125
118 void FromDcmtkBridge::Convert(DicomMap& target, DcmDataset& dataset) 126 void FromDcmtkBridge::Convert(DicomMap& target, DcmDataset& dataset)
119 { 127 {
128 Encoding encoding = DetectEncoding(dataset);
129
120 target.Clear(); 130 target.Clear();
121 for (unsigned long i = 0; i < dataset.card(); i++) 131 for (unsigned long i = 0; i < dataset.card(); i++)
122 { 132 {
123 DcmElement* element = dataset.getElement(i); 133 DcmElement* element = dataset.getElement(i);
124 if (element && element->isLeaf()) 134 if (element && element->isLeaf())
125 { 135 {
126 target.SetValue(element->getTag().getGTag(), 136 target.SetValue(element->getTag().getGTag(),
127 element->getTag().getETag(), 137 element->getTag().getETag(),
128 ConvertLeafElement(*element)); 138 ConvertLeafElement(*element, encoding));
129 } 139 }
130 } 140 }
131 } 141 }
132 142
133 143
135 { 145 {
136 return DicomTag(element.getGTag(), element.getETag()); 146 return DicomTag(element.getGTag(), element.getETag());
137 } 147 }
138 148
139 149
140 DicomValue* FromDcmtkBridge::ConvertLeafElement(DcmElement& element) 150 DicomValue* FromDcmtkBridge::ConvertLeafElement(DcmElement& element,
151 Encoding encoding)
141 { 152 {
142 if (!element.isLeaf()) 153 if (!element.isLeaf())
143 { 154 {
144 throw OrthancException("Only applicable to leaf elements"); 155 throw OrthancException("Only applicable to leaf elements");
145 } 156 }
149 char *c; 160 char *c;
150 if (element.getString(c).good() && 161 if (element.getString(c).good() &&
151 c != NULL) 162 c != NULL)
152 { 163 {
153 std::string s(c); 164 std::string s(c);
154 std::string utf8 = Toolbox::ConvertToUtf8(s, Encoding_Latin1); // TODO Parameter? 165 std::string utf8 = Toolbox::ConvertToUtf8(s, encoding);
155 return new DicomString(utf8); 166 return new DicomString(utf8);
156 } 167 }
157 else 168 else
158 { 169 {
159 return new DicomNullValue; 170 return new DicomNullValue;
311 } 322 }
312 323
313 324
314 static void StoreElement(Json::Value& target, 325 static void StoreElement(Json::Value& target,
315 DcmElement& element, 326 DcmElement& element,
316 unsigned int maxStringLength); 327 unsigned int maxStringLength,
328 Encoding encoding);
317 329
318 static void StoreItem(Json::Value& target, 330 static void StoreItem(Json::Value& target,
319 DcmItem& item, 331 DcmItem& item,
320 unsigned int maxStringLength) 332 unsigned int maxStringLength,
333 Encoding encoding)
321 { 334 {
322 target = Json::Value(Json::objectValue); 335 target = Json::Value(Json::objectValue);
323 336
324 for (unsigned long i = 0; i < item.card(); i++) 337 for (unsigned long i = 0; i < item.card(); i++)
325 { 338 {
326 DcmElement* element = item.getElement(i); 339 DcmElement* element = item.getElement(i);
327 StoreElement(target, *element, maxStringLength); 340 StoreElement(target, *element, maxStringLength, encoding);
328 } 341 }
329 } 342 }
330 343
331 344
332 static void StoreElement(Json::Value& target, 345 static void StoreElement(Json::Value& target,
333 DcmElement& element, 346 DcmElement& element,
334 unsigned int maxStringLength) 347 unsigned int maxStringLength,
348 Encoding encoding)
335 { 349 {
336 assert(target.type() == Json::objectValue); 350 assert(target.type() == Json::objectValue);
337 351
338 DicomTag tag(FromDcmtkBridge::GetTag(element)); 352 DicomTag tag(FromDcmtkBridge::GetTag(element));
339 const std::string formattedTag = tag.Format(); 353 const std::string formattedTag = tag.Format();
354 if (tagbis.getPrivateCreator() != NULL) 368 if (tagbis.getPrivateCreator() != NULL)
355 { 369 {
356 value["PrivateCreator"] = tagbis.getPrivateCreator(); 370 value["PrivateCreator"] = tagbis.getPrivateCreator();
357 } 371 }
358 372
359 std::auto_ptr<DicomValue> v(FromDcmtkBridge::ConvertLeafElement(element)); 373 std::auto_ptr<DicomValue> v(FromDcmtkBridge::ConvertLeafElement(element, encoding));
360 if (v->IsNull()) 374 if (v->IsNull())
361 { 375 {
362 value["Type"] = "Null"; 376 value["Type"] = "Null";
363 value["Value"] = Json::nullValue; 377 value["Value"] = Json::nullValue;
364 } 378 }
391 405
392 for (unsigned long i = 0; i < sequence.card(); i++) 406 for (unsigned long i = 0; i < sequence.card(); i++)
393 { 407 {
394 DcmItem* child = sequence.getItem(i); 408 DcmItem* child = sequence.getItem(i);
395 Json::Value& v = children.append(Json::objectValue); 409 Json::Value& v = children.append(Json::objectValue);
396 StoreItem(v, *child, maxStringLength); 410 StoreItem(v, *child, maxStringLength, encoding);
397 } 411 }
398 412
399 target[formattedTag]["Name"] = tagName; 413 target[formattedTag]["Name"] = tagName;
400 target[formattedTag]["Type"] = "Sequence"; 414 target[formattedTag]["Type"] = "Sequence";
401 target[formattedTag]["Value"] = children; 415 target[formattedTag]["Value"] = children;
405 419
406 void FromDcmtkBridge::ToJson(Json::Value& root, 420 void FromDcmtkBridge::ToJson(Json::Value& root,
407 DcmDataset& dataset, 421 DcmDataset& dataset,
408 unsigned int maxStringLength) 422 unsigned int maxStringLength)
409 { 423 {
410 StoreItem(root, dataset, maxStringLength); 424 StoreItem(root, dataset, maxStringLength, DetectEncoding(dataset));
411 } 425 }
412 426
413 427
414 428
415 void FromDcmtkBridge::ToJson(Json::Value& target, 429 void FromDcmtkBridge::ToJson(Json::Value& target,