comparison Framework/Orthanc/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 1:dc730d11b101

orthanc dependencies
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 22 Oct 2016 21:50:15 +0200
parents
children 77b76c1a213f
comparison
equal deleted inserted replaced
0:4a7a53257c7d 1:dc730d11b101
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "OrthancPluginCppWrapper.h"
34
35 #include <json/reader.h>
36 #include <json/writer.h>
37
38
39 namespace OrthancPlugins
40 {
41 const char* PluginException::GetErrorDescription(OrthancPluginContext* context) const
42 {
43 const char* description = OrthancPluginGetErrorDescription(context, code_);
44 if (description)
45 {
46 return description;
47 }
48 else
49 {
50 return "No description available";
51 }
52 }
53
54
55 void PluginException::Check(OrthancPluginErrorCode code)
56 {
57 if (code != OrthancPluginErrorCode_Success)
58 {
59 throw PluginException(code);
60 }
61 }
62
63
64 void MemoryBuffer::Check(OrthancPluginErrorCode code)
65 {
66 if (code != OrthancPluginErrorCode_Success)
67 {
68 // Prevent using garbage information
69 buffer_.data = NULL;
70 buffer_.size = 0;
71 throw PluginException(code);
72 }
73 }
74
75
76 MemoryBuffer::MemoryBuffer(OrthancPluginContext* context) :
77 context_(context)
78 {
79 buffer_.data = NULL;
80 buffer_.size = 0;
81 }
82
83
84 void MemoryBuffer::Clear()
85 {
86 if (buffer_.data != NULL)
87 {
88 OrthancPluginFreeMemoryBuffer(context_, &buffer_);
89 buffer_.data = NULL;
90 buffer_.size = 0;
91 }
92 }
93
94
95 void MemoryBuffer::Assign(OrthancPluginMemoryBuffer& other)
96 {
97 Clear();
98
99 buffer_.data = other.data;
100 buffer_.size = other.size;
101
102 other.data = NULL;
103 other.size = 0;
104 }
105
106
107 void MemoryBuffer::ToString(std::string& target) const
108 {
109 if (buffer_.size == 0)
110 {
111 target.clear();
112 }
113 else
114 {
115 target.assign(reinterpret_cast<const char*>(buffer_.data), buffer_.size);
116 }
117 }
118
119
120 void MemoryBuffer::ToJson(Json::Value& target) const
121 {
122 if (buffer_.data == NULL ||
123 buffer_.size == 0)
124 {
125 throw PluginException(OrthancPluginErrorCode_InternalError);
126 }
127
128 const char* tmp = reinterpret_cast<const char*>(buffer_.data);
129
130 Json::Reader reader;
131 if (!reader.parse(tmp, tmp + buffer_.size, target))
132 {
133 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
134 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
135 }
136 }
137
138
139 bool MemoryBuffer::RestApiGet(const std::string& uri,
140 bool applyPlugins)
141 {
142 Clear();
143
144 OrthancPluginErrorCode error;
145
146 if (applyPlugins)
147 {
148 error = OrthancPluginRestApiGetAfterPlugins(context_, &buffer_, uri.c_str());
149 }
150 else
151 {
152 error = OrthancPluginRestApiGet(context_, &buffer_, uri.c_str());
153 }
154
155 if (error == OrthancPluginErrorCode_Success)
156 {
157 return true;
158 }
159 else if (error == OrthancPluginErrorCode_UnknownResource ||
160 error == OrthancPluginErrorCode_InexistentItem)
161 {
162 return false;
163 }
164 else
165 {
166 throw PluginException(error);
167 }
168 }
169
170
171 bool MemoryBuffer::RestApiPost(const std::string& uri,
172 const char* body,
173 size_t bodySize,
174 bool applyPlugins)
175 {
176 Clear();
177
178 OrthancPluginErrorCode error;
179
180 if (applyPlugins)
181 {
182 error = OrthancPluginRestApiPostAfterPlugins(context_, &buffer_, uri.c_str(), body, bodySize);
183 }
184 else
185 {
186 error = OrthancPluginRestApiPost(context_, &buffer_, uri.c_str(), body, bodySize);
187 }
188
189 if (error == OrthancPluginErrorCode_Success)
190 {
191 return true;
192 }
193 else if (error == OrthancPluginErrorCode_UnknownResource ||
194 error == OrthancPluginErrorCode_InexistentItem)
195 {
196 return false;
197 }
198 else
199 {
200 throw PluginException(error);
201 }
202 }
203
204
205 bool MemoryBuffer::RestApiPut(const std::string& uri,
206 const char* body,
207 size_t bodySize,
208 bool applyPlugins)
209 {
210 Clear();
211
212 OrthancPluginErrorCode error;
213
214 if (applyPlugins)
215 {
216 error = OrthancPluginRestApiPutAfterPlugins(context_, &buffer_, uri.c_str(), body, bodySize);
217 }
218 else
219 {
220 error = OrthancPluginRestApiPut(context_, &buffer_, uri.c_str(), body, bodySize);
221 }
222
223 if (error == OrthancPluginErrorCode_Success)
224 {
225 return true;
226 }
227 else if (error == OrthancPluginErrorCode_UnknownResource ||
228 error == OrthancPluginErrorCode_InexistentItem)
229 {
230 return false;
231 }
232 else
233 {
234 throw PluginException(error);
235 }
236 }
237
238
239 bool MemoryBuffer::RestApiPost(const std::string& uri,
240 const Json::Value& body,
241 bool applyPlugins)
242 {
243 Json::FastWriter writer;
244 return RestApiPost(uri, writer.write(body), applyPlugins);
245 }
246
247
248 bool MemoryBuffer::RestApiPut(const std::string& uri,
249 const Json::Value& body,
250 bool applyPlugins)
251 {
252 Json::FastWriter writer;
253 return RestApiPut(uri, writer.write(body), applyPlugins);
254 }
255
256
257 void MemoryBuffer::CreateDicom(const Json::Value& tags,
258 OrthancPluginCreateDicomFlags flags)
259 {
260 Clear();
261
262 Json::FastWriter writer;
263 std::string s = writer.write(tags);
264
265 Check(OrthancPluginCreateDicom(context_, &buffer_, s.c_str(), NULL, flags));
266 }
267
268
269 void MemoryBuffer::ReadFile(const std::string& path)
270 {
271 Clear();
272 Check(OrthancPluginReadFile(context_, &buffer_, path.c_str()));
273 }
274
275
276 OrthancString::OrthancString(OrthancPluginContext* context,
277 char* str) :
278 context_(context),
279 str_(str)
280 {
281 }
282
283
284 void OrthancString::Clear()
285 {
286 if (str_ != NULL)
287 {
288 OrthancPluginFreeString(context_, str_);
289 str_ = NULL;
290 }
291 }
292
293
294 void OrthancString::ToString(std::string& target) const
295 {
296 if (str_ == NULL)
297 {
298 target.clear();
299 }
300 else
301 {
302 target.assign(str_);
303 }
304 }
305
306
307 void OrthancString::ToJson(Json::Value& target) const
308 {
309 if (str_ == NULL)
310 {
311 OrthancPluginLogError(context_, "Cannot convert an empty memory buffer to JSON");
312 throw PluginException(OrthancPluginErrorCode_InternalError);
313 }
314
315 Json::Reader reader;
316 if (!reader.parse(str_, target))
317 {
318 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
319 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
320 }
321 }
322
323
324 OrthancConfiguration::OrthancConfiguration(OrthancPluginContext* context) :
325 context_(context)
326 {
327 OrthancString str(context, OrthancPluginGetConfiguration(context));
328
329 if (str.GetContent() == NULL)
330 {
331 OrthancPluginLogError(context, "Cannot access the Orthanc configuration");
332 throw PluginException(OrthancPluginErrorCode_InternalError);
333 }
334
335 str.ToJson(configuration_);
336
337 if (configuration_.type() != Json::objectValue)
338 {
339 OrthancPluginLogError(context, "Unable to read the Orthanc configuration");
340 throw PluginException(OrthancPluginErrorCode_InternalError);
341 }
342 }
343
344
345 OrthancPluginContext* OrthancConfiguration::GetContext() const
346 {
347 if (context_ == NULL)
348 {
349 throw PluginException(OrthancPluginErrorCode_Plugin);
350 }
351 else
352 {
353 return context_;
354 }
355 }
356
357
358 std::string OrthancConfiguration::GetPath(const std::string& key) const
359 {
360 if (path_.empty())
361 {
362 return key;
363 }
364 else
365 {
366 return path_ + "." + key;
367 }
368 }
369
370
371 void OrthancConfiguration::GetSection(OrthancConfiguration& target,
372 const std::string& key) const
373 {
374 assert(configuration_.type() == Json::objectValue);
375
376 target.context_ = context_;
377 target.path_ = GetPath(key);
378
379 if (!configuration_.isMember(key))
380 {
381 target.configuration_ = Json::objectValue;
382 }
383 else
384 {
385 if (configuration_[key].type() != Json::objectValue)
386 {
387 if (context_ != NULL)
388 {
389 std::string s = "The configuration section \"" + target.path_ + "\" is not an associative array as expected";
390 OrthancPluginLogError(context_, s.c_str());
391 }
392
393 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
394 }
395
396 target.configuration_ = configuration_[key];
397 }
398 }
399
400
401 bool OrthancConfiguration::LookupStringValue(std::string& target,
402 const std::string& key) const
403 {
404 assert(configuration_.type() == Json::objectValue);
405
406 if (!configuration_.isMember(key))
407 {
408 return false;
409 }
410
411 if (configuration_[key].type() != Json::stringValue)
412 {
413 if (context_ != NULL)
414 {
415 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a string as expected";
416 OrthancPluginLogError(context_, s.c_str());
417 }
418
419 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
420 }
421
422 target = configuration_[key].asString();
423 return true;
424 }
425
426
427 bool OrthancConfiguration::LookupIntegerValue(int& target,
428 const std::string& key) const
429 {
430 assert(configuration_.type() == Json::objectValue);
431
432 if (!configuration_.isMember(key))
433 {
434 return false;
435 }
436
437 switch (configuration_[key].type())
438 {
439 case Json::intValue:
440 target = configuration_[key].asInt();
441 return true;
442
443 case Json::uintValue:
444 target = configuration_[key].asUInt();
445 return true;
446
447 default:
448 if (context_ != NULL)
449 {
450 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected";
451 OrthancPluginLogError(context_, s.c_str());
452 }
453
454 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
455 }
456 }
457
458
459 bool OrthancConfiguration::LookupUnsignedIntegerValue(unsigned int& target,
460 const std::string& key) const
461 {
462 int tmp;
463 if (!LookupIntegerValue(tmp, key))
464 {
465 return false;
466 }
467
468 if (tmp < 0)
469 {
470 if (context_ != NULL)
471 {
472 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a positive integer as expected";
473 OrthancPluginLogError(context_, s.c_str());
474 }
475
476 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
477 }
478 else
479 {
480 target = static_cast<unsigned int>(tmp);
481 return true;
482 }
483 }
484
485
486 bool OrthancConfiguration::LookupBooleanValue(bool& target,
487 const std::string& key) const
488 {
489 assert(configuration_.type() == Json::objectValue);
490
491 if (!configuration_.isMember(key))
492 {
493 return false;
494 }
495
496 if (configuration_[key].type() != Json::booleanValue)
497 {
498 if (context_ != NULL)
499 {
500 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a Boolean as expected";
501 OrthancPluginLogError(context_, s.c_str());
502 }
503
504 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
505 }
506
507 target = configuration_[key].asBool();
508 return true;
509 }
510
511
512 bool OrthancConfiguration::LookupFloatValue(float& target,
513 const std::string& key) const
514 {
515 assert(configuration_.type() == Json::objectValue);
516
517 if (!configuration_.isMember(key))
518 {
519 return false;
520 }
521
522 switch (configuration_[key].type())
523 {
524 case Json::realValue:
525 target = configuration_[key].asFloat();
526 return true;
527
528 case Json::intValue:
529 target = configuration_[key].asInt();
530 return true;
531
532 case Json::uintValue:
533 target = configuration_[key].asUInt();
534 return true;
535
536 default:
537 if (context_ != NULL)
538 {
539 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected";
540 OrthancPluginLogError(context_, s.c_str());
541 }
542
543 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
544 }
545 }
546
547
548 std::string OrthancConfiguration::GetStringValue(const std::string& key,
549 const std::string& defaultValue) const
550 {
551 std::string tmp;
552 if (LookupStringValue(tmp, key))
553 {
554 return tmp;
555 }
556 else
557 {
558 return defaultValue;
559 }
560 }
561
562
563 int OrthancConfiguration::GetIntegerValue(const std::string& key,
564 int defaultValue) const
565 {
566 int tmp;
567 if (LookupIntegerValue(tmp, key))
568 {
569 return tmp;
570 }
571 else
572 {
573 return defaultValue;
574 }
575 }
576
577
578 unsigned int OrthancConfiguration::GetUnsignedIntegerValue(const std::string& key,
579 unsigned int defaultValue) const
580 {
581 unsigned int tmp;
582 if (LookupUnsignedIntegerValue(tmp, key))
583 {
584 return tmp;
585 }
586 else
587 {
588 return defaultValue;
589 }
590 }
591
592
593 bool OrthancConfiguration::GetBooleanValue(const std::string& key,
594 bool defaultValue) const
595 {
596 bool tmp;
597 if (LookupBooleanValue(tmp, key))
598 {
599 return tmp;
600 }
601 else
602 {
603 return defaultValue;
604 }
605 }
606
607
608 float OrthancConfiguration::GetFloatValue(const std::string& key,
609 float defaultValue) const
610 {
611 float tmp;
612 if (LookupFloatValue(tmp, key))
613 {
614 return tmp;
615 }
616 else
617 {
618 return defaultValue;
619 }
620 }
621
622
623 void OrthancImage::Clear()
624 {
625 if (image_ != NULL)
626 {
627 OrthancPluginFreeImage(context_, image_);
628 image_ = NULL;
629 }
630 }
631
632
633 void OrthancImage::CheckImageAvailable()
634 {
635 if (image_ == NULL)
636 {
637 OrthancPluginLogError(context_, "Trying to access a NULL image");
638 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
639 }
640 }
641
642
643 OrthancImage::OrthancImage(OrthancPluginContext* context) :
644 context_(context),
645 image_(NULL)
646 {
647 if (context == NULL)
648 {
649 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
650 }
651 }
652
653
654 OrthancImage::OrthancImage(OrthancPluginContext* context,
655 OrthancPluginImage* image) :
656 context_(context),
657 image_(image)
658 {
659 if (context == NULL)
660 {
661 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
662 }
663 }
664
665
666 OrthancImage::OrthancImage(OrthancPluginContext* context,
667 OrthancPluginPixelFormat format,
668 uint32_t width,
669 uint32_t height) :
670 context_(context)
671 {
672 if (context == NULL)
673 {
674 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
675 }
676 else
677 {
678 image_ = OrthancPluginCreateImage(context, format, width, height);
679 }
680 }
681
682
683 void OrthancImage::UncompressPngImage(const void* data,
684 size_t size)
685 {
686 Clear();
687 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Png);
688 if (image_ == NULL)
689 {
690 OrthancPluginLogError(context_, "Cannot uncompress a PNG image");
691 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
692 }
693 }
694
695
696 void OrthancImage::UncompressJpegImage(const void* data,
697 size_t size)
698 {
699 Clear();
700 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Jpeg);
701 if (image_ == NULL)
702 {
703 OrthancPluginLogError(context_, "Cannot uncompress a JPEG image");
704 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
705 }
706 }
707
708
709 void OrthancImage::DecodeDicomImage(const void* data,
710 size_t size,
711 unsigned int frame)
712 {
713 Clear();
714 image_ = OrthancPluginDecodeDicomImage(context_, data, size, frame);
715 if (image_ == NULL)
716 {
717 OrthancPluginLogError(context_, "Cannot uncompress a DICOM image");
718 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
719 }
720 }
721
722
723 OrthancPluginPixelFormat OrthancImage::GetPixelFormat()
724 {
725 CheckImageAvailable();
726 return OrthancPluginGetImagePixelFormat(context_, image_);
727 }
728
729
730 unsigned int OrthancImage::GetWidth()
731 {
732 CheckImageAvailable();
733 return OrthancPluginGetImageWidth(context_, image_);
734 }
735
736
737 unsigned int OrthancImage::GetHeight()
738 {
739 CheckImageAvailable();
740 return OrthancPluginGetImageHeight(context_, image_);
741 }
742
743
744 unsigned int OrthancImage::GetPitch()
745 {
746 CheckImageAvailable();
747 return OrthancPluginGetImagePitch(context_, image_);
748 }
749
750
751 const void* OrthancImage::GetBuffer()
752 {
753 CheckImageAvailable();
754 return OrthancPluginGetImageBuffer(context_, image_);
755 }
756
757
758 void OrthancImage::CompressPngImage(MemoryBuffer& target)
759 {
760 CheckImageAvailable();
761
762 OrthancPluginMemoryBuffer tmp;
763 OrthancPluginCompressPngImage(context_, &tmp, GetPixelFormat(),
764 GetWidth(), GetHeight(), GetPitch(), GetBuffer());
765
766 target.Assign(tmp);
767 }
768
769
770 void OrthancImage::CompressJpegImage(MemoryBuffer& target,
771 uint8_t quality)
772 {
773 CheckImageAvailable();
774
775 OrthancPluginMemoryBuffer tmp;
776 OrthancPluginCompressJpegImage(context_, &tmp, GetPixelFormat(),
777 GetWidth(), GetHeight(), GetPitch(), GetBuffer(), quality);
778
779 target.Assign(tmp);
780 }
781
782
783 void OrthancImage::AnswerPngImage(OrthancPluginRestOutput* output)
784 {
785 CheckImageAvailable();
786 OrthancPluginCompressAndAnswerPngImage(context_, output, GetPixelFormat(),
787 GetWidth(), GetHeight(), GetPitch(), GetBuffer());
788 }
789
790
791 void OrthancImage::AnswerJpegImage(OrthancPluginRestOutput* output,
792 uint8_t quality)
793 {
794 CheckImageAvailable();
795 OrthancPluginCompressAndAnswerJpegImage(context_, output, GetPixelFormat(),
796 GetWidth(), GetHeight(), GetPitch(), GetBuffer(), quality);
797 }
798
799
800 bool RestApiGet(Json::Value& result,
801 OrthancPluginContext* context,
802 const std::string& uri,
803 bool applyPlugins)
804 {
805 MemoryBuffer answer(context);
806 if (!answer.RestApiGet(uri, applyPlugins))
807 {
808 return false;
809 }
810 else
811 {
812 answer.ToJson(result);
813 return true;
814 }
815 }
816
817
818 bool RestApiPost(Json::Value& result,
819 OrthancPluginContext* context,
820 const std::string& uri,
821 const char* body,
822 size_t bodySize,
823 bool applyPlugins)
824 {
825 MemoryBuffer answer(context);
826 if (!answer.RestApiPost(uri, body, bodySize, applyPlugins))
827 {
828 return false;
829 }
830 else
831 {
832 answer.ToJson(result);
833 return true;
834 }
835 }
836
837
838 bool RestApiPost(Json::Value& result,
839 OrthancPluginContext* context,
840 const std::string& uri,
841 const Json::Value& body,
842 bool applyPlugins)
843 {
844 Json::FastWriter writer;
845 return RestApiPost(result, context, uri, writer.write(body), applyPlugins);
846 }
847
848
849 bool RestApiPut(Json::Value& result,
850 OrthancPluginContext* context,
851 const std::string& uri,
852 const char* body,
853 size_t bodySize,
854 bool applyPlugins)
855 {
856 MemoryBuffer answer(context);
857 if (!answer.RestApiPut(uri, body, bodySize, applyPlugins))
858 {
859 return false;
860 }
861 else
862 {
863 answer.ToJson(result);
864 return true;
865 }
866 }
867
868
869 bool RestApiPut(Json::Value& result,
870 OrthancPluginContext* context,
871 const std::string& uri,
872 const Json::Value& body,
873 bool applyPlugins)
874 {
875 Json::FastWriter writer;
876 return RestApiPut(result, context, uri, writer.write(body), applyPlugins);
877 }
878
879
880 bool RestApiDelete(OrthancPluginContext* context,
881 const std::string& uri,
882 bool applyPlugins)
883 {
884 OrthancPluginErrorCode error;
885
886 if (applyPlugins)
887 {
888 error = OrthancPluginRestApiDeleteAfterPlugins(context, uri.c_str());
889 }
890 else
891 {
892 error = OrthancPluginRestApiDelete(context, uri.c_str());
893 }
894
895 if (error == OrthancPluginErrorCode_Success)
896 {
897 return true;
898 }
899 else if (error == OrthancPluginErrorCode_UnknownResource ||
900 error == OrthancPluginErrorCode_InexistentItem)
901 {
902 return false;
903 }
904 else
905 {
906 throw PluginException(error);
907 }
908 }
909 }
910