Mercurial > hg > orthanc-stone
comparison Framework/Toolbox/DicomFrameConverter.cpp @ 0:351ab0da0150
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 14 Oct 2016 15:34:11 +0200 |
parents | |
children | 4b7e0244881f |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:351ab0da0150 |
---|---|
1 /** | |
2 * Stone of Orthanc | |
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 "DicomFrameConverter.h" | |
34 | |
35 #include "../Orthanc/Core/Images/Image.h" | |
36 #include "../Orthanc/Core/Images/ImageProcessing.h" | |
37 #include "../Orthanc/Core/OrthancException.h" | |
38 #include "../Orthanc/Core/Toolbox.h" | |
39 | |
40 namespace OrthancStone | |
41 { | |
42 void DicomFrameConverter::SetDefaultParameters() | |
43 { | |
44 isSigned_ = true; | |
45 isColor_ = false; | |
46 hasRescale_ = false; | |
47 rescaleIntercept_ = 0; | |
48 rescaleSlope_ = 1; | |
49 defaultWindowCenter_ = 128; | |
50 defaultWindowWidth_ = 256; | |
51 } | |
52 | |
53 | |
54 Orthanc::PixelFormat DicomFrameConverter::GetExpectedPixelFormat() const | |
55 { | |
56 // TODO Add more checks, e.g. on the number of bytes per value | |
57 // (cf. DicomImageInformation.h in Orthanc) | |
58 | |
59 if (isColor_) | |
60 { | |
61 return Orthanc::PixelFormat_RGB24; | |
62 } | |
63 else if (isSigned_) | |
64 { | |
65 return Orthanc::PixelFormat_SignedGrayscale16; | |
66 } | |
67 else | |
68 { | |
69 return Orthanc::PixelFormat_Grayscale16; | |
70 } | |
71 } | |
72 | |
73 | |
74 void DicomFrameConverter::ReadParameters(const DicomDataset& dicom) | |
75 { | |
76 SetDefaultParameters(); | |
77 | |
78 if (dicom.HasTag(DICOM_TAG_WINDOW_CENTER)) | |
79 { | |
80 Vector c, w; | |
81 dicom.GetVectorValue(c, DICOM_TAG_WINDOW_CENTER); | |
82 dicom.GetVectorValue(w, DICOM_TAG_WINDOW_WIDTH); | |
83 | |
84 if (c.size() > 0 && w.size() > 0) | |
85 { | |
86 defaultWindowCenter_ = static_cast<float>(c[0]); | |
87 defaultWindowWidth_ = static_cast<float>(w[0]); | |
88 } | |
89 } | |
90 | |
91 isSigned_ = (dicom.GetIntegerValue(DICOM_TAG_PIXEL_REPRESENTATION) == 1); // Type 1 tag, must be present | |
92 | |
93 if (dicom.HasTag(DICOM_TAG_RESCALE_INTERCEPT)) | |
94 { | |
95 rescaleIntercept_ = dicom.GetFloatValue(DICOM_TAG_RESCALE_INTERCEPT); | |
96 rescaleSlope_ = dicom.GetFloatValue(DICOM_TAG_RESCALE_SLOPE); | |
97 hasRescale_ = true; | |
98 } | |
99 | |
100 std::string photometric = dicom.GetStringValue(DICOM_TAG_PHOTOMETRIC_INTERPRETATION); // Type 1 tag, must be present | |
101 photometric = Orthanc::Toolbox::StripSpaces(photometric); | |
102 isColor_ = (photometric != "MONOCHROME1" && | |
103 photometric != "MONOCHROME2"); | |
104 } | |
105 | |
106 | |
107 void DicomFrameConverter::ConvertFrame(std::auto_ptr<Orthanc::ImageAccessor>& source) const | |
108 { | |
109 assert(sizeof(float) == 4); | |
110 | |
111 if (source.get() == NULL) | |
112 { | |
113 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
114 } | |
115 | |
116 Orthanc::PixelFormat sourceFormat = source->GetFormat(); | |
117 | |
118 if (sourceFormat != GetExpectedPixelFormat()) | |
119 { | |
120 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
121 } | |
122 | |
123 if (sourceFormat == Orthanc::PixelFormat_RGB24) | |
124 { | |
125 // No conversion has to be done | |
126 return; | |
127 } | |
128 | |
129 assert(sourceFormat == Orthanc::PixelFormat_Grayscale16 || | |
130 sourceFormat == Orthanc::PixelFormat_SignedGrayscale16); | |
131 | |
132 // This is the case of a grayscale frame. Convert it to Float32. | |
133 std::auto_ptr<Orthanc::Image> converted(new Orthanc::Image(Orthanc::PixelFormat_Float32, | |
134 source->GetWidth(), | |
135 source->GetHeight())); | |
136 Orthanc::ImageProcessing::Convert(*converted, *source); | |
137 | |
138 source.reset(NULL); // We don't need the source frame anymore | |
139 | |
140 // Correct rescale slope/intercept if need be | |
141 if (hasRescale_) | |
142 { | |
143 for (unsigned int y = 0; y < converted->GetHeight(); y++) | |
144 { | |
145 float* p = reinterpret_cast<float*>(converted->GetRow(y)); | |
146 for (unsigned int x = 0; x < converted->GetWidth(); x++, p++) | |
147 { | |
148 float value = *p; | |
149 | |
150 if (hasRescale_) | |
151 { | |
152 value = value * rescaleSlope_ + rescaleIntercept_; | |
153 } | |
154 | |
155 *p = value; | |
156 } | |
157 } | |
158 } | |
159 | |
160 source = converted; | |
161 } | |
162 } |