comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 2047:438f86ee19fc

toolbox shared by all plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 24 Jun 2016 22:28:25 +0200
parents
children 154f3e73ad3a
comparison
equal deleted inserted replaced
2046:b534834a300e 2047:438f86ee19fc
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
37
38 namespace OrthancPlugins
39 {
40 const char* PluginException::GetErrorDescription(OrthancPluginContext* context) const
41 {
42 const char* description = OrthancPluginGetErrorDescription(context, code_);
43 if (description)
44 {
45 return description;
46 }
47 else
48 {
49 return "No description available";
50 }
51 }
52
53
54 MemoryBuffer::MemoryBuffer(OrthancPluginContext* context) :
55 context_(context)
56 {
57 buffer_.data = NULL;
58 buffer_.size = 0;
59 }
60
61
62 void MemoryBuffer::Clear()
63 {
64 if (buffer_.data != NULL)
65 {
66 OrthancPluginFreeMemoryBuffer(context_, &buffer_);
67 buffer_.data = NULL;
68 buffer_.size = 0;
69 }
70 }
71
72
73 void MemoryBuffer::Assign(OrthancPluginMemoryBuffer& other)
74 {
75 Clear();
76
77 buffer_.data = other.data;
78 buffer_.size = other.size;
79
80 other.data = NULL;
81 other.size = 0;
82 }
83
84
85 void MemoryBuffer::ToString(std::string& target) const
86 {
87 if (buffer_.size == 0)
88 {
89 target.clear();
90 }
91 else
92 {
93 target.assign(reinterpret_cast<const char*>(buffer_.data), buffer_.size);
94 }
95 }
96
97
98 void MemoryBuffer::ToJson(Json::Value& target) const
99 {
100 if (buffer_.data == NULL ||
101 buffer_.size == 0)
102 {
103 throw PluginException(OrthancPluginErrorCode_InternalError);
104 }
105
106 const char* tmp = reinterpret_cast<const char*>(buffer_.data);
107
108 Json::Reader reader;
109 if (!reader.parse(tmp, tmp + buffer_.size, target))
110 {
111 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
112 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
113 }
114 }
115
116
117 bool MemoryBuffer::RestApiGet(const std::string& uri,
118 bool applyPlugins)
119 {
120 Clear();
121
122 OrthancPluginErrorCode error;
123
124 if (applyPlugins)
125 {
126 error = OrthancPluginRestApiGetAfterPlugins(context_, &buffer_, uri.c_str());
127 }
128 else
129 {
130 error = OrthancPluginRestApiGet(context_, &buffer_, uri.c_str());
131 }
132
133 if (error == OrthancPluginErrorCode_Success)
134 {
135 return true;
136 }
137 else if (error == OrthancPluginErrorCode_UnknownResource ||
138 error == OrthancPluginErrorCode_InexistentItem)
139 {
140 return false;
141 }
142 else
143 {
144 throw PluginException(error);
145 }
146 }
147
148
149 bool MemoryBuffer::RestApiPost(const std::string& uri,
150 const char* body,
151 size_t bodySize,
152 bool applyPlugins)
153 {
154 Clear();
155
156 OrthancPluginErrorCode error;
157
158 if (applyPlugins)
159 {
160 error = OrthancPluginRestApiPostAfterPlugins(context_, &buffer_, uri.c_str(), body, bodySize);
161 }
162 else
163 {
164 error = OrthancPluginRestApiPost(context_, &buffer_, uri.c_str(), body, bodySize);
165 }
166
167 if (error == OrthancPluginErrorCode_Success)
168 {
169 return true;
170 }
171 else if (error == OrthancPluginErrorCode_UnknownResource ||
172 error == OrthancPluginErrorCode_InexistentItem)
173 {
174 return false;
175 }
176 else
177 {
178 throw PluginException(error);
179 }
180 }
181
182
183 bool MemoryBuffer::RestApiPut(const std::string& uri,
184 const char* body,
185 size_t bodySize,
186 bool applyPlugins)
187 {
188 Clear();
189
190 OrthancPluginErrorCode error;
191
192 if (applyPlugins)
193 {
194 error = OrthancPluginRestApiPutAfterPlugins(context_, &buffer_, uri.c_str(), body, bodySize);
195 }
196 else
197 {
198 error = OrthancPluginRestApiPut(context_, &buffer_, uri.c_str(), body, bodySize);
199 }
200
201 if (error == OrthancPluginErrorCode_Success)
202 {
203 return true;
204 }
205 else if (error == OrthancPluginErrorCode_UnknownResource ||
206 error == OrthancPluginErrorCode_InexistentItem)
207 {
208 return false;
209 }
210 else
211 {
212 throw PluginException(error);
213 }
214 }
215
216
217 OrthancString::OrthancString(OrthancPluginContext* context,
218 char* str) :
219 context_(context),
220 str_(str)
221 {
222 }
223
224
225 void OrthancString::Clear()
226 {
227 if (str_ != NULL)
228 {
229 OrthancPluginFreeString(context_, str_);
230 str_ = NULL;
231 }
232 }
233
234
235 void OrthancString::ToString(std::string& target) const
236 {
237 if (str_ == NULL)
238 {
239 target.clear();
240 }
241 else
242 {
243 target.assign(str_);
244 }
245 }
246
247
248 void OrthancString::ToJson(Json::Value& target) const
249 {
250 if (str_ == NULL)
251 {
252 OrthancPluginLogError(context_, "Cannot convert an empty memory buffer to JSON");
253 throw PluginException(OrthancPluginErrorCode_InternalError);
254 }
255
256 Json::Reader reader;
257 if (!reader.parse(str_, target))
258 {
259 OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
260 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
261 }
262 }
263
264
265 OrthancConfiguration::OrthancConfiguration(OrthancPluginContext* context) :
266 context_(context)
267 {
268 OrthancString str(context, OrthancPluginGetConfiguration(context));
269
270 if (str.GetContent() == NULL)
271 {
272 OrthancPluginLogError(context, "Cannot access the Orthanc configuration");
273 throw PluginException(OrthancPluginErrorCode_InternalError);
274 }
275
276 str.ToJson(configuration_);
277
278 if (configuration_.type() != Json::objectValue)
279 {
280 OrthancPluginLogError(context, "Unable to read the Orthanc configuration");
281 throw PluginException(OrthancPluginErrorCode_InternalError);
282 }
283 }
284
285
286 OrthancPluginContext* OrthancConfiguration::GetContext() const
287 {
288 if (context_ == NULL)
289 {
290 throw PluginException(OrthancPluginErrorCode_Plugin);
291 }
292 else
293 {
294 return context_;
295 }
296 }
297
298
299 std::string OrthancConfiguration::GetPath(const std::string& key) const
300 {
301 if (path_.empty())
302 {
303 return key;
304 }
305 else
306 {
307 return path_ + "." + key;
308 }
309 }
310
311
312 void OrthancConfiguration::GetSection(OrthancConfiguration& target,
313 const std::string& key) const
314 {
315 assert(configuration_.type() == Json::objectValue);
316
317 target.context_ = context_;
318 target.path_ = GetPath(key);
319
320 if (!configuration_.isMember(key))
321 {
322 target.configuration_ = Json::objectValue;
323 }
324 else
325 {
326 if (configuration_[key].type() != Json::objectValue)
327 {
328 if (context_ != NULL)
329 {
330 std::string s = "The configuration section \"" + target.path_ + "\" is not an associative array as expected";
331 OrthancPluginLogError(context_, s.c_str());
332 }
333
334 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
335 }
336
337 target.configuration_ = configuration_[key];
338 }
339 }
340
341
342 bool OrthancConfiguration::LookupStringValue(std::string& target,
343 const std::string& key) const
344 {
345 assert(configuration_.type() == Json::objectValue);
346
347 if (!configuration_.isMember(key))
348 {
349 return false;
350 }
351
352 if (configuration_[key].type() != Json::stringValue)
353 {
354 if (context_ != NULL)
355 {
356 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a string as expected";
357 OrthancPluginLogError(context_, s.c_str());
358 }
359
360 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
361 }
362
363 target = configuration_[key].asString();
364 return true;
365 }
366
367
368 bool OrthancConfiguration::LookupIntegerValue(int& target,
369 const std::string& key) const
370 {
371 assert(configuration_.type() == Json::objectValue);
372
373 if (!configuration_.isMember(key))
374 {
375 return false;
376 }
377
378 switch (configuration_[key].type())
379 {
380 case Json::intValue:
381 target = configuration_[key].asInt();
382 return true;
383
384 case Json::uintValue:
385 target = configuration_[key].asUInt();
386 return true;
387
388 default:
389 if (context_ != NULL)
390 {
391 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected";
392 OrthancPluginLogError(context_, s.c_str());
393 }
394
395 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
396 }
397 }
398
399
400 bool OrthancConfiguration::LookupUnsignedIntegerValue(unsigned int& target,
401 const std::string& key) const
402 {
403 int tmp;
404 if (!LookupIntegerValue(tmp, key))
405 {
406 return false;
407 }
408
409 if (tmp < 0)
410 {
411 if (context_ != NULL)
412 {
413 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a positive integer as expected";
414 OrthancPluginLogError(context_, s.c_str());
415 }
416
417 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
418 }
419 else
420 {
421 target = static_cast<unsigned int>(tmp);
422 return true;
423 }
424 }
425
426
427 bool OrthancConfiguration::LookupBooleanValue(bool& 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 if (configuration_[key].type() != Json::booleanValue)
438 {
439 if (context_ != NULL)
440 {
441 std::string s = "The configuration option \"" + GetPath(key) + "\" is not a Boolean as expected";
442 OrthancPluginLogError(context_, s.c_str());
443 }
444
445 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
446 }
447
448 target = configuration_[key].asBool();
449 return true;
450 }
451
452
453 bool OrthancConfiguration::LookupFloatValue(float& target,
454 const std::string& key) const
455 {
456 assert(configuration_.type() == Json::objectValue);
457
458 if (!configuration_.isMember(key))
459 {
460 return false;
461 }
462
463 switch (configuration_[key].type())
464 {
465 case Json::realValue:
466 target = configuration_[key].asFloat();
467 return true;
468
469 case Json::intValue:
470 target = configuration_[key].asInt();
471 return true;
472
473 case Json::uintValue:
474 target = configuration_[key].asUInt();
475 return true;
476
477 default:
478 if (context_ != NULL)
479 {
480 std::string s = "The configuration option \"" + GetPath(key) + "\" is not an integer as expected";
481 OrthancPluginLogError(context_, s.c_str());
482 }
483
484 throw PluginException(OrthancPluginErrorCode_BadFileFormat);
485 }
486 }
487
488
489 std::string OrthancConfiguration::GetStringValue(const std::string& key,
490 const std::string& defaultValue) const
491 {
492 std::string tmp;
493 if (LookupStringValue(tmp, key))
494 {
495 return tmp;
496 }
497 else
498 {
499 return defaultValue;
500 }
501 }
502
503
504 int OrthancConfiguration::GetIntegerValue(const std::string& key,
505 int defaultValue) const
506 {
507 int tmp;
508 if (LookupIntegerValue(tmp, key))
509 {
510 return tmp;
511 }
512 else
513 {
514 return defaultValue;
515 }
516 }
517
518
519 unsigned int OrthancConfiguration::GetUnsignedIntegerValue(const std::string& key,
520 unsigned int defaultValue) const
521 {
522 unsigned int tmp;
523 if (LookupUnsignedIntegerValue(tmp, key))
524 {
525 return tmp;
526 }
527 else
528 {
529 return defaultValue;
530 }
531 }
532
533
534 bool OrthancConfiguration::GetBooleanValue(const std::string& key,
535 bool defaultValue) const
536 {
537 bool tmp;
538 if (LookupBooleanValue(tmp, key))
539 {
540 return tmp;
541 }
542 else
543 {
544 return defaultValue;
545 }
546 }
547
548
549 float OrthancConfiguration::GetFloatValue(const std::string& key,
550 float defaultValue) const
551 {
552 float tmp;
553 if (LookupFloatValue(tmp, key))
554 {
555 return tmp;
556 }
557 else
558 {
559 return defaultValue;
560 }
561 }
562
563
564 void OrthancImage::Clear()
565 {
566 if (image_ != NULL)
567 {
568 OrthancPluginFreeImage(context_, image_);
569 image_ = NULL;
570 }
571 }
572
573
574 void OrthancImage::CheckImageAvailable()
575 {
576 if (image_ == NULL)
577 {
578 OrthancPluginLogError(context_, "Trying to access a NULL image");
579 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
580 }
581 }
582
583
584 OrthancImage::Image(OrthancPluginContext* context) :
585 context_(context),
586 image_(NULL)
587 {
588 if (context == NULL)
589 {
590 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
591 }
592 }
593
594
595 OrthancImage::Image(OrthancPluginContext* context,
596 OrthancPluginImage* image) :
597 context_(context),
598 image_(image)
599 {
600 if (context == NULL)
601 {
602 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
603 }
604 }
605
606
607 OrthancImage::Image(OrthancPluginContext* context,
608 OrthancPluginPixelFormat format,
609 uint32_t width,
610 uint32_t height) :
611 context_(context)
612 {
613 if (context == NULL)
614 {
615 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
616 }
617 else
618 {
619 image_ = OrthancPluginCreateImage(context, format, width, height);
620 }
621 }
622
623
624 void OrthancImage::UncompressPngImage(const void* data,
625 size_t size)
626 {
627 Clear();
628 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Png);
629 if (image_ == NULL)
630 {
631 OrthancPluginLogError(context_, "Cannot uncompress a PNG image");
632 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
633 }
634 }
635
636
637 void OrthancImage::UncompressJpegImage(const void* data,
638 size_t size)
639 {
640 Clear();
641 image_ = OrthancPluginUncompressImage(context_, data, size, OrthancPluginImageFormat_Jpeg);
642 if (image_ == NULL)
643 {
644 OrthancPluginLogError(context_, "Cannot uncompress a JPEG image");
645 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
646 }
647 }
648
649
650 void OrthancImage::DecodeDicomImage(const void* data,
651 size_t size,
652 unsigned int frame)
653 {
654 Clear();
655 image_ = OrthancPluginDecodeDicomImage(context_, data, size, frame);
656 if (image_ == NULL)
657 {
658 OrthancPluginLogError(context_, "Cannot uncompress a DICOM image");
659 throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
660 }
661 }
662
663
664 OrthancPluginPixelFormat OrthancImage::GetPixelFormat()
665 {
666 CheckImageAvailable();
667 return OrthancPluginGetImagePixelFormat(context_, image_);
668 }
669
670
671 unsigned int OrthancImage::GetWidth()
672 {
673 CheckImageAvailable();
674 return OrthancPluginGetImageWidth(context_, image_);
675 }
676
677
678 unsigned int OrthancImage::GetHeight()
679 {
680 CheckImageAvailable();
681 return OrthancPluginGetImageHeight(context_, image_);
682 }
683
684
685 unsigned int OrthancImage::GetPitch()
686 {
687 CheckImageAvailable();
688 return OrthancPluginGetImagePitch(context_, image_);
689 }
690
691
692 const void* OrthancImage::GetBuffer()
693 {
694 CheckImageAvailable();
695 return OrthancPluginGetImageBuffer(context_, image_);
696 }
697
698
699 void OrthancImage::CompressPngImage(MemoryBuffer& target)
700 {
701 CheckImageAvailable();
702
703 OrthancPluginMemoryBuffer tmp;
704 OrthancPluginCompressPngImage(context_, &tmp, GetPixelFormat(),
705 GetWidth(), GetHeight(), GetPitch(), GetBuffer());
706
707 target.Assign(tmp);
708 }
709
710
711 void OrthancImage::CompressJpegImage(MemoryBuffer& target,
712 uint8_t quality)
713 {
714 CheckImageAvailable();
715
716 OrthancPluginMemoryBuffer tmp;
717 OrthancPluginCompressJpegImage(context_, &tmp, GetPixelFormat(),
718 GetWidth(), GetHeight(), GetPitch(), GetBuffer(), quality);
719
720 target.Assign(tmp);
721 }
722
723
724 void OrthancImage::AnswerPngImage(OrthancPluginRestOutput* output)
725 {
726 CheckImageAvailable();
727 OrthancPluginCompressAndAnswerPngImage(context_, output, GetPixelFormat(),
728 GetWidth(), GetHeight(), GetPitch(), GetBuffer());
729 }
730
731
732 void OrthancImage::AnswerJpegImage(OrthancPluginRestOutput* output,
733 uint8_t quality)
734 {
735 CheckImageAvailable();
736 OrthancPluginCompressAndAnswerJpegImage(context_, output, GetPixelFormat(),
737 GetWidth(), GetHeight(), GetPitch(), GetBuffer(), quality);
738 }
739
740
741 bool RestApiGetJson(Json::Value& result,
742 OrthancPluginContext* context,
743 const std::string& uri,
744 bool applyPlugins)
745 {
746 MemoryBuffer answer(context);
747 if (!answer.RestApiGet(uri, applyPlugins))
748 {
749 return false;
750 }
751 else
752 {
753 answer.ToJson(result);
754 return true;
755 }
756 }
757
758
759 bool RestApiPostJson(Json::Value& result,
760 OrthancPluginContext* context,
761 const std::string& uri,
762 const char* body,
763 size_t bodySize,
764 bool applyPlugins)
765 {
766 MemoryBuffer answer(context);
767 if (!answer.RestApiPost(uri, body, bodySize, applyPlugins))
768 {
769 return false;
770 }
771 else
772 {
773 answer.ToJson(result);
774 return true;
775 }
776 }
777
778
779 bool RestApiPutJson(Json::Value& result,
780 OrthancPluginContext* context,
781 const std::string& uri,
782 const char* body,
783 size_t bodySize,
784 bool applyPlugins)
785 {
786 MemoryBuffer answer(context);
787 if (!answer.RestApiPut(uri, body, bodySize, applyPlugins))
788 {
789 return false;
790 }
791 else
792 {
793 answer.ToJson(result);
794 return true;
795 }
796 }
797
798
799 bool RestApiDelete(OrthancPluginContext* context,
800 const std::string& uri,
801 bool applyPlugins)
802 {
803 OrthancPluginErrorCode error;
804
805 if (applyPlugins)
806 {
807 error = OrthancPluginRestApiDeleteAfterPlugins(context, uri.c_str());
808 }
809 else
810 {
811 error = OrthancPluginRestApiDelete(context, uri.c_str());
812 }
813
814 if (error == OrthancPluginErrorCode_Success)
815 {
816 return true;
817 }
818 else if (error == OrthancPluginErrorCode_UnknownResource ||
819 error == OrthancPluginErrorCode_InexistentItem)
820 {
821 return false;
822 }
823 else
824 {
825 throw PluginException(error);
826 }
827 }
828 }
829