comparison UnitTestsSources/ImageTests.cpp @ 2884:497a637366b4 db-changes

integration mainline->db-changes
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 12 Oct 2018 15:18:10 +0200
parents 38a3054b22ff
children 4767d36679ed
comparison
equal deleted inserted replaced
1762:2b91363cc1d1 2884:497a637366b4
1 /** 1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store 2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium 4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
5 * 6 *
6 * This program is free software: you can redistribute it and/or 7 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as 8 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the 9 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version. 10 * License, or (at your option) any later version.
38 #include "../Core/Images/ImageProcessing.h" 39 #include "../Core/Images/ImageProcessing.h"
39 #include "../Core/Images/JpegReader.h" 40 #include "../Core/Images/JpegReader.h"
40 #include "../Core/Images/JpegWriter.h" 41 #include "../Core/Images/JpegWriter.h"
41 #include "../Core/Images/PngReader.h" 42 #include "../Core/Images/PngReader.h"
42 #include "../Core/Images/PngWriter.h" 43 #include "../Core/Images/PngWriter.h"
44 #include "../Core/Images/PamReader.h"
45 #include "../Core/Images/PamWriter.h"
43 #include "../Core/Toolbox.h" 46 #include "../Core/Toolbox.h"
44 #include "../Core/Uuid.h" 47 #include "../Core/TemporaryFile.h"
45 #include "../OrthancServer/OrthancInitialization.h" 48 #include "../OrthancServer/OrthancInitialization.h" // For the FontRegistry
46 49
47 #include <stdint.h> 50 #include <stdint.h>
48 51
49 52
50 TEST(PngWriter, ColorPattern) 53 TEST(PngWriter, ColorPattern)
64 p[1] = (y % 3 == 1) ? 255 : 0; 67 p[1] = (y % 3 == 1) ? 255 : 0;
65 p[2] = (y % 3 == 2) ? 255 : 0; 68 p[2] = (y % 3 == 2) ? 255 : 0;
66 } 69 }
67 } 70 }
68 71
69 w.WriteToFile("UnitTestsResults/ColorPattern.png", width, height, pitch, Orthanc::PixelFormat_RGB24, &image[0]); 72 Orthanc::ImageAccessor accessor;
70 73 accessor.AssignReadOnly(Orthanc::PixelFormat_RGB24, width, height, pitch, &image[0]);
71 std::string f, md5; 74
72 Orthanc::Toolbox::ReadFile(f, "UnitTestsResults/ColorPattern.png"); 75 w.WriteToFile("UnitTestsResults/ColorPattern.png", accessor);
76
77 std::string f, md5;
78 Orthanc::SystemToolbox::ReadFile(f, "UnitTestsResults/ColorPattern.png");
73 Orthanc::Toolbox::ComputeMD5(md5, f); 79 Orthanc::Toolbox::ComputeMD5(md5, f);
74 ASSERT_EQ("604e785f53c99cae6ea4584870b2c41d", md5); 80 ASSERT_EQ("604e785f53c99cae6ea4584870b2c41d", md5);
75 } 81 }
76 82
77 TEST(PngWriter, Gray8Pattern) 83 TEST(PngWriter, Gray8Pattern)
89 { 95 {
90 *p = y; 96 *p = y;
91 } 97 }
92 } 98 }
93 99
94 w.WriteToFile("UnitTestsResults/Gray8Pattern.png", width, height, pitch, Orthanc::PixelFormat_Grayscale8, &image[0]); 100 Orthanc::ImageAccessor accessor;
95 101 accessor.AssignReadOnly(Orthanc::PixelFormat_Grayscale8, width, height, pitch, &image[0]);
96 std::string f, md5; 102
97 Orthanc::Toolbox::ReadFile(f, "UnitTestsResults/Gray8Pattern.png"); 103 w.WriteToFile("UnitTestsResults/Gray8Pattern.png", accessor);
104
105 std::string f, md5;
106 Orthanc::SystemToolbox::ReadFile(f, "UnitTestsResults/Gray8Pattern.png");
98 Orthanc::Toolbox::ComputeMD5(md5, f); 107 Orthanc::Toolbox::ComputeMD5(md5, f);
99 ASSERT_EQ("5a9b98bea3d0a6d983980cc38bfbcdb3", md5); 108 ASSERT_EQ("5a9b98bea3d0a6d983980cc38bfbcdb3", md5);
100 } 109 }
101 110
102 TEST(PngWriter, Gray16Pattern) 111 TEST(PngWriter, Gray16Pattern)
116 { 125 {
117 *p = v; 126 *p = v;
118 } 127 }
119 } 128 }
120 129
121 w.WriteToFile("UnitTestsResults/Gray16Pattern.png", width, height, pitch, Orthanc::PixelFormat_Grayscale16, &image[0]); 130 Orthanc::ImageAccessor accessor;
122 131 accessor.AssignReadOnly(Orthanc::PixelFormat_Grayscale16, width, height, pitch, &image[0]);
123 std::string f, md5; 132 w.WriteToFile("UnitTestsResults/Gray16Pattern.png", accessor);
124 Orthanc::Toolbox::ReadFile(f, "UnitTestsResults/Gray16Pattern.png"); 133
134 std::string f, md5;
135 Orthanc::SystemToolbox::ReadFile(f, "UnitTestsResults/Gray16Pattern.png");
125 Orthanc::Toolbox::ComputeMD5(md5, f); 136 Orthanc::Toolbox::ComputeMD5(md5, f);
126 ASSERT_EQ("0785866a08bf0a02d2eeff87f658571c", md5); 137 ASSERT_EQ("0785866a08bf0a02d2eeff87f658571c", md5);
127 } 138 }
128 139
129 TEST(PngWriter, EndToEnd) 140 TEST(PngWriter, EndToEnd)
143 { 154 {
144 *p = v; 155 *p = v;
145 } 156 }
146 } 157 }
147 158
159 Orthanc::ImageAccessor accessor;
160 accessor.AssignReadOnly(Orthanc::PixelFormat_Grayscale16, width, height, pitch, &image[0]);
161
148 std::string s; 162 std::string s;
149 w.WriteToMemory(s, width, height, pitch, Orthanc::PixelFormat_Grayscale16, &image[0]); 163 w.WriteToMemory(s, accessor);
150 164
151 { 165 {
152 Orthanc::PngReader r; 166 Orthanc::PngReader r;
153 r.ReadFromMemory(s); 167 r.ReadFromMemory(s);
154 168
167 } 181 }
168 } 182 }
169 } 183 }
170 184
171 { 185 {
172 Orthanc::Toolbox::TemporaryFile tmp; 186 Orthanc::TemporaryFile tmp;
173 Orthanc::Toolbox::WriteFile(s, tmp.GetPath()); 187 Orthanc::SystemToolbox::WriteFile(s, tmp.GetPath());
174 188
175 Orthanc::PngReader r2; 189 Orthanc::PngReader r2;
176 r2.ReadFromFile(tmp.GetPath()); 190 r2.ReadFromFile(tmp.GetPath());
177 191
178 ASSERT_EQ(r2.GetFormat(), Orthanc::PixelFormat_Grayscale16); 192 ASSERT_EQ(r2.GetFormat(), Orthanc::PixelFormat_Grayscale16);
198 TEST(JpegWriter, Basic) 212 TEST(JpegWriter, Basic)
199 { 213 {
200 std::string s; 214 std::string s;
201 215
202 { 216 {
203 Orthanc::Image img(Orthanc::PixelFormat_Grayscale8, 16, 16); 217 Orthanc::Image img(Orthanc::PixelFormat_Grayscale8, 16, 16, false);
204 for (unsigned int y = 0, value = 0; y < img.GetHeight(); y++) 218 for (unsigned int y = 0, value = 0; y < img.GetHeight(); y++)
205 { 219 {
206 uint8_t* p = reinterpret_cast<uint8_t*>(img.GetRow(y)); 220 uint8_t* p = reinterpret_cast<uint8_t*>(img.GetRow(y));
207 for (unsigned int x = 0; x < img.GetWidth(); x++, p++) 221 for (unsigned int x = 0; x < img.GetWidth(); x++, p++)
208 { 222 {
212 226
213 Orthanc::JpegWriter w; 227 Orthanc::JpegWriter w;
214 w.WriteToFile("UnitTestsResults/hello.jpg", img); 228 w.WriteToFile("UnitTestsResults/hello.jpg", img);
215 229
216 w.WriteToMemory(s, img); 230 w.WriteToMemory(s, img);
217 Orthanc::Toolbox::WriteFile(s, "UnitTestsResults/hello2.jpg"); 231 Orthanc::SystemToolbox::WriteFile(s, "UnitTestsResults/hello2.jpg");
218 232
219 std::string t; 233 std::string t;
220 Orthanc::Toolbox::ReadFile(t, "UnitTestsResults/hello.jpg"); 234 Orthanc::SystemToolbox::ReadFile(t, "UnitTestsResults/hello.jpg");
221 ASSERT_EQ(s.size(), t.size()); 235 ASSERT_EQ(s.size(), t.size());
222 ASSERT_EQ(0, memcmp(s.c_str(), t.c_str(), s.size())); 236 ASSERT_EQ(0, memcmp(s.c_str(), t.c_str(), s.size()));
223 } 237 }
224 238
225 { 239 {
226 Orthanc::JpegReader r1, r2; 240 Orthanc::JpegReader r1, r2;
227 r1.ReadFromFile("UnitTestsResults/hello.jpg"); 241 r1.ReadFromFile("UnitTestsResults/hello.jpg");
228 ASSERT_EQ(16, r1.GetWidth()); 242 ASSERT_EQ(16u, r1.GetWidth());
229 ASSERT_EQ(16, r1.GetHeight()); 243 ASSERT_EQ(16u, r1.GetHeight());
230 244
231 r2.ReadFromMemory(s); 245 r2.ReadFromMemory(s);
232 ASSERT_EQ(16, r2.GetWidth()); 246 ASSERT_EQ(16u, r2.GetWidth());
233 ASSERT_EQ(16, r2.GetHeight()); 247 ASSERT_EQ(16u, r2.GetHeight());
234 248
235 for (unsigned int y = 0; y < r1.GetHeight(); y++) 249 for (unsigned int y = 0; y < r1.GetHeight(); y++)
236 { 250 {
237 const uint8_t* p1 = reinterpret_cast<const uint8_t*>(r1.GetConstRow(y)); 251 const uint8_t* p1 = reinterpret_cast<const uint8_t*>(r1.GetConstRow(y));
238 const uint8_t* p2 = reinterpret_cast<const uint8_t*>(r2.GetConstRow(y)); 252 const uint8_t* p2 = reinterpret_cast<const uint8_t*>(r2.GetConstRow(y));
245 } 259 }
246 260
247 261
248 TEST(Font, Basic) 262 TEST(Font, Basic)
249 { 263 {
250 Orthanc::Image s(Orthanc::PixelFormat_RGB24, 640, 480); 264 Orthanc::Image s(Orthanc::PixelFormat_RGB24, 640, 480, false);
251 memset(s.GetBuffer(), 0, s.GetPitch() * s.GetHeight()); 265 memset(s.GetBuffer(), 0, s.GetPitch() * s.GetHeight());
252 266
253 ASSERT_GE(1, Orthanc::Configuration::GetFontRegistry().GetSize()); 267 ASSERT_GE(1u, Orthanc::Configuration::GetFontRegistry().GetSize());
254 Orthanc::Configuration::GetFontRegistry().GetFont(0).Draw(s, "Hello world É\n\rComment ça va ?\nq", 50, 60, 255, 0, 0); 268 Orthanc::Configuration::GetFontRegistry().GetFont(0).Draw(s, "Hello world É\n\rComment ça va ?\nq", 50, 60, 255, 0, 0);
255 269
256 Orthanc::PngWriter w; 270 Orthanc::PngWriter w;
257 w.WriteToFile("UnitTestsResults/font.png", s); 271 w.WriteToFile("UnitTestsResults/font.png", s);
258 } 272 }
259 273
274 TEST(PamWriter, ColorPattern)
275 {
276 Orthanc::PamWriter w;
277 unsigned int width = 17;
278 unsigned int height = 61;
279 unsigned int pitch = width * 3;
280
281 std::vector<uint8_t> image(height * pitch);
282 for (unsigned int y = 0; y < height; y++)
283 {
284 uint8_t *p = &image[0] + y * pitch;
285 for (unsigned int x = 0; x < width; x++, p += 3)
286 {
287 p[0] = (y % 3 == 0) ? 255 : 0;
288 p[1] = (y % 3 == 1) ? 255 : 0;
289 p[2] = (y % 3 == 2) ? 255 : 0;
290 }
291 }
292
293 Orthanc::ImageAccessor accessor;
294 accessor.AssignReadOnly(Orthanc::PixelFormat_RGB24, width, height, pitch, &image[0]);
295
296 w.WriteToFile("UnitTestsResults/ColorPattern.pam", accessor);
297
298 std::string f, md5;
299 Orthanc::SystemToolbox::ReadFile(f, "UnitTestsResults/ColorPattern.pam");
300 Orthanc::Toolbox::ComputeMD5(md5, f);
301 ASSERT_EQ("81a3441754e88969ebbe53e69891e841", md5);
302 }
303
304 TEST(PamWriter, Gray8Pattern)
305 {
306 Orthanc::PamWriter w;
307 int width = 17;
308 int height = 256;
309 int pitch = width;
310
311 std::vector<uint8_t> image(height * pitch);
312 for (int y = 0; y < height; y++)
313 {
314 uint8_t *p = &image[0] + y * pitch;
315 for (int x = 0; x < width; x++, p++)
316 {
317 *p = y;
318 }
319 }
320
321 Orthanc::ImageAccessor accessor;
322 accessor.AssignReadOnly(Orthanc::PixelFormat_Grayscale8, width, height, pitch, &image[0]);
323
324 w.WriteToFile("UnitTestsResults/Gray8Pattern.pam", accessor);
325
326 std::string f, md5;
327 Orthanc::SystemToolbox::ReadFile(f, "UnitTestsResults/Gray8Pattern.pam");
328 Orthanc::Toolbox::ComputeMD5(md5, f);
329 ASSERT_EQ("7873c408d26a9d11dd1c1de5e69cc0a3", md5);
330 }
331
332 TEST(PamWriter, Gray16Pattern)
333 {
334 Orthanc::PamWriter w;
335 int width = 256;
336 int height = 256;
337 int pitch = width * 2 + 16;
338
339 std::vector<uint8_t> image(height * pitch);
340
341 int v = 0;
342 for (int y = 0; y < height; y++)
343 {
344 uint16_t *p = reinterpret_cast<uint16_t*>(&image[0] + y * pitch);
345 for (int x = 0; x < width; x++, p++, v++)
346 {
347 *p = v;
348 }
349 }
350
351 Orthanc::ImageAccessor accessor;
352 accessor.AssignReadOnly(Orthanc::PixelFormat_Grayscale16, width, height, pitch, &image[0]);
353 w.WriteToFile("UnitTestsResults/Gray16Pattern.pam", accessor);
354
355 std::string f, md5;
356 Orthanc::SystemToolbox::ReadFile(f, "UnitTestsResults/Gray16Pattern.pam");
357 Orthanc::Toolbox::ComputeMD5(md5, f);
358 ASSERT_EQ("b268772bf28f3b2b8520ff21c5e3dcb6", md5);
359 }
360
361 TEST(PamWriter, EndToEnd)
362 {
363 Orthanc::PamWriter w;
364 unsigned int width = 256;
365 unsigned int height = 256;
366 unsigned int pitch = width * 2 + 16;
367
368 std::vector<uint8_t> image(height * pitch);
369
370 int v = 0;
371 for (unsigned int y = 0; y < height; y++)
372 {
373 uint16_t *p = reinterpret_cast<uint16_t*>(&image[0] + y * pitch);
374 for (unsigned int x = 0; x < width; x++, p++, v++)
375 {
376 *p = v;
377 }
378 }
379
380 Orthanc::ImageAccessor accessor;
381 accessor.AssignReadOnly(Orthanc::PixelFormat_Grayscale16, width, height, pitch, &image[0]);
382
383 std::string s;
384 w.WriteToMemory(s, accessor);
385
386 {
387 Orthanc::PamReader r;
388 r.ReadFromMemory(s);
389
390 ASSERT_EQ(r.GetFormat(), Orthanc::PixelFormat_Grayscale16);
391 ASSERT_EQ(r.GetWidth(), width);
392 ASSERT_EQ(r.GetHeight(), height);
393
394 v = 0;
395 for (unsigned int y = 0; y < height; y++)
396 {
397 const uint16_t *p = reinterpret_cast<const uint16_t*>
398 ((const uint8_t*) r.GetConstBuffer() + y * r.GetPitch());
399 ASSERT_EQ(p, r.GetConstRow(y));
400 for (unsigned int x = 0; x < width; x++, p++, v++)
401 {
402 ASSERT_EQ(v, *p);
403 }
404 }
405 }
406
407 {
408 Orthanc::TemporaryFile tmp;
409 Orthanc::SystemToolbox::WriteFile(s, tmp.GetPath());
410
411 Orthanc::PamReader r2;
412 r2.ReadFromFile(tmp.GetPath());
413
414 ASSERT_EQ(r2.GetFormat(), Orthanc::PixelFormat_Grayscale16);
415 ASSERT_EQ(r2.GetWidth(), width);
416 ASSERT_EQ(r2.GetHeight(), height);
417
418 v = 0;
419 for (unsigned int y = 0; y < height; y++)
420 {
421 const uint16_t *p = reinterpret_cast<const uint16_t*>
422 ((const uint8_t*) r2.GetConstBuffer() + y * r2.GetPitch());
423 ASSERT_EQ(p, r2.GetConstRow(y));
424 for (unsigned int x = 0; x < width; x++, p++, v++)
425 {
426 ASSERT_EQ(*p, v);
427 }
428 }
429 }
430 }
431