File: image_reader_writer_test.cpp

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,184 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (583 lines) | stat: -rw-r--r-- 23,686 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/************************************************************************
 *
 * Copyright (C) 2009-2024 IRCAD France
 * Copyright (C) 2012-2020 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "image_reader_writer_test.hpp"

#include <core/os/temp_path.hpp>
#include <core/tools/failed.hpp>

#include <data/image.hpp>
#include <data/image_series.hpp>

#include <service/op.hpp>

#include <utest_data/data.hpp>
#include <utest_data/generator/image.hpp>

#include <boost/property_tree/xml_parser.hpp>

#include <filesystem>
#include <fstream>

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(sight::module::io::vtk::ut::image_reader_writer_test);

static const double EPSILON = 0.00001;

namespace sight::module::io::vtk::ut
{

//------------------------------------------------------------------------------

void run_image_srv(
    const std::string& _srvname,
    const boost::property_tree::ptree& _cfg,
    const SPTR(data::object)& _image
)
{
    service::base::sptr srv = service::add(_srvname);

    CPPUNIT_ASSERT_MESSAGE(std::string("Failed to create service ") + _srvname, srv);

    if(srv->is_a("sight::io::service::reader"))
    {
        srv->set_inout(_image, "data");
    }
    else
    {
        srv->set_input(_image, "data");
    }

    CPPUNIT_ASSERT_NO_THROW(srv->set_config(_cfg));
    CPPUNIT_ASSERT_NO_THROW(srv->configure());
    CPPUNIT_ASSERT_NO_THROW(srv->start().wait());
    CPPUNIT_ASSERT_NO_THROW(srv->update().wait());
    CPPUNIT_ASSERT_NO_THROW(srv->stop().wait());
    service::remove(srv);
}

//------------------------------------------------------------------------------

void image_reader_writer_test::setUp()
{
}

//------------------------------------------------------------------------------

void image_reader_writer_test::tearDown()
{
}

//------------------------------------------------------------------------------

boost::property_tree::ptree get_io_configuration(const std::filesystem::path& _file)
{
    service::config_t reader_srv_cfg;
    reader_srv_cfg.add("file", _file.string());

    return reader_srv_cfg;
}

//------------------------------------------------------------------------------

void image_reader_writer_test::test_vtk_image_reader()
{
    const std::filesystem::path file = utest_data::dir() / "sight/image/vtk/img.vtk";

    CPPUNIT_ASSERT_MESSAGE(
        "The file '" + file.string() + "' does not exist",
        std::filesystem::exists(file)
    );

    data::image::sptr image = std::make_shared<data::image>();

    // Data expected
    data::image::spacing_t spacing_expected;
    // NOLINTNEXTLINE(modernize-use-std-numbers)
    spacing_expected[0] = 1.732;
    // NOLINTNEXTLINE(modernize-use-std-numbers)
    spacing_expected[1] = 1.732;
    spacing_expected[2] = 3.2;

    data::image::origin_t origin_expected;
    origin_expected[0] = 34.64;
    origin_expected[1] = 86.6;
    origin_expected[2] = 56;

    data::image::size_t size_expected;
    size_expected[0] = 230;
    size_expected[1] = 170;
    size_expected[2] = 58;

    run_image_srv("sight::module::io::vtk::image_reader", get_io_configuration(file), image);

    // Data read.
    data::image::spacing_t spacing_read = image->spacing();
    data::image::origin_t origin_read   = image->origin();
    data::image::size_t size_read       = image->size();

    CPPUNIT_ASSERT_EQUAL(spacing_expected.size(), spacing_read.size());
    CPPUNIT_ASSERT_EQUAL(origin_expected.size(), origin_read.size());
    CPPUNIT_ASSERT_EQUAL(size_expected.size(), size_read.size());

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on x", spacing_expected[0], spacing_read[0], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on y", spacing_expected[1], spacing_read[1], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on z", spacing_expected[2], spacing_read[2], EPSILON);

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on x", origin_expected[0], origin_read[0], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on y", origin_expected[1], origin_read[1], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on z", origin_expected[2], origin_read[2], EPSILON);

    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on x", size_expected[0], size_read[0]);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on y", size_expected[1], size_read[1]);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on z", size_expected[2], size_read[2]);
}

//------------------------------------------------------------------------------

void image_reader_writer_test::test_vti_image_reader()
{
    const std::filesystem::path file = utest_data::dir() / "sight/image/vti/BostonTeapot.vti";

    CPPUNIT_ASSERT_MESSAGE(
        "The file '" + file.string() + "' does not exist",
        std::filesystem::exists(file)
    );

    data::image::sptr image = std::make_shared<data::image>();
    run_image_srv("sight::module::io::vtk::image_reader", get_io_configuration(file), image);

    // Data expected
    data::image::spacing_t spacing_expected;
    spacing_expected[0] = 1.0;
    spacing_expected[1] = 1.0;
    spacing_expected[2] = 1.0;

    data::image::origin_t origin_expected;
    origin_expected[0] = 1.1;
    origin_expected[1] = 2.2;
    origin_expected[2] = 3.3;

    data::image::size_t size_expected;
    size_expected[0] = 256;
    size_expected[1] = 256;
    size_expected[2] = 178;

    core::type expected_type("int8"); // MHD File image type : MET_CHAR

    // Data read.
    data::image::spacing_t spacing_read = image->spacing();
    data::image::origin_t origin_read   = image->origin();
    data::image::size_t size_read       = image->size();

    CPPUNIT_ASSERT_EQUAL(spacing_expected.size(), spacing_read.size());
    CPPUNIT_ASSERT_EQUAL(origin_expected.size(), origin_read.size());
    CPPUNIT_ASSERT_EQUAL(size_expected.size(), size_read.size());

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on x", spacing_expected[0], spacing_read[0], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on y", spacing_expected[1], spacing_read[1], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on z", spacing_expected[2], spacing_read[2], EPSILON);

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on x", origin_expected[0], origin_read[0], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on y", origin_expected[1], origin_read[1], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on z", origin_expected[2], origin_read[2], EPSILON);

    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on x", size_expected[0], size_read[0]);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on y", size_expected[1], size_read[1]);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on z", size_expected[2], size_read[2]);

    CPPUNIT_ASSERT_EQUAL(expected_type, image->type());
}

//------------------------------------------------------------------------------
void image_reader_writer_test::test_mhd_image_reader()
{
    const std::filesystem::path file = utest_data::dir() / "sight/image/mhd/BostonTeapot.mhd";

    CPPUNIT_ASSERT_MESSAGE(
        "The file '" + file.string() + "' does not exist",
        std::filesystem::exists(file)
    );

    data::image::sptr image = std::make_shared<data::image>();
    run_image_srv("sight::module::io::vtk::image_reader", get_io_configuration(file), image);

    // Data expected
    data::image::spacing_t spacing_expected;
    spacing_expected[0] = 1.0;
    spacing_expected[1] = 1.0;
    spacing_expected[2] = 1.0;

    data::image::origin_t origin_expected;
    origin_expected[0] = 1.1;
    origin_expected[1] = 2.2;
    origin_expected[2] = 3.3;

    data::image::size_t size_expected;
    size_expected[0] = 256;
    size_expected[1] = 256;
    size_expected[2] = 178;

    core::type expected_type("int8"); // MHD File image type : MET_CHAR

    // Data read.
    data::image::spacing_t spacing_read = image->spacing();
    data::image::origin_t origin_read   = image->origin();
    data::image::size_t size_read       = image->size();

    CPPUNIT_ASSERT_EQUAL(spacing_expected.size(), spacing_read.size());
    CPPUNIT_ASSERT_EQUAL(origin_expected.size(), origin_read.size());
    CPPUNIT_ASSERT_EQUAL(size_expected.size(), size_read.size());

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on x", spacing_expected[0], spacing_read[0], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on y", spacing_expected[1], spacing_read[1], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on z", spacing_expected[2], spacing_read[2], EPSILON);

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on x", origin_expected[0], origin_read[0], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on y", origin_expected[1], origin_read[1], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on z", origin_expected[2], origin_read[2], EPSILON);

    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on x", size_expected[0], size_read[0]);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on y", size_expected[1], size_read[1]);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on z", size_expected[2], size_read[2]);

    CPPUNIT_ASSERT_EQUAL(expected_type, image->type());
}

//------------------------------------------------------------------------------

void image_reader_writer_test::test_image_reader_extension()
{
    core::os::temp_file tmp_file;

    std::ofstream o_file(tmp_file, std::ios::out | std::ios::trunc | std::ios::binary);
    o_file.close();

    data::image::sptr image = std::make_shared<data::image>();

    {
        const std::string srvname("sight::module::io::vtk::image_reader");

        service::base::sptr srv = service::add(srvname);

        CPPUNIT_ASSERT_MESSAGE(std::string("Failed to create service ") + srvname, srv);

        srv->set_inout(image, "data");

        CPPUNIT_ASSERT_NO_THROW(srv->set_config(get_io_configuration(tmp_file)));
        CPPUNIT_ASSERT_NO_THROW(srv->configure());
        CPPUNIT_ASSERT_NO_THROW(srv->start().wait());
        CPPUNIT_ASSERT_THROW(srv->update().get(), core::tools::failed);
        CPPUNIT_ASSERT_NO_THROW(srv->stop().wait());
        service::remove(srv);
    }
}

//------------------------------------------------------------------------------
void image_reader_writer_test::test_vtk_image_writer()
{
    // Data to write
    core::type type                                       = core::type::UINT8;
    const data::image::size_t size_expected               = {10, 20, 30};
    const data::image::spacing_t spacing_expected         = {0.24, 1.07, 2.21};
    const data::image::origin_t origin_expected           = {-5.6, 15.16, 11.11};
    const data::image::orientation_t orientation_expected = {0.36, 0.48, -0.8, -0.8, 0.6, 0.0, 0.48, 0.64, 0.6};

    data::image::sptr image = std::make_shared<data::image>();
    utest_data::generator::image::generate_image(
        image,
        size_expected,
        spacing_expected,
        origin_expected,
        orientation_expected,
        type,
        data::image::rgba,
        0
    );

    // Write to vtk image.
    core::os::temp_dir tmp_dir;
    const auto file = tmp_dir / "tempFile.vtk";

    run_image_srv("sight::module::io::vtk::image_writer", get_io_configuration(file), image);

    // Read image from disk
    data::image::sptr image_from_disk = std::make_shared<data::image>();
    run_image_srv("sight::module::io::vtk::image_reader", get_io_configuration(file), image_from_disk);

    // Data read
    data::image::spacing_t spacing_read = image->spacing();
    data::image::origin_t origin_read   = image->origin();
    data::image::size_t size_read       = image->size();

    CPPUNIT_ASSERT_EQUAL(spacing_expected.size(), spacing_read.size());
    CPPUNIT_ASSERT_EQUAL(origin_expected.size(), origin_read.size());
    CPPUNIT_ASSERT_EQUAL(size_expected.size(), size_read.size());

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on x", spacing_expected[0], spacing_read[0], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on y", spacing_expected[1], spacing_read[1], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on z", spacing_expected[2], spacing_read[2], EPSILON);

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on x", origin_expected[0], origin_read[0], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on y", origin_expected[1], origin_read[1], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on z", origin_expected[2], origin_read[2], EPSILON);

    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on x", size_expected[0], size_read[0]);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on y", size_expected[1], size_read[1]);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on z", size_expected[2], size_read[2]);

    const auto image_dump_lock           = image->dump_lock();
    const auto image_from_disk_dump_lock = image_from_disk->dump_lock();

    const char* const ptr_on_generated_image = static_cast<char*>(image->buffer());
    const char* const ptr_on_read_image      = static_cast<char*>(image_from_disk->buffer());

    CPPUNIT_ASSERT_EQUAL(image->type(), image_from_disk->type());
    CPPUNIT_ASSERT(
        std::equal(
            ptr_on_generated_image,
            ptr_on_generated_image + image->size_in_bytes(),
            ptr_on_read_image
        )
    );
}

//------------------------------------------------------------------------------

void image_reader_writer_test::test_vtk_image_series_writer()
{
    core::type type   = core::type::FLOAT;
    auto image_series = std::make_shared<data::image_series>();
    utest_data::generator::image::generate_random_image(image_series, type);

    // Orientation seems to be unsupported by VTK
    image_series->set_orientation({1, 0, 0, 0, 1, 0, 0, 0, 1});

    core::os::temp_dir tmp_dir;
    const auto file = tmp_dir / "imageSeries.vtk";

    // Write image series
    run_image_srv("sight::module::io::vtk::image_series_writer", get_io_configuration(file), image_series);

    // Read image series
    auto image_series2 = std::make_shared<data::image_series>();
    run_image_srv("sight::module::io::vtk::image_reader", get_io_configuration(file), image_series2);

    image_series2->set_window_center(image_series->window_center());
    image_series2->set_window_width(image_series->window_width());

    CPPUNIT_ASSERT(*image_series == *image_series2);
}

//------------------------------------------------------------------------------

void image_reader_writer_test::test_vti_image_writer()
{
    // Data to write
    core::type type                                       = core::type::UINT8;
    const data::image::size_t size_expected               = {10, 20, 30};
    const data::image::spacing_t spacing_expected         = {0.24, 1.07, 2.21};
    const data::image::origin_t origin_expected           = {-5.6, 15.16, 11.11};
    const data::image::orientation_t orientation_expected = {0.36, 0.48, -0.8, -0.8, 0.6, 0.0, 0.48, 0.64, 0.6};

    data::image::sptr image = std::make_shared<data::image>();
    utest_data::generator::image::generate_image(
        image,
        size_expected,
        spacing_expected,
        origin_expected,
        orientation_expected,
        type,
        data::image::gray_scale,
        0
    );

    // Write to vtk image.
    core::os::temp_dir tmp_dir;
    const auto file = tmp_dir / "tempFile.vti";

    run_image_srv("sight::module::io::vtk::image_writer", get_io_configuration(file), image);

    // Read image from disk
    data::image::sptr image_from_disk = std::make_shared<data::image>();
    run_image_srv("sight::module::io::vtk::image_reader", get_io_configuration(file), image_from_disk);

    // Data read
    data::image::spacing_t spacing_read         = image->spacing();
    data::image::origin_t origin_read           = image->origin();
    data::image::orientation_t orientation_read = image->orientation();
    data::image::size_t size_read               = image->size();

    CPPUNIT_ASSERT_EQUAL(spacing_expected.size(), spacing_read.size());
    CPPUNIT_ASSERT_EQUAL(origin_expected.size(), origin_read.size());
    CPPUNIT_ASSERT_EQUAL(size_expected.size(), size_read.size());

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on x", spacing_expected[0], spacing_read[0], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on y", spacing_expected[1], spacing_read[1], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on z", spacing_expected[2], spacing_read[2], EPSILON);

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on x", origin_expected[0], origin_read[0], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on y", origin_expected[1], origin_read[1], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on z", origin_expected[2], origin_read[2], EPSILON);

    for(std::size_t i = 0 ; i < orientation_read.size() ; ++i)
    {
        CPPUNIT_ASSERT_DOUBLES_EQUAL(orientation_expected[i], orientation_read[i], EPSILON);
    }

    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on x", size_expected[0], size_read[0]);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on y", size_expected[1], size_read[1]);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on z", size_expected[2], size_read[2]);

    const auto image_dump_lock           = image->dump_lock();
    const auto image_from_disk_dump_lock = image_from_disk->dump_lock();

    const char* const ptr_on_generated_image = static_cast<char*>(image->buffer());
    const char* const ptr_on_read_image      = static_cast<char*>(image_from_disk->buffer());

    CPPUNIT_ASSERT_EQUAL(image->type(), image_from_disk->type());
    CPPUNIT_ASSERT(
        std::equal(
            ptr_on_generated_image,
            ptr_on_generated_image + image->size_in_bytes(),
            ptr_on_read_image
        )
    );
}

//------------------------------------------------------------------------------

void image_reader_writer_test::test_mhd_image_writer()
{
    // Data to write
    core::type type                                       = core::type::UINT8;
    const data::image::size_t size_expected               = {10, 20, 30};
    const data::image::spacing_t spacing_expected         = {0.24, 1.07, 2.21};
    const data::image::origin_t origin_expected           = {-5.6, 15.16, 11.11};
    const data::image::orientation_t orientation_expected = {0.36, 0.48, -0.8, -0.8, 0.6, 0.0, 0.48, 0.64, 0.6};

    data::image::sptr image = std::make_shared<data::image>();
    utest_data::generator::image::generate_image(
        image,
        size_expected,
        spacing_expected,
        origin_expected,
        orientation_expected,
        type,
        data::image::rgb,
        0
    );

    // Write to vtk image.
    core::os::temp_dir tmp_dir;
    const auto file = tmp_dir / "tempFile.mhd";

    run_image_srv("sight::module::io::vtk::image_writer", get_io_configuration(file), image);

    // Read image from disk
    data::image::sptr image_from_disk = std::make_shared<data::image>();
    run_image_srv("sight::module::io::vtk::image_reader", get_io_configuration(file), image_from_disk);

    // Data read
    data::image::spacing_t spacing_read = image->spacing();
    data::image::origin_t origin_read   = image->origin();
    data::image::size_t size_read       = image->size();

    CPPUNIT_ASSERT_EQUAL(spacing_expected.size(), spacing_read.size());
    CPPUNIT_ASSERT_EQUAL(origin_expected.size(), origin_read.size());
    CPPUNIT_ASSERT_EQUAL(size_expected.size(), size_read.size());

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on x", spacing_expected[0], spacing_read[0], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on y", spacing_expected[1], spacing_read[1], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect spacing on z", spacing_expected[2], spacing_read[2], EPSILON);

    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on x", origin_expected[0], origin_read[0], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on y", origin_expected[1], origin_read[1], EPSILON);
    CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Incorrect origin on z", origin_expected[2], origin_read[2], EPSILON);

    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on x", size_expected[0], size_read[0]);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on y", size_expected[1], size_read[1]);
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Incorrect size on z", size_expected[2], size_read[2]);

    const auto image_dump_lock           = image->dump_lock();
    const auto image_from_disk_dump_lock = image_from_disk->dump_lock();

    const char* const ptr_on_generated_image = static_cast<char*>(image->buffer());
    const char* const ptr_on_read_image      = static_cast<char*>(image_from_disk->buffer());

    CPPUNIT_ASSERT_EQUAL(image->type(), image_from_disk->type());
    CPPUNIT_ASSERT(
        std::equal(
            ptr_on_generated_image,
            ptr_on_generated_image + image->size_in_bytes(),
            ptr_on_read_image
        )
    );
}

//------------------------------------------------------------------------------

void image_reader_writer_test::test_image_writer_extension()
{
    // Data to write
    const auto type = core::type::UINT8;
    const data::image::size_t size_expected {10, 20, 30};
    const data::image::spacing_t spacing_expected {0.24, 1.07, 2.21};
    const data::image::origin_t origin_expected {-5.6, 15.16, 11.11};
    const data::image::orientation_t orientation_expected = {0.36, 0.48, -0.8, -0.8, 0.6, 0.0, 0.48, 0.64, 0.6};

    data::image::sptr image = std::make_shared<data::image>();
    utest_data::generator::image::generate_image(
        image,
        size_expected,
        spacing_expected,
        origin_expected,
        orientation_expected,
        type,
        data::image::gray_scale,
        0
    );

    // Write to vtk image.
    core::os::temp_dir tmp_dir;
    const auto file = tmp_dir / "tempFile.xxx";

    {
        const std::string srvname("sight::module::io::vtk::image_writer");

        service::base::sptr srv = service::add(srvname);

        CPPUNIT_ASSERT_MESSAGE(std::string("Failed to create service ") + srvname, srv);

        srv->set_input(image, "data");
        CPPUNIT_ASSERT_NO_THROW(srv->set_config(get_io_configuration(file)));
        CPPUNIT_ASSERT_NO_THROW(srv->configure());
        CPPUNIT_ASSERT_NO_THROW(srv->start().wait());
        CPPUNIT_ASSERT_THROW(srv->update().get(), core::tools::failed);
        CPPUNIT_ASSERT_NO_THROW(srv->stop().wait());
        service::remove(srv);
    }
}

//------------------------------------------------------------------------------

} // namespace sight::module::io::vtk::ut