File: file.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 (787 lines) | stat: -rw-r--r-- 28,043 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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/************************************************************************
 *
 * 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 "file.hpp"

#include "data/model_series.hpp"

#include "io/dicom/codec/nvjpeg2k.hpp"

#include <core/macros.hpp>
#include <core/progress/observer.hpp>

#include <data/fiducials_series.hpp>
#include <data/helper/medical_image.hpp>
#include <data/image_series.hpp>

#include <io/bitmap/writer.hpp>

#include <gdcmImageChangeTransferSyntax.h>
#include <gdcmImageWriter.h>

#include <filesystem>
#include <iomanip>
#include <sstream>

// cspell:ignore JPEGLS NEARLOSSLESS

namespace sight::io::dicom::writer
{

/// Decode image orientation / position
/// GDCM doesn't handle Enhanced US Volume (which is rather a complicated case)
/// see @link https://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.8.24.2.html
inline static void compute_transform(
    data::matrix4& _matrix,
    const data::image_series& _image_series
)
{
    // Use the image metadata as a lower priority
    _matrix.set_position(_image_series.origin());

    // Try also the magical field "direction"
    ///@todo remove this when we get rid of the direction "field" from medical image
    if(const auto& direction_matrix = data::helper::medical_image::get_direction(_image_series); direction_matrix)
    {
        for(std::size_t i = 0 ; i < 3 ; ++i)
        {
            for(std::size_t j = 0 ; j < 3 ; ++j)
            {
                _matrix(i, j) = (*direction_matrix)(i, j);
            }
        }
    }

    // But DICOM metadata should have the highest priority !
    if(_image_series.get_ultrasound_acquisition_geometry() == data::dicom::ultrasound_acquisition_geometry_t::apex)
    {
        // Try first the shared group
        if(const auto& shared_orientation = _image_series.get_image_orientation_volume(std::nullopt);
           shared_orientation.size() == 6)
        {
            _matrix.set_orientation(data::image_series::from_dicom_orientation(shared_orientation));
        }

        if(const auto& shared_position = _image_series.get_image_position_volume(std::nullopt);
           shared_position.size() == 3)
        {
            _matrix.set_position(shared_position);
        }

        // Then the first frame from the per_frame group
        if(const auto& frame_orientation = _image_series.get_image_orientation_volume(0);
           frame_orientation.size() == 6)
        {
            _matrix.set_orientation(data::image_series::from_dicom_orientation(frame_orientation));
        }

        if(const auto& frame_position = _image_series.get_image_position_volume(0);
           frame_position.size() == 3)
        {
            _matrix.set_position(frame_position);
        }
    }
    else // data::dicom::ultrasound_acquisition_geometry_t::patient or unknown
    {
        // Try the shared group
        if(const auto& shared_orientation = _image_series.get_image_orientation_patient(std::nullopt);
           shared_orientation.size() == 6)
        {
            _matrix.set_orientation(data::image_series::from_dicom_orientation(shared_orientation));
        }

        if(const auto& shared_position = _image_series.get_image_position_patient(std::nullopt);
           shared_position.size() == 3)
        {
            _matrix.set_position(shared_position);
        }
    }
}

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

inline static void write_enhanced_us_volume(
    const data::image_series& _image_series,
    data::series& _series_copy,
    const std::string& _filepath,
    writer::file::transfer_syntax _transfer_syntax,
    [[maybe_unused]] bool _force_cpu
)
{
    data::matrix4 transform;
    compute_transform(transform, _image_series);

    if(_series_copy.get_ultrasound_acquisition_geometry()
       == data::dicom::ultrasound_acquisition_geometry_t::unknown)
    {
        SIGHT_INFO(
            "The Ultrasound Acquisition Geometry is unknown. "
            "The default 'PATIENT' will be used."
        );

        _series_copy.set_ultrasound_acquisition_geometry(data::dicom::ultrasound_acquisition_geometry_t::patient);
    }

    if(_series_copy.get_ultrasound_acquisition_geometry()
       == data::dicom::ultrasound_acquisition_geometry_t::patient
       && _series_copy.get_patient_frame_of_reference_source()
       == data::dicom::patient_frame_of_reference_source_t::unknown)
    {
        SIGHT_INFO(
            "The Patient Frame of Reference Source is unknown but Ultrasound Acquisition Geometry is `PATIENT`. "
            "The default 'TABLE' will be used."
        );

        _series_copy.set_patient_frame_of_reference_source(
            data::dicom::patient_frame_of_reference_source_t::table
        );
    }

    if(_series_copy.get_ultrasound_acquisition_geometry()
       == data::dicom::ultrasound_acquisition_geometry_t::apex
       && !_series_copy.get_volume_to_transducer_mapping_matrix())
    {
        SIGHT_INFO(
            "The Volume to Transducer mapping is not present but Ultrasound Acquisition Geometry is `APEX`. "
            "The mapping matrix will be set."
        );

        _series_copy.set_volume_to_transducer_mapping_matrix(transform);
    }

    if(_series_copy.get_patient_frame_of_reference_source()
       == data::dicom::patient_frame_of_reference_source_t::table
       && !_series_copy.get_volume_to_table_mapping_matrix())
    {
        SIGHT_INFO(
            "The Volume to Table mapping is not present but Patient Frame of Reference Source is 'TABLE'. "
            "The mapping matrix will be set."
        );

        _series_copy.set_volume_to_table_mapping_matrix(transform);
    }

    if(_series_copy.get_dimension_organization_type()
       == data::dicom::dimension_organization_t::unknown)
    {
        ///@note TILED_SPARSE is not really documented in enhanced US volume, but before the volume is reconstructed
        ///      We need to tells that the slices may not be contiguous
        SIGHT_INFO(
            "The Dimension Organization Type is unknown. "
            "The default 'TILED_SPARSE' will be used."
        );

        _series_copy.set_dimension_organization_type(
            data::dicom::dimension_organization_t::tiled_sparse
        );
    }

    // Now that the defaults are set, we can set the image position / orientation if missing and needed
    if(_series_copy.get_ultrasound_acquisition_geometry()
       == data::dicom::ultrasound_acquisition_geometry_t::apex)
    {
        // Orientation
        if(_series_copy.get_image_orientation_volume(std::nullopt).size() != 6
           && _series_copy.get_image_orientation_volume(0).size() != 6)
        {
            // Orientation can be shared for all frames
            // It won't overwrite the per-frame orientation
            _series_copy.set_image_orientation_volume(
                data::image_series::to_dicom_orientation(transform.orientation()),
                std::nullopt
            );
        }

        // Position
        if(_series_copy.get_image_position_volume(std::nullopt).size() != 3
           && _series_copy.get_image_position_volume(0).size() != 3)
        {
            // Do it for each frames
            // Use the sight image origin for the first frame
            auto frame_position = transform.position<std::vector<double> >();

            // Get z spacing
            const auto z_spacing = _image_series.spacing()[2];

            // We need to compute the frame position from image origin and z spacing
            for(std::size_t frame = 0, end_index = _series_copy.num_frames() ;
                frame < end_index ; ++frame)
            {
                SIGHT_WARN_IF(
                    "ImagePositionVolume for frame " + std::to_string(frame) + " will be overwritten",
                    _series_copy.get_image_position_volume(frame).size() == 3
                );

                _series_copy.set_image_position_volume(frame_position, frame);
                frame_position[2] += z_spacing;
            }
        }
    }
    else
    {
        // Orientation
        if(_series_copy.get_image_orientation_patient(std::nullopt).size() != 6
           && _series_copy.get_image_orientation_patient(0).size() != 6)
        {
            // Orientation can be shared for all frames
            // It won't overwrite the per-frame orientation
            _series_copy.set_image_orientation_patient(
                data::image_series::to_dicom_orientation(transform.orientation()),
                std::nullopt
            );
        }

        // Position
        if(_series_copy.get_image_position_patient(0).size() != 3)
        {
            // Do it for each frames
            // Use the sight image origin for the first frame
            auto frame_position = transform.position<std::vector<double> >();

            // Get z spacing
            const auto z_spacing = _image_series.spacing()[2];

            // We need to compute the frame position from image origin and z spacing
            for(std::size_t frame = 0, end_index = std::max(std::size_t(1), _image_series.size()[2]) ;
                frame < end_index ; ++frame)
            {
                SIGHT_WARN_IF(
                    "ImagePositionPatient for frame " + std::to_string(frame) + " will be overwritten",
                    _series_copy.get_image_position_patient(frame).size() == 3
                );

                _series_copy.set_image_position_patient(frame_position, frame);
                frame_position[2] += z_spacing;
            }
        }
    }

    // Create the writer
    gdcm::ImageWriter writer;
    writer.SetFileName(_filepath.c_str());
    writer.GetFile().SetDataSet(_series_copy.get_data_set());

    auto& gdcm_image = writer.GetImage();

    // Set the dimension to 3. This magically allows to write multi-frame images.
    // Or at least, they are saved and we can read them back.
    gdcm_image.SetNumberOfDimensions(3);

    // Set the image dimensions
    const auto& image_sizes = _image_series.size();
    const std::array<std::uint32_t, 3> dimensions {
        std::uint32_t(image_sizes[0]),
        std::uint32_t(image_sizes[1]),
        std::uint32_t(image_sizes[2])
    };

    gdcm_image.SetDimensions(dimensions.data());

    // This is not enough for Enhanced US Volume
    // We still do it, just in case it will be managed correctly in the future
    const auto& gdcm_direction = transform.orientation();
    gdcm_image.SetDirectionCosines(gdcm_direction.data());
    const auto& gdcm_origin = transform.position();
    gdcm_image.SetOrigin(gdcm_origin.data());

    // Pixel Format
    switch(_image_series.type())
    {
        case core::type::INT8:
            gdcm_image.SetPixelFormat(gdcm::PixelFormat::INT8);
            break;

        case core::type::UINT8:
            gdcm_image.SetPixelFormat(gdcm::PixelFormat::UINT8);
            break;

        case core::type::INT16:
            gdcm_image.SetPixelFormat(gdcm::PixelFormat::INT16);
            break;

        case core::type::UINT16:
            gdcm_image.SetPixelFormat(gdcm::PixelFormat::UINT16);
            break;

        case core::type::INT32:
            gdcm_image.SetPixelFormat(gdcm::PixelFormat::INT32);
            break;

        case core::type::UINT32:
            gdcm_image.SetPixelFormat(gdcm::PixelFormat::UINT32);
            break;

        case core::type::INT64:
            gdcm_image.SetPixelFormat(gdcm::PixelFormat::INT64);
            break;

        case core::type::UINT64:
            gdcm_image.SetPixelFormat(gdcm::PixelFormat::UINT64);
            break;

        case core::type::FLOAT:
            gdcm_image.SetPixelFormat(gdcm::PixelFormat::FLOAT32);
            break;

        case core::type::DOUBLE:
            gdcm_image.SetPixelFormat(gdcm::PixelFormat::FLOAT64);
            break;

        default:
            SIGHT_THROW("Unsupported type: '" << _image_series.type().name() << "'");
    }

    // Photometric Interpretation
    switch(_image_series.pixel_format())
    {
        case data::image::pixel_format_t::gray_scale:
            gdcm_image.SetPhotometricInterpretation(gdcm::PhotometricInterpretation::MONOCHROME2);
            gdcm_image.GetPixelFormat().SetSamplesPerPixel(1);
            break;

        case data::image::pixel_format_t::rgb:
            gdcm_image.SetPhotometricInterpretation(gdcm::PhotometricInterpretation::RGB);
            gdcm_image.GetPixelFormat().SetSamplesPerPixel(3);
            break;

        case data::image::pixel_format_t::rgba:
            gdcm_image.SetPhotometricInterpretation(gdcm::PhotometricInterpretation::RGB);
            gdcm_image.GetPixelFormat().SetSamplesPerPixel(4);
            break;

        default:
            SIGHT_THROW("Unsupported pixel format: '" << _image_series.pixel_format() << "'");
    }

    // Planar Configuration is always 0 (R1G1B1 R2G2B2 ...)
    gdcm_image.SetPlanarConfiguration(0);

    // Spacing
    gdcm_image.SetSpacing(_image_series.spacing().data());

    // Dump the image data to GDCM
    /// @note This seems to be sub-optimal as we are going to recompress the data in JPEG2000
    /// @todo Find a way to avoid this double copying of possibly huge image data
    const auto image_locked  = _image_series.dump_lock();
    const auto size_in_bytes = _image_series.size_in_bytes();

    SIGHT_THROW_IF("Size in Bytes is greater than 4GB", _image_series.size_in_bytes() > 0xFFFFFFFF);

    using pixel_data_t = data::dicom::attribute::Attribute<data::dicom::attribute::Keyword::PixelData>;
    gdcm::DataElement pixeldata(gdcm::Tag(pixel_data_t::s_group, pixel_data_t::s_element));

    pixeldata.SetByteValue(
        reinterpret_cast<const char*>(_image_series.buffer()),
        std::uint32_t(size_in_bytes)
    );

    gdcm_image.SetDataElement(pixeldata);

    std::unique_ptr<codec::nvjpeg2k> nvjpeg2k_codec;
    gdcm::ImageChangeTransferSyntax transfer_syntax_changer;

    switch(_transfer_syntax)
    {
        case writer::file::transfer_syntax::raw:
            transfer_syntax_changer.SetTransferSyntax(gdcm::TransferSyntax::ExplicitVRLittleEndian);
            break;

        case writer::file::transfer_syntax::rle:
            transfer_syntax_changer.SetTransferSyntax(gdcm::TransferSyntax::RLELossless);
            break;

        case writer::file::transfer_syntax::jpeg:
            transfer_syntax_changer.SetTransferSyntax(gdcm::TransferSyntax::JPEGBaselineProcess1);
            break;

        case writer::file::transfer_syntax::jpeg_lossless:
            transfer_syntax_changer.SetTransferSyntax(gdcm::TransferSyntax::JPEGLosslessProcess14_1);
            break;

        case writer::file::transfer_syntax::jpeg_ls_nearlossless:
            transfer_syntax_changer.SetTransferSyntax(gdcm::TransferSyntax::JPEGLSNearLossless);
            break;

        case writer::file::transfer_syntax::jpeg_ls_lossless:
            transfer_syntax_changer.SetTransferSyntax(gdcm::TransferSyntax::JPEGLSLossless);
            break;

        case writer::file::transfer_syntax::jpeg_2000:
            transfer_syntax_changer.SetTransferSyntax(gdcm::TransferSyntax::JPEG2000);
            break;

        default:
        {
            if(_image_series.type().size() <= 2)
            {
                // Default is JPEG2000 lossless for 16 bits or less since jpeg2000 doesn't support more than 16 bits
                transfer_syntax_changer.SetTransferSyntax(gdcm::TransferSyntax::JPEG2000Lossless);

#ifdef SIGHT_ENABLE_NVJPEG2K
                if(!_force_cpu)
                {
                    SIGHT_THROW_IF(
                        "nvJPEG2000 is not available, but the support has been compiled in. "
                        "Check your nvJPEG2000 library installation",
                        !io::bitmap::nvjpeg2k()
                    );

                    nvjpeg2k_codec = std::make_unique<codec::nvjpeg2k>();
                    transfer_syntax_changer.SetUserCodec(nvjpeg2k_codec.get());

                    SIGHT_INFO("nvJPEG2000 will be used for JPEG2000 compression.");
                }
                else
                {
                    SIGHT_WARN(
                        "nvJPEG2000 support is available, but OpenJPEG has been forced for JPEG2000 compression."
                    );
                }
#else
                SIGHT_INFO("OpenJPEG will be used for JPEG2000 compression.");
#endif
            }
            else
            {
                // For more thant 16 bits, we use RLE
                transfer_syntax_changer.SetTransferSyntax(gdcm::TransferSyntax::RLELossless);

                SIGHT_WARN(
                    "The image has more than 16 bits per component. "
                    "RLE compression will be used instead of JPEG2000."
                );
            }

            break;
        }
    }

    // Final check for JPEG compression
    switch(transfer_syntax_changer.GetTransferSyntax())
    {
        case gdcm::TransferSyntax::JPEGBaselineProcess1:
        case gdcm::TransferSyntax::JPEGExtendedProcess2_4:
        case gdcm::TransferSyntax::JPEGExtendedProcess3_5:
        case gdcm::TransferSyntax::JPEGSpectralSelectionProcess6_8:
        case gdcm::TransferSyntax::JPEGFullProgressionProcess10_12:
        case gdcm::TransferSyntax::JPEGLosslessProcess14:
        case gdcm::TransferSyntax::JPEGLosslessProcess14_1:
        case gdcm::TransferSyntax::JPEGLSLossless:
        case gdcm::TransferSyntax::JPEGLSNearLossless:
        case gdcm::TransferSyntax::JPEG2000Lossless:
        case gdcm::TransferSyntax::JPEG2000:
        case gdcm::TransferSyntax::JPEG2000Part2Lossless:
        case gdcm::TransferSyntax::JPEG2000Part2:
        {
            SIGHT_THROW_IF(
                "JPEG compression doesn't support more than 16 nits per component.",
                _image_series.type().size() > 2
            );

            break;
        }

        default:
            break;
    }

    transfer_syntax_changer.SetInput(gdcm_image);
    transfer_syntax_changer.Change();

    // This is hackish, but the only way to get the correct Photometric Interpretation without having to deep copying
    // the whole image. Anyway, this should have been done in gdcm::ImageChangeTransferSyntax.
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
    auto& changed_gdcm_image = const_cast<gdcm::Image&>(transfer_syntax_changer.GetOutput());

    // Correct the Photometric Interpretation (This avoid a warning when GDCM decodes back the image)
    if(_image_series.pixel_format() == data::image::pixel_format_t::gray_scale)
    {
        // For grayscale, we must have MONOCHROME2
        changed_gdcm_image.SetPhotometricInterpretation(gdcm::PhotometricInterpretation::MONOCHROME2);
        changed_gdcm_image.GetPixelFormat().SetSamplesPerPixel(1);
    }
    else
    {
        switch(_transfer_syntax)
        {
            case writer::file::transfer_syntax::jpeg:
            case writer::file::transfer_syntax::jpeg_ls_nearlossless:
                changed_gdcm_image.SetPhotometricInterpretation(gdcm::PhotometricInterpretation::YBR_FULL_422);
                break;

            case writer::file::transfer_syntax::jpeg_2000:
                changed_gdcm_image.SetPhotometricInterpretation(gdcm::PhotometricInterpretation::YBR_ICT);
                break;

            case writer::file::transfer_syntax::jpeg_2000_lossless:
            case writer::file::transfer_syntax::sop_default:
                changed_gdcm_image.SetPhotometricInterpretation(gdcm::PhotometricInterpretation::YBR_RCT);
                break;

            default:
                // Nothing to do
                break;
        }
    }

    writer.SetImage(changed_gdcm_image);

    // Finally write the file
    SIGHT_THROW_IF(
        "Failed to write the DICOM file '" + _filepath + "'.",
        !writer.Write()
    );
}

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

static void write_spatial_fiducials(
    const data::fiducials_series& _fiducials_series,
    const std::filesystem::path& _image_path
)
{
    gdcm::Writer writer;
    std::filesystem::path folder = _image_path.parent_path();
    std::string filename         = _image_path.stem().string() + "_fiducials.dcm";

    writer.SetFileName((folder / filename).string().c_str());
    writer.GetFile().SetDataSet(_fiducials_series.get_data_set());
    // Dummy value else GDCM complains. TransferSyntax shouldn't be required since Fiducials aren't an image.
    writer.GetFile().GetHeader().SetDataSetTransferSyntax(gdcm::TransferSyntax::JPEG2000Lossless);
    writer.Write();
}

/// Private Writer implementation
class file::writer_impl
{
public:

    /// Delete default constructors and assignment operators
    writer_impl(const writer_impl&)            = delete;
    writer_impl(writer_impl&&)                 = delete;
    writer_impl& operator=(const writer_impl&) = delete;
    writer_impl& operator=(writer_impl&&)      = delete;

    /// Constructor
    explicit writer_impl(file* const _writer) noexcept :
        m_writer(_writer)
    {
    }

    /// Default destructor
    ~writer_impl() noexcept = default;

    /// Pointer to the public interface
    file* const m_writer;

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

    [[nodiscard]] bool cancel_requested() const noexcept
    {
        return m_progress && m_progress->cancel_requested();
    }

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

    void progress(std::uint64_t _units) const
    {
        if(m_progress)
        {
            m_progress->done_work(_units);
        }
    }

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

    /// Allows to watch for cancellation and report progress.
    core::progress::observer::sptr m_progress;

    /// True to disable GPU codec
    bool m_force_cpu {false};

    /// The overriden transfer syntax
    transfer_syntax m_transfer_syntax {transfer_syntax::sop_default};
};

file::file() :
    core::location::single_folder(),
    m_pimpl(std::make_unique<writer_impl>(this))
{
}

// Defining the destructor here, allows us to use PImpl with a unique_ptr
file::~file() noexcept = default;

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

void file::write(sight::core::progress::observer::sptr _progress)
{
    SIGHT_ASSERT("Some work have already be reported.", _progress->get_done_work_units() == 0);
    m_pimpl->m_progress = _progress;
    m_pimpl->m_progress->set_total_work_units(100);
    m_pimpl->m_progress->done_work(10);

    // Get the destination folder
    auto folder = get_folder();

    if(folder.empty() && get_file().has_parent_path())
    {
        folder = get_file().parent_path();
    }

    if(!std::filesystem::exists(folder))
    {
        std::filesystem::create_directories(folder);
    }

    SIGHT_THROW_IF("The folder '" + folder.string() + "' is not a directory.", !std::filesystem::is_directory(folder));

    // Retrieve the object to write
    const auto& series_set = get_concrete_object();

    // Compute progress for one step
    const size_t progress_step = (100 - 20) / series_set->size();

    // Compute the base name
    const auto& file         = get_file();
    const bool prepend_index = file.empty() || series_set->size() > 1;
    const auto& basename     =
        [&]
        {
            if(file.empty())
            {
                return extension();
            }

            if(prepend_index)
            {
                return "-" + file.filename().string();
            }

            return file.filename().string();
        }();

    // For each series..
    for(std::size_t index = 0 ; const auto& series : *series_set)
    {
        if(m_pimpl->cancel_requested())
        {
            break;
        }

        const auto& filepath =
            [&]
            {
                if(prepend_index)
                {
                    std::stringstream ss;
                    ss << std::setw(3) << std::setfill('0') << index;
                    return folder / (ss.str() + basename);
                }

                return folder / basename;
            }();

        if(const auto& sop_keyword = series->get_sop_keyword();
           sop_keyword == data::dicom::sop::Keyword::EnhancedUSVolumeStorage)
        {
            const auto& image_series = std::dynamic_pointer_cast<data::image_series>(series);

            SIGHT_THROW_IF(
                "The series '" + series->get_series_instance_uid() + "' is not an image series.",
                !image_series
            );

            // Shallow copy the series (but not the pixel data !)
            // This will allow to modify the GDCM DICOM context using series API to workaround some GDCM bugs or
            // unimplemented features like the image origin for Enhanced US Volume
            auto series_copy = std::make_shared<data::series>();
            series_copy->shallow_copy(series);

            write_enhanced_us_volume(
                *image_series,
                *series_copy,
                filepath.string(),
                m_pimpl->m_transfer_syntax,
                m_pimpl->m_force_cpu
            );
        }
        else
        {
            try
            {
                SIGHT_THROW(
                    "SOP Class '" << data::dicom::sop::get(sop_keyword).m_name << "' is not supported."
                );
            }
            catch(const std::exception&)
            {
                SIGHT_THROW("SOP Class ID '" << int(sop_keyword) << "' is unknown.");
            }
        }

        if(const auto& image_series = std::dynamic_pointer_cast<data::image_series>(series))
        {
            if(auto fiducials = image_series->get_fiducials(); !fiducials->get_fiducial_sets().empty())
            {
                // Make sure to include this field as it is necessary for creating a correct header for the DICOM file
                if(fiducials->get_sop_instance_uid().empty())
                {
                    fiducials->new_sop_instance();
                }

                write_spatial_fiducials(*fiducials, filepath);
            }
        }
        else if(const auto& model_series = std::dynamic_pointer_cast<data::model_series>(series))
        {
            if(auto fiducials = model_series->get_fiducials(); !fiducials->get_fiducial_sets().empty())
            {
                // Make sure to include this field as it is necessary for creating a correct header for the DICOM file
                if(fiducials->get_sop_instance_uid().empty())
                {
                    fiducials->new_sop_instance();
                }

                write_spatial_fiducials(*fiducials, filepath);
            }
        }

        m_pimpl->progress(progress_step);
        ++index;
    }
}

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

void file::force_cpu(bool _force)
{
    m_pimpl->m_force_cpu = _force;
}

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

void file::set_transfer_syntax(file::transfer_syntax _transfer_syntax)
{
    m_pimpl->m_transfer_syntax = _transfer_syntax;
}

} // namespace sight::io::dicom::writer