File: nvjpeg2k.cpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (182 lines) | stat: -rw-r--r-- 5,752 bytes parent folder | download
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
/************************************************************************
 *
 * Copyright (C) 2023-2025 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 "nvjpeg2k.hpp"

#include "gdcmSequenceOfFragments.h"

#include <io/bitmap/writer.hpp>

namespace sight::io::dicom::codec
{

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

static inline core::type gdcm_to_sight_pf(const gdcm::PixelFormat& _pf)
{
    switch(_pf.GetScalarType())
    {
        case gdcm::PixelFormat::SINGLEBIT:
        case gdcm::PixelFormat::UINT8:
            return core::type::UINT8;

        case gdcm::PixelFormat::INT8:
            return core::type::INT8;

        case gdcm::PixelFormat::UINT16:
            return core::type::UINT16;

        case gdcm::PixelFormat::INT16:
            return core::type::INT16;

        case gdcm::PixelFormat::UINT32:
            return core::type::UINT32;

        case gdcm::PixelFormat::INT32:
            return core::type::INT32;

        case gdcm::PixelFormat::UINT64:
            return core::type::UINT64;

        case gdcm::PixelFormat::INT64:
            return core::type::INT64;

        case gdcm::PixelFormat::FLOAT32:
            return core::type::FLOAT;

        case gdcm::PixelFormat::FLOAT64:
            return core::type::DOUBLE;

        default:
            return core::type::NONE;
    }
}

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

static inline enum data::image::pixel_format_t gdcm_to_sight_pi(
    const gdcm::PhotometricInterpretation& _pi,
    const gdcm::PixelFormat& _pf
)
{
    if(_pi == gdcm::PhotometricInterpretation::PALETTE_COLOR)
    {
        // PALETTE_COLOR is always expended as RGB
        return data::image::pixel_format_t::rgb;
    }

    const auto gdcm_sample_per_pixel = _pf.GetSamplesPerPixel();

    if(gdcm_sample_per_pixel == 1)
    {
        // No need to check, no color space conversion...
        return data::image::pixel_format_t::gray_scale;
    }

    if(gdcm_sample_per_pixel == 3
       && (_pi == gdcm::PhotometricInterpretation::YBR_FULL
           || _pi == gdcm::PhotometricInterpretation::YBR_FULL_422
           || _pi == gdcm::PhotometricInterpretation::YBR_ICT
           || _pi == gdcm::PhotometricInterpretation::YBR_RCT
           || _pi == gdcm::PhotometricInterpretation::RGB))
    {
        return data::image::pixel_format_t::rgb;
    }

    // Unsupported...
    return data::image::pixel_format_t::undefined;
}

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

bool nvjpeg2k::Code(gdcm::DataElement const& _in, gdcm::DataElement& _out)
{
    _out = _in;

    gdcm::SmartPointer<gdcm::SequenceOfFragments> sq = new gdcm::SequenceOfFragments;

    const auto* dims = this->GetDimensions();

    const auto* in_byte_value = _in.GetByteValue();
    const auto* in_pointer    = in_byte_value->GetPointer();
    const auto in_length      = in_byte_value->GetLength();
    const auto frame_size     = in_length / dims[2];

    // Create the image used as input buffer
    auto image           = std::make_shared<data::image>();
    const auto dump_lock = image->dump_lock();

    // Create the writer
    auto writer = std::make_shared<bitmap::writer>();
    writer->set_object(image);

    // The output buffer is resized by the writer if not big enough
    std::vector<std::uint8_t> output_buffer(frame_size);

    const auto& sight_type   = gdcm_to_sight_pf(this->GetPixelFormat());
    const auto& sight_size   = sight::data::image::size_t {dims[0], dims[1], 1};
    const auto& sight_format = gdcm_to_sight_pi(this->GetPhotometricInterpretation(), this->GetPixelFormat());

    for(std::size_t z = 0, end = dims[2] ; z < end ; ++z)
    {
        // Compute the address of the current frame
        const char* in_frame = in_pointer + (z * std::size_t(frame_size));

        // We change the buffer address of the image to avoid unneeded copy
        image->set_buffer(
            // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
            const_cast<char*>(in_frame),
            false,
            sight_type,
            sight_size,
            sight_format,
            std::make_shared<core::memory::buffer_no_alloc_policy>()
        );

        // Encode the frame
        const auto output_size = writer->write(
            output_buffer,
            bitmap::backend::nvjpeg2k_j2k,
            bitmap::writer::mode::fast
        );

        SIGHT_THROW_IF("Output size is greater than 4GB", output_size > 0xFFFFFFFF);

        // Add the encoded frame to the sequence
        gdcm::Fragment frag;
        frag.SetByteValue(reinterpret_cast<char*>(output_buffer.data()), std::uint32_t(output_size));
        sq->AddFragment(frag);
    }

    _out.SetValue(*sq);

    return true;
}

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

gdcm::ImageCodec* nvjpeg2k::Clone() const
{
    auto* copy = new nvjpeg2k;
    return copy;
}

} // namespace sight::io::dicom::codec