annotate Plugins/Samples/GdcmDecoder/GdcmImageDecoder.cpp @ 2919:2ca9cd064b15

PamReader::ReadFromMemory
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 06 Nov 2018 15:41:21 +0100
parents 5c83e5cf9d79
children 4e43e67f8ecf
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
2447
878b59270859 upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2428
diff changeset
5 * Copyright (C) 2017-2018 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
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "OrthancImageWrapper.h"
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include <gdcmImageReader.h>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <gdcmImageApplyLookupTable.h>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include <gdcmImageChangePlanarConfiguration.h>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include <gdcmImageChangePhotometricInterpretation.h>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include <stdexcept>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #include <boost/iostreams/stream.hpp>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 #include <boost/iostreams/device/array.hpp>
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
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 namespace OrthancPlugins
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 struct GdcmImageDecoder::PImpl
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 const void* dicom_;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 size_t size_;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 gdcm::ImageReader reader_;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 std::auto_ptr<gdcm::ImageApplyLookupTable> lut_;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 std::auto_ptr<gdcm::ImageChangePhotometricInterpretation> photometric_;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 std::auto_ptr<gdcm::ImageChangePlanarConfiguration> interleaved_;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 std::string decoded_;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 PImpl(const void* dicom,
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 size_t size) :
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 dicom_(dicom),
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 size_(size)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
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 const gdcm::DataSet& GetDataSet() const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 return reader_.GetFile().GetDataSet();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 }
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 const gdcm::Image& GetImage() const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 if (interleaved_.get() != NULL)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 return interleaved_->GetOutput();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 }
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 if (lut_.get() != NULL)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 return lut_->GetOutput();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 }
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 if (photometric_.get() != NULL)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 return photometric_->GetOutput();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
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 return reader_.GetImage();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 }
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 void Decode()
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 // Change photometric interpretation or apply LUT, if required
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 const gdcm::Image& image = GetImage();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 if (image.GetPixelFormat().GetSamplesPerPixel() == 1 &&
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::PALETTE_COLOR)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 lut_.reset(new gdcm::ImageApplyLookupTable());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 lut_->SetInput(image);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 if (!lut_->Apply())
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 throw std::runtime_error( "GDCM cannot apply the lookup table");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
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 else if (image.GetPixelFormat().GetSamplesPerPixel() == 1)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 if (image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::MONOCHROME1 &&
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::MONOCHROME2)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 photometric_.reset(new gdcm::ImageChangePhotometricInterpretation());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 photometric_->SetInput(image);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 photometric_->SetPhotometricInterpretation(gdcm::PhotometricInterpretation::MONOCHROME2);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 if (!photometric_->Change() ||
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 GetImage().GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::MONOCHROME2)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 throw std::runtime_error("GDCM cannot change the photometric interpretation");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 }
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 else
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 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
116 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
117 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
118 (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
119 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::YBR_RCT))
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 photometric_.reset(new gdcm::ImageChangePhotometricInterpretation());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 photometric_->SetInput(image);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 photometric_->SetPhotometricInterpretation(gdcm::PhotometricInterpretation::RGB);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 if (!photometric_->Change() ||
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 GetImage().GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::RGB)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 throw std::runtime_error("GDCM cannot change the photometric interpretation");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 }
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 // Possibly convert planar configuration to interleaved
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 const gdcm::Image& image = GetImage();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 if (image.GetPlanarConfiguration() != 0 &&
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 image.GetPixelFormat().GetSamplesPerPixel() != 1)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 interleaved_.reset(new gdcm::ImageChangePlanarConfiguration());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 interleaved_->SetInput(image);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 if (!interleaved_->Change() ||
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 GetImage().GetPlanarConfiguration() != 0)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 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
145 }
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 GdcmImageDecoder::GdcmImageDecoder(const void* dicom,
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 size_t size) :
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 pimpl_(new PImpl(dicom, size))
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 // Setup a stream to the memory buffer
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 using namespace boost::iostreams;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 basic_array_source<char> source(reinterpret_cast<const char*>(dicom), size);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 stream<basic_array_source<char> > stream(source);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 // Parse the DICOM instance using GDCM
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 pimpl_->reader_.SetStream(stream);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 if (!pimpl_->reader_.Read())
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 throw std::runtime_error("Bad file format");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 }
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 pimpl_->Decode();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 }
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 OrthancPluginPixelFormat GdcmImageDecoder::GetFormat() const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 const gdcm::Image& image = pimpl_->GetImage();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 if (image.GetPixelFormat().GetSamplesPerPixel() == 1 &&
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 (image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME1 ||
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME2))
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 switch (image.GetPixelFormat())
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 case gdcm::PixelFormat::UINT16:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 return OrthancPluginPixelFormat_Grayscale16;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 case gdcm::PixelFormat::INT16:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 return OrthancPluginPixelFormat_SignedGrayscale16;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 case gdcm::PixelFormat::UINT8:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 return OrthancPluginPixelFormat_Grayscale8;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 default:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 throw std::runtime_error("Unsupported pixel format");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 }
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 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
195 (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
196 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
197 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::YBR_RCT))
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 switch (image.GetPixelFormat())
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 case gdcm::PixelFormat::UINT8:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 return OrthancPluginPixelFormat_RGB24;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203
2424
7ef9207f31d4 New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
204 case gdcm::PixelFormat::UINT16:
7ef9207f31d4 New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
205 #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
206 return OrthancPluginPixelFormat_RGB48;
7ef9207f31d4 New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
207 #else
2428
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2427
diff changeset
208 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
209 #endif
7ef9207f31d4 New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
210
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 default:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 break;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 }
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 throw std::runtime_error("Unsupported pixel format");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 }
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 unsigned int GdcmImageDecoder::GetWidth() const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 return pimpl_->GetImage().GetColumns();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 }
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 unsigned int GdcmImageDecoder::GetHeight() const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 return pimpl_->GetImage().GetRows();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 }
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 unsigned int GdcmImageDecoder::GetFramesCount() const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 return pimpl_->GetImage().GetDimension(2);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 }
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 size_t GdcmImageDecoder::GetBytesPerPixel(OrthancPluginPixelFormat format)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 switch (format)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 case OrthancPluginPixelFormat_Grayscale8:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 return 1;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 case OrthancPluginPixelFormat_Grayscale16:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 case OrthancPluginPixelFormat_SignedGrayscale16:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 return 2;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 case OrthancPluginPixelFormat_RGB24:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 return 3;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251
2427
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2426
diff changeset
252 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 3, 1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2426
diff changeset
253 case OrthancPluginPixelFormat_RGB48:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2426
diff changeset
254 return 6;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2426
diff changeset
255 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2426
diff changeset
256
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 default:
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 throw std::runtime_error("Unsupport pixel format");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 }
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
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
262 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
263 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
264 {
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 // 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
266 // 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
267
2485
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2464
diff changeset
268 // TODO - Check out the outcome of Mathieu's discussion about
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2464
diff changeset
269 // truncation of YCbCr-to-RGB conversion:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2464
diff changeset
270 // https://groups.google.com/forum/#!msg/comp.protocols.dicom/JHuGeyWbTz8/ARoTWrJzAQAJ
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2464
diff changeset
271
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
272 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
273 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
274 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
275
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 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
277 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
278 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
279 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
280 };
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 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
283 {
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 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
285 {
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 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
287 }
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 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
289 {
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 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
291 }
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 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
293 {
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 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
295 }
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 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
301 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
302 {
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 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
304 {
2798
5c83e5cf9d79 fix decoding of MONOCHROME1 & MONOCHROME2 images
am@osimis.io
parents: 2485
diff changeset
305 case gdcm::PhotometricInterpretation::MONOCHROME1:
5c83e5cf9d79 fix decoding of MONOCHROME1 & MONOCHROME2 images
am@osimis.io
parents: 2485
diff changeset
306 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
307 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
308 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
309
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 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
311 {
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 // 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
313
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 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
315 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
316 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
317 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
318
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 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
320 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
321 {
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 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
323 }
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 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
326 {
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 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
328 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
329 {
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 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
331 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
332 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
333 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
334 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
335 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
336 }
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 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
340 }
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 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
343 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
344 }
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
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 OrthancPluginImage* GdcmImageDecoder::Decode(OrthancPluginContext* context,
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 unsigned int frameIndex) const
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 unsigned int frames = GetFramesCount();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 unsigned int width = GetWidth();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 unsigned int height = GetHeight();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 OrthancPluginPixelFormat format = GetFormat();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 size_t bpp = GetBytesPerPixel(format);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 if (frameIndex >= frames)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 throw std::runtime_error("Inexistent frame index");
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 }
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 std::string& decoded = pimpl_->decoded_;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 OrthancImageWrapper target(context, format, width, height);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365 if (width == 0 ||
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366 height == 0)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 return target.Release();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 }
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 if (decoded.empty())
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 decoded.resize(pimpl_->GetImage().GetBufferLength());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 pimpl_->GetImage().GetBuffer(&decoded[0]);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 const void* sourceBuffer = &decoded[0];
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 if (target.GetPitch() == bpp * width &&
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 frames == 1)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 assert(decoded.size() == target.GetPitch() * target.GetHeight());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383 memcpy(target.GetBuffer(), sourceBuffer, decoded.size());
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 else
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 size_t targetPitch = target.GetPitch();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 size_t sourcePitch = width * bpp;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 const char* a = &decoded[sourcePitch * height * frameIndex];
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 char* b = target.GetBuffer();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 for (uint32_t y = 0; y < height; y++)
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 {
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 memcpy(b, a, sourcePitch);
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 a += sourcePitch;
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397 b += targetPitch;
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 }
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
400
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
401 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
402
1834
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 return target.Release();
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
404 }
b1a6f49b21dd GDCM decoder sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 }