Mercurial > hg > orthanc
annotate Core/DicomFormat/DicomIntegerPixelAccessor.cpp @ 3351:5069af20932a
Web viewer: Check the return value of gdcm::Bitmap::GetBuffer()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 23 Apr 2019 10:28:37 +0200 |
parents | 4e43e67f8ecf |
children | 94f4a18a79cc |
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 |
3060
4e43e67f8ecf
preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
0 | 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. | |
136 | 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. | |
0 | 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 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
34 #include "../PrecompiledHeaders.h" |
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
35 |
6 | 36 #ifndef NOMINMAX |
2 | 37 #define NOMINMAX |
6 | 38 #endif |
39 | |
627 | 40 #include "DicomIntegerPixelAccessor.h" |
41 | |
79 | 42 #include "../OrthancException.h" |
0 | 43 #include <boost/lexical_cast.hpp> |
2 | 44 #include <limits> |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
80
diff
changeset
|
45 #include <cassert> |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
46 #include <stdio.h> |
0 | 47 |
62 | 48 namespace Orthanc |
0 | 49 { |
50 DicomIntegerPixelAccessor::DicomIntegerPixelAccessor(const DicomMap& values, | |
51 const void* pixelData, | |
52 size_t size) : | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
53 information_(values), |
0 | 54 pixelData_(pixelData), |
55 size_(size) | |
56 { | |
2279
16e45af17a4d
Ability to retrieve raw frames encoded as unsigned 32-bits integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
57 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
|
58 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
|
59 { |
16e45af17a4d
Ability to retrieve raw frames encoded as unsigned 32-bits integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
60 // 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
|
61 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
|
62 } |
16e45af17a4d
Ability to retrieve raw frames encoded as unsigned 32-bits integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
63 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
64 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
|
65 frameOffset_ = information_.GetFrameSize(); |
0 | 66 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
67 if (information_.GetNumberOfFrames() * frameOffset_ > size) |
0 | 68 { |
319 | 69 throw OrthancException(ErrorCode_BadFileFormat); |
0 | 70 } |
71 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
72 if (information_.IsSigned()) |
0 | 73 { |
537 | 74 // Pixels are signed |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
75 mask_ = (1 << (information_.GetBitsStored() - 1)) - 1; |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
76 signMask_ = (1 << (information_.GetBitsStored() - 1)); |
0 | 77 } |
78 else | |
79 { | |
537 | 80 // Pixels are unsigned |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
81 mask_ = (1 << information_.GetBitsStored()) - 1; |
0 | 82 signMask_ = 0; |
83 } | |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
84 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
85 if (information_.IsPlanar()) |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
86 { |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
87 /** |
857 | 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 **/ | |
860 | 92 rowOffset_ = information_.GetWidth() * information_.GetBytesPerValue(); |
857 | 93 } |
94 else | |
95 { | |
96 /** | |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
97 * 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
|
98 * 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
|
99 * 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
|
100 * R2, G2, B2, ..., etc. |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
101 **/ |
860 | 102 rowOffset_ = information_.GetWidth() * information_.GetBytesPerValue() * information_.GetChannelCount(); |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
103 } |
0 | 104 } |
105 | |
106 | |
107 void DicomIntegerPixelAccessor::GetExtremeValues(int32_t& min, | |
108 int32_t& max) const | |
109 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
110 if (information_.GetHeight() == 0 || information_.GetWidth() == 0) |
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 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
119 for (unsigned int y = 0; y < information_.GetHeight(); y++) |
0 | 120 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
121 for (unsigned int x = 0; x < information_.GetWidth(); x++) |
0 | 122 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
123 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
|
124 { |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
125 int32_t v = GetValue(x, y); |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
126 if (v < min) |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
127 min = v; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
128 if (v > max) |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
129 max = v; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
130 } |
0 | 131 } |
132 } | |
133 } | |
134 | |
135 | |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
136 int32_t DicomIntegerPixelAccessor::GetValue(unsigned int x, |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
137 unsigned int y, |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
138 unsigned int channel) const |
0 | 139 { |
857 | 140 assert(x < information_.GetWidth() && |
141 y < information_.GetHeight() && | |
142 channel < information_.GetChannelCount()); | |
0 | 143 |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
144 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
|
145 y * rowOffset_ + frame_ * frameOffset_; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
146 |
1908
5096681efce6
direct hyperlinks to the DICOM standard
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
147 // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#sect_C.7.6.3.1.3 |
857 | 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); | |
860 | 156 pixel += channel * frameOffset_ / information_.GetChannelCount() + x * information_.GetBytesPerValue(); |
857 | 157 } |
158 else | |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
159 { |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
160 /** |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
161 * 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
|
162 * 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
|
163 * 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
|
164 * R2, G2, B2, ..., etc. |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
165 **/ |
860 | 166 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
|
167 } |
0 | 168 |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
169 uint32_t v; |
0 | 170 v = pixel[0]; |
860 | 171 if (information_.GetBytesPerValue() >= 2) |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
172 v = v + (static_cast<uint32_t>(pixel[1]) << 8); |
860 | 173 if (information_.GetBytesPerValue() >= 3) |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
174 v = v + (static_cast<uint32_t>(pixel[2]) << 16); |
860 | 175 if (information_.GetBytesPerValue() >= 4) |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
176 v = v + (static_cast<uint32_t>(pixel[3]) << 24); |
0 | 177 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
178 v = v >> information_.GetShift(); |
0 | 179 |
180 if (v & signMask_) | |
181 { | |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
182 // Signed value |
465
7a966b440f19
signed images to PNG
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
464
diff
changeset
|
183 // 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
|
184 return -static_cast<int32_t>(mask_) + static_cast<int32_t>(v & mask_) - 1; |
0 | 185 } |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
186 else |
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
187 { |
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
188 // Unsigned value |
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
189 return static_cast<int32_t>(v & mask_); |
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
190 } |
0 | 191 } |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
192 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
193 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
194 void DicomIntegerPixelAccessor::SetCurrentFrame(unsigned int frame) |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
195 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
196 if (frame >= information_.GetNumberOfFrames()) |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
197 { |
62 | 198 throw OrthancException(ErrorCode_ParameterOutOfRange); |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
199 } |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
200 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
201 frame_ = frame; |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
202 } |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
203 |
0 | 204 } |