comparison PalanthirServer/DicomIntegerPixelAccessor.cpp @ 45:33d67e1ab173

r
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Sep 2012 13:24:59 +0200
parents PalantirServer/DicomIntegerPixelAccessor.cpp@96e57b863dd9
children a15e90e5d6fc
comparison
equal deleted inserted replaced
43:9be852ad33d2 45:33d67e1ab173
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
23 #ifndef NOMINMAX
24 #define NOMINMAX
25 #endif
26
27 #include "../Core/PalantirException.h"
28 #include "FromDcmtkBridge.h"
29 #include <boost/lexical_cast.hpp>
30 #include <limits>
31
32 namespace Palantir
33 {
34 DicomIntegerPixelAccessor::DicomIntegerPixelAccessor(const DicomMap& values,
35 const void* pixelData,
36 size_t size) :
37 pixelData_(pixelData),
38 size_(size)
39 {
40 unsigned int bitsAllocated;
41 unsigned int bitsStored;
42 unsigned int highBit;
43 unsigned int pixelRepresentation;
44
45 try
46 {
47 width_ = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "Columns").AsString());
48 height_ = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "Rows").AsString());
49 samplesPerPixel_ = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "SamplesPerPixel").AsString());
50 bitsAllocated = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "BitsAllocated").AsString());
51 bitsStored = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "BitsStored").AsString());
52 highBit = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "HighBit").AsString());
53 pixelRepresentation = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "PixelRepresentation").AsString());
54 }
55 catch (boost::bad_lexical_cast)
56 {
57 throw PalantirException(ErrorCode_NotImplemented);
58 }
59
60 if (bitsAllocated != 8 && bitsAllocated != 16 &&
61 bitsAllocated != 24 && bitsAllocated != 32)
62 {
63 throw PalantirException(ErrorCode_NotImplemented);
64 }
65
66 if (bitsAllocated > 32 ||
67 bitsStored >= 32)
68 {
69 // Not available, as the accessor internally uses int32_t values
70 throw PalantirException(ErrorCode_NotImplemented);
71 }
72
73 if (samplesPerPixel_ != 1)
74 {
75 throw PalantirException(ErrorCode_NotImplemented);
76 }
77
78 if (width_ * height_ * bitsAllocated / 8 != size)
79 {
80 throw PalantirException(ErrorCode_NotImplemented);
81 }
82
83 /*printf("%d %d %d %d %d %d %d\n", width_, height_, samplesPerPixel_, bitsAllocated,
84 bitsStored, highBit, pixelRepresentation);*/
85
86 bytesPerPixel_ = bitsAllocated / 8;
87 shift_ = highBit + 1 - bitsStored;
88
89 if (pixelRepresentation)
90 {
91 mask_ = (1 << (bitsStored - 1)) - 1;
92 signMask_ = (1 << (bitsStored - 1));
93 }
94 else
95 {
96 mask_ = (1 << bitsStored) - 1;
97 signMask_ = 0;
98 }
99 }
100
101
102 void DicomIntegerPixelAccessor::GetExtremeValues(int32_t& min,
103 int32_t& max) const
104 {
105 if (height_ == 0 || width_ == 0)
106 {
107 min = max = 0;
108 return;
109 }
110
111 min = std::numeric_limits<int32_t>::max();
112 max = std::numeric_limits<int32_t>::min();
113
114 for (unsigned int y = 0; y < height_; y++)
115 {
116 for (unsigned int x = 0; x < width_; x++)
117 {
118 int32_t v = GetValue(x, y);
119 if (v < min)
120 min = v;
121 if (v > max)
122 max = v;
123 }
124 }
125 }
126
127
128 int32_t DicomIntegerPixelAccessor::GetValue(unsigned int x, unsigned int y) const
129 {
130 assert(x < width_ && y < height_);
131
132 const uint8_t* pixel = reinterpret_cast<const uint8_t*>(pixelData_) + (y * width_ + x) * bytesPerPixel_;
133
134 int32_t v;
135 v = pixel[0];
136 if (bytesPerPixel_ >= 2)
137 v = v + (static_cast<int32_t>(pixel[1]) << 8);
138 if (bytesPerPixel_ >= 3)
139 v = v + (static_cast<int32_t>(pixel[2]) << 16);
140 if (bytesPerPixel_ >= 4)
141 v = v + (static_cast<int32_t>(pixel[3]) << 24);
142
143 v = (v >> shift_) & mask_;
144
145 if (v & signMask_)
146 {
147 // Signed value: Not implemented yet
148 //throw PalantirException(ErrorCode_NotImplemented);
149 v = 0;
150 }
151
152 return v;
153 }
154 }