Mercurial > hg > orthanc-stone
comparison Samples/Sdl/Loader.cpp @ 637:afc91cdc5128
decoding of images by oracle
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 09 May 2019 18:03:03 +0200 |
parents | fb00a8be03e2 |
children | e626f38c5512 |
comparison
equal
deleted
inserted
replaced
636:fb00a8be03e2 | 637:afc91cdc5128 |
---|---|
26 #include "../../Framework/StoneInitialization.h" | 26 #include "../../Framework/StoneInitialization.h" |
27 #include "../../Framework/Toolbox/GeometryToolbox.h" | 27 #include "../../Framework/Toolbox/GeometryToolbox.h" |
28 #include "../../Framework/Volumes/ImageBuffer3D.h" | 28 #include "../../Framework/Volumes/ImageBuffer3D.h" |
29 | 29 |
30 // From Orthanc framework | 30 // From Orthanc framework |
31 #include <Core/Compression/GzipCompressor.h> | |
32 #include <Core/Compression/ZlibCompressor.h> | |
31 #include <Core/DicomFormat/DicomArray.h> | 33 #include <Core/DicomFormat/DicomArray.h> |
32 #include <Core/DicomFormat/DicomImageInformation.h> | 34 #include <Core/DicomFormat/DicomImageInformation.h> |
33 #include <Core/Compression/ZlibCompressor.h> | |
34 #include <Core/Compression/GzipCompressor.h> | |
35 #include <Core/HttpClient.h> | 35 #include <Core/HttpClient.h> |
36 #include <Core/IDynamicObject.h> | 36 #include <Core/IDynamicObject.h> |
37 #include <Core/Images/Image.h> | 37 #include <Core/Images/Image.h> |
38 #include <Core/Images/ImageProcessing.h> | 38 #include <Core/Images/ImageProcessing.h> |
39 #include <Core/Images/JpegReader.h> | |
40 #include <Core/Images/PamReader.h> | |
41 #include <Core/Images/PngReader.h> | |
39 #include <Core/Images/PngWriter.h> | 42 #include <Core/Images/PngWriter.h> |
40 #include <Core/Logging.h> | 43 #include <Core/Logging.h> |
41 #include <Core/MultiThreading/SharedMessageQueue.h> | 44 #include <Core/MultiThreading/SharedMessageQueue.h> |
42 #include <Core/OrthancException.h> | 45 #include <Core/OrthancException.h> |
43 #include <Core/Toolbox.h> | 46 #include <Core/Toolbox.h> |
302 DecodeOrthancImageCommand> | 305 DecodeOrthancImageCommand> |
303 { | 306 { |
304 private: | 307 private: |
305 std::auto_ptr<Orthanc::ImageAccessor> image_; | 308 std::auto_ptr<Orthanc::ImageAccessor> image_; |
306 Orthanc::MimeType mime_; | 309 Orthanc::MimeType mime_; |
307 unsigned int quality_; | |
308 | 310 |
309 public: | 311 public: |
310 SuccessMessage(const DecodeOrthancImageCommand& command, | 312 SuccessMessage(const DecodeOrthancImageCommand& command, |
311 Orthanc::ImageAccessor* image, // Takes ownership | 313 Orthanc::ImageAccessor* image, // Takes ownership |
312 Orthanc::MimeType mime, | 314 Orthanc::MimeType mime) : |
313 unsigned int quality) : | |
314 OriginMessage(command), | 315 OriginMessage(command), |
315 image_(image), | 316 image_(image), |
316 mime_(mime), | 317 mime_(mime) |
317 quality_(quality) | |
318 { | 318 { |
319 if (image == NULL) | 319 if (image == NULL) |
320 { | 320 { |
321 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | 321 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); |
322 } | 322 } |
328 } | 328 } |
329 | 329 |
330 Orthanc::MimeType GetMimeType() const | 330 Orthanc::MimeType GetMimeType() const |
331 { | 331 { |
332 return mime_; | 332 return mime_; |
333 } | |
334 | |
335 unsigned int GetQuality() const | |
336 { | |
337 return quality_; | |
338 } | 333 } |
339 }; | 334 }; |
340 | 335 |
341 | 336 |
342 class FailureMessage : public OrthancStone::OriginMessage<OrthancStone::MessageType_HttpRequestError, // TODO | 337 class FailureMessage : public OrthancStone::OriginMessage<OrthancStone::MessageType_HttpRequestError, // TODO |
538 success = false; | 533 success = false; |
539 } | 534 } |
540 | 535 |
541 if (success) | 536 if (success) |
542 { | 537 { |
543 printf("OK %d\n", answer.size()); | |
544 | |
545 Orthanc::MimeType contentType = Orthanc::MimeType_Binary; | 538 Orthanc::MimeType contentType = Orthanc::MimeType_Binary; |
546 Orthanc::HttpCompression contentEncoding = Orthanc::HttpCompression_None; | 539 Orthanc::HttpCompression contentEncoding = Orthanc::HttpCompression_None; |
547 | 540 |
548 for (HttpHeaders::const_iterator it = answerHeaders.begin(); | 541 for (HttpHeaders::const_iterator it = answerHeaders.begin(); |
549 it != answerHeaders.end(); ++it) | 542 it != answerHeaders.end(); ++it) |
557 { | 550 { |
558 contentEncoding = Orthanc::HttpCompression_Gzip; | 551 contentEncoding = Orthanc::HttpCompression_Gzip; |
559 } | 552 } |
560 else | 553 else |
561 { | 554 { |
555 // TODO - Emit error message? | |
562 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, | 556 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, |
563 "Unsupported HTTP Content-Encoding: " + it->second); | 557 "Unsupported HTTP Content-Encoding: " + it->second); |
564 } | 558 } |
565 } | 559 } |
566 | 560 |
567 if (s == "content-type") | 561 if (s == "content-type") |
568 { | 562 { |
569 contentType = Orthanc::StringToMimeType(it->second); | 563 contentType = Orthanc::StringToMimeType(it->second); |
570 } | 564 } |
571 | |
572 printf(" [%s] == [%s]\n", it->first.c_str(), it->second.c_str()); | |
573 } | 565 } |
574 | 566 |
575 if (contentEncoding == Orthanc::HttpCompression_Gzip) | 567 if (contentEncoding == Orthanc::HttpCompression_Gzip) |
576 { | 568 { |
577 std::string compressed; | 569 std::string compressed; |
579 | 571 |
580 Orthanc::GzipCompressor compressor; | 572 Orthanc::GzipCompressor compressor; |
581 compressor.Uncompress(answer, compressed.c_str(), compressed.size()); | 573 compressor.Uncompress(answer, compressed.c_str(), compressed.size()); |
582 } | 574 } |
583 | 575 |
584 printf("[%s] %d => %d\n", Orthanc::EnumerationToString(contentType), contentEncoding, answer.size()); | 576 std::auto_ptr<Orthanc::ImageAccessor> image; |
585 | 577 |
586 | 578 switch (contentType) |
587 | 579 { |
588 //DecodeOrthancImageCommand::SuccessMessage message(command, answerHeaders, answer); | 580 case Orthanc::MimeType_Png: |
589 //emitter_.EmitMessage(receiver, message); | 581 { |
582 image.reset(new Orthanc::PngReader); | |
583 dynamic_cast<Orthanc::PngReader&>(*image).ReadFromMemory(answer); | |
584 break; | |
585 } | |
586 | |
587 case Orthanc::MimeType_Pam: | |
588 { | |
589 image.reset(new Orthanc::PamReader); | |
590 dynamic_cast<Orthanc::PamReader&>(*image).ReadFromMemory(answer); | |
591 break; | |
592 } | |
593 | |
594 case Orthanc::MimeType_Jpeg: | |
595 { | |
596 image.reset(new Orthanc::JpegReader); | |
597 dynamic_cast<Orthanc::JpegReader&>(*image).ReadFromMemory(answer); | |
598 break; | |
599 } | |
600 | |
601 default: | |
602 // TODO - Emit error message? | |
603 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, | |
604 "Unsupported HTTP Content-Type for an image: " + | |
605 std::string(Orthanc::EnumerationToString(contentType))); | |
606 } | |
607 | |
608 DecodeOrthancImageCommand::SuccessMessage message(command, image.release(), contentType); | |
609 emitter_.EmitMessage(receiver, message); | |
590 } | 610 } |
591 else | 611 else |
592 { | 612 { |
593 DecodeOrthancImageCommand::FailureMessage message(command, client.GetLastStatus()); | 613 DecodeOrthancImageCommand::FailureMessage message(command, client.GetLastStatus()); |
594 emitter_.EmitMessage(receiver, message); | 614 emitter_.EmitMessage(receiver, message); |
1285 message.ParseJsonBody(v); | 1305 message.ParseJsonBody(v); |
1286 | 1306 |
1287 printf("ICI [%s]\n", v.toStyledString().c_str()); | 1307 printf("ICI [%s]\n", v.toStyledString().c_str()); |
1288 } | 1308 } |
1289 | 1309 |
1310 void Handle(const Refactoring::DecodeOrthancImageCommand::SuccessMessage& message) | |
1311 { | |
1312 printf("IMAGE %dx%d\n", message.GetImage().GetWidth(), message.GetImage().GetHeight()); | |
1313 } | |
1314 | |
1290 void Handle(const Refactoring::OrthancRestApiCommand::FailureMessage& message) | 1315 void Handle(const Refactoring::OrthancRestApiCommand::FailureMessage& message) |
1291 { | 1316 { |
1292 printf("ERROR %d\n", message.GetHttpStatus()); | 1317 printf("ERROR %d\n", message.GetHttpStatus()); |
1293 } | 1318 } |
1294 | 1319 |
1297 IObserver(oracle.GetBroker()) | 1322 IObserver(oracle.GetBroker()) |
1298 { | 1323 { |
1299 oracle.RegisterObserverCallback | 1324 oracle.RegisterObserverCallback |
1300 (new OrthancStone::Callable | 1325 (new OrthancStone::Callable |
1301 <Toto, Refactoring::OrthancRestApiCommand::SuccessMessage>(*this, &Toto::Handle)); | 1326 <Toto, Refactoring::OrthancRestApiCommand::SuccessMessage>(*this, &Toto::Handle)); |
1327 | |
1328 oracle.RegisterObserverCallback | |
1329 (new OrthancStone::Callable | |
1330 <Toto, Refactoring::DecodeOrthancImageCommand::SuccessMessage>(*this, &Toto::Handle)); | |
1302 } | 1331 } |
1303 }; | 1332 }; |
1304 | 1333 |
1305 | 1334 |
1306 void Run(Refactoring::NativeApplicationContext& context) | 1335 void Run(Refactoring::NativeApplicationContext& context) |