comparison Core/DicomFormat/DicomImageInformation.cpp @ 945:427a1f996b7b templating

integration mainline -> templating
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Jun 2014 11:56:48 +0200
parents 3c0d0836f704
children f5b0207967bc
comparison
equal deleted inserted replaced
838:aabc3b430890 945:427a1f996b7b
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2014 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 * 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.
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 "../PrecompiledHeaders.h"
34
35 #ifndef NOMINMAX
36 #define NOMINMAX
37 #endif
38
39 #include "DicomImageInformation.h"
40
41 #include "../OrthancException.h"
42 #include <boost/lexical_cast.hpp>
43 #include <limits>
44 #include <cassert>
45 #include <stdio.h>
46
47 namespace Orthanc
48 {
49 DicomImageInformation::DicomImageInformation(const DicomMap& values)
50 {
51 unsigned int pixelRepresentation;
52 unsigned int planarConfiguration = 0;
53
54 try
55 {
56 width_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_COLUMNS).AsString());
57 height_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_ROWS).AsString());
58 bitsAllocated_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_BITS_ALLOCATED).AsString());
59
60 try
61 {
62 samplesPerPixel_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_SAMPLES_PER_PIXEL).AsString());
63 }
64 catch (OrthancException&)
65 {
66 samplesPerPixel_ = 1; // Assume 1 color channel
67 }
68
69 try
70 {
71 bitsStored_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_BITS_STORED).AsString());
72 }
73 catch (OrthancException&)
74 {
75 bitsStored_ = bitsAllocated_;
76 }
77
78 try
79 {
80 highBit_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_HIGH_BIT).AsString());
81 }
82 catch (OrthancException&)
83 {
84 highBit_ = bitsStored_ - 1;
85 }
86
87 try
88 {
89 pixelRepresentation = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_PIXEL_REPRESENTATION).AsString());
90 }
91 catch (OrthancException&)
92 {
93 pixelRepresentation = 0; // Assume unsigned pixels
94 }
95
96 if (samplesPerPixel_ > 1)
97 {
98 // The "Planar Configuration" is only set when "Samples per Pixels" is greater than 1
99 // https://www.dabsoft.ch/dicom/3/C.7.6.3.1.3/
100 try
101 {
102 planarConfiguration = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_PLANAR_CONFIGURATION).AsString());
103 }
104 catch (OrthancException&)
105 {
106 planarConfiguration = 0; // Assume interleaved color channels
107 }
108 }
109 }
110 catch (boost::bad_lexical_cast&)
111 {
112 throw OrthancException(ErrorCode_NotImplemented);
113 }
114 catch (OrthancException&)
115 {
116 throw OrthancException(ErrorCode_NotImplemented);
117 }
118
119 try
120 {
121 numberOfFrames_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_NUMBER_OF_FRAMES).AsString());
122 }
123 catch (OrthancException)
124 {
125 // If the tag "NumberOfFrames" is absent, assume there is a single frame
126 numberOfFrames_ = 1;
127 }
128 catch (boost::bad_lexical_cast&)
129 {
130 throw OrthancException(ErrorCode_NotImplemented);
131 }
132
133 if ((bitsAllocated_ != 8 && bitsAllocated_ != 16 &&
134 bitsAllocated_ != 24 && bitsAllocated_ != 32) ||
135 numberOfFrames_ == 0 ||
136 (planarConfiguration != 0 && planarConfiguration != 1))
137 {
138 throw OrthancException(ErrorCode_NotImplemented);
139 }
140
141 if (bitsAllocated_ > 32 ||
142 bitsStored_ >= 32)
143 {
144 // Not available, as the accessor internally uses int32_t values
145 throw OrthancException(ErrorCode_NotImplemented);
146 }
147
148 if (samplesPerPixel_ == 0)
149 {
150 throw OrthancException(ErrorCode_NotImplemented);
151 }
152
153 bytesPerValue_ = bitsAllocated_ / 8;
154
155 isPlanar_ = (planarConfiguration != 0 ? true : false);
156 isSigned_ = (pixelRepresentation != 0 ? true : false);
157 }
158
159
160 bool DicomImageInformation::ExtractPixelFormat(PixelFormat& format) const
161 {
162 if (GetBitsStored() == 8 && GetChannelCount() == 1 && !IsSigned())
163 {
164 format = PixelFormat_Grayscale8;
165 return true;
166 }
167
168 if (GetBitsStored() == 8 && GetChannelCount() == 3 && !IsSigned())
169 {
170 format = PixelFormat_RGB24;
171 return true;
172 }
173
174 if (GetBitsAllocated() == 16 && GetChannelCount() == 1 && !IsSigned())
175 {
176 format = PixelFormat_Grayscale16;
177 return true;
178 }
179
180 if (GetBitsAllocated() == 16 && GetChannelCount() == 1 && IsSigned())
181 {
182 format = PixelFormat_SignedGrayscale16;
183 return true;
184 }
185
186 return false;
187 }
188 }