Mercurial > hg > orthanc
annotate OrthancFramework/Sources/DicomFormat/DicomIntegerPixelAccessor.cpp @ 4543:5b1b46f3fcca Orthanc-1.7.3
close branch
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 02 Mar 2021 16:52:24 +0100 |
parents | bf7b9edf6b81 |
children | 9279de56a405 |
rev | line source |
---|---|
0 | 1 /** |
62 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
860
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * the License, or (at your option) any later version. |
136 | 11 * |
0 | 12 * This program is distributed in the hope that it will be useful, but |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
15 * Lesser General Public License for more details. |
0 | 16 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * <http://www.gnu.org/licenses/>. |
0 | 20 **/ |
21 | |
22 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
23 #include "../PrecompiledHeaders.h" |
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
24 |
6 | 25 #ifndef NOMINMAX |
2 | 26 #define NOMINMAX |
6 | 27 #endif |
28 | |
627 | 29 #include "DicomIntegerPixelAccessor.h" |
30 | |
79 | 31 #include "../OrthancException.h" |
0 | 32 #include <boost/lexical_cast.hpp> |
2 | 33 #include <limits> |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
80
diff
changeset
|
34 #include <cassert> |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
35 #include <stdio.h> |
0 | 36 |
62 | 37 namespace Orthanc |
0 | 38 { |
39 DicomIntegerPixelAccessor::DicomIntegerPixelAccessor(const DicomMap& values, | |
40 const void* pixelData, | |
41 size_t size) : | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
42 information_(values), |
0 | 43 pixelData_(pixelData), |
44 size_(size) | |
45 { | |
2279
16e45af17a4d
Ability to retrieve raw frames encoded as unsigned 32-bits integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
46 if (information_.GetBitsAllocated() > 32 || |
16e45af17a4d
Ability to retrieve raw frames encoded as unsigned 32-bits integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
47 information_.GetBitsStored() >= 32) |
16e45af17a4d
Ability to retrieve raw frames encoded as unsigned 32-bits integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
48 { |
16e45af17a4d
Ability to retrieve raw frames encoded as unsigned 32-bits integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
49 // Not available, as the accessor internally uses int32_t values |
16e45af17a4d
Ability to retrieve raw frames encoded as unsigned 32-bits integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
50 throw OrthancException(ErrorCode_NotImplemented); |
16e45af17a4d
Ability to retrieve raw frames encoded as unsigned 32-bits integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
51 } |
16e45af17a4d
Ability to retrieve raw frames encoded as unsigned 32-bits integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
52 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
53 frame_ = 0; |
1924
6c73df12ca51
New URI: "/instances/.../frames/.../raw" to access the raw frames (bypass image decoding)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1908
diff
changeset
|
54 frameOffset_ = information_.GetFrameSize(); |
0 | 55 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
56 if (information_.GetNumberOfFrames() * frameOffset_ > size) |
0 | 57 { |
319 | 58 throw OrthancException(ErrorCode_BadFileFormat); |
0 | 59 } |
60 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
61 if (information_.IsSigned()) |
0 | 62 { |
537 | 63 // Pixels are signed |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
64 mask_ = (1 << (information_.GetBitsStored() - 1)) - 1; |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
65 signMask_ = (1 << (information_.GetBitsStored() - 1)); |
0 | 66 } |
67 else | |
68 { | |
537 | 69 // Pixels are unsigned |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
70 mask_ = (1 << information_.GetBitsStored()) - 1; |
0 | 71 signMask_ = 0; |
72 } | |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
73 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
74 if (information_.IsPlanar()) |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
75 { |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
76 /** |
857 | 77 * Each color plane shall be sent contiguously. For RGB images, |
78 * this means the order of the pixel values sent is R1, R2, R3, | |
79 * ..., G1, G2, G3, ..., B1, B2, B3, etc. | |
80 **/ | |
860 | 81 rowOffset_ = information_.GetWidth() * information_.GetBytesPerValue(); |
857 | 82 } |
83 else | |
84 { | |
85 /** | |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
86 * The sample values for the first pixel are followed by the |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
87 * sample values for the second pixel, etc. For RGB images, this |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
88 * means the order of the pixel values sent shall be R1, G1, B1, |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
89 * R2, G2, B2, ..., etc. |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
90 **/ |
860 | 91 rowOffset_ = information_.GetWidth() * information_.GetBytesPerValue() * information_.GetChannelCount(); |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
92 } |
0 | 93 } |
94 | |
95 | |
96 void DicomIntegerPixelAccessor::GetExtremeValues(int32_t& min, | |
97 int32_t& max) const | |
98 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
99 if (information_.GetHeight() == 0 || information_.GetWidth() == 0) |
0 | 100 { |
101 min = max = 0; | |
102 return; | |
103 } | |
104 | |
105 min = std::numeric_limits<int32_t>::max(); | |
106 max = std::numeric_limits<int32_t>::min(); | |
107 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
108 for (unsigned int y = 0; y < information_.GetHeight(); y++) |
0 | 109 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
110 for (unsigned int x = 0; x < information_.GetWidth(); x++) |
0 | 111 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
112 for (unsigned int c = 0; c < information_.GetChannelCount(); c++) |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
113 { |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
114 int32_t v = GetValue(x, y); |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
115 if (v < min) |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
116 min = v; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
117 if (v > max) |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
118 max = v; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
119 } |
0 | 120 } |
121 } | |
122 } | |
123 | |
124 | |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
125 int32_t DicomIntegerPixelAccessor::GetValue(unsigned int x, |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
126 unsigned int y, |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
127 unsigned int channel) const |
0 | 128 { |
857 | 129 assert(x < information_.GetWidth() && |
130 y < information_.GetHeight() && | |
131 channel < information_.GetChannelCount()); | |
0 | 132 |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
133 const uint8_t* pixel = reinterpret_cast<const uint8_t*>(pixelData_) + |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
134 y * rowOffset_ + frame_ * frameOffset_; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
135 |
1908
5096681efce6
direct hyperlinks to the DICOM standard
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
136 // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#sect_C.7.6.3.1.3 |
857 | 137 if (information_.IsPlanar()) |
138 { | |
139 /** | |
140 * Each color plane shall be sent contiguously. For RGB images, | |
141 * this means the order of the pixel values sent is R1, R2, R3, | |
142 * ..., G1, G2, G3, ..., B1, B2, B3, etc. | |
143 **/ | |
144 assert(frameOffset_ % information_.GetChannelCount() == 0); | |
860 | 145 pixel += channel * frameOffset_ / information_.GetChannelCount() + x * information_.GetBytesPerValue(); |
857 | 146 } |
147 else | |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
148 { |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
149 /** |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
150 * The sample values for the first pixel are followed by the |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
151 * sample values for the second pixel, etc. For RGB images, this |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
152 * means the order of the pixel values sent shall be R1, G1, B1, |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
153 * R2, G2, B2, ..., etc. |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
154 **/ |
860 | 155 pixel += channel * information_.GetBytesPerValue() + x * information_.GetChannelCount() * information_.GetBytesPerValue(); |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
156 } |
0 | 157 |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
158 uint32_t v; |
0 | 159 v = pixel[0]; |
860 | 160 if (information_.GetBytesPerValue() >= 2) |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
161 v = v + (static_cast<uint32_t>(pixel[1]) << 8); |
860 | 162 if (information_.GetBytesPerValue() >= 3) |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
163 v = v + (static_cast<uint32_t>(pixel[2]) << 16); |
860 | 164 if (information_.GetBytesPerValue() >= 4) |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
165 v = v + (static_cast<uint32_t>(pixel[3]) << 24); |
0 | 166 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
167 v = v >> information_.GetShift(); |
0 | 168 |
169 if (v & signMask_) | |
170 { | |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
171 // Signed value |
465
7a966b440f19
signed images to PNG
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
464
diff
changeset
|
172 // http://en.wikipedia.org/wiki/Two%27s_complement#Subtraction_from_2N |
7a966b440f19
signed images to PNG
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
464
diff
changeset
|
173 return -static_cast<int32_t>(mask_) + static_cast<int32_t>(v & mask_) - 1; |
0 | 174 } |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
175 else |
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
176 { |
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
177 // Unsigned value |
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
178 return static_cast<int32_t>(v & mask_); |
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
179 } |
0 | 180 } |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
181 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
182 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
183 void DicomIntegerPixelAccessor::SetCurrentFrame(unsigned int frame) |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
184 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
185 if (frame >= information_.GetNumberOfFrames()) |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
186 { |
62 | 187 throw OrthancException(ErrorCode_ParameterOutOfRange); |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
188 } |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
189 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
190 frame_ = frame; |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
191 } |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
192 |
0 | 193 } |