Mercurial > hg > orthanc
annotate Core/DicomFormat/DicomIntegerPixelAccessor.cpp @ 335:daff912c9ffe
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 11 Jan 2013 14:54:39 +0100 |
parents | 760d0f32cb34 |
children | 80011cd589e6 |
rev | line source |
---|---|
0 | 1 /** |
62 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
0 | 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. | |
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 | |
33 #include "DicomIntegerPixelAccessor.h" | |
34 | |
6 | 35 #ifndef NOMINMAX |
2 | 36 #define NOMINMAX |
6 | 37 #endif |
38 | |
79 | 39 #include "../OrthancException.h" |
0 | 40 #include <boost/lexical_cast.hpp> |
2 | 41 #include <limits> |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
80
diff
changeset
|
42 #include <cassert> |
0 | 43 |
62 | 44 namespace Orthanc |
0 | 45 { |
77 | 46 static const DicomTag COLUMNS(0x0028, 0x0011); |
47 static const DicomTag ROWS(0x0028, 0x0010); | |
48 static const DicomTag SAMPLES_PER_PIXEL(0x0028, 0x0002); | |
49 static const DicomTag BITS_ALLOCATED(0x0028, 0x0100); | |
50 static const DicomTag BITS_STORED(0x0028, 0x0101); | |
51 static const DicomTag HIGH_BIT(0x0028, 0x0102); | |
52 static const DicomTag PIXEL_REPRESENTATION(0x0028, 0x0103); | |
53 | |
0 | 54 DicomIntegerPixelAccessor::DicomIntegerPixelAccessor(const DicomMap& values, |
55 const void* pixelData, | |
56 size_t size) : | |
57 pixelData_(pixelData), | |
58 size_(size) | |
59 { | |
60 unsigned int bitsAllocated; | |
61 unsigned int bitsStored; | |
62 unsigned int highBit; | |
63 unsigned int pixelRepresentation; | |
64 | |
65 try | |
66 { | |
77 | 67 width_ = boost::lexical_cast<unsigned int>(values.GetValue(COLUMNS).AsString()); |
68 height_ = boost::lexical_cast<unsigned int>(values.GetValue(ROWS).AsString()); | |
69 samplesPerPixel_ = boost::lexical_cast<unsigned int>(values.GetValue(SAMPLES_PER_PIXEL).AsString()); | |
70 bitsAllocated = boost::lexical_cast<unsigned int>(values.GetValue(BITS_ALLOCATED).AsString()); | |
71 bitsStored = boost::lexical_cast<unsigned int>(values.GetValue(BITS_STORED).AsString()); | |
72 highBit = boost::lexical_cast<unsigned int>(values.GetValue(HIGH_BIT).AsString()); | |
73 pixelRepresentation = boost::lexical_cast<unsigned int>(values.GetValue(PIXEL_REPRESENTATION).AsString()); | |
0 | 74 } |
75 catch (boost::bad_lexical_cast) | |
76 { | |
62 | 77 throw OrthancException(ErrorCode_NotImplemented); |
0 | 78 } |
79 | |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
80 frame_ = 0; |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
81 try |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
82 { |
80 | 83 numberOfFrames_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_NUMBER_OF_FRAMES).AsString()); |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
84 } |
62 | 85 catch (OrthancException) |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
86 { |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
87 // If the tag "NumberOfFrames" is absent, assume there is a single frame |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
88 numberOfFrames_ = 1; |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
89 } |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
90 catch (boost::bad_lexical_cast) |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
91 { |
62 | 92 throw OrthancException(ErrorCode_NotImplemented); |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
93 } |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
94 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
95 if ((bitsAllocated != 8 && bitsAllocated != 16 && |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
96 bitsAllocated != 24 && bitsAllocated != 32) || |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
97 numberOfFrames_ == 0) |
0 | 98 { |
62 | 99 throw OrthancException(ErrorCode_NotImplemented); |
0 | 100 } |
101 | |
102 if (bitsAllocated > 32 || | |
103 bitsStored >= 32) | |
104 { | |
105 // Not available, as the accessor internally uses int32_t values | |
62 | 106 throw OrthancException(ErrorCode_NotImplemented); |
0 | 107 } |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
108 |
0 | 109 if (samplesPerPixel_ != 1) |
110 { | |
62 | 111 throw OrthancException(ErrorCode_NotImplemented); |
0 | 112 } |
113 | |
319 | 114 if (width_ * height_ * bitsAllocated / 8 * numberOfFrames_ > size) |
0 | 115 { |
319 | 116 throw OrthancException(ErrorCode_BadFileFormat); |
0 | 117 } |
118 | |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
119 /*printf("%d %d %d %d %d %d %d %d\n", width_, height_, samplesPerPixel_, bitsAllocated, |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
120 bitsStored, highBit, pixelRepresentation, numberOfFrames_);*/ |
0 | 121 |
122 bytesPerPixel_ = bitsAllocated / 8; | |
123 shift_ = highBit + 1 - bitsStored; | |
124 | |
125 if (pixelRepresentation) | |
126 { | |
127 mask_ = (1 << (bitsStored - 1)) - 1; | |
128 signMask_ = (1 << (bitsStored - 1)); | |
129 } | |
130 else | |
131 { | |
132 mask_ = (1 << bitsStored) - 1; | |
133 signMask_ = 0; | |
134 } | |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
135 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
136 rowOffset_ = width_ * bytesPerPixel_; |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
137 frameOffset_ = height_ * width_ * bytesPerPixel_; |
0 | 138 } |
139 | |
140 | |
141 void DicomIntegerPixelAccessor::GetExtremeValues(int32_t& min, | |
142 int32_t& max) const | |
143 { | |
144 if (height_ == 0 || width_ == 0) | |
145 { | |
146 min = max = 0; | |
147 return; | |
148 } | |
149 | |
150 min = std::numeric_limits<int32_t>::max(); | |
151 max = std::numeric_limits<int32_t>::min(); | |
152 | |
153 for (unsigned int y = 0; y < height_; y++) | |
154 { | |
155 for (unsigned int x = 0; x < width_; x++) | |
156 { | |
157 int32_t v = GetValue(x, y); | |
158 if (v < min) | |
159 min = v; | |
160 if (v > max) | |
161 max = v; | |
162 } | |
163 } | |
164 } | |
165 | |
166 | |
167 int32_t DicomIntegerPixelAccessor::GetValue(unsigned int x, unsigned int y) const | |
168 { | |
169 assert(x < width_ && y < height_); | |
170 | |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
171 const uint8_t* pixel = reinterpret_cast<const uint8_t*>(pixelData_) + |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
172 y * rowOffset_ + x * bytesPerPixel_ + frame_ * frameOffset_; |
0 | 173 |
174 int32_t v; | |
175 v = pixel[0]; | |
176 if (bytesPerPixel_ >= 2) | |
177 v = v + (static_cast<int32_t>(pixel[1]) << 8); | |
178 if (bytesPerPixel_ >= 3) | |
179 v = v + (static_cast<int32_t>(pixel[2]) << 16); | |
180 if (bytesPerPixel_ >= 4) | |
181 v = v + (static_cast<int32_t>(pixel[3]) << 24); | |
182 | |
183 v = (v >> shift_) & mask_; | |
184 | |
185 if (v & signMask_) | |
186 { | |
187 // Signed value: Not implemented yet | |
62 | 188 //throw OrthancException(ErrorCode_NotImplemented); |
0 | 189 v = 0; |
190 } | |
191 | |
192 return v; | |
193 } | |
53
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 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
196 void DicomIntegerPixelAccessor::SetCurrentFrame(unsigned int frame) |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
197 { |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
198 if (frame >= numberOfFrames_) |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
199 { |
62 | 200 throw OrthancException(ErrorCode_ParameterOutOfRange); |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
201 } |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
202 |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
203 frame_ = frame; |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
204 } |
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
205 |
0 | 206 } |