comparison OrthancServer/DicomIntegerPixelAccessor.cpp @ 57:4bc019d2f969 orthanc-renaming

renaming
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 16 Sep 2012 09:22:48 +0200
parents PalanthirServer/DicomIntegerPixelAccessor.cpp@293038baf8f1
children a70bb32802ae
comparison
equal deleted inserted replaced
56:088c4f23e2c8 57:4bc019d2f969
1 /**
2 * Palanthir - 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/PalanthirException.h"
28 #include "FromDcmtkBridge.h"
29 #include <boost/lexical_cast.hpp>
30 #include <limits>
31
32 namespace Palanthir
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 PalanthirException(ErrorCode_NotImplemented);
58 }
59
60 frame_ = 0;
61 try
62 {
63 numberOfFrames_ = boost::lexical_cast<unsigned int>(FromDcmtkBridge::GetValue(values, "NumberOfFrames").AsString());
64 }
65 catch (PalanthirException)
66 {
67 // If the tag "NumberOfFrames" is absent, assume there is a single frame
68 numberOfFrames_ = 1;
69 }
70 catch (boost::bad_lexical_cast)
71 {
72 throw PalanthirException(ErrorCode_NotImplemented);
73 }
74
75 if ((bitsAllocated != 8 && bitsAllocated != 16 &&
76 bitsAllocated != 24 && bitsAllocated != 32) ||
77 numberOfFrames_ == 0)
78 {
79 throw PalanthirException(ErrorCode_NotImplemented);
80 }
81
82 if (bitsAllocated > 32 ||
83 bitsStored >= 32)
84 {
85 // Not available, as the accessor internally uses int32_t values
86 throw PalanthirException(ErrorCode_NotImplemented);
87 }
88
89 if (samplesPerPixel_ != 1)
90 {
91 throw PalanthirException(ErrorCode_NotImplemented);
92 }
93
94 if (width_ * height_ * bitsAllocated / 8 * numberOfFrames_ != size)
95 {
96 throw PalanthirException(ErrorCode_NotImplemented);
97 }
98
99 /*printf("%d %d %d %d %d %d %d %d\n", width_, height_, samplesPerPixel_, bitsAllocated,
100 bitsStored, highBit, pixelRepresentation, numberOfFrames_);*/
101
102 bytesPerPixel_ = bitsAllocated / 8;
103 shift_ = highBit + 1 - bitsStored;
104
105 if (pixelRepresentation)
106 {
107 mask_ = (1 << (bitsStored - 1)) - 1;
108 signMask_ = (1 << (bitsStored - 1));
109 }
110 else
111 {
112 mask_ = (1 << bitsStored) - 1;
113 signMask_ = 0;
114 }
115
116 rowOffset_ = width_ * bytesPerPixel_;
117 frameOffset_ = height_ * width_ * bytesPerPixel_;
118 }
119
120
121 void DicomIntegerPixelAccessor::GetExtremeValues(int32_t& min,
122 int32_t& max) const
123 {
124 if (height_ == 0 || width_ == 0)
125 {
126 min = max = 0;
127 return;
128 }
129
130 min = std::numeric_limits<int32_t>::max();
131 max = std::numeric_limits<int32_t>::min();
132
133 for (unsigned int y = 0; y < height_; y++)
134 {
135 for (unsigned int x = 0; x < width_; x++)
136 {
137 int32_t v = GetValue(x, y);
138 if (v < min)
139 min = v;
140 if (v > max)
141 max = v;
142 }
143 }
144 }
145
146
147 int32_t DicomIntegerPixelAccessor::GetValue(unsigned int x, unsigned int y) const
148 {
149 assert(x < width_ && y < height_);
150
151 const uint8_t* pixel = reinterpret_cast<const uint8_t*>(pixelData_) +
152 y * rowOffset_ + x * bytesPerPixel_ + frame_ * frameOffset_;
153
154 int32_t v;
155 v = pixel[0];
156 if (bytesPerPixel_ >= 2)
157 v = v + (static_cast<int32_t>(pixel[1]) << 8);
158 if (bytesPerPixel_ >= 3)
159 v = v + (static_cast<int32_t>(pixel[2]) << 16);
160 if (bytesPerPixel_ >= 4)
161 v = v + (static_cast<int32_t>(pixel[3]) << 24);
162
163 v = (v >> shift_) & mask_;
164
165 if (v & signMask_)
166 {
167 // Signed value: Not implemented yet
168 //throw PalanthirException(ErrorCode_NotImplemented);
169 v = 0;
170 }
171
172 return v;
173 }
174
175
176 void DicomIntegerPixelAccessor::SetCurrentFrame(unsigned int frame)
177 {
178 if (frame >= numberOfFrames_)
179 {
180 throw PalanthirException(ErrorCode_ParameterOutOfRange);
181 }
182
183 frame_ = frame;
184 }
185
186 }