0
|
1 /**
|
|
2 * Palantir - A Lightweight, RESTful DICOM Store
|
|
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
|
|
4 * Belgium
|
|
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.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful, but
|
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18 **/
|
|
19
|
|
20
|
|
21 #include "DicomIntegerPixelAccessor.h"
|
|
22
|
2
|
23 #define NOMINMAX
|
0
|
24 #include "../Core/PalantirException.h"
|
|
25 #include "FromDcmtkBridge.h"
|
|
26 #include <boost/lexical_cast.hpp>
|
2
|
27 #include <limits>
|
0
|
28
|
|
29 namespace Palantir
|
|
30 {
|
|
31 DicomIntegerPixelAccessor::DicomIntegerPixelAccessor(const DicomMap& values,
|
|
32 const void* pixelData,
|
|
33 size_t size) :
|
|
34 pixelData_(pixelData),
|
|
35 size_(size)
|
|
36 {
|
|
37 unsigned int bitsAllocated;
|
|
38 unsigned int bitsStored;
|
|
39 unsigned int highBit;
|
|
40 unsigned int pixelRepresentation;
|
|
41
|
|
42 try
|
|
43 {
|
|
44 width_ = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "Columns").AsString());
|
|
45 height_ = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "Rows").AsString());
|
|
46 samplesPerPixel_ = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "SamplesPerPixel").AsString());
|
|
47 bitsAllocated = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "BitsAllocated").AsString());
|
|
48 bitsStored = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "BitsStored").AsString());
|
|
49 highBit = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "HighBit").AsString());
|
|
50 pixelRepresentation = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "PixelRepresentation").AsString());
|
|
51 }
|
|
52 catch (boost::bad_lexical_cast)
|
|
53 {
|
|
54 throw PalantirException(ErrorCode_NotImplemented);
|
|
55 }
|
|
56
|
|
57 if (bitsAllocated != 8 && bitsAllocated != 16 &&
|
|
58 bitsAllocated != 24 && bitsAllocated != 32)
|
|
59 {
|
|
60 throw PalantirException(ErrorCode_NotImplemented);
|
|
61 }
|
|
62
|
|
63 if (bitsAllocated > 32 ||
|
|
64 bitsStored >= 32)
|
|
65 {
|
|
66 // Not available, as the accessor internally uses int32_t values
|
|
67 throw PalantirException(ErrorCode_NotImplemented);
|
|
68 }
|
|
69
|
|
70 if (samplesPerPixel_ != 1)
|
|
71 {
|
|
72 throw PalantirException(ErrorCode_NotImplemented);
|
|
73 }
|
|
74
|
|
75 if (width_ * height_ * bitsAllocated / 8 != size)
|
|
76 {
|
|
77 throw PalantirException(ErrorCode_NotImplemented);
|
|
78 }
|
|
79
|
|
80 printf("%d %d %d %d %d %d %d\n", width_, height_, samplesPerPixel_, bitsAllocated,
|
|
81 bitsStored, highBit, pixelRepresentation);
|
|
82
|
|
83 bytesPerPixel_ = bitsAllocated / 8;
|
|
84 shift_ = highBit + 1 - bitsStored;
|
|
85
|
|
86 if (pixelRepresentation)
|
|
87 {
|
|
88 mask_ = (1 << (bitsStored - 1)) - 1;
|
|
89 signMask_ = (1 << (bitsStored - 1));
|
|
90 }
|
|
91 else
|
|
92 {
|
|
93 mask_ = (1 << bitsStored) - 1;
|
|
94 signMask_ = 0;
|
|
95 }
|
|
96 }
|
|
97
|
|
98
|
|
99 void DicomIntegerPixelAccessor::GetExtremeValues(int32_t& min,
|
|
100 int32_t& max) const
|
|
101 {
|
|
102 if (height_ == 0 || width_ == 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
|
|
111 for (unsigned int y = 0; y < height_; y++)
|
|
112 {
|
|
113 for (unsigned int x = 0; x < width_; x++)
|
|
114 {
|
|
115 int32_t v = GetValue(x, y);
|
|
116 if (v < min)
|
|
117 min = v;
|
|
118 if (v > max)
|
|
119 max = v;
|
|
120 }
|
|
121 }
|
|
122 }
|
|
123
|
|
124
|
|
125 int32_t DicomIntegerPixelAccessor::GetValue(unsigned int x, unsigned int y) const
|
|
126 {
|
|
127 assert(x < width_ && y < height_);
|
|
128
|
|
129 const uint8_t* pixel = reinterpret_cast<const uint8_t*>(pixelData_) + (y * width_ + x) * bytesPerPixel_;
|
|
130
|
|
131 int32_t v;
|
|
132 v = pixel[0];
|
|
133 if (bytesPerPixel_ >= 2)
|
|
134 v = v + (static_cast<int32_t>(pixel[1]) << 8);
|
|
135 if (bytesPerPixel_ >= 3)
|
|
136 v = v + (static_cast<int32_t>(pixel[2]) << 16);
|
|
137 if (bytesPerPixel_ >= 4)
|
|
138 v = v + (static_cast<int32_t>(pixel[3]) << 24);
|
|
139
|
|
140 v = (v >> shift_) & mask_;
|
|
141
|
|
142 if (v & signMask_)
|
|
143 {
|
|
144 // Signed value: Not implemented yet
|
|
145 //throw PalantirException(ErrorCode_NotImplemented);
|
|
146 v = 0;
|
|
147 }
|
|
148
|
|
149 return v;
|
|
150 }
|
|
151 }
|