comparison OrthancFramework/Sources/DicomFormat/DicomIntegerPixelAccessor.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/DicomFormat/DicomIntegerPixelAccessor.cpp@94f4a18a79cc
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeaders.h"
35
36 #ifndef NOMINMAX
37 #define NOMINMAX
38 #endif
39
40 #include "DicomIntegerPixelAccessor.h"
41
42 #include "../OrthancException.h"
43 #include <boost/lexical_cast.hpp>
44 #include <limits>
45 #include <cassert>
46 #include <stdio.h>
47
48 namespace Orthanc
49 {
50 DicomIntegerPixelAccessor::DicomIntegerPixelAccessor(const DicomMap& values,
51 const void* pixelData,
52 size_t size) :
53 information_(values),
54 pixelData_(pixelData),
55 size_(size)
56 {
57 if (information_.GetBitsAllocated() > 32 ||
58 information_.GetBitsStored() >= 32)
59 {
60 // Not available, as the accessor internally uses int32_t values
61 throw OrthancException(ErrorCode_NotImplemented);
62 }
63
64 frame_ = 0;
65 frameOffset_ = information_.GetFrameSize();
66
67 if (information_.GetNumberOfFrames() * frameOffset_ > size)
68 {
69 throw OrthancException(ErrorCode_BadFileFormat);
70 }
71
72 if (information_.IsSigned())
73 {
74 // Pixels are signed
75 mask_ = (1 << (information_.GetBitsStored() - 1)) - 1;
76 signMask_ = (1 << (information_.GetBitsStored() - 1));
77 }
78 else
79 {
80 // Pixels are unsigned
81 mask_ = (1 << information_.GetBitsStored()) - 1;
82 signMask_ = 0;
83 }
84
85 if (information_.IsPlanar())
86 {
87 /**
88 * Each color plane shall be sent contiguously. For RGB images,
89 * this means the order of the pixel values sent is R1, R2, R3,
90 * ..., G1, G2, G3, ..., B1, B2, B3, etc.
91 **/
92 rowOffset_ = information_.GetWidth() * information_.GetBytesPerValue();
93 }
94 else
95 {
96 /**
97 * The sample values for the first pixel are followed by the
98 * sample values for the second pixel, etc. For RGB images, this
99 * means the order of the pixel values sent shall be R1, G1, B1,
100 * R2, G2, B2, ..., etc.
101 **/
102 rowOffset_ = information_.GetWidth() * information_.GetBytesPerValue() * information_.GetChannelCount();
103 }
104 }
105
106
107 void DicomIntegerPixelAccessor::GetExtremeValues(int32_t& min,
108 int32_t& max) const
109 {
110 if (information_.GetHeight() == 0 || information_.GetWidth() == 0)
111 {
112 min = max = 0;
113 return;
114 }
115
116 min = std::numeric_limits<int32_t>::max();
117 max = std::numeric_limits<int32_t>::min();
118
119 for (unsigned int y = 0; y < information_.GetHeight(); y++)
120 {
121 for (unsigned int x = 0; x < information_.GetWidth(); x++)
122 {
123 for (unsigned int c = 0; c < information_.GetChannelCount(); c++)
124 {
125 int32_t v = GetValue(x, y);
126 if (v < min)
127 min = v;
128 if (v > max)
129 max = v;
130 }
131 }
132 }
133 }
134
135
136 int32_t DicomIntegerPixelAccessor::GetValue(unsigned int x,
137 unsigned int y,
138 unsigned int channel) const
139 {
140 assert(x < information_.GetWidth() &&
141 y < information_.GetHeight() &&
142 channel < information_.GetChannelCount());
143
144 const uint8_t* pixel = reinterpret_cast<const uint8_t*>(pixelData_) +
145 y * rowOffset_ + frame_ * frameOffset_;
146
147 // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#sect_C.7.6.3.1.3
148 if (information_.IsPlanar())
149 {
150 /**
151 * Each color plane shall be sent contiguously. For RGB images,
152 * this means the order of the pixel values sent is R1, R2, R3,
153 * ..., G1, G2, G3, ..., B1, B2, B3, etc.
154 **/
155 assert(frameOffset_ % information_.GetChannelCount() == 0);
156 pixel += channel * frameOffset_ / information_.GetChannelCount() + x * information_.GetBytesPerValue();
157 }
158 else
159 {
160 /**
161 * The sample values for the first pixel are followed by the
162 * sample values for the second pixel, etc. For RGB images, this
163 * means the order of the pixel values sent shall be R1, G1, B1,
164 * R2, G2, B2, ..., etc.
165 **/
166 pixel += channel * information_.GetBytesPerValue() + x * information_.GetChannelCount() * information_.GetBytesPerValue();
167 }
168
169 uint32_t v;
170 v = pixel[0];
171 if (information_.GetBytesPerValue() >= 2)
172 v = v + (static_cast<uint32_t>(pixel[1]) << 8);
173 if (information_.GetBytesPerValue() >= 3)
174 v = v + (static_cast<uint32_t>(pixel[2]) << 16);
175 if (information_.GetBytesPerValue() >= 4)
176 v = v + (static_cast<uint32_t>(pixel[3]) << 24);
177
178 v = v >> information_.GetShift();
179
180 if (v & signMask_)
181 {
182 // Signed value
183 // http://en.wikipedia.org/wiki/Two%27s_complement#Subtraction_from_2N
184 return -static_cast<int32_t>(mask_) + static_cast<int32_t>(v & mask_) - 1;
185 }
186 else
187 {
188 // Unsigned value
189 return static_cast<int32_t>(v & mask_);
190 }
191 }
192
193
194 void DicomIntegerPixelAccessor::SetCurrentFrame(unsigned int frame)
195 {
196 if (frame >= information_.GetNumberOfFrames())
197 {
198 throw OrthancException(ErrorCode_ParameterOutOfRange);
199 }
200
201 frame_ = frame;
202 }
203
204 }