Mercurial > hg > orthanc-stone
comparison OrthancStone/UnitTestsSources/ImageToolboxTests.cpp @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | UnitTestsSources/ImageToolboxTests.cpp@30deba7bc8e2 |
children |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
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-2020 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 #include "../Sources/Toolbox/ImageToolbox.h" | |
22 | |
23 // #include <boost/chrono.hpp> | |
24 // #include <boost/lexical_cast.hpp> | |
25 | |
26 #include <Compatibility.h> | |
27 #include <Images/Image.h> | |
28 #include <Images/PixelTraits.h> | |
29 | |
30 #include "stdint.h" | |
31 | |
32 #include <gtest/gtest.h> | |
33 | |
34 #include <cmath> | |
35 | |
36 | |
37 TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize1) | |
38 { | |
39 using OrthancStone::HistogramData; | |
40 using OrthancStone::DumpHistogramResult; | |
41 using OrthancStone::ComputeHistogram; | |
42 | |
43 const unsigned int W = 16; | |
44 const unsigned int H = 16; | |
45 | |
46 // 256/17 = 15,... | |
47 // 256 % 17 = 1 | |
48 // 0 will be 16 times | |
49 // 1 will be 15 times | |
50 // 2 will be 15 times | |
51 // ... | |
52 // 16 will be 15 times | |
53 | |
54 size_t pixCounter = 0; | |
55 | |
56 std::unique_ptr<Orthanc::Image> image(new Orthanc::Image( | |
57 Orthanc::PixelFormat_Grayscale8, W, H, false)); | |
58 | |
59 for (unsigned int y = 0; y < H; ++y) | |
60 { | |
61 uint8_t* buffer = reinterpret_cast<uint8_t*>(image->GetRow(y)); | |
62 for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter) | |
63 { | |
64 *buffer = static_cast<uint8_t>(pixCounter % 17); | |
65 } | |
66 } | |
67 | |
68 HistogramData hd; | |
69 ComputeHistogram(*image, hd, 1); | |
70 ASSERT_EQ(-0.5, hd.minValue); | |
71 ASSERT_EQ(17u, hd.bins.size()); | |
72 ASSERT_EQ(16u, hd.bins[0]); | |
73 for (size_t i = 1; i < hd.bins.size(); ++i) | |
74 ASSERT_EQ(15u, hd.bins[i]); | |
75 } | |
76 | |
77 TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize1_FormatString) | |
78 { | |
79 using OrthancStone::HistogramData; | |
80 using OrthancStone::DumpHistogramResult; | |
81 using OrthancStone::ComputeHistogram; | |
82 | |
83 const unsigned int W = 16; | |
84 const unsigned int H = 16; | |
85 | |
86 // 256/17 = 15,... | |
87 // 256 % 17 = 1 | |
88 // 0 will be 16 times | |
89 // 1 will be 15 times | |
90 // 2 will be 15 times | |
91 // ... | |
92 // 16 will be 15 times | |
93 | |
94 size_t pixCounter = 0; | |
95 | |
96 std::unique_ptr<Orthanc::Image> image(new Orthanc::Image( | |
97 Orthanc::PixelFormat_Grayscale8, W, H, false)); | |
98 | |
99 for (unsigned int y = 0; y < H; ++y) | |
100 { | |
101 uint8_t* buffer = reinterpret_cast<uint8_t*>(image->GetRow(y)); | |
102 for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter) | |
103 { | |
104 *buffer = static_cast<uint8_t>(pixCounter % 17); | |
105 } | |
106 } | |
107 | |
108 HistogramData hd; | |
109 ComputeHistogram(*image, hd, 1); | |
110 | |
111 // void DumpHistogramResult(std::string& s, const HistogramData& hd) | |
112 std::string s; | |
113 DumpHistogramResult(s, hd); | |
114 std::cout << s; | |
115 } | |
116 | |
117 template<Orthanc::PixelFormat Format> | |
118 void SimpleHisto_T_BinSize1_2() | |
119 { | |
120 using OrthancStone::HistogramData; | |
121 using OrthancStone::DumpHistogramResult; | |
122 using OrthancStone::ComputeHistogram; | |
123 | |
124 const unsigned int W = 16; | |
125 const unsigned int H = 16; | |
126 | |
127 // 256/17 = 15,... | |
128 // 256 % 17 = 1 | |
129 // 0 will be 16 times | |
130 // 1 will be 15 times | |
131 // 2 will be 15 times | |
132 // ... | |
133 // 16 will be 15 times | |
134 | |
135 size_t pixCounter = 0; | |
136 | |
137 std::unique_ptr<Orthanc::Image> image(new Orthanc::Image( | |
138 Format, W, H, false)); | |
139 | |
140 typedef typename Orthanc::PixelTraits<Format>::PixelType PixelType; | |
141 | |
142 PixelType pixValue = 0; | |
143 | |
144 for (unsigned int y = 0; y < H; ++y) | |
145 { | |
146 PixelType* buffer = reinterpret_cast<PixelType*>(image->GetRow(y)); | |
147 for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter) | |
148 { | |
149 // 0..99 0..99 0..55 | |
150 *buffer = pixValue; | |
151 pixValue++; | |
152 if (pixValue >= 100) | |
153 pixValue = 0; | |
154 } | |
155 } | |
156 | |
157 HistogramData hd; | |
158 ComputeHistogram(*image, hd, 1); | |
159 ASSERT_EQ(-0.5, hd.minValue); | |
160 ASSERT_EQ(100u, hd.bins.size()); | |
161 for (size_t i = 0; i <= 55; ++i) | |
162 ASSERT_EQ(3u, hd.bins[i]); | |
163 for (size_t i = 56; i <= 99; ++i) | |
164 ASSERT_EQ(2u, hd.bins[i]); | |
165 } | |
166 | |
167 TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize1_2) | |
168 { | |
169 SimpleHisto_T_BinSize1_2<Orthanc::PixelFormat_Grayscale8>(); | |
170 } | |
171 | |
172 TEST(ImageToolbox, SimpleHisto_Grayscale16_BinSize1_2) | |
173 { | |
174 SimpleHisto_T_BinSize1_2<Orthanc::PixelFormat_Grayscale16>(); | |
175 } | |
176 | |
177 TEST(ImageToolbox, SimpleHisto_SignedGrayscale16_BinSize1_2) | |
178 { | |
179 SimpleHisto_T_BinSize1_2<Orthanc::PixelFormat_SignedGrayscale16>(); | |
180 } | |
181 | |
182 TEST(ImageToolbox, SimpleHisto_Grayscale32_BinSize1_2) | |
183 { | |
184 SimpleHisto_T_BinSize1_2<Orthanc::PixelFormat_Grayscale32>(); | |
185 } | |
186 | |
187 template<Orthanc::PixelFormat Format> | |
188 void SimpleHisto_T_BinSize10_2() | |
189 { | |
190 using OrthancStone::HistogramData; | |
191 using OrthancStone::DumpHistogramResult; | |
192 using OrthancStone::ComputeHistogram; | |
193 | |
194 const unsigned int W = 16; | |
195 const unsigned int H = 16; | |
196 | |
197 // 256/17 = 15,... | |
198 // 256 % 17 = 1 | |
199 // 0 will be 16 times | |
200 // 1 will be 15 times | |
201 // 2 will be 15 times | |
202 // ... | |
203 // 16 will be 15 times | |
204 | |
205 size_t pixCounter = 0; | |
206 | |
207 std::unique_ptr<Orthanc::Image> image(new Orthanc::Image( | |
208 Format, W, H, false)); | |
209 | |
210 typedef typename Orthanc::PixelTraits<Format>::PixelType PixelType; | |
211 | |
212 PixelType pixValue = 0; | |
213 | |
214 for (unsigned int y = 0; y < H; ++y) | |
215 { | |
216 PixelType* buffer = reinterpret_cast<PixelType*>(image->GetRow(y)); | |
217 for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter) | |
218 { | |
219 // 0..99 0..99 0..55 | |
220 *buffer = pixValue; | |
221 pixValue++; | |
222 if (pixValue >= 100) | |
223 pixValue = 0; | |
224 } | |
225 } | |
226 | |
227 HistogramData hd; | |
228 ComputeHistogram(*image, hd, 10); | |
229 ASSERT_EQ(-0.5, hd.minValue); | |
230 ASSERT_EQ(10u, hd.bins.size()); | |
231 | |
232 for (size_t i = 0; i <= 4; ++i) | |
233 ASSERT_EQ(30u, hd.bins[i]); | |
234 | |
235 ASSERT_EQ(26u, hd.bins[5]); | |
236 | |
237 for (size_t i = 6; i <= 9; ++i) | |
238 ASSERT_EQ(20u, hd.bins[i]); | |
239 } | |
240 | |
241 TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize10_2) | |
242 { | |
243 SimpleHisto_T_BinSize10_2<Orthanc::PixelFormat_Grayscale8>(); | |
244 } | |
245 | |
246 TEST(ImageToolbox, SimpleHisto_Grayscale16_BinSize10_2) | |
247 { | |
248 SimpleHisto_T_BinSize10_2<Orthanc::PixelFormat_Grayscale16>(); | |
249 } | |
250 | |
251 TEST(ImageToolbox, SimpleHisto_SignedGrayscale16_BinSize10_2) | |
252 { | |
253 SimpleHisto_T_BinSize10_2<Orthanc::PixelFormat_SignedGrayscale16>(); | |
254 } | |
255 | |
256 TEST(ImageToolbox, SimpleHisto_Grayscale32_BinSize10_2) | |
257 { | |
258 SimpleHisto_T_BinSize10_2<Orthanc::PixelFormat_Grayscale32>(); | |
259 } | |
260 |