Mercurial > hg > orthanc
comparison Plugins/Samples/GdcmDecoding/Plugin.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 | 0bfdfc0c6b9b |
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 <map> | |
29 #include <string> | |
30 #include <stdexcept> | |
31 #include <sstream> | |
32 | |
33 #include "OrthancContext.h" | |
34 #include "../../../Core/ImageFormats/ImageProcessing.h" | |
35 | |
36 #include <gdcmReader.h> | |
37 #include <gdcmImageReader.h> | |
38 | |
39 | |
40 static bool GetOrthancPixelFormat(Orthanc::PixelFormat& format, | |
41 const gdcm::Image& image) | |
42 { | |
43 if (image.GetPlanarConfiguration() != 0 && | |
44 image.GetPixelFormat().GetSamplesPerPixel() != 1) | |
45 { | |
46 OrthancContext::GetInstance().LogError("Planar configurations are not supported"); | |
47 return false; | |
48 } | |
49 | |
50 if (image.GetPixelFormat().GetSamplesPerPixel() == 1) | |
51 { | |
52 switch (image.GetPixelFormat().GetScalarType()) | |
53 { | |
54 case gdcm::PixelFormat::UINT8: | |
55 format = Orthanc::PixelFormat_Grayscale8; | |
56 return true; | |
57 | |
58 case gdcm::PixelFormat::UINT16: | |
59 format = Orthanc::PixelFormat_Grayscale16; | |
60 return true; | |
61 | |
62 case gdcm::PixelFormat::INT16: | |
63 format = Orthanc::PixelFormat_SignedGrayscale16; | |
64 return true; | |
65 | |
66 default: | |
67 return false; | |
68 } | |
69 } | |
70 else if (image.GetPixelFormat().GetSamplesPerPixel() == 3 && | |
71 image.GetPixelFormat().GetScalarType() == gdcm::PixelFormat::UINT8) | |
72 { | |
73 format = Orthanc::PixelFormat_RGB24; | |
74 } | |
75 else if (image.GetPixelFormat().GetSamplesPerPixel() == 4 && | |
76 image.GetPixelFormat().GetScalarType() == gdcm::PixelFormat::UINT8) | |
77 { | |
78 format = Orthanc::PixelFormat_RGBA32; | |
79 } | |
80 | |
81 return false; | |
82 } | |
83 | |
84 | |
85 ORTHANC_PLUGINS_API int32_t DecodeImage(OrthancPluginRestOutput* output, | |
86 const char* url, | |
87 const OrthancPluginHttpRequest* request) | |
88 { | |
89 std::string instance(request->groups[0]); | |
90 std::string outputFormat(request->groups[1]); | |
91 OrthancContext::GetInstance().LogWarning("Using GDCM to decode instance " + instance); | |
92 | |
93 // Download the request DICOM instance from Orthanc into a memory buffer | |
94 std::string dicom; | |
95 OrthancContext::GetInstance().GetDicomForInstance(dicom, instance); | |
96 | |
97 // Prepare a memory stream over the DICOM instance | |
98 std::stringstream stream(dicom); | |
99 | |
100 // Parse the DICOM instance using GDCM | |
101 gdcm::ImageReader imageReader; | |
102 imageReader.SetStream(stream); | |
103 if (!imageReader.Read()) | |
104 { | |
105 OrthancContext::GetInstance().LogError("GDCM cannot extract an image from this DICOM instance"); | |
106 return -1; // Error | |
107 } | |
108 | |
109 gdcm::Image& image = imageReader.GetImage(); | |
110 | |
111 // Log information about the decoded image | |
112 char tmp[1024]; | |
113 sprintf(tmp, "Image format: %dx%d %s with %d color channel(s)", image.GetRows(), image.GetColumns(), | |
114 image.GetPixelFormat().GetScalarTypeAsString(), image.GetPixelFormat().GetSamplesPerPixel()); | |
115 OrthancContext::GetInstance().LogWarning(tmp); | |
116 | |
117 // Create a read-only accessor to the bitmap decoded by GDCM | |
118 Orthanc::PixelFormat format; | |
119 if (!GetOrthancPixelFormat(format, image)) | |
120 { | |
121 OrthancContext::GetInstance().LogError("This sample plugin does not support this image format"); | |
122 return -1; // Error | |
123 } | |
124 | |
125 Orthanc::ImageAccessor decodedImage; | |
126 std::vector<char> decodedBuffer(image.GetBufferLength()); | |
127 | |
128 if (decodedBuffer.size()) | |
129 { | |
130 image.GetBuffer(&decodedBuffer[0]); | |
131 unsigned int pitch = image.GetColumns() * ::Orthanc::GetBytesPerPixel(format); | |
132 decodedImage.AssignWritable(format, image.GetColumns(), image.GetRows(), pitch, &decodedBuffer[0]); | |
133 } | |
134 else | |
135 { | |
136 // Empty image | |
137 decodedImage.AssignWritable(format, 0, 0, 0, NULL); | |
138 } | |
139 | |
140 | |
141 // Convert the pixel format from GDCM to the format requested by the REST query | |
142 Orthanc::ImageBuffer converted; | |
143 converted.SetWidth(decodedImage.GetWidth()); | |
144 converted.SetHeight(decodedImage.GetHeight()); | |
145 | |
146 if (outputFormat == "preview") | |
147 { | |
148 if (format == Orthanc::PixelFormat_RGB24 || | |
149 format == Orthanc::PixelFormat_RGBA32) | |
150 { | |
151 // Do not rescale color image | |
152 converted.SetFormat(Orthanc::PixelFormat_RGB24); | |
153 } | |
154 else | |
155 { | |
156 converted.SetFormat(Orthanc::PixelFormat_Grayscale8); | |
157 | |
158 // Rescale the image to the [0,255] range | |
159 int64_t a, b; | |
160 Orthanc::ImageProcessing::GetMinMaxValue(a, b, decodedImage); | |
161 | |
162 float offset = -a; | |
163 float scaling = 255.0f / static_cast<float>(b - a); | |
164 Orthanc::ImageProcessing::ShiftScale(decodedImage, offset, scaling); | |
165 } | |
166 } | |
167 else if (outputFormat == "image-uint8") | |
168 { | |
169 converted.SetFormat(Orthanc::PixelFormat_Grayscale8); | |
170 } | |
171 else if (outputFormat == "image-uint16") | |
172 { | |
173 converted.SetFormat(Orthanc::PixelFormat_Grayscale16); | |
174 } | |
175 else if (outputFormat == "image-int16") | |
176 { | |
177 converted.SetFormat(Orthanc::PixelFormat_SignedGrayscale16); | |
178 } | |
179 else | |
180 { | |
181 OrthancContext::GetInstance().LogError("Unknown output format: " + outputFormat); | |
182 return -1; | |
183 } | |
184 | |
185 Orthanc::ImageAccessor convertedAccessor(converted.GetAccessor()); | |
186 Orthanc::ImageProcessing::Convert(convertedAccessor, decodedImage); | |
187 | |
188 | |
189 // Compress the converted image as a PNG file | |
190 OrthancContext::GetInstance().CompressAndAnswerPngImage(output, convertedAccessor); | |
191 | |
192 return 0; // Success | |
193 } | |
194 | |
195 | |
196 extern "C" | |
197 { | |
198 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) | |
199 { | |
200 OrthancContext::GetInstance().Initialize(context); | |
201 OrthancContext::GetInstance().LogWarning("Initializing GDCM decoding"); | |
202 OrthancContext::GetInstance().Register("/instances/([^/]+)/(preview|image-uint8|image-uint16|image-int16)", DecodeImage); | |
203 return 0; | |
204 } | |
205 | |
206 ORTHANC_PLUGINS_API void OrthancPluginFinalize() | |
207 { | |
208 OrthancContext::GetInstance().LogWarning("Finalizing GDCM decoding"); | |
209 OrthancContext::GetInstance().Finalize(); | |
210 } | |
211 | |
212 ORTHANC_PLUGINS_API const char* OrthancPluginGetName() | |
213 { | |
214 return "gdcm-decoding"; | |
215 } | |
216 | |
217 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion() | |
218 { | |
219 return "1.0"; | |
220 } | |
221 } |