Mercurial > hg > orthanc
annotate Core/DicomFormat/DicomIntegerPixelAccessor.cpp @ 2106:09cb73980740
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 17 Oct 2016 15:36:50 +0200 |
parents | 6c73df12ca51 |
children | a3a65de1840f |
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 |
0 | 5 * |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
136 | 10 * |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
0 | 22 * |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
34 |
6 | 35 #ifndef NOMINMAX |
2 | 36 #define NOMINMAX |
6 | 37 #endif |
38 | |
627 | 39 #include "DicomIntegerPixelAccessor.h" |
40 | |
79 | 41 #include "../OrthancException.h" |
0 | 42 #include <boost/lexical_cast.hpp> |
2 | 43 #include <limits> |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
80
diff
changeset
|
44 #include <cassert> |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
45 #include <stdio.h> |
0 | 46 |
62 | 47 namespace Orthanc |
0 | 48 { |
49 DicomIntegerPixelAccessor::DicomIntegerPixelAccessor(const DicomMap& values, | |
50 const void* pixelData, | |
51 size_t size) : | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
52 information_(values), |
0 | 53 pixelData_(pixelData), |
54 size_(size) | |
55 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
56 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
|
57 frameOffset_ = information_.GetFrameSize(); |
0 | 58 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
59 if (information_.GetNumberOfFrames() * frameOffset_ > size) |
0 | 60 { |
319 | 61 throw OrthancException(ErrorCode_BadFileFormat); |
0 | 62 } |
63 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
64 if (information_.IsSigned()) |
0 | 65 { |
537 | 66 // Pixels are signed |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
67 mask_ = (1 << (information_.GetBitsStored() - 1)) - 1; |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
68 signMask_ = (1 << (information_.GetBitsStored() - 1)); |
0 | 69 } |
70 else | |
71 { | |
537 | 72 // Pixels are unsigned |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
73 mask_ = (1 << information_.GetBitsStored()) - 1; |
0 | 74 signMask_ = 0; |
75 } | |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
76 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
77 if (information_.IsPlanar()) |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
78 { |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
79 /** |
857 | 80 * Each color plane shall be sent contiguously. For RGB images, |
81 * this means the order of the pixel values sent is R1, R2, R3, | |
82 * ..., G1, G2, G3, ..., B1, B2, B3, etc. | |
83 **/ | |
860 | 84 rowOffset_ = information_.GetWidth() * information_.GetBytesPerValue(); |
857 | 85 } |
86 else | |
87 { | |
88 /** | |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
89 * 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
|
90 * 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
|
91 * 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
|
92 * R2, G2, B2, ..., etc. |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
93 **/ |
860 | 94 rowOffset_ = information_.GetWidth() * information_.GetBytesPerValue() * information_.GetChannelCount(); |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
95 } |
0 | 96 } |
97 | |
98 | |
99 void DicomIntegerPixelAccessor::GetExtremeValues(int32_t& min, | |
100 int32_t& max) const | |
101 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
102 if (information_.GetHeight() == 0 || information_.GetWidth() == 0) |
0 | 103 { |
104 min = max = 0; | |
105 return; | |
106 } | |
107 | |
108 min = std::numeric_limits<int32_t>::max(); | |
109 max = std::numeric_limits<int32_t>::min(); | |
110 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
111 for (unsigned int y = 0; y < information_.GetHeight(); y++) |
0 | 112 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
113 for (unsigned int x = 0; x < information_.GetWidth(); x++) |
0 | 114 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
115 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
|
116 { |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
117 int32_t v = GetValue(x, y); |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
118 if (v < min) |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
119 min = v; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
120 if (v > max) |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
121 max = v; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
122 } |
0 | 123 } |
124 } | |
125 } | |
126 | |
127 | |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
128 int32_t DicomIntegerPixelAccessor::GetValue(unsigned int x, |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
129 unsigned int y, |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
130 unsigned int channel) const |
0 | 131 { |
857 | 132 assert(x < information_.GetWidth() && |
133 y < information_.GetHeight() && | |
134 channel < information_.GetChannelCount()); | |
0 | 135 |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
136 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
|
137 y * rowOffset_ + frame_ * frameOffset_; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
138 |
1908
5096681efce6
direct hyperlinks to the DICOM standard
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
139 // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#sect_C.7.6.3.1.3 |
857 | 140 if (information_.IsPlanar()) |
141 { | |
142 /** | |
143 * Each color plane shall be sent contiguously. For RGB images, | |
144 * this means the order of the pixel values sent is R1, R2, R3, | |
145 * ..., G1, G2, G3, ..., B1, B2, B3, etc. | |
146 **/ | |
147 assert(frameOffset_ % information_.GetChannelCount() == 0); | |
860 | 148 pixel += channel * frameOffset_ / information_.GetChannelCount() + x * information_.GetBytesPerValue(); |
857 | 149 } |
150 else | |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
151 { |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
152 /** |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
153 * 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
|
154 * 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
|
155 * 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
|
156 * R2, G2, B2, ..., etc. |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
319
diff
changeset
|
157 **/ |
860 | 158 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
|
159 } |
0 | 160 |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
161 uint32_t v; |
0 | 162 v = pixel[0]; |
860 | 163 if (information_.GetBytesPerValue() >= 2) |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
164 v = v + (static_cast<uint32_t>(pixel[1]) << 8); |
860 | 165 if (information_.GetBytesPerValue() >= 3) |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
166 v = v + (static_cast<uint32_t>(pixel[2]) << 16); |
860 | 167 if (information_.GetBytesPerValue() >= 4) |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
168 v = v + (static_cast<uint32_t>(pixel[3]) << 24); |
0 | 169 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
170 v = v >> information_.GetShift(); |
0 | 171 |
172 if (v & signMask_) | |
173 { | |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
174 // Signed value |
465
7a966b440f19
signed images to PNG
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
464
diff
changeset
|
175 // 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
|
176 return -static_cast<int32_t>(mask_) + static_cast<int32_t>(v & mask_) - 1; |
0 | 177 } |
464
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
178 else |
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
179 { |
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
180 // Unsigned value |
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
181 return static_cast<int32_t>(v & mask_); |
5987dd8e0776
fix reading signed values in dicom
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
182 } |
0 | 183 } |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
184 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
185 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
186 void DicomIntegerPixelAccessor::SetCurrentFrame(unsigned int frame) |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
187 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
188 if (frame >= information_.GetNumberOfFrames()) |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
189 { |
62 | 190 throw OrthancException(ErrorCode_ParameterOutOfRange); |
53
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 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
193 frame_ = frame; |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
194 } |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
195 |
0 | 196 } |