Mercurial > hg > orthanc
annotate Core/DicomFormat/DicomImageInformation.cpp @ 1193:e95e7f9eba96
fix crash when stopping Orthanc during a DICOM connection
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 20 Oct 2014 16:46:40 +0200 |
parents | 3c0d0836f704 |
children | f5b0207967bc |
rev | line source |
---|---|
853 | 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 { | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
62 samplesPerPixel_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_SAMPLES_PER_PIXEL).AsString()); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
63 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
64 catch (OrthancException&) |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
65 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
66 samplesPerPixel_ = 1; // Assume 1 color channel |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
67 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
68 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
69 try |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
70 { |
853 | 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 } | |
857 | 128 catch (boost::bad_lexical_cast&) |
853 | 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 | |
860 | 153 bytesPerValue_ = bitsAllocated_ / 8; |
853 | 154 |
155 isPlanar_ = (planarConfiguration != 0 ? true : false); | |
156 isSigned_ = (pixelRepresentation != 0 ? true : false); | |
157 } | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
158 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
159 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
160 bool DicomImageInformation::ExtractPixelFormat(PixelFormat& format) const |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
161 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
162 if (GetBitsStored() == 8 && GetChannelCount() == 1 && !IsSigned()) |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
163 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
164 format = PixelFormat_Grayscale8; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
165 return true; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
166 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
167 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
168 if (GetBitsStored() == 8 && GetChannelCount() == 3 && !IsSigned()) |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
169 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
170 format = PixelFormat_RGB24; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
171 return true; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
172 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
173 |
855 | 174 if (GetBitsAllocated() == 16 && GetChannelCount() == 1 && !IsSigned()) |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
175 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
176 format = PixelFormat_Grayscale16; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
177 return true; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
178 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
179 |
855 | 180 if (GetBitsAllocated() == 16 && GetChannelCount() == 1 && IsSigned()) |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
181 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
182 format = PixelFormat_SignedGrayscale16; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
183 return true; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
184 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
185 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
186 return false; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
187 } |
853 | 188 } |