comparison OrthancServer/ServerContext.cpp @ 3912:7610af1532c3 transcoding

prototyping automated transcoding of incoming DICOM files
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 08 May 2020 13:43:50 +0200
parents 0ef7f4528be2
children 6ddad3e0b569
comparison
equal deleted inserted replaced
3911:0ef7f4528be2 3912:7610af1532c3
342 StorageAccessor accessor(area_, GetMetricsRegistry()); 342 StorageAccessor accessor(area_, GetMetricsRegistry());
343 accessor.Remove(fileUuid, type); 343 accessor.Remove(fileUuid, type);
344 } 344 }
345 345
346 346
347 StoreStatus ServerContext::Store(std::string& resultPublicId, 347 StoreStatus ServerContext::StoreAfterTranscoding(std::string& resultPublicId,
348 DicomInstanceToStore& dicom, 348 DicomInstanceToStore& dicom,
349 StoreInstanceMode mode) 349 StoreInstanceMode mode)
350 { 350 {
351 bool overwrite; 351 bool overwrite;
352 switch (mode) 352 switch (mode)
353 { 353 {
354 case StoreInstanceMode_Default: 354 case StoreInstanceMode_Default:
500 throw; 500 throw;
501 } 501 }
502 } 502 }
503 503
504 504
505 StoreStatus ServerContext::Store(std::string& resultPublicId,
506 DicomInstanceToStore& dicom,
507 StoreInstanceMode mode)
508 {
509 const DicomTransferSyntax option = DicomTransferSyntax_JPEGProcess1;
510
511 if (1)
512 {
513 return StoreAfterTranscoding(resultPublicId, dicom, mode);
514 }
515 else
516 {
517 // TODO => Automated transcoding of incoming DICOM files
518
519 DicomTransferSyntax sourceSyntax;
520 if (!FromDcmtkBridge::LookupOrthancTransferSyntax(
521 sourceSyntax, dicom.GetParsedDicomFile().GetDcmtkObject()) ||
522 sourceSyntax == option)
523 {
524 // No transcoding
525 return StoreAfterTranscoding(resultPublicId, dicom, mode);
526 }
527 else
528 {
529 std::set<DicomTransferSyntax> syntaxes;
530 syntaxes.insert(option);
531
532 std::unique_ptr<IDicomTranscoder::TranscodedDicom> transcoded(
533 GetTranscoder().TranscodeToParsed(dicom.GetParsedDicomFile().GetDcmtkObject(),
534 dicom.GetBufferData(), dicom.GetBufferSize(),
535 syntaxes, true /* allow new SOP instance UID */));
536
537 if (transcoded.get() == NULL)
538 {
539 // Cannot transcode => store the original file
540 return StoreAfterTranscoding(resultPublicId, dicom, mode);
541 }
542 else
543 {
544 std::unique_ptr<ParsedDicomFile> tmp(
545 ParsedDicomFile::AcquireDcmtkObject(transcoded->ReleaseDicom()));
546
547 DicomInstanceToStore toStore;
548 toStore.SetParsedDicomFile(*tmp);
549 toStore.SetOrigin(dicom.GetOrigin());
550
551 StoreStatus ok = StoreAfterTranscoding(resultPublicId, toStore, mode);
552 printf(">> %s\n", resultPublicId.c_str());
553 return ok;
554 }
555 }
556 }
557 }
558
559
505 void ServerContext::AnswerAttachment(RestApiOutput& output, 560 void ServerContext::AnswerAttachment(RestApiOutput& output,
506 const std::string& resourceId, 561 const std::string& resourceId,
507 FileContentType content) 562 FileContentType content)
508 { 563 {
509 FileInfo attachment; 564 FileInfo attachment;
1113 1168
1114 return NULL; 1169 return NULL;
1115 } 1170 }
1116 1171
1117 1172
1173 IDicomTranscoder& ServerContext::GetTranscoder()
1174 {
1175 IDicomTranscoder* transcoder = dcmtkTranscoder_.get();
1176
1177 #if ORTHANC_ENABLE_PLUGINS == 1
1178 if (HasPlugins())
1179 {
1180 transcoder = &GetPlugins();
1181 }
1182 #endif
1183
1184 if (transcoder == NULL)
1185 {
1186 throw OrthancException(ErrorCode_InternalError);
1187 }
1188 else
1189 {
1190 return *transcoder;
1191 }
1192 }
1193
1194
1118 void ServerContext::StoreWithTranscoding(std::string& sopClassUid, 1195 void ServerContext::StoreWithTranscoding(std::string& sopClassUid,
1119 std::string& sopInstanceUid, 1196 std::string& sopInstanceUid,
1120 DicomStoreUserConnection& connection, 1197 DicomStoreUserConnection& connection,
1121 const std::string& dicom, 1198 const std::string& dicom,
1122 bool hasMoveOriginator, 1199 bool hasMoveOriginator,
1131 connection.Store(sopClassUid, sopInstanceUid, data, dicom.size(), 1208 connection.Store(sopClassUid, sopInstanceUid, data, dicom.size(),
1132 hasMoveOriginator, moveOriginatorAet, moveOriginatorId); 1209 hasMoveOriginator, moveOriginatorAet, moveOriginatorId);
1133 } 1210 }
1134 else 1211 else
1135 { 1212 {
1136 IDicomTranscoder* transcoder = dcmtkTranscoder_.get(); 1213 connection.Transcode(sopClassUid, sopInstanceUid, GetTranscoder(), data, dicom.size(),
1137 1214 hasMoveOriginator, moveOriginatorAet, moveOriginatorId);
1138 #if ORTHANC_ENABLE_PLUGINS == 1
1139 if (HasPlugins())
1140 {
1141 transcoder = &GetPlugins();
1142 }
1143 #endif
1144
1145 if (transcoder == NULL)
1146 {
1147 throw OrthancException(ErrorCode_InternalError);
1148 }
1149 else
1150 {
1151 connection.Transcode(sopClassUid, sopInstanceUid, *transcoder, data, dicom.size(),
1152 hasMoveOriginator, moveOriginatorAet, moveOriginatorId);
1153 }
1154 } 1215 }
1155 } 1216 }
1156 1217
1157 1218
1158 bool ServerContext::Transcode(std::string& target /* out */, 1219 bool ServerContext::Transcode(std::string& target /* out */,
1160 bool& hasSopInstanceUidChanged /* out */, 1221 bool& hasSopInstanceUidChanged /* out */,
1161 ParsedDicomFile& dicom, // Possibly modified 1222 ParsedDicomFile& dicom, // Possibly modified
1162 DicomTransferSyntax targetSyntax, 1223 DicomTransferSyntax targetSyntax,
1163 bool allowNewSopInstanceUid) 1224 bool allowNewSopInstanceUid)
1164 { 1225 {
1165 IDicomTranscoder* transcoder = dcmtkTranscoder_.get(); 1226 return GetTranscoder().TranscodeParsedToBuffer(
1166 1227 target, sourceSyntax, hasSopInstanceUidChanged,
1167 #if ORTHANC_ENABLE_PLUGINS == 1 1228 dicom.GetDcmtkObject(), targetSyntax, allowNewSopInstanceUid);
1168 if (HasPlugins())
1169 {
1170 transcoder = &GetPlugins();
1171 }
1172 #endif
1173
1174 if (transcoder == NULL)
1175 {
1176 throw OrthancException(ErrorCode_InternalError);
1177 }
1178 else
1179 {
1180 return transcoder->TranscodeParsedToBuffer(
1181 target, sourceSyntax, hasSopInstanceUidChanged,
1182 dicom.GetDcmtkObject(), targetSyntax, allowNewSopInstanceUid);
1183 }
1184 } 1229 }
1185 } 1230 }