File: dicomsamples.cpp

package info (click to toggle)
charls 2.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,636 kB
  • sloc: cpp: 13,392; ansic: 986; makefile: 22
file content (90 lines) | stat: -rw-r--r-- 2,960 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
// Copyright (c) Team CharLS.
// SPDX-License-Identifier: BSD-3-Clause

#include "dicomsamples.h"
#include "util.h"

#include <array>
#include <iostream>
#include <vector>

using std::array;
using std::cout;
using std::error_code;
using std::vector;

namespace {

bool contains_string(const uint8_t* container, const uint8_t* bytes_to_find, const size_t bytes_length) noexcept
{
    for (size_t j{}; j != bytes_length; ++j)
    {
        if (bytes_to_find[j] != container[j])
            return false;
    }

    return true;
}

int find_string(const vector<uint8_t>& container, const uint8_t* bytes_to_find, const size_t bytes_length) noexcept
{
    for (size_t i{}; i != container.size() - bytes_length; ++i)
    {
        if (contains_string(&container[i], bytes_to_find, bytes_length))
            return static_cast<int>(i);
    }
    return -1;
}

// ReSharper disable CppDeprecatedEntity
DISABLE_DEPRECATED_WARNING

void test_dicom_sample_image(const char* name)
{
    vector<uint8_t> data{read_file(name)};

    const array<uint8_t, 8> pixel_data_start{0x00, 0x00, 0x01, 0x00, 0xFF, 0xD8, 0xFF, 0xF7};

    const int offset{find_string(data, pixel_data_start.data(), pixel_data_start.size())};

    data.erase(data.begin(), data.begin() + offset - 4);

    // remove the DICOM fragment headers (in the concerned images they occur every 64k)
    for (unsigned int i{}; i < data.size(); i += 64 * 1024)
    {
        data.erase(data.begin() + static_cast<int>(i), data.begin() + static_cast<int>(i) + 8);
    }

    JlsParameters params{};
    error_code error{JpegLsReadHeader(data.data(), data.size(), &params, nullptr)};
    assert::is_true(!error);

    vector<uint8_t> data_unc;
    data_unc.resize(static_cast<size_t>(params.stride) * params.height);

    error = JpegLsDecode(data_unc.data(), data_unc.size(), data.data(), data.size(), nullptr, nullptr);
    assert::is_true(!error);
    cout << ".";
}

// ReSharper restore CppDeprecatedEntity
RESTORE_DEPRECATED_WARNING

} // namespace


void test_dicom_wg4_images()
{
    test_dicom_sample_image("test/compsamples_jpegls/IMAGES/JLSL/XA1_JLSL");
    test_dicom_sample_image("test/compsamples_jpegls/IMAGES/JLSL/CT2_JLSL");
    test_dicom_sample_image("test/compsamples_jpegls/IMAGES/JLSL/MG1_JLSL");
    test_dicom_sample_image("test/compsamples_jpegls/IMAGES/JLSL/MR1_JLSL");
    test_dicom_sample_image("test/compsamples_jpegls/IMAGES/JLSL/MR2_JLSL");
    test_dicom_sample_image("test/compsamples_jpegls/IMAGES/JLSL/MR3_JLSL");
    test_dicom_sample_image("test/compsamples_jpegls/IMAGES/JLSL/MR4_JLSL");
    test_dicom_sample_image("test/compsamples_jpegls/IMAGES/JLSL/NM1_JLSL");
    test_dicom_sample_image("test/compsamples_jpegls/IMAGES/JLSL/RG1_JLSL");
    test_dicom_sample_image("test/compsamples_jpegls/IMAGES/JLSL/RG2_JLSL");
    test_dicom_sample_image("test/compsamples_jpegls/IMAGES/JLSL/RG3_JLSL");
    test_dicom_sample_image("test/compsamples_jpegls/IMAGES/JLSL/SC1_JLSL");
}