Mercurial > hg > orthanc-stone
comparison Samples/Sdl/Loader.cpp @ 638:e626f38c5512
DecodeOrthancWebViewerJpegCommand
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 09 May 2019 18:45:52 +0200 |
parents | afc91cdc5128 |
children | d1f4521b9bbb |
comparison
equal
deleted
inserted
replaced
637:afc91cdc5128 | 638:e626f38c5512 |
---|---|
60 { | 60 { |
61 public: | 61 public: |
62 enum Type | 62 enum Type |
63 { | 63 { |
64 Type_OrthancRestApi, | 64 Type_OrthancRestApi, |
65 Type_DecodeOrthancImage | 65 Type_DecodeOrthancImage, |
66 Type_DecodeOrthancWebViewerJpeg | |
66 }; | 67 }; |
67 | 68 |
68 virtual ~IOracleCommand() | 69 virtual ~IOracleCommand() |
69 { | 70 { |
70 } | 71 } |
407 } | 408 } |
408 }; | 409 }; |
409 | 410 |
410 | 411 |
411 | 412 |
413 class DecodeOrthancWebViewerJpegCommand : public OracleCommandWithPayload | |
414 { | |
415 public: | |
416 class SuccessMessage : public OrthancStone::OriginMessage<OrthancStone::MessageType_ImageReady, // TODO | |
417 DecodeOrthancWebViewerJpegCommand> | |
418 { | |
419 private: | |
420 std::auto_ptr<Orthanc::ImageAccessor> image_; | |
421 | |
422 public: | |
423 SuccessMessage(const DecodeOrthancWebViewerJpegCommand& command, | |
424 Orthanc::ImageAccessor* image) : // Takes ownership | |
425 OriginMessage(command), | |
426 image_(image) | |
427 { | |
428 if (image == NULL) | |
429 { | |
430 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
431 } | |
432 } | |
433 | |
434 const Orthanc::ImageAccessor& GetImage() const | |
435 { | |
436 return *image_; | |
437 } | |
438 }; | |
439 | |
440 | |
441 class FailureMessage : public OrthancStone::OriginMessage<OrthancStone::MessageType_HttpRequestError, // TODO | |
442 DecodeOrthancWebViewerJpegCommand> | |
443 { | |
444 private: | |
445 Orthanc::HttpStatus status_; | |
446 | |
447 public: | |
448 FailureMessage(const DecodeOrthancWebViewerJpegCommand& command, | |
449 Orthanc::HttpStatus status) : | |
450 OriginMessage(command), | |
451 status_(status) | |
452 { | |
453 } | |
454 | |
455 Orthanc::HttpStatus GetHttpStatus() const | |
456 { | |
457 return status_; | |
458 } | |
459 }; | |
460 | |
461 | |
462 private: | |
463 std::string instanceId_; | |
464 unsigned int frame_; | |
465 unsigned int quality_; | |
466 HttpHeaders headers_; | |
467 unsigned int timeout_; | |
468 | |
469 std::auto_ptr< OrthancStone::MessageHandler<SuccessMessage> > successCallback_; | |
470 std::auto_ptr< OrthancStone::MessageHandler<FailureMessage> > failureCallback_; | |
471 | |
472 public: | |
473 DecodeOrthancWebViewerJpegCommand() : | |
474 frame_(0), | |
475 quality_(95), | |
476 timeout_(10) | |
477 { | |
478 } | |
479 | |
480 virtual Type GetType() const | |
481 { | |
482 return Type_DecodeOrthancWebViewerJpeg; | |
483 } | |
484 | |
485 void SetInstance(const std::string& instanceId) | |
486 { | |
487 instanceId_ = instanceId; | |
488 } | |
489 | |
490 void SetFrame(unsigned int frame) | |
491 { | |
492 frame_ = frame; | |
493 } | |
494 | |
495 void SetQuality(unsigned int quality) | |
496 { | |
497 if (quality <= 0 || | |
498 quality > 100) | |
499 { | |
500 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
501 } | |
502 else | |
503 { | |
504 quality_ = quality; | |
505 } | |
506 } | |
507 | |
508 void SetHttpHeader(const std::string& key, | |
509 const std::string& value) | |
510 { | |
511 headers_[key] = value; | |
512 } | |
513 | |
514 const std::string& GetInstanceId() const | |
515 { | |
516 return instanceId_; | |
517 } | |
518 | |
519 unsigned int GetFrame() const | |
520 { | |
521 return frame_; | |
522 } | |
523 | |
524 unsigned int GetQuality() const | |
525 { | |
526 return quality_; | |
527 } | |
528 | |
529 const HttpHeaders& GetHttpHeaders() const | |
530 { | |
531 return headers_; | |
532 } | |
533 | |
534 void SetTimeout(unsigned int seconds) | |
535 { | |
536 timeout_ = seconds; | |
537 } | |
538 | |
539 unsigned int GetTimeout() const | |
540 { | |
541 return timeout_; | |
542 } | |
543 | |
544 std::string GetUri() const | |
545 { | |
546 return ("/web-viewer/instances/jpeg" + boost::lexical_cast<std::string>(quality_) + | |
547 "-" + instanceId_ + "_" + boost::lexical_cast<std::string>(frame_)); | |
548 } | |
549 }; | |
550 | |
551 | |
552 | |
412 | 553 |
413 | 554 |
414 class NativeOracle : public IOracle | 555 class NativeOracle : public IOracle |
415 { | 556 { |
416 private: | 557 private: |
469 client.AddHeader(it->first, it->second); | 610 client.AddHeader(it->first, it->second); |
470 } | 611 } |
471 } | 612 } |
472 | 613 |
473 | 614 |
615 void DecodeAnswer(std::string& answer, | |
616 const HttpHeaders& headers) | |
617 { | |
618 Orthanc::HttpCompression contentEncoding = Orthanc::HttpCompression_None; | |
619 | |
620 for (HttpHeaders::const_iterator it = headers.begin(); | |
621 it != headers.end(); ++it) | |
622 { | |
623 std::string s; | |
624 Orthanc::Toolbox::ToLowerCase(s, it->first); | |
625 | |
626 if (s == "content-encoding") | |
627 { | |
628 if (it->second == "gzip") | |
629 { | |
630 contentEncoding = Orthanc::HttpCompression_Gzip; | |
631 } | |
632 else | |
633 { | |
634 // TODO - Emit error message? | |
635 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, | |
636 "Unsupported HTTP Content-Encoding: " + it->second); | |
637 } | |
638 | |
639 break; | |
640 } | |
641 } | |
642 | |
643 if (contentEncoding == Orthanc::HttpCompression_Gzip) | |
644 { | |
645 std::string compressed; | |
646 answer.swap(compressed); | |
647 | |
648 Orthanc::GzipCompressor compressor; | |
649 compressor.Uncompress(answer, compressed.c_str(), compressed.size()); | |
650 } | |
651 } | |
652 | |
653 | |
474 void Execute(const OrthancStone::IObserver& receiver, | 654 void Execute(const OrthancStone::IObserver& receiver, |
475 const OrthancRestApiCommand& command) | 655 const OrthancRestApiCommand& command) |
476 { | 656 { |
477 Orthanc::HttpClient client(orthanc_, command.GetUri()); | 657 Orthanc::HttpClient client(orthanc_, command.GetUri()); |
478 client.SetMethod(command.GetMethod()); | 658 client.SetMethod(command.GetMethod()); |
491 | 671 |
492 bool success; | 672 bool success; |
493 try | 673 try |
494 { | 674 { |
495 success = client.Apply(answer, answerHeaders); | 675 success = client.Apply(answer, answerHeaders); |
676 DecodeAnswer(answer, answerHeaders); | |
496 } | 677 } |
497 catch (Orthanc::OrthancException& e) | 678 catch (Orthanc::OrthancException& e) |
498 { | 679 { |
499 success = false; | 680 success = false; |
500 } | 681 } |
533 success = false; | 714 success = false; |
534 } | 715 } |
535 | 716 |
536 if (success) | 717 if (success) |
537 { | 718 { |
719 DecodeAnswer(answer, answerHeaders); | |
720 | |
538 Orthanc::MimeType contentType = Orthanc::MimeType_Binary; | 721 Orthanc::MimeType contentType = Orthanc::MimeType_Binary; |
539 Orthanc::HttpCompression contentEncoding = Orthanc::HttpCompression_None; | |
540 | 722 |
541 for (HttpHeaders::const_iterator it = answerHeaders.begin(); | 723 for (HttpHeaders::const_iterator it = answerHeaders.begin(); |
542 it != answerHeaders.end(); ++it) | 724 it != answerHeaders.end(); ++it) |
543 { | 725 { |
544 std::string s; | 726 std::string s; |
545 Orthanc::Toolbox::ToLowerCase(s, it->first); | 727 Orthanc::Toolbox::ToLowerCase(s, it->first); |
546 | |
547 if (s == "content-encoding") | |
548 { | |
549 if (it->second == "gzip") | |
550 { | |
551 contentEncoding = Orthanc::HttpCompression_Gzip; | |
552 } | |
553 else | |
554 { | |
555 // TODO - Emit error message? | |
556 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, | |
557 "Unsupported HTTP Content-Encoding: " + it->second); | |
558 } | |
559 } | |
560 | 728 |
561 if (s == "content-type") | 729 if (s == "content-type") |
562 { | 730 { |
563 contentType = Orthanc::StringToMimeType(it->second); | 731 contentType = Orthanc::StringToMimeType(it->second); |
732 break; | |
564 } | 733 } |
565 } | |
566 | |
567 if (contentEncoding == Orthanc::HttpCompression_Gzip) | |
568 { | |
569 std::string compressed; | |
570 answer.swap(compressed); | |
571 | |
572 Orthanc::GzipCompressor compressor; | |
573 compressor.Uncompress(answer, compressed.c_str(), compressed.size()); | |
574 } | 734 } |
575 | 735 |
576 std::auto_ptr<Orthanc::ImageAccessor> image; | 736 std::auto_ptr<Orthanc::ImageAccessor> image; |
577 | 737 |
578 switch (contentType) | 738 switch (contentType) |
614 emitter_.EmitMessage(receiver, message); | 774 emitter_.EmitMessage(receiver, message); |
615 } | 775 } |
616 } | 776 } |
617 | 777 |
618 | 778 |
779 void Execute(const OrthancStone::IObserver& receiver, | |
780 const DecodeOrthancWebViewerJpegCommand& command) | |
781 { | |
782 Orthanc::HttpClient client(orthanc_, command.GetUri()); | |
783 client.SetTimeout(command.GetTimeout()); | |
784 | |
785 CopyHttpHeaders(client, command.GetHttpHeaders()); | |
786 | |
787 std::string answer; | |
788 HttpHeaders answerHeaders; | |
789 | |
790 bool success; | |
791 try | |
792 { | |
793 success = client.Apply(answer, answerHeaders); | |
794 } | |
795 catch (Orthanc::OrthancException& e) | |
796 { | |
797 success = false; | |
798 } | |
799 | |
800 if (success) | |
801 { | |
802 DecodeAnswer(answer, answerHeaders); | |
803 | |
804 Json::Value value; | |
805 Json::Reader reader; | |
806 if (reader.parse(answer, value)) | |
807 { | |
808 std::cout << value.toStyledString() << std::endl; | |
809 } | |
810 | |
811 //DecodeOrthancWebViewerJpegCommand::SuccessMessage message(command, image.release(), contentType); | |
812 //emitter_.EmitMessage(receiver, message); | |
813 } | |
814 else | |
815 { | |
816 DecodeOrthancWebViewerJpegCommand::FailureMessage message(command, client.GetLastStatus()); | |
817 emitter_.EmitMessage(receiver, message); | |
818 } | |
819 } | |
820 | |
821 | |
619 | 822 |
620 void Step() | 823 void Step() |
621 { | 824 { |
622 std::auto_ptr<Orthanc::IDynamicObject> object(queue_.Dequeue(100)); | 825 std::auto_ptr<Orthanc::IDynamicObject> object(queue_.Dequeue(100)); |
623 | 826 |
635 break; | 838 break; |
636 | 839 |
637 case IOracleCommand::Type_DecodeOrthancImage: | 840 case IOracleCommand::Type_DecodeOrthancImage: |
638 Execute(item.GetReceiver(), | 841 Execute(item.GetReceiver(), |
639 dynamic_cast<const DecodeOrthancImageCommand&>(item.GetCommand())); | 842 dynamic_cast<const DecodeOrthancImageCommand&>(item.GetCommand())); |
843 break; | |
844 | |
845 case IOracleCommand::Type_DecodeOrthancWebViewerJpeg: | |
846 Execute(item.GetReceiver(), | |
847 dynamic_cast<const DecodeOrthancWebViewerJpegCommand&>(item.GetCommand())); | |
640 break; | 848 break; |
641 | 849 |
642 default: | 850 default: |
643 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | 851 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
644 } | 852 } |
1407 std::auto_ptr<Refactoring::DecodeOrthancImageCommand> command(new Refactoring::DecodeOrthancImageCommand); | 1615 std::auto_ptr<Refactoring::DecodeOrthancImageCommand> command(new Refactoring::DecodeOrthancImageCommand); |
1408 command->SetHttpHeader("Accept", std::string(Orthanc::EnumerationToString(Orthanc::MimeType_Pam))); | 1616 command->SetHttpHeader("Accept", std::string(Orthanc::EnumerationToString(Orthanc::MimeType_Pam))); |
1409 command->SetUri("/instances/6687cc73-07cae193-52ff29c8-f646cb16-0753ed92/image-uint16"); | 1617 command->SetUri("/instances/6687cc73-07cae193-52ff29c8-f646cb16-0753ed92/image-uint16"); |
1410 oracle.Schedule(*toto, command.release()); | 1618 oracle.Schedule(*toto, command.release()); |
1411 } | 1619 } |
1412 | 1620 |
1621 if (1) | |
1622 { | |
1623 std::auto_ptr<Refactoring::DecodeOrthancWebViewerJpegCommand> command(new Refactoring::DecodeOrthancWebViewerJpegCommand); | |
1624 command->SetHttpHeader("Accept-Encoding", "gzip"); | |
1625 command->SetInstance("e6c7c20b-c9f65d7e-0d76f2e2-830186f2-3e3c600e"); | |
1626 command->SetQuality(90); | |
1627 oracle.Schedule(*toto, command.release()); | |
1628 } | |
1629 | |
1630 | |
1413 // 2017-11-17-Anonymized | 1631 // 2017-11-17-Anonymized |
1414 loader1->LoadSeries(oracle, "cb3ea4d1-d08f3856-ad7b6314-74d88d77-60b05618"); // CT | 1632 loader1->LoadSeries(oracle, "cb3ea4d1-d08f3856-ad7b6314-74d88d77-60b05618"); // CT |
1415 loader2->LoadInstance(oracle, "41029085-71718346-811efac4-420e2c15-d39f99b6"); // RT-DOSE | 1633 loader2->LoadInstance(oracle, "41029085-71718346-811efac4-420e2c15-d39f99b6"); // RT-DOSE |
1416 | 1634 |
1417 boost::this_thread::sleep(boost::posix_time::seconds(1)); | 1635 boost::this_thread::sleep(boost::posix_time::seconds(1)); |