comparison Framework/Scene2D/LookupTableTextureSceneLayer.cpp @ 860:238693c3bc51 am-dev

merge default -> am-dev
author Alain Mazy <alain@mazy.be>
date Mon, 24 Jun 2019 14:35:00 +0200
parents 11fc84650e4b
children 391fb6d6905d
comparison
equal deleted inserted replaced
856:a6e17a5a39e7 860:238693c3bc51
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-2019 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 "LookupTableTextureSceneLayer.h"
23
24 #include <Core/Images/Image.h>
25 #include <Core/Images/ImageProcessing.h>
26 #include <Core/OrthancException.h>
27
28 namespace OrthancStone
29 {
30 static void StringToVector(std::vector<uint8_t>& target,
31 const std::string& source)
32 {
33 target.resize(source.size());
34
35 for (size_t i = 0; i < source.size(); i++)
36 {
37 target[i] = source[i];
38 }
39 }
40
41
42 LookupTableTextureSceneLayer::LookupTableTextureSceneLayer(const Orthanc::ImageAccessor& texture)
43 {
44 {
45 std::auto_ptr<Orthanc::ImageAccessor> t(
46 new Orthanc::Image(Orthanc::PixelFormat_Float32,
47 texture.GetWidth(),
48 texture.GetHeight(),
49 false));
50
51 Orthanc::ImageProcessing::Convert(*t, texture);
52 SetTexture(t.release());
53 }
54
55 SetLookupTableGrayscale();
56 SetRange(0, 1);
57 }
58
59
60 void LookupTableTextureSceneLayer::SetLookupTableGrayscale()
61 {
62 std::vector<uint8_t> rgb(3 * 256);
63
64 for (size_t i = 0; i < 256; i++)
65 {
66 rgb[3 * i] = static_cast<uint8_t>(i);
67 rgb[3 * i + 1] = static_cast<uint8_t>(i);
68 rgb[3 * i + 2] = static_cast<uint8_t>(i);
69 }
70
71 SetLookupTableRgb(rgb);
72 }
73
74
75 void LookupTableTextureSceneLayer::SetLookupTableRgb(const std::vector<uint8_t>& lut)
76 {
77 if (lut.size() != 3 * 256)
78 {
79 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
80 }
81
82 lut_.resize(4 * 256);
83
84 for (size_t i = 0; i < 256; i++)
85 {
86 // Premultiplied alpha
87
88 if (i == 0)
89 {
90 // Make zero transparent
91 lut_[4 * i] = 0; // R
92 lut_[4 * i + 1] = 0; // G
93 lut_[4 * i + 2] = 0; // B
94 lut_[4 * i + 3] = 0; // A
95 }
96 else
97 {
98 float a = static_cast<float>(i) / 255.0f;
99
100 float r = static_cast<float>(lut[3 * i]) * a;
101 float g = static_cast<float>(lut[3 * i + 1]) * a;
102 float b = static_cast<float>(lut[3 * i + 2]) * a;
103
104 lut_[4 * i] = static_cast<uint8_t>(std::floor(r));
105 lut_[4 * i + 1] = static_cast<uint8_t>(std::floor(g));
106 lut_[4 * i + 2] = static_cast<uint8_t>(std::floor(b));
107 lut_[4 * i + 3] = static_cast<uint8_t>(std::floor(a * 255.0f));
108 }
109 }
110
111 IncrementRevision();
112 }
113
114
115 void LookupTableTextureSceneLayer::SetLookupTable(const std::vector<uint8_t>& lut)
116 {
117 if (lut.size() == 4 * 256)
118 {
119 lut_ = lut;
120 IncrementRevision();
121 }
122 else if (lut.size() == 3 * 256)
123 {
124 SetLookupTableRgb(lut);
125 }
126 else
127 {
128 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
129 }
130 }
131
132
133 void LookupTableTextureSceneLayer::SetLookupTable(const std::string& lut)
134 {
135 std::vector<uint8_t> tmp;
136 StringToVector(tmp, lut);
137 SetLookupTable(tmp);
138 }
139
140
141 void LookupTableTextureSceneLayer::SetRange(float minValue,
142 float maxValue)
143 {
144 if (minValue > maxValue)
145 {
146 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
147 }
148 else
149 {
150 minValue_ = minValue;
151 maxValue_ = maxValue;
152 IncrementRevision();
153 }
154 }
155
156
157 void LookupTableTextureSceneLayer::FitRange()
158 {
159 Orthanc::ImageProcessing::GetMinMaxFloatValue(minValue_, maxValue_, GetTexture());
160 assert(minValue_ <= maxValue_);
161
162 IncrementRevision();
163 }
164
165
166 ISceneLayer* LookupTableTextureSceneLayer::Clone() const
167 {
168 std::auto_ptr<LookupTableTextureSceneLayer> cloned
169 (new LookupTableTextureSceneLayer(GetTexture()));
170
171 cloned->CopyParameters(*this);
172 cloned->minValue_ = minValue_;
173 cloned->maxValue_ = maxValue_;
174 cloned->lut_ = lut_;
175
176 return cloned.release();
177 }
178 }