comparison PalantirServer/DicomIntegerPixelAccessor.cpp @ 0:3959d33612cc

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