Mercurial > hg > orthanc
comparison Plugins/Samples/GdcmDecoding/OrthancContext.cpp @ 983:80d4f1618b33 plugins
Sample plugin to replace DCMTK by GDCM when decoding images
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 01 Jul 2014 12:01:58 +0200 |
parents | |
children | 501880d76474 |
comparison
equal
deleted
inserted
replaced
982:5983e59ac670 | 983:80d4f1618b33 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, | |
4 * Belgium | |
5 * | |
6 * Permission is hereby granted, free of charge, to any person | |
7 * obtaining a copy of this software and associated documentation | |
8 * files (the "Software"), to deal in the Software without | |
9 * restriction, including without limitation the rights to use, copy, | |
10 * modify, merge, publish, distribute, sublicense, and/or sell copies | |
11 * of the Software, and to permit persons to whom the Software is | |
12 * furnished to do so, subject to the following conditions: | |
13 * | |
14 * The above copyright notice and this permission notice shall be | |
15 * included in all copies or substantial portions of the Software. | |
16 * | |
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 * SOFTWARE. | |
25 **/ | |
26 | |
27 | |
28 #include "OrthancContext.h" | |
29 | |
30 #include <stdexcept> | |
31 | |
32 | |
33 void OrthancContext::Check() | |
34 { | |
35 if (context_ == NULL) | |
36 { | |
37 throw std::runtime_error("The Orthanc plugin context is not initialized"); | |
38 } | |
39 } | |
40 | |
41 | |
42 OrthancContext& OrthancContext::GetInstance() | |
43 { | |
44 static OrthancContext instance; | |
45 return instance; | |
46 } | |
47 | |
48 | |
49 OrthancContext::~OrthancContext() | |
50 { | |
51 if (context_ != NULL) | |
52 { | |
53 throw std::runtime_error("The Orthanc plugin was not properly finalized"); | |
54 } | |
55 } | |
56 | |
57 | |
58 void OrthancContext::ExtractGetArguments(Arguments& arguments, | |
59 const OrthancPluginHttpRequest& request) | |
60 { | |
61 Check(); | |
62 arguments.clear(); | |
63 | |
64 for (uint32_t i = 0; i < request.getCount; i++) | |
65 { | |
66 arguments[request.getKeys[i]] = request.getValues[i]; | |
67 } | |
68 } | |
69 | |
70 | |
71 void OrthancContext::LogError(const std::string& s) | |
72 { | |
73 Check(); | |
74 OrthancPluginLogError(context_, s.c_str()); | |
75 } | |
76 | |
77 | |
78 void OrthancContext::LogWarning(const std::string& s) | |
79 { | |
80 Check(); | |
81 OrthancPluginLogWarning(context_, s.c_str()); | |
82 } | |
83 | |
84 | |
85 void OrthancContext::LogInfo(const std::string& s) | |
86 { | |
87 Check(); | |
88 OrthancPluginLogInfo(context_, s.c_str()); | |
89 } | |
90 | |
91 | |
92 void OrthancContext::Register(const std::string& uri, | |
93 OrthancPluginRestCallback callback) | |
94 { | |
95 Check(); | |
96 OrthancPluginRegisterRestCallback(context_, uri.c_str(), callback); | |
97 } | |
98 | |
99 | |
100 void OrthancContext::GetDicomForInstance(std::string& result, | |
101 const std::string& instanceId) | |
102 { | |
103 Check(); | |
104 OrthancPluginMemoryBuffer buffer; | |
105 | |
106 if (OrthancPluginGetDicomForInstance(context_, &buffer, instanceId.c_str())) | |
107 { | |
108 throw std::runtime_error("No DICOM instance with Orthanc ID: " + instanceId); | |
109 } | |
110 | |
111 if (buffer.size == 0) | |
112 { | |
113 result.clear(); | |
114 } | |
115 else | |
116 { | |
117 result.assign(reinterpret_cast<char*>(buffer.data), buffer.size); | |
118 } | |
119 | |
120 OrthancPluginFreeMemoryBuffer(context_, &buffer); | |
121 } | |
122 | |
123 | |
124 void OrthancContext::CompressAndAnswerPngImage(OrthancPluginRestOutput* output, | |
125 const Orthanc::ImageAccessor& accessor) | |
126 { | |
127 Check(); | |
128 | |
129 OrthancPluginPixelFormat format; | |
130 switch (accessor.GetFormat()) | |
131 { | |
132 case Orthanc::PixelFormat_Grayscale8: | |
133 format = OrthancPluginPixelFormat_Grayscale8; | |
134 break; | |
135 | |
136 case Orthanc::PixelFormat_Grayscale16: | |
137 format = OrthancPluginPixelFormat_Grayscale16; | |
138 break; | |
139 | |
140 case Orthanc::PixelFormat_SignedGrayscale16: | |
141 format = OrthancPluginPixelFormat_SignedGrayscale16; | |
142 break; | |
143 | |
144 case Orthanc::PixelFormat_RGB24: | |
145 format = OrthancPluginPixelFormat_RGB24; | |
146 break; | |
147 | |
148 case Orthanc::PixelFormat_RGBA32: | |
149 format = OrthancPluginPixelFormat_RGBA32; | |
150 break; | |
151 | |
152 default: | |
153 throw std::runtime_error("Unsupported pixel format"); | |
154 } | |
155 | |
156 OrthancPluginCompressAndAnswerPngImage(context_, output, format, accessor.GetWidth(), | |
157 accessor.GetHeight(), accessor.GetPitch(), accessor.GetConstBuffer()); | |
158 } |