annotate Plugins/Samples/GdcmDecoder/GdcmImageDecoder.cpp @ 3858:3ab2d48c8f69 c-get

integration mainline->c-get
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 21 Apr 2020 16:37:25 +0200
parents 6110a4995ace
children 7e33516965f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1840
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
3640
94f4a18a79cc upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3351
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU General Public License as
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * License, or (at your option) any later version.
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * General Public License for more details.
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "GdcmImageDecoder.h"
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
3768
6110a4995ace replacing std::auto_ptr by std::unique_ptr in GDCM sample plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
24 #include "../../../Core/Compatibility.h"
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "OrthancImageWrapper.h"
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <gdcmImageReader.h>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include <gdcmImageApplyLookupTable.h>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include <gdcmImageChangePlanarConfiguration.h>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include <gdcmImageChangePhotometricInterpretation.h>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #include <stdexcept>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 #include <boost/iostreams/stream.hpp>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include <boost/iostreams/device/array.hpp>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 namespace OrthancPlugins
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 struct GdcmImageDecoder::PImpl
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 const void* dicom_;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 size_t size_;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 gdcm::ImageReader reader_;
3768
6110a4995ace replacing std::auto_ptr by std::unique_ptr in GDCM sample plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
44 std::unique_ptr<gdcm::ImageApplyLookupTable> lut_;
6110a4995ace replacing std::auto_ptr by std::unique_ptr in GDCM sample plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
45 std::unique_ptr<gdcm::ImageChangePhotometricInterpretation> photometric_;
6110a4995ace replacing std::auto_ptr by std::unique_ptr in GDCM sample plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
46 std::unique_ptr<gdcm::ImageChangePlanarConfiguration> interleaved_;
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 std::string decoded_;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 PImpl(const void* dicom,
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 size_t size) :
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 dicom_(dicom),
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 size_(size)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 const gdcm::DataSet& GetDataSet() const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 return reader_.GetFile().GetDataSet();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 const gdcm::Image& GetImage() const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 if (interleaved_.get() != NULL)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 return interleaved_->GetOutput();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 if (lut_.get() != NULL)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 return lut_->GetOutput();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 if (photometric_.get() != NULL)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 return photometric_->GetOutput();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 return reader_.GetImage();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 void Decode()
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 // Change photometric interpretation or apply LUT, if required
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 const gdcm::Image& image = GetImage();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 if (image.GetPixelFormat().GetSamplesPerPixel() == 1 &&
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::PALETTE_COLOR)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 lut_.reset(new gdcm::ImageApplyLookupTable());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 lut_->SetInput(image);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 if (!lut_->Apply())
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 throw std::runtime_error( "GDCM cannot apply the lookup table");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 else if (image.GetPixelFormat().GetSamplesPerPixel() == 1)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 if (image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::MONOCHROME1 &&
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::MONOCHROME2)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 photometric_.reset(new gdcm::ImageChangePhotometricInterpretation());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 photometric_->SetInput(image);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 photometric_->SetPhotometricInterpretation(gdcm::PhotometricInterpretation::MONOCHROME2);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 if (!photometric_->Change() ||
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 GetImage().GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::MONOCHROME2)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 throw std::runtime_error("GDCM cannot change the photometric interpretation");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 else
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 if (image.GetPixelFormat().GetSamplesPerPixel() == 3 &&
1978
0c9c4e36c2b9 support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
117 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::RGB &&
2464
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
118 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::YBR_FULL &&
1978
0c9c4e36c2b9 support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
119 (image.GetTransferSyntax() != gdcm::TransferSyntax::JPEG2000Lossless ||
0c9c4e36c2b9 support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
120 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::YBR_RCT))
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 photometric_.reset(new gdcm::ImageChangePhotometricInterpretation());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 photometric_->SetInput(image);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 photometric_->SetPhotometricInterpretation(gdcm::PhotometricInterpretation::RGB);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 if (!photometric_->Change() ||
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 GetImage().GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::RGB)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 throw std::runtime_error("GDCM cannot change the photometric interpretation");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 // Possibly convert planar configuration to interleaved
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 const gdcm::Image& image = GetImage();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 if (image.GetPlanarConfiguration() != 0 &&
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 image.GetPixelFormat().GetSamplesPerPixel() != 1)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 interleaved_.reset(new gdcm::ImageChangePlanarConfiguration());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 interleaved_->SetInput(image);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 if (!interleaved_->Change() ||
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 GetImage().GetPlanarConfiguration() != 0)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 throw std::runtime_error("GDCM cannot change the planar configuration to interleaved");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 };
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 GdcmImageDecoder::GdcmImageDecoder(const void* dicom,
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 size_t size) :
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 pimpl_(new PImpl(dicom, size))
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 // Setup a stream to the memory buffer
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 using namespace boost::iostreams;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 basic_array_source<char> source(reinterpret_cast<const char*>(dicom), size);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 stream<basic_array_source<char> > stream(source);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 // Parse the DICOM instance using GDCM
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 pimpl_->reader_.SetStream(stream);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 if (!pimpl_->reader_.Read())
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 throw std::runtime_error("Bad file format");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 pimpl_->Decode();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 OrthancPluginPixelFormat GdcmImageDecoder::GetFormat() const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 const gdcm::Image& image = pimpl_->GetImage();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 if (image.GetPixelFormat().GetSamplesPerPixel() == 1 &&
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 (image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME1 ||
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME2))
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 switch (image.GetPixelFormat())
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 case gdcm::PixelFormat::UINT16:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 return OrthancPluginPixelFormat_Grayscale16;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 case gdcm::PixelFormat::INT16:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 return OrthancPluginPixelFormat_SignedGrayscale16;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 case gdcm::PixelFormat::UINT8:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 return OrthancPluginPixelFormat_Grayscale8;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 default:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 throw std::runtime_error("Unsupported pixel format");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 else if (image.GetPixelFormat().GetSamplesPerPixel() == 3 &&
1978
0c9c4e36c2b9 support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
196 (image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::RGB ||
2464
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
197 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::YBR_FULL ||
1978
0c9c4e36c2b9 support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
198 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::YBR_RCT))
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 switch (image.GetPixelFormat())
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 case gdcm::PixelFormat::UINT8:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 return OrthancPluginPixelFormat_RGB24;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
2424
7ef9207f31d4 New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
205 case gdcm::PixelFormat::UINT16:
7ef9207f31d4 New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
206 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 3, 1)
7ef9207f31d4 New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
207 return OrthancPluginPixelFormat_RGB48;
7ef9207f31d4 New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
208 #else
2428
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2427
diff changeset
209 throw std::runtime_error("RGB48 pixel format is only supported if compiled against Orthanc SDK >= 1.3.1");
2424
7ef9207f31d4 New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
210 #endif
7ef9207f31d4 New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
211
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 default:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 break;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 throw std::runtime_error("Unsupported pixel format");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 unsigned int GdcmImageDecoder::GetWidth() const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 return pimpl_->GetImage().GetColumns();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 unsigned int GdcmImageDecoder::GetHeight() const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 return pimpl_->GetImage().GetRows();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 unsigned int GdcmImageDecoder::GetFramesCount() const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 return pimpl_->GetImage().GetDimension(2);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 size_t GdcmImageDecoder::GetBytesPerPixel(OrthancPluginPixelFormat format)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 switch (format)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 case OrthancPluginPixelFormat_Grayscale8:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 return 1;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 case OrthancPluginPixelFormat_Grayscale16:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 case OrthancPluginPixelFormat_SignedGrayscale16:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 return 2;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 case OrthancPluginPixelFormat_RGB24:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 return 3;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252
2427
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2426
diff changeset
253 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 3, 1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2426
diff changeset
254 case OrthancPluginPixelFormat_RGB48:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2426
diff changeset
255 return 6;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2426
diff changeset
256 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2426
diff changeset
257
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 default:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 throw std::runtime_error("Unsupport pixel format");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262
2464
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
263 static void ConvertYbrToRgb(uint8_t rgb[3],
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
264 const uint8_t ybr[3])
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
265 {
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
266 // http://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.7.6.3.html#sect_C.7.6.3.1.2
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
267 // https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
268
2485
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2464
diff changeset
269 // TODO - Check out the outcome of Mathieu's discussion about
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2464
diff changeset
270 // truncation of YCbCr-to-RGB conversion:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2464
diff changeset
271 // https://groups.google.com/forum/#!msg/comp.protocols.dicom/JHuGeyWbTz8/ARoTWrJzAQAJ
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2464
diff changeset
272
2464
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
273 const float Y = ybr[0];
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
274 const float Cb = ybr[1];
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
275 const float Cr = ybr[2];
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
276
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
277 const float result[3] = {
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
278 Y + 1.402f * (Cr - 128.0f),
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
279 Y - 0.344136f * (Cb - 128.0f) - 0.714136f * (Cr - 128.0f),
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
280 Y + 1.772f * (Cb - 128.0f)
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
281 };
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
282
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
283 for (uint8_t i = 0; i < 3 ; i++)
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
284 {
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
285 if (result[i] < 0)
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
286 {
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
287 rgb[i] = 0;
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
288 }
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
289 else if (result[i] > 255)
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
290 {
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
291 rgb[i] = 255;
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
292 }
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
293 else
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
294 {
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
295 rgb[i] = static_cast<uint8_t>(result[i]);
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
296 }
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
297 }
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
298 }
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
299
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
300
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
301 static void FixPhotometricInterpretation(OrthancImageWrapper& image,
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
302 gdcm::PhotometricInterpretation interpretation)
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
303 {
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
304 switch (interpretation)
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
305 {
2798
5c83e5cf9d79 fix decoding of MONOCHROME1 & MONOCHROME2 images
am@osimis.io
parents: 2485
diff changeset
306 case gdcm::PhotometricInterpretation::MONOCHROME1:
5c83e5cf9d79 fix decoding of MONOCHROME1 & MONOCHROME2 images
am@osimis.io
parents: 2485
diff changeset
307 case gdcm::PhotometricInterpretation::MONOCHROME2:
2464
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
308 case gdcm::PhotometricInterpretation::RGB:
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
309 return;
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
310
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
311 case gdcm::PhotometricInterpretation::YBR_FULL:
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
312 {
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
313 // Fix for Osimis issue WVB-319: Some images are not loading in US_MF
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
314
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
315 uint32_t width = image.GetWidth();
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
316 uint32_t height = image.GetHeight();
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
317 uint32_t pitch = image.GetPitch();
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
318 uint8_t* buffer = reinterpret_cast<uint8_t*>(image.GetBuffer());
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
319
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
320 if (image.GetFormat() != OrthancPluginPixelFormat_RGB24 ||
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
321 pitch < 3 * width)
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
322 {
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
323 throw std::runtime_error("Internal error");
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
324 }
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
325
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
326 for (uint32_t y = 0; y < height; y++)
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
327 {
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
328 uint8_t* p = buffer + y * pitch;
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
329 for (uint32_t x = 0; x < width; x++, p += 3)
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
330 {
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
331 const uint8_t ybr[3] = { p[0], p[1], p[2] };
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
332 uint8_t rgb[3];
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
333 ConvertYbrToRgb(rgb, ybr);
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
334 p[0] = rgb[0];
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
335 p[1] = rgb[1];
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
336 p[2] = rgb[2];
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
337 }
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
338 }
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
339
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
340 return;
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
341 }
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
342
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
343 default:
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
344 throw std::runtime_error("Unsupported output photometric interpretation");
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
345 }
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
346 }
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
347
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 OrthancPluginImage* GdcmImageDecoder::Decode(OrthancPluginContext* context,
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 unsigned int frameIndex) const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 unsigned int frames = GetFramesCount();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 unsigned int width = GetWidth();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 unsigned int height = GetHeight();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 OrthancPluginPixelFormat format = GetFormat();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 size_t bpp = GetBytesPerPixel(format);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 if (frameIndex >= frames)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 throw std::runtime_error("Inexistent frame index");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 std::string& decoded = pimpl_->decoded_;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364 OrthancImageWrapper target(context, format, width, height);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366 if (width == 0 ||
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367 height == 0)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 return target.Release();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 if (decoded.empty())
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 decoded.resize(pimpl_->GetImage().GetBufferLength());
3351
5069af20932a Web viewer: Check the return value of gdcm::Bitmap::GetBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
375 if (!pimpl_->GetImage().GetBuffer(&decoded[0]))
5069af20932a Web viewer: Check the return value of gdcm::Bitmap::GetBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
376 {
5069af20932a Web viewer: Check the return value of gdcm::Bitmap::GetBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
377 throw std::runtime_error("Image not properly decoded to a memory buffer");
5069af20932a Web viewer: Check the return value of gdcm::Bitmap::GetBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
378 }
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 const void* sourceBuffer = &decoded[0];
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383 if (target.GetPitch() == bpp * width &&
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 frames == 1)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 assert(decoded.size() == target.GetPitch() * target.GetHeight());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 memcpy(target.GetBuffer(), sourceBuffer, decoded.size());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 else
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 size_t targetPitch = target.GetPitch();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 size_t sourcePitch = width * bpp;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 const char* a = &decoded[sourcePitch * height * frameIndex];
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 char* b = target.GetBuffer();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397 for (uint32_t y = 0; y < height; y++)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399 memcpy(b, a, sourcePitch);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400 a += sourcePitch;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 b += targetPitch;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 }
2464
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
404
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
405 FixPhotometricInterpretation(target, pimpl_->GetImage().GetPhotometricInterpretation());
61fc5133e5d5 Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
406
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 return target.Release();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 }