comparison Framework/Toolbox/DicomFrameConverter.cpp @ 338:b3b3fa0e3689 am-2

BitmapStack
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 19 Oct 2018 12:50:38 +0200
parents c80b5bddf86b
children b70e9be013e4
comparison
equal deleted inserted replaced
337:c4d4213f095c 338:b3b3fa0e3689
28 #include <Core/OrthancException.h> 28 #include <Core/OrthancException.h>
29 #include <Core/Toolbox.h> 29 #include <Core/Toolbox.h>
30 30
31 namespace OrthancStone 31 namespace OrthancStone
32 { 32 {
33 static const Orthanc::DicomTag IMAGE_TAGS[] =
34 {
35 Orthanc::DICOM_TAG_BITS_STORED,
36 Orthanc::DICOM_TAG_DOSE_GRID_SCALING,
37 Orthanc::DICOM_TAG_PHOTOMETRIC_INTERPRETATION,
38 Orthanc::DICOM_TAG_PIXEL_REPRESENTATION,
39 Orthanc::DICOM_TAG_RESCALE_INTERCEPT,
40 Orthanc::DICOM_TAG_RESCALE_SLOPE,
41 Orthanc::DICOM_TAG_WINDOW_CENTER,
42 Orthanc::DICOM_TAG_WINDOW_WIDTH
43 };
44
45
33 void DicomFrameConverter::SetDefaultParameters() 46 void DicomFrameConverter::SetDefaultParameters()
34 { 47 {
35 isSigned_ = true; 48 isSigned_ = true;
36 isColor_ = false; 49 isColor_ = false;
37 hasRescale_ = false; 50 hasRescale_ = false;
38 rescaleIntercept_ = 0; 51 rescaleIntercept_ = 0;
39 rescaleSlope_ = 1; 52 rescaleSlope_ = 1;
53 hasDefaultWindow_ = false;
40 defaultWindowCenter_ = 128; 54 defaultWindowCenter_ = 128;
41 defaultWindowWidth_ = 256; 55 defaultWindowWidth_ = 256;
42 expectedPixelFormat_ = Orthanc::PixelFormat_Grayscale16; 56 expectedPixelFormat_ = Orthanc::PixelFormat_Grayscale16;
43 } 57 }
44 58
51 if (LinearAlgebra::ParseVector(c, dicom, Orthanc::DICOM_TAG_WINDOW_CENTER) && 65 if (LinearAlgebra::ParseVector(c, dicom, Orthanc::DICOM_TAG_WINDOW_CENTER) &&
52 LinearAlgebra::ParseVector(w, dicom, Orthanc::DICOM_TAG_WINDOW_WIDTH) && 66 LinearAlgebra::ParseVector(w, dicom, Orthanc::DICOM_TAG_WINDOW_WIDTH) &&
53 c.size() > 0 && 67 c.size() > 0 &&
54 w.size() > 0) 68 w.size() > 0)
55 { 69 {
70 hasDefaultWindow_ = true;
56 defaultWindowCenter_ = static_cast<float>(c[0]); 71 defaultWindowCenter_ = static_cast<float>(c[0]);
57 defaultWindowWidth_ = static_cast<float>(w[0]); 72 defaultWindowWidth_ = static_cast<float>(w[0]);
58 } 73 }
59 74
60 int32_t tmp; 75 int32_t tmp;
137 expectedPixelFormat_ = Orthanc::PixelFormat_Grayscale16; 152 expectedPixelFormat_ = Orthanc::PixelFormat_Grayscale16;
138 } 153 }
139 } 154 }
140 } 155 }
141 156
142 157
143 void DicomFrameConverter::ConvertFrame(std::auto_ptr<Orthanc::ImageAccessor>& source) const 158 void DicomFrameConverter::ReadParameters(const OrthancPlugins::IDicomDataset& dicom)
159 {
160 Orthanc::DicomMap converted;
161
162 for (size_t i = 0; i < sizeof(IMAGE_TAGS) / sizeof(Orthanc::DicomTag); i++)
163 {
164 OrthancPlugins::DicomTag tag(IMAGE_TAGS[i].GetGroup(), IMAGE_TAGS[i].GetElement());
165
166 std::string value;
167 if (dicom.GetStringValue(value, tag))
168 {
169 converted.SetValue(IMAGE_TAGS[i], value, false);
170 }
171 }
172
173 ReadParameters(converted);
174 }
175
176
177 void DicomFrameConverter::ConvertFrameInplace(std::auto_ptr<Orthanc::ImageAccessor>& source) const
144 { 178 {
145 assert(sizeof(float) == 4); 179 assert(sizeof(float) == 4);
146 180
147 if (source.get() == NULL) 181 if (source.get() == NULL)
148 { 182 {
149 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); 183 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
150 } 184 }
151 185
152 Orthanc::PixelFormat sourceFormat = source->GetFormat(); 186 if (source->GetFormat() == GetExpectedPixelFormat() &&
187 source->GetFormat() == Orthanc::PixelFormat_RGB24)
188 {
189 // No conversion has to be done, check out (*)
190 return;
191 }
192 else
193 {
194 source.reset(ConvertFrame(*source));
195 }
196 }
197
198
199 Orthanc::ImageAccessor* DicomFrameConverter::ConvertFrame(const Orthanc::ImageAccessor& source) const
200 {
201 assert(sizeof(float) == 4);
202
203 Orthanc::PixelFormat sourceFormat = source.GetFormat();
153 204
154 if (sourceFormat != GetExpectedPixelFormat()) 205 if (sourceFormat != GetExpectedPixelFormat())
155 { 206 {
156 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); 207 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat);
157 } 208 }
158 209
159 if (sourceFormat == Orthanc::PixelFormat_RGB24) 210 if (sourceFormat == Orthanc::PixelFormat_RGB24)
160 { 211 {
161 // No conversion has to be done 212 // This is the case of a color image. No conversion has to be done (*)
162 return; 213 std::auto_ptr<Orthanc::Image> converted(new Orthanc::Image(Orthanc::PixelFormat_RGB24,
163 } 214 source.GetWidth(),
164 215 source.GetHeight(),
165 assert(sourceFormat == Orthanc::PixelFormat_Grayscale16 || 216 false));
166 sourceFormat == Orthanc::PixelFormat_Grayscale32 || 217 Orthanc::ImageProcessing::Copy(*converted, source);
167 sourceFormat == Orthanc::PixelFormat_SignedGrayscale16); 218 return converted.release();
168 219 }
169 // This is the case of a grayscale frame. Convert it to Float32. 220 else
170 std::auto_ptr<Orthanc::Image> converted(new Orthanc::Image(Orthanc::PixelFormat_Float32, 221 {
171 source->GetWidth(), 222 assert(sourceFormat == Orthanc::PixelFormat_Grayscale16 ||
172 source->GetHeight(), 223 sourceFormat == Orthanc::PixelFormat_Grayscale32 ||
173 false)); 224 sourceFormat == Orthanc::PixelFormat_SignedGrayscale16);
174 Orthanc::ImageProcessing::Convert(*converted, *source); 225
175 226 // This is the case of a grayscale frame. Convert it to Float32.
176 source.reset(NULL); // We don't need the source frame anymore 227 std::auto_ptr<Orthanc::Image> converted(new Orthanc::Image(Orthanc::PixelFormat_Float32,
177 228 source.GetWidth(),
178 // Correct rescale slope/intercept if need be 229 source.GetHeight(),
179 ApplyRescale(*converted, sourceFormat != Orthanc::PixelFormat_Grayscale32); 230 false));
231 Orthanc::ImageProcessing::Convert(*converted, source);
232
233 // Correct rescale slope/intercept if need be
234 ApplyRescale(*converted, sourceFormat != Orthanc::PixelFormat_Grayscale32);
180 235
181 source = converted; 236 return converted.release();
237 }
182 } 238 }
183 239
184 240
185 void DicomFrameConverter::ApplyRescale(Orthanc::ImageAccessor& image, 241 void DicomFrameConverter::ApplyRescale(Orthanc::ImageAccessor& image,
186 bool useDouble) const 242 bool useDouble) const