comparison OrthancStone/UnitTestsSources/ImageToolboxTests.cpp @ 1877:a2955abe4c2e

skeleton for the RenderingPlugin
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 12 Jan 2022 08:23:38 +0100
parents UnitTestsSources/ImageToolboxTests.cpp@7053b8a0aaec
children 07964689cb0b
comparison
equal deleted inserted replaced
1876:b1f510e601d2 1877:a2955abe4c2e
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-2022 Osimis S.A., Belgium
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
7 *
8 * This program is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation, either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 **/
22
23
24 #include "../Sources/Toolbox/ImageToolbox.h"
25
26 // #include <boost/chrono.hpp>
27 // #include <boost/lexical_cast.hpp>
28
29 #include <Compatibility.h>
30 #include <Images/Image.h>
31 #include <Images/PixelTraits.h>
32
33 #include "stdint.h"
34
35 #include <gtest/gtest.h>
36
37 #include <cmath>
38
39
40 TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize1)
41 {
42 using OrthancStone::HistogramData;
43 using OrthancStone::DumpHistogramResult;
44 using OrthancStone::ComputeHistogram;
45
46 const unsigned int W = 16;
47 const unsigned int H = 16;
48
49 // 256/17 = 15,...
50 // 256 % 17 = 1
51 // 0 will be 16 times
52 // 1 will be 15 times
53 // 2 will be 15 times
54 // ...
55 // 16 will be 15 times
56
57 size_t pixCounter = 0;
58
59 std::unique_ptr<Orthanc::Image> image(new Orthanc::Image(
60 Orthanc::PixelFormat_Grayscale8, W, H, false));
61
62 for (unsigned int y = 0; y < H; ++y)
63 {
64 uint8_t* buffer = reinterpret_cast<uint8_t*>(image->GetRow(y));
65 for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter)
66 {
67 *buffer = static_cast<uint8_t>(pixCounter % 17);
68 }
69 }
70
71 HistogramData hd;
72 ComputeHistogram(*image, hd, 1);
73 ASSERT_EQ(-0.5, hd.minValue);
74 ASSERT_EQ(17u, hd.bins.size());
75 ASSERT_EQ(16u, hd.bins[0]);
76 for (size_t i = 1; i < hd.bins.size(); ++i)
77 ASSERT_EQ(15u, hd.bins[i]);
78 }
79
80 TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize1_FormatString)
81 {
82 using OrthancStone::HistogramData;
83 using OrthancStone::DumpHistogramResult;
84 using OrthancStone::ComputeHistogram;
85
86 const unsigned int W = 16;
87 const unsigned int H = 16;
88
89 // 256/17 = 15,...
90 // 256 % 17 = 1
91 // 0 will be 16 times
92 // 1 will be 15 times
93 // 2 will be 15 times
94 // ...
95 // 16 will be 15 times
96
97 size_t pixCounter = 0;
98
99 std::unique_ptr<Orthanc::Image> image(new Orthanc::Image(
100 Orthanc::PixelFormat_Grayscale8, W, H, false));
101
102 for (unsigned int y = 0; y < H; ++y)
103 {
104 uint8_t* buffer = reinterpret_cast<uint8_t*>(image->GetRow(y));
105 for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter)
106 {
107 *buffer = static_cast<uint8_t>(pixCounter % 17);
108 }
109 }
110
111 HistogramData hd;
112 ComputeHistogram(*image, hd, 1);
113
114 // void DumpHistogramResult(std::string& s, const HistogramData& hd)
115 std::string s;
116 DumpHistogramResult(s, hd);
117 std::cout << s;
118 }
119
120 template<Orthanc::PixelFormat Format>
121 void SimpleHisto_T_BinSize1_2()
122 {
123 using OrthancStone::HistogramData;
124 using OrthancStone::DumpHistogramResult;
125 using OrthancStone::ComputeHistogram;
126
127 const unsigned int W = 16;
128 const unsigned int H = 16;
129
130 // 256/17 = 15,...
131 // 256 % 17 = 1
132 // 0 will be 16 times
133 // 1 will be 15 times
134 // 2 will be 15 times
135 // ...
136 // 16 will be 15 times
137
138 size_t pixCounter = 0;
139
140 std::unique_ptr<Orthanc::Image> image(new Orthanc::Image(
141 Format, W, H, false));
142
143 typedef typename Orthanc::PixelTraits<Format>::PixelType PixelType;
144
145 PixelType pixValue = 0;
146
147 for (unsigned int y = 0; y < H; ++y)
148 {
149 PixelType* buffer = reinterpret_cast<PixelType*>(image->GetRow(y));
150 for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter)
151 {
152 // 0..99 0..99 0..55
153 *buffer = pixValue;
154 pixValue++;
155 if (pixValue >= 100)
156 pixValue = 0;
157 }
158 }
159
160 HistogramData hd;
161 ComputeHistogram(*image, hd, 1);
162 ASSERT_EQ(-0.5, hd.minValue);
163 ASSERT_EQ(100u, hd.bins.size());
164 for (size_t i = 0; i <= 55; ++i)
165 ASSERT_EQ(3u, hd.bins[i]);
166 for (size_t i = 56; i <= 99; ++i)
167 ASSERT_EQ(2u, hd.bins[i]);
168 }
169
170 TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize1_2)
171 {
172 SimpleHisto_T_BinSize1_2<Orthanc::PixelFormat_Grayscale8>();
173 }
174
175 TEST(ImageToolbox, SimpleHisto_Grayscale16_BinSize1_2)
176 {
177 SimpleHisto_T_BinSize1_2<Orthanc::PixelFormat_Grayscale16>();
178 }
179
180 TEST(ImageToolbox, SimpleHisto_SignedGrayscale16_BinSize1_2)
181 {
182 SimpleHisto_T_BinSize1_2<Orthanc::PixelFormat_SignedGrayscale16>();
183 }
184
185 TEST(ImageToolbox, SimpleHisto_Grayscale32_BinSize1_2)
186 {
187 SimpleHisto_T_BinSize1_2<Orthanc::PixelFormat_Grayscale32>();
188 }
189
190 template<Orthanc::PixelFormat Format>
191 void SimpleHisto_T_BinSize10_2()
192 {
193 using OrthancStone::HistogramData;
194 using OrthancStone::DumpHistogramResult;
195 using OrthancStone::ComputeHistogram;
196
197 const unsigned int W = 16;
198 const unsigned int H = 16;
199
200 // 256/17 = 15,...
201 // 256 % 17 = 1
202 // 0 will be 16 times
203 // 1 will be 15 times
204 // 2 will be 15 times
205 // ...
206 // 16 will be 15 times
207
208 size_t pixCounter = 0;
209
210 std::unique_ptr<Orthanc::Image> image(new Orthanc::Image(
211 Format, W, H, false));
212
213 typedef typename Orthanc::PixelTraits<Format>::PixelType PixelType;
214
215 PixelType pixValue = 0;
216
217 for (unsigned int y = 0; y < H; ++y)
218 {
219 PixelType* buffer = reinterpret_cast<PixelType*>(image->GetRow(y));
220 for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter)
221 {
222 // 0..99 0..99 0..55
223 *buffer = pixValue;
224 pixValue++;
225 if (pixValue >= 100)
226 pixValue = 0;
227 }
228 }
229
230 HistogramData hd;
231 ComputeHistogram(*image, hd, 10);
232 ASSERT_EQ(-0.5, hd.minValue);
233 ASSERT_EQ(10u, hd.bins.size());
234
235 for (size_t i = 0; i <= 4; ++i)
236 ASSERT_EQ(30u, hd.bins[i]);
237
238 ASSERT_EQ(26u, hd.bins[5]);
239
240 for (size_t i = 6; i <= 9; ++i)
241 ASSERT_EQ(20u, hd.bins[i]);
242 }
243
244 TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize10_2)
245 {
246 SimpleHisto_T_BinSize10_2<Orthanc::PixelFormat_Grayscale8>();
247 }
248
249 TEST(ImageToolbox, SimpleHisto_Grayscale16_BinSize10_2)
250 {
251 SimpleHisto_T_BinSize10_2<Orthanc::PixelFormat_Grayscale16>();
252 }
253
254 TEST(ImageToolbox, SimpleHisto_SignedGrayscale16_BinSize10_2)
255 {
256 SimpleHisto_T_BinSize10_2<Orthanc::PixelFormat_SignedGrayscale16>();
257 }
258
259 TEST(ImageToolbox, SimpleHisto_Grayscale32_BinSize10_2)
260 {
261 SimpleHisto_T_BinSize10_2<Orthanc::PixelFormat_Grayscale32>();
262 }
263