comparison Framework/Radiography/RadiographyDicomLayer.cpp @ 430:b85f635f1eb5 am-vsol-upgrade

added serialization for RadiographyScene
author am@osimis.io
date Thu, 29 Nov 2018 15:11:19 +0100
parents
children 4eb96c6b4e96
comparison
equal deleted inserted replaced
429:c7fb700a7d12 430:b85f635f1eb5
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "RadiographyDicomLayer.h"
23
24 #include "RadiographyScene.h"
25 #include "../Toolbox/DicomFrameConverter.h"
26
27 #include <Core/OrthancException.h>
28 #include <Core/Images/Image.h>
29 #include <Core/Images/ImageProcessing.h>
30 #include <Plugins/Samples/Common/DicomDatasetReader.h>
31
32 static OrthancPlugins::DicomTag ConvertTag(const Orthanc::DicomTag& tag)
33 {
34 return OrthancPlugins::DicomTag(tag.GetGroup(), tag.GetElement());
35 }
36
37 namespace OrthancStone
38 {
39
40 void RadiographyDicomLayer::ApplyConverter()
41 {
42 if (source_.get() != NULL &&
43 converter_.get() != NULL)
44 {
45 converted_.reset(converter_->ConvertFrame(*source_));
46 }
47 }
48
49
50 void RadiographyDicomLayer::SetDicomTags(const OrthancPlugins::FullOrthancDataset& dataset)
51 {
52 converter_.reset(new DicomFrameConverter);
53 converter_->ReadParameters(dataset);
54 ApplyConverter();
55
56 std::string tmp;
57 Vector pixelSpacing;
58
59 if (dataset.GetStringValue(tmp, ConvertTag(Orthanc::DICOM_TAG_PIXEL_SPACING)) &&
60 LinearAlgebra::ParseVector(pixelSpacing, tmp) &&
61 pixelSpacing.size() == 2)
62 {
63 SetPixelSpacing(pixelSpacing[0], pixelSpacing[1]);
64 }
65
66 //SetPan(-0.5 * GetPixelSpacingX(), -0.5 * GetPixelSpacingY());
67
68 OrthancPlugins::DicomDatasetReader reader(dataset);
69
70 unsigned int width, height;
71 if (!reader.GetUnsignedIntegerValue(width, ConvertTag(Orthanc::DICOM_TAG_COLUMNS)) ||
72 !reader.GetUnsignedIntegerValue(height, ConvertTag(Orthanc::DICOM_TAG_ROWS)))
73 {
74 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
75 }
76 else
77 {
78 SetSize(width, height);
79 }
80 }
81
82 void RadiographyDicomLayer::SetSourceImage(Orthanc::ImageAccessor* image) // Takes ownership
83 {
84 std::auto_ptr<Orthanc::ImageAccessor> raii(image);
85
86 if (image == NULL)
87 {
88 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
89 }
90
91 SetSize(image->GetWidth(), image->GetHeight());
92
93 source_ = raii;
94 ApplyConverter();
95 }
96
97 void RadiographyDicomLayer::Render(Orthanc::ImageAccessor& buffer,
98 const AffineTransform2D& viewTransform,
99 ImageInterpolation interpolation) const
100 {
101 if (converted_.get() != NULL)
102 {
103 if (converted_->GetFormat() != Orthanc::PixelFormat_Float32)
104 {
105 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
106 }
107
108 unsigned int cropX, cropY, cropWidth, cropHeight;
109 GetCrop(cropX, cropY, cropWidth, cropHeight);
110
111 AffineTransform2D t = AffineTransform2D::Combine(
112 viewTransform, GetTransform(),
113 AffineTransform2D::CreateOffset(cropX, cropY));
114
115 Orthanc::ImageAccessor cropped;
116 converted_->GetRegion(cropped, cropX, cropY, cropWidth, cropHeight);
117
118 t.Apply(buffer, cropped, interpolation, false);
119 }
120 }
121
122
123 bool RadiographyDicomLayer::GetDefaultWindowing(float& center,
124 float& width) const
125 {
126 if (converter_.get() != NULL &&
127 converter_->HasDefaultWindow())
128 {
129 center = static_cast<float>(converter_->GetDefaultWindowCenter());
130 width = static_cast<float>(converter_->GetDefaultWindowWidth());
131 return true;
132 }
133 else
134 {
135 return false;
136 }
137 }
138
139
140 bool RadiographyDicomLayer::GetRange(float& minValue,
141 float& maxValue) const
142 {
143 if (converted_.get() != NULL)
144 {
145 if (converted_->GetFormat() != Orthanc::PixelFormat_Float32)
146 {
147 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
148 }
149
150 Orthanc::ImageProcessing::GetMinMaxFloatValue(minValue, maxValue, *converted_);
151 return true;
152 }
153 else
154 {
155 return false;
156 }
157 }
158
159 }