Mercurial > hg > orthanc-stone
annotate Framework/Deprecated/Radiography/RadiographyAlphaLayer.cpp @ 1510:1005c1cbe4dd
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 09:45:27 +0200 |
parents | 30deba7bc8e2 |
children |
rev | line source |
---|---|
430 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
1280
1c7ae79c426d
fix copyright years
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1238
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
430 | 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 "RadiographyAlphaLayer.h" | |
23 | |
24 #include "RadiographyScene.h" | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1398
diff
changeset
|
25 #include "../Toolbox/ImageGeometry.h" |
430 | 26 |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1398
diff
changeset
|
27 #include <Compatibility.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1398
diff
changeset
|
28 #include <Images/Image.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1398
diff
changeset
|
29 #include <OrthancException.h> |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1280
diff
changeset
|
30 |
430 | 31 |
32 namespace OrthancStone | |
33 { | |
34 | |
35 void RadiographyAlphaLayer::SetAlpha(Orthanc::ImageAccessor* image) | |
36 { | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1280
diff
changeset
|
37 std::unique_ptr<Orthanc::ImageAccessor> raii(image); |
430 | 38 |
39 if (image == NULL) | |
40 { | |
41 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
42 } | |
43 | |
44 if (image->GetFormat() != Orthanc::PixelFormat_Grayscale8) | |
45 { | |
46 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
47 } | |
48 | |
49 SetSize(image->GetWidth(), image->GetHeight()); | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1280
diff
changeset
|
50 |
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1280
diff
changeset
|
51 #if __cplusplus < 201103L |
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1280
diff
changeset
|
52 alpha_.reset(raii.release()); |
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1280
diff
changeset
|
53 #else |
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1280
diff
changeset
|
54 alpha_ = std::move(raii); |
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1280
diff
changeset
|
55 #endif |
503
77e0eb83ff63
layers are now Observable and emitting LayerEdited messages
amazy
parents:
432
diff
changeset
|
56 |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
503
diff
changeset
|
57 BroadcastMessage(RadiographyLayer::LayerEditedMessage(*this)); |
430 | 58 } |
59 | |
60 void RadiographyAlphaLayer::Render(Orthanc::ImageAccessor& buffer, | |
61 const AffineTransform2D& viewTransform, | |
1196
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
62 ImageInterpolation interpolation, |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
63 float windowCenter, |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
64 float windowWidth, |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
65 bool applyWindowing) const |
430 | 66 { |
67 if (alpha_.get() == NULL) | |
68 { | |
69 return; | |
70 } | |
71 | |
72 if (buffer.GetFormat() != Orthanc::PixelFormat_Float32) | |
73 { | |
74 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
75 } | |
76 | |
77 unsigned int cropX, cropY, cropWidth, cropHeight; | |
78 GetCrop(cropX, cropY, cropWidth, cropHeight); | |
79 | |
80 const AffineTransform2D t = AffineTransform2D::Combine( | |
81 viewTransform, GetTransform(), | |
82 AffineTransform2D::CreateOffset(cropX, cropY)); | |
83 | |
84 Orthanc::ImageAccessor cropped; | |
85 alpha_->GetRegion(cropped, cropX, cropY, cropWidth, cropHeight); | |
86 | |
87 Orthanc::Image tmp(Orthanc::PixelFormat_Grayscale8, buffer.GetWidth(), buffer.GetHeight(), false); | |
88 | |
1201 | 89 unsigned int x1, y1, x2, y2; |
430 | 90 |
1201 | 91 if (!OrthancStone::GetProjectiveTransformExtent(x1, y1, x2, y2, |
92 t.GetHomogeneousMatrix(), | |
93 cropped.GetWidth(), | |
94 cropped.GetHeight(), | |
95 buffer.GetWidth(), | |
96 buffer.GetHeight())) | |
97 { | |
98 return; // layer is outside the buffer | |
99 } | |
100 | |
101 t.Apply(tmp, cropped, interpolation, true /* clear */); | |
430 | 102 |
103 float value = foreground_; | |
104 | |
1196
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
105 if (!applyWindowing) // if applying the windowing, it means we are ie rendering the image for a realtime visualization -> the foreground_ value is the value we want to see on the screen -> don't change it |
430 | 106 { |
1196
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
107 // if not applying the windowing, it means ie that we are saving a dicom image to file and the windowing will be applied by a viewer later on -> we want the "foreground" value to be correct once the windowing will be applied |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
108 value = windowCenter - windowWidth/2 + (foreground_ / 65535.0f) * windowWidth; |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
109 |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
110 if (value < 0.0f) |
430 | 111 { |
1196
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
112 value = 0.0f; |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
113 } |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
114 if (value > 65535.0f) |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
115 { |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
116 value = 65535.0f; |
430 | 117 } |
118 } | |
119 | |
1196
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
120 for (unsigned int y = y1; y <= y2; y++) |
430 | 121 { |
1196
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
122 float *q = reinterpret_cast<float*>(buffer.GetRow(y)) + x1; |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
123 const uint8_t *p = reinterpret_cast<uint8_t*>(tmp.GetRow(y)) + x1; |
430 | 124 |
1196
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
125 for (unsigned int x = x1; x <= x2; x++, p++, q++) |
430 | 126 { |
127 float a = static_cast<float>(*p) / 255.0f; | |
128 | |
129 *q = (a * value + (1.0f - a) * (*q)); | |
130 } | |
131 } | |
132 } | |
133 | |
134 bool RadiographyAlphaLayer::GetRange(float& minValue, | |
135 float& maxValue) const | |
136 { | |
1196
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
137 minValue = 0; |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
138 maxValue = 0; |
430 | 139 |
1196
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
140 if (foreground_ < 0) |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
141 { |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
142 minValue = foreground_; |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
143 } |
430 | 144 |
1196
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
145 if (foreground_ > 0) |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
146 { |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
147 maxValue = foreground_; |
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
148 } |
430 | 149 |
1196
a5f2a6b04a31
RadiographyScene: windowing is now only applied to the Dicom layer
Alain Mazy <alain@mazy.be>
parents:
623
diff
changeset
|
150 return true; |
430 | 151 } |
152 } |