comparison Framework/Scene2D/LookupTableTextureSceneLayer.cpp @ 768:55411e7da2f7

LookupTableTextureSceneLayer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 23 May 2019 20:04:33 +0200
parents
children 4ba8892870a2
comparison
equal deleted inserted replaced
767:dce5b067d040 768:55411e7da2f7
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(1);
56 SetRange(0, 1);
57 }
58
59
60 void LookupTableTextureSceneLayer::SetLookupTableGrayscale(float alpha)
61 {
62 std::vector<uint8_t> rgb(3 * 256);
63
64 for (size_t i = 0; i < 256; i++)
65 {
66 rgb[3 * i] = i;
67 rgb[3 * i + 1] = i;
68 rgb[3 * i + 2] = i;
69 }
70
71 SetLookupTableRgb(rgb, alpha);
72 }
73
74
75 void LookupTableTextureSceneLayer::SetLookupTableRgb(const std::vector<uint8_t>& lut,
76 float alpha)
77 {
78 if (lut.size() != 3 * 256 ||
79 alpha < 0 ||
80 alpha > 1)
81 {
82 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
83 }
84
85 lut_.resize(4 * 256);
86
87 for (size_t i = 0; i < 256; i++)
88 {
89 // Premultiplied alpha
90
91 if (i == 0)
92 {
93 // Make zero transparent
94 lut_[4 * i] = 0; // R
95 lut_[4 * i + 1] = 0; // G
96 lut_[4 * i + 2] = 0; // B
97 lut_[4 * i + 3] = 0; // A
98 }
99 else
100 {
101 float r = static_cast<float>(lut[3 * i]) * alpha;
102 float g = static_cast<float>(lut[3 * i + 1]) * alpha;
103 float b = static_cast<float>(lut[3 * i + 2]) * alpha;
104
105 lut_[4 * i] = static_cast<uint8_t>(std::floor(r));
106 lut_[4 * i + 1] = static_cast<uint8_t>(std::floor(g));
107 lut_[4 * i + 2] = static_cast<uint8_t>(std::floor(b));
108 lut_[4 * i + 3] = static_cast<uint8_t>(std::floor(alpha * 255.0f));
109 }
110 }
111
112 IncrementRevision();
113 }
114
115
116 void LookupTableTextureSceneLayer::SetLookupTableRgb(const std::string& lut,
117 float alpha)
118 {
119 std::vector<uint8_t> tmp;
120 StringToVector(tmp, lut);
121 SetLookupTableRgb(tmp, alpha);
122 }
123
124
125 void LookupTableTextureSceneLayer::SetLookupTable(const std::vector<uint8_t>& lut)
126 {
127 if (lut.size() != 4 * 256)
128 {
129 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
130 }
131
132 lut_ = lut;
133
134 IncrementRevision();
135 }
136
137
138 void LookupTableTextureSceneLayer::SetLookupTable(const std::string& lut)
139 {
140 std::vector<uint8_t> tmp;
141 StringToVector(tmp, lut);
142 SetLookupTable(tmp);
143 }
144
145
146 void LookupTableTextureSceneLayer::SetRange(float minValue,
147 float maxValue)
148 {
149 if (minValue > maxValue)
150 {
151 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
152 }
153 else
154 {
155 minValue_ = minValue;
156 maxValue_ = maxValue;
157 IncrementRevision();
158 }
159 }
160
161
162 void LookupTableTextureSceneLayer::FitRange()
163 {
164 Orthanc::ImageProcessing::GetMinMaxFloatValue(minValue_, maxValue_, GetTexture());
165 assert(minValue_ <= maxValue_);
166
167 IncrementRevision();
168 }
169
170
171 ISceneLayer* LookupTableTextureSceneLayer::Clone() const
172 {
173 std::auto_ptr<LookupTableTextureSceneLayer> cloned
174 (new LookupTableTextureSceneLayer(GetTexture()));
175
176 cloned->CopyParameters(*this);
177 cloned->minValue_ = minValue_;
178 cloned->maxValue_ = maxValue_;
179 cloned->lut_ = lut_;
180
181 return cloned.release();
182 }
183 }