File: reader_test.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (440 lines) | stat: -rw-r--r-- 12,990 bytes parent folder | download | duplicates (2)
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
/************************************************************************
 *
 * Copyright (C) 2023-2024 IRCAD France
 *
 * 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 "reader_test.hpp"
#include "helper.hxx"

#include <io/bitmap/reader.hpp>
#include <core/os/temp_path.hpp>

#include <utest/filter.hpp>
#include <utest/profiling.hpp>

#include <cstdlib>
#include <future>

// This is for putenv() which is part of <cstdlib>
// cspell:ignore hicpp nvjpeg LIBJPEG LIBTIFF LUMA Acuson IMWRITE IMREAD ANYDEPTH ANYCOLOR OPENCV stoull
// NOLINTNEXTLINE(hicpp-deprecated-headers,modernize-deprecated-headers)
#include <stdlib.h>

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(sight::io::bitmap::ut::reader_test);

namespace sight::io::bitmap::ut
{

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

inline static void test_backend(
    const std::filesystem::path& _file,
    backend _backend,
    data::image::sptr _expected_image = data::image::sptr()
)
{
    core::os::temp_dir temp_dir;
    std::filesystem::path filepath;

    if(_expected_image)
    {
        filepath = temp_dir / _file;

        // Write a expected image to disk
        const auto& mat = image_to_mat(_expected_image);
        cv::imwrite(
            filepath.string(),
            mat,
            {
                // Use best possible quality
                cv::IMWRITE_JPEG_QUALITY, 100,
                cv::IMWRITE_JPEG_CHROMA_QUALITY, 100,
                cv::IMWRITE_JPEG_LUMA_QUALITY, 100,
                cv::IMWRITE_JPEG_OPTIMIZE, 1
            });
    }
    else if(std::filesystem::exists(_file) && std::filesystem::is_regular_file(_file))
    {
        filepath        = _file;
        _expected_image = read_image(filepath);
    }
    else
    {
        CPPUNIT_FAIL("File not found: " + _file.string());
    }

    auto actual_image = std::make_shared<data::image>();

    // Read the image from disk
    {
        auto reader = std::make_shared<io::bitmap::reader>();
        reader->set_object(actual_image);
        reader->set_file(filepath);

        CPPUNIT_ASSERT_NO_THROW(reader->read(_backend));
    }

    const auto& expected_sizes = _expected_image->size();
    const auto& actual_sizes   = actual_image->size();

    CPPUNIT_ASSERT_EQUAL(expected_sizes[0], actual_sizes[0]);
    CPPUNIT_ASSERT_EQUAL(expected_sizes[1], actual_sizes[1]);
    CPPUNIT_ASSERT_EQUAL(expected_sizes[2], actual_sizes[2]);
    CPPUNIT_ASSERT_EQUAL(_expected_image->pixel_format(), actual_image->pixel_format());
    CPPUNIT_ASSERT_EQUAL(_expected_image->type(), actual_image->type());

    if(_backend == backend::libjpeg || _backend == backend::nvjpeg)
    {
        CPPUNIT_ASSERT(compute_psnr(_expected_image, actual_image) > 20.0);
    }
    else
    {
        CPPUNIT_ASSERT(*_expected_image == *actual_image);
    }

    // test non existant file
    {
        constexpr auto path = "<<[{:}]>>";

        auto reader = std::make_shared<io::bitmap::reader>();
        reader->set_object(actual_image);
        reader->set_file(path);

        CPPUNIT_ASSERT_THROW(reader->read(_backend), core::exception);
    }

    // test existing corrupted file
    {
        core::os::temp_file corrupted_file(std::ios_base::out);
        corrupted_file << "This is a corrupted file";
        corrupted_file.stream().close();

        auto reader = std::make_shared<io::bitmap::reader>();
        reader->set_object(actual_image);
        reader->set_file(corrupted_file);

        CPPUNIT_ASSERT_THROW(reader->read(_backend), core::exception);
    }

    // test file with bad extension
    if(std::filesystem::exists(filepath))
    {
        auto reader = std::make_shared<io::bitmap::reader>();
        reader->set_object(actual_image);
        reader->set_file(filepath.replace_extension(".bad"));

        CPPUNIT_ASSERT_THROW(reader->read(_backend), core::exception);
    }
}

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

inline static void profile_reader(
    std::size_t _loop,
    backend _backend
)
{
    auto reader = std::make_shared<io::bitmap::reader>();

    auto actual_image = std::make_shared<data::image>();
    reader->set_object(actual_image);

    const auto& filename = "wild" + extensions(_backend).front();
    const auto& filepath = utest_data::dir() / "sight" / "image" / "bitmap" / filename;
    reader->set_file(filepath);

    const std::string backend_name =
        [&]
        {
            switch(_backend)
            {
                case backend::libtiff:
                    return "libTIFF";

                case backend::libpng:
                    return "libPNG";

                case backend::nvjpeg:
                    return "nvJPEG";

                case backend::libjpeg:
                    return "libjpeg";

                case backend::nvjpeg2k:
                    return "nv_jpeg_2k";

                case backend::openjpeg:
                    return "openJPEG";

                default:
                    SIGHT_THROW("Unknown backend: '" << std::uint8_t(_backend) << "'");
            }
        }();

    // Now profile reading
    SIGHT_PROFILE_FUNC(
        [&]
        (std::size_t)
        {
            CPPUNIT_ASSERT_NO_THROW(reader->read(_backend));
        },
        _loop,
        backend_name,
        0.1
    );

    // Now profile openCV reading to compare
    if(!utest::filter::ignore_slow_tests() && _loop > 1)
    {
        const auto& opencv_label = "(openCV): " + filepath.extension().string();
        SIGHT_PROFILE_FUNC(
            [&]
            (std::size_t)
            {
                CPPUNIT_ASSERT_NO_THROW(read_image(filepath));
            },
            _loop,
            opencv_label,
            0.1
        );
    }
}

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

void reader_test::setUp()
{
    std::string jasper("OPENCV_IO_ENABLE_JASPER=1");
    putenv(jasper.data());
}

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

void reader_test::tearDown()
{
}

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

void reader_test::basic_test()
{
    auto reader = std::make_shared<io::bitmap::reader>();

    CPPUNIT_ASSERT_EQUAL(io::bitmap::extensions(backend::libtiff).front(), reader->extension());
}

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

void reader_test::extensions_test()
{
    std::vector<data::sequenced_set<std::string> > extensions {
        {".jpg", ".jpeg"},
        {".jp2"},
        {".j2k"},
        {".tiff", ".tif"},
        {".png"}
    };

    std::vector<backend> backends {
        io::bitmap::nv_jpeg() ? backend::nvjpeg : backend::libjpeg,
        io::bitmap::nv_jpeg_2k() ? backend::nvjpeg2k : backend::openjpeg,
        io::bitmap::nv_jpeg_2k() ? backend::nvjpeg2k_j2k : backend::openjpeg_j2k,
        backend::libtiff,
        backend::libpng
    };

    for(std::size_t i = 0 ; i < extensions.size() ; ++i)
    {
        const auto& extension_set        = extensions[i];
        const auto& backend              = backends[i];
        const auto& actual_extension_set = io::bitmap::extensions(backend);

        CPPUNIT_ASSERT_EQUAL(extension_set.size(), actual_extension_set.size());

        for(std::size_t j = 0 ; j < extension_set.size() ; ++j)
        {
            CPPUNIT_ASSERT_EQUAL(extension_set[j], actual_extension_set[j]);
        }
    }
}

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

void reader_test::wildcard_test()
{
    std::vector<backend> backends {
        io::bitmap::nv_jpeg() ? backend::nvjpeg : backend::libjpeg,
        io::bitmap::nv_jpeg_2k() ? backend::nvjpeg2k : backend::openjpeg,
        io::bitmap::nv_jpeg_2k() ? backend::nvjpeg2k_j2k : backend::openjpeg_j2k,
        backend::libtiff,
        backend::libpng
    };

    static constexpr auto s_JPEG_LABEL {"JPEG image"};
    static constexpr auto s_TIFF_LABEL {"TIFF image"};
    static constexpr auto s_PNG_LABEL {"PNG image"};
    static constexpr auto s_J2_K_LABEL {"JPEG2000 image"};

    std::vector<std::string> labels {
        s_JPEG_LABEL,
        s_J2_K_LABEL,
        s_J2_K_LABEL,
        s_TIFF_LABEL,
        s_PNG_LABEL
    };

    static constexpr auto s_JPEG_EXT {".jpeg"};
    static constexpr auto s_JPG_EXT {".jpg"};
    static constexpr auto s_TIF_EXT {".tif"};
    static constexpr auto s_TIFF_EXT {".tiff"};
    static constexpr auto s_PNG_EXT {".png"};
    static constexpr auto s_J_P2_EXT {".jp2"};
    static constexpr auto s_J2_K_EXT {".j2k"};

    std::vector<std::string> wildcards {
        std::string("*") + s_JPG_EXT + " *" + s_JPEG_EXT,
        std::string("*") + s_J_P2_EXT,
        std::string("*") + s_J2_K_EXT,
        std::string("*") + s_TIF_EXT + " *" + s_TIFF_EXT,
        std::string("*") + s_PNG_EXT
    };

    for(std::size_t index = 0 ; const auto& backend : backends)
    {
        const auto& [label, wildcard] = io::bitmap::wildcard_filter(backend);

        CPPUNIT_ASSERT_EQUAL(labels[index], label);
        CPPUNIT_ASSERT_EQUAL(wildcards[index], wildcard);
        ++index;
    }
}

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

void reader_test::nv_jpeg_test()
{
    if(!io::bitmap::nv_jpeg())
    {
        return;
    }

    test_backend("nvJPEG.jpg", backend::nvjpeg, get_synthetic_image(0));
}

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

void reader_test::nv_jpe_g2_k_test()
{
    if(!io::bitmap::nv_jpeg_2k())
    {
        return;
    }

    test_backend(
        // Pure synthetic images cannot be read/written correctly with JASPER and openCV.
        // For that reason, we use a real image.
        utest_data::dir() / "sight" / "image" / "bitmap" / "wild.jp2",
        backend::nvjpeg2k
    );
}

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

void reader_test::lib_png_test()
{
    test_backend("libPNG_RGB_UINT8.png", backend::libpng, get_synthetic_image(0));

    test_backend(
        "libPNG_RGBA_UINT8.png",
        backend::libpng,
        get_synthetic_image(1, core::type::UINT8, data::image::pixel_format_t::rgba)
    );
}

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

void reader_test::lib_jpeg_test()
{
    test_backend("libJPEG.jpg", backend::libjpeg, get_synthetic_image(0));
}

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

void reader_test::open_jpeg_test()
{
    test_backend(
        // Pure synthetic images cannot be read/written correctly with JASPER and openCV.
        // For that reason, we use a real image.
        utest_data::dir() / "sight" / "image" / "bitmap" / "wild.jp2",
        backend::openjpeg
    );
}

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

void reader_test::lib_tiff_test()
{
    test_backend("libTIFF_RGB_UINT8.tiff", backend::libtiff, get_synthetic_image(0));

    test_backend(
        "libTIFF_RGBA_UINT16.tiff",
        backend::libtiff,
        get_synthetic_image(1, core::type::UINT16, data::image::pixel_format_t::rgba)
    );

    test_backend(
        "libTIFF_GRAYSCALE_DOUBLE.tiff",
        backend::libtiff,
        get_synthetic_image(2, core::type::DOUBLE, data::image::pixel_format_t::gray_scale)
    );
}

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

void reader_test::profiling_test()
{
    // Check how many loop to perform
    static const char* const s_ENV_LOOP   = std::getenv("PROFILETEST_LOOP");
    static const std::size_t s_LOOP_COUNT =
        s_ENV_LOOP != nullptr
        ? std::stoull(s_ENV_LOOP)
        : 1;

    SIGHT_INFO("Loop: " << s_LOOP_COUNT);

    profile_reader(s_LOOP_COUNT, backend::libjpeg);

    if(io::bitmap::nv_jpeg())
    {
        profile_reader(s_LOOP_COUNT, backend::nvjpeg);
    }

    profile_reader(s_LOOP_COUNT, backend::openjpeg);

    if(io::bitmap::nv_jpeg_2k())
    {
        profile_reader(s_LOOP_COUNT, backend::nvjpeg2k);
    }

    profile_reader(s_LOOP_COUNT, backend::libpng);
    profile_reader(s_LOOP_COUNT, backend::libtiff);
}

} // namespace sight::io::bitmap::ut