GDCM 3.2.1

gdcmPixelFormat.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: GDCM (Grassroots DICOM). A DICOM library
4
5 Copyright (c) 2006-2011 Mathieu Malaterre
6 All rights reserved.
7 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notice for more information.
12
13=========================================================================*/
14
15#ifndef GDCMPIXELFORMAT_H
16#define GDCMPIXELFORMAT_H
17
18#include "gdcmTypes.h"
19#include <iostream>
20#include <assert.h>
21
22 namespace gdcm
23{
24
25 class TransferSyntax;
26
45 class GDCM_EXPORT PixelFormat
46{
47 friend class Bitmap;
48 friend std::ostream& operator<<(std::ostream &_os, const PixelFormat &pf);
49 public:
50 // When adding a type please add its dual type (its unsigned counterpart)
51 typedef enum {
52 UINT8,
53 INT8,
54 UINT12,
55 INT12,
56 UINT16,
57 INT16,
58 UINT32, // For some DICOM files (RT or SC)
59 INT32, // " "
60 UINT64, // Needed when input is 32bits + intercept/slope (incomplete support)
61 INT64, // " "
62 FLOAT16, // sure why not...
63 FLOAT32, // good ol' 'float'
64 FLOAT64, // aka 'double'
65 SINGLEBIT, // bool / monochrome
66 UNKNOWN // aka BitsAllocated == 0 && PixelRepresentation == 0
67 } ScalarType;
68
69 // default cstor:
70 PixelFormat () : PixelFormat(1, 8, 8, 7, 0) {}
71
72 explicit PixelFormat (
73 unsigned short samplesperpixel,
74 unsigned short bitsallocated = 8,
75 unsigned short bitsstored = 8,
76 unsigned short highbit = 7,
77 unsigned short pixelrepresentation = 0 ) :
78 SamplesPerPixel(samplesperpixel),
79 BitsAllocated(bitsallocated),
80 BitsStored(bitsstored),
81 HighBit(highbit),
82 PixelRepresentation(pixelrepresentation) {}
83 // helper, for the common case
84 PixelFormat(ScalarType st);
85
86 // For transparency of use
87 operator ScalarType() const { return GetScalarType(); }
88
91 unsigned short GetSamplesPerPixel() const;
92 void SetSamplesPerPixel(unsigned short spp)
93 {
94 gdcmAssertMacro( spp <= 4 );
95 SamplesPerPixel = spp;
96 gdcm_assert( SamplesPerPixel == 1 || SamplesPerPixel == 3 || SamplesPerPixel == 4 );
97 }
98
100 unsigned short GetBitsAllocated() const
101 {
102 return BitsAllocated;
103 }
104 void SetBitsAllocated(unsigned short ba)
105 {
106 if( ba )
107 {
108 switch( ba )
109 {
110 /* some devices (FUJIFILM CR + MONO1) incorrectly set BitsAllocated/BitsStored
111 * as bitmask instead of value. Do what they mean instead of what they say.
112 */
113 case 0xffff: ba = 16; break;
114 case 0x0fff: ba = 12; break;
115 case 0x00ff: ba = 8; break;
116 }
117 BitsAllocated = ba;
118 BitsStored = ba;
119 HighBit = (unsigned short)(ba - 1);
120 }
121 else // Make the PixelFormat as UNKNOWN
122 {
123 BitsAllocated = 0;
124 PixelRepresentation = 0;
125 }
126 }
127
129 unsigned short GetBitsStored() const
130 {
131 gdcm_assert( BitsStored <= BitsAllocated );
132 return BitsStored;
133 }
134 void SetBitsStored(unsigned short bs)
135 {
136 switch( bs )
137 {
138 /* see SetBitsAllocated for explanation
139 */
140 case 0xffff: bs = 16; break;
141 case 0x0fff: bs = 12; break;
142 case 0x00ff: bs = 8; break;
143 }
144 if( bs <= BitsAllocated && bs )
145 {
146 BitsStored = bs;
147 SetHighBit( (unsigned short) (bs - 1) );
148 }
149 }
150
152 unsigned short GetHighBit() const
153 {
154 gdcm_assert( HighBit < BitsStored );
155 return HighBit;
156 }
157 void SetHighBit(unsigned short hb)
158 {
159 switch( hb )
160 {
161 /* broken implementations that use bitmask for BitsAllocated/Stored
162 * nonetheless use (BitsStored-1) for HighBit. correct for this here.
163 */
164 case 0xfffe: hb = 15; break;
165 case 0x0ffe: hb = 11; break;
166 case 0x00fe: hb = 7; break;
167 }
168 if( hb < BitsStored )
169 HighBit = hb;
170 }
171
173 unsigned short GetPixelRepresentation() const
174 {
175 return (unsigned short)(PixelRepresentation ? 1 : 0);
176 }
177 void SetPixelRepresentation(unsigned short pr)
178 {
179 PixelRepresentation = (unsigned short)(pr ? 1 : 0);
180 }
181
183 ScalarType GetScalarType() const;
184
187 void SetScalarType(ScalarType st);
188 const char *GetScalarTypeAsString() const;
189
195 uint8_t GetPixelSize() const;
196
198 void Print(std::ostream &os) const;
199
201 int64_t GetMin() const;
202
204 int64_t GetMax() const;
205
207 bool IsValid() const;
208
209 bool operator==(ScalarType st) const
210 {
211 return GetScalarType() == st;
212 }
213 bool operator!=(ScalarType st) const
214 {
215 return GetScalarType() != st;
216 }
217 bool operator==(const PixelFormat &pf) const
218 {
219 return
220 SamplesPerPixel == pf.SamplesPerPixel &&
221 BitsAllocated == pf.BitsAllocated &&
222 BitsStored == pf.BitsStored &&
223 HighBit == pf.HighBit &&
224 PixelRepresentation == pf.PixelRepresentation;
225 }
226 bool operator!=(const PixelFormat &pf) const
227 {
228 return
229 SamplesPerPixel != pf.SamplesPerPixel ||
230 BitsAllocated != pf.BitsAllocated ||
231 BitsStored != pf.BitsStored ||
232 HighBit != pf.HighBit ||
233 PixelRepresentation != pf.PixelRepresentation;
234 }
235
236 bool IsCompatible(const TransferSyntax & ts ) const;
237 protected:
239 bool Validate();
240
241 private:
242 // D 0028|0002 [US] [Samples per Pixel] [1]
243 unsigned short SamplesPerPixel;
244 // D 0028|0100 [US] [Bits Allocated] [8]
245 unsigned short BitsAllocated;
246 // D 0028|0101 [US] [Bits Stored] [8]
247 unsigned short BitsStored;
248 // D 0028|0102 [US] [High Bit] [7]
249 unsigned short HighBit;
250 // D 0028|0103 [US] [Pixel Representation] [0]
251 unsigned short PixelRepresentation;
252};
253//-----------------------------------------------------------------------------
254 inline std::ostream& operator<<(std::ostream &os, const PixelFormat &pf)
255{
256 pf.Print( os );
257 return os;
258}
259
260} // end namespace gdcm
261
262#endif //GDCMPIXELFORMAT_H
gdcm::PixelFormat::IsCompatible
bool IsCompatible(const TransferSyntax &ts) const
gdcm::PixelFormat::Bitmap
friend class Bitmap
Definition gdcmPixelFormat.h:47
gdcm::PixelFormat::PixelFormat
PixelFormat(unsigned short samplesperpixel, unsigned short bitsallocated=8, unsigned short bitsstored=8, unsigned short highbit=7, unsigned short pixelrepresentation=0)
Definition gdcmPixelFormat.h:72
gdcm::PixelFormat::SetScalarType
void SetScalarType(ScalarType st)
gdcm::PixelFormat::operator!=
bool operator!=(ScalarType st) const
Definition gdcmPixelFormat.h:213
gdcm::PixelFormat::SetBitsStored
void SetBitsStored(unsigned short bs)
Definition gdcmPixelFormat.h:134
gdcm::PixelFormat::GetSamplesPerPixel
unsigned short GetSamplesPerPixel() const
gdcm::PixelFormat::GetPixelRepresentation
unsigned short GetPixelRepresentation() const
PixelRepresentation: 0 or 1, see Tag (0028,0103) US Pixel Representation.
Definition gdcmPixelFormat.h:173
gdcm::PixelFormat::SetSamplesPerPixel
void SetSamplesPerPixel(unsigned short spp)
Definition gdcmPixelFormat.h:92
gdcm::PixelFormat::GetPixelSize
uint8_t GetPixelSize() const
gdcm::PixelFormat::PixelFormat
PixelFormat(ScalarType st)
gdcm::PixelFormat::GetScalarTypeAsString
const char * GetScalarTypeAsString() const
gdcm::PixelFormat::SetHighBit
void SetHighBit(unsigned short hb)
Definition gdcmPixelFormat.h:157
gdcm::PixelFormat::SetBitsAllocated
void SetBitsAllocated(unsigned short ba)
Definition gdcmPixelFormat.h:104
gdcm::PixelFormat::operator!=
bool operator!=(const PixelFormat &pf) const
Definition gdcmPixelFormat.h:226
gdcm::PixelFormat::Print
void Print(std::ostream &os) const
Print.
gdcm::PixelFormat::Validate
bool Validate()
When image with 24/24/23 was read, need to validate.
gdcm::PixelFormat::GetMax
int64_t GetMax() const
return the max possible of the pixel
gdcm::PixelFormat::operator<<
friend std::ostream & operator<<(std::ostream &_os, const PixelFormat &pf)
Definition gdcmPixelFormat.h:254
gdcm::PixelFormat::PixelFormat
PixelFormat()
Definition gdcmPixelFormat.h:70
gdcm::PixelFormat::GetHighBit
unsigned short GetHighBit() const
HighBit see Tag (0028,0102) US High Bit.
Definition gdcmPixelFormat.h:152
gdcm::PixelFormat::GetBitsAllocated
unsigned short GetBitsAllocated() const
BitsAllocated see Tag (0028,0100) US Bits Allocated.
Definition gdcmPixelFormat.h:100
gdcm::PixelFormat::operator==
bool operator==(const PixelFormat &pf) const
Definition gdcmPixelFormat.h:217
gdcm::PixelFormat::operator==
bool operator==(ScalarType st) const
Definition gdcmPixelFormat.h:209
gdcm::PixelFormat::GetBitsStored
unsigned short GetBitsStored() const
BitsStored see Tag (0028,0101) US Bits Stored.
Definition gdcmPixelFormat.h:129
gdcm::PixelFormat::ScalarType
ScalarType
Definition gdcmPixelFormat.h:51
gdcm::PixelFormat::UINT32
@ UINT32
Definition gdcmPixelFormat.h:58
gdcm::PixelFormat::INT32
@ INT32
Definition gdcmPixelFormat.h:59
gdcm::PixelFormat::UNKNOWN
@ UNKNOWN
Definition gdcmPixelFormat.h:66
gdcm::PixelFormat::INT8
@ INT8
Definition gdcmPixelFormat.h:53
gdcm::PixelFormat::FLOAT32
@ FLOAT32
Definition gdcmPixelFormat.h:63
gdcm::PixelFormat::SINGLEBIT
@ SINGLEBIT
Definition gdcmPixelFormat.h:65
gdcm::PixelFormat::INT12
@ INT12
Definition gdcmPixelFormat.h:55
gdcm::PixelFormat::FLOAT64
@ FLOAT64
Definition gdcmPixelFormat.h:64
gdcm::PixelFormat::FLOAT16
@ FLOAT16
Definition gdcmPixelFormat.h:62
gdcm::PixelFormat::UINT8
@ UINT8
Definition gdcmPixelFormat.h:52
gdcm::PixelFormat::UINT12
@ UINT12
Definition gdcmPixelFormat.h:54
gdcm::PixelFormat::UINT64
@ UINT64
Definition gdcmPixelFormat.h:60
gdcm::PixelFormat::UINT16
@ UINT16
Definition gdcmPixelFormat.h:56
gdcm::PixelFormat::INT16
@ INT16
Definition gdcmPixelFormat.h:57
gdcm::PixelFormat::INT64
@ INT64
Definition gdcmPixelFormat.h:61
gdcm::PixelFormat::GetScalarType
ScalarType GetScalarType() const
ScalarType does not take into account the sample per pixel.
gdcm::PixelFormat::IsValid
bool IsValid() const
return IsValid
gdcm::PixelFormat::SetPixelRepresentation
void SetPixelRepresentation(unsigned short pr)
Definition gdcmPixelFormat.h:177
gdcm::PixelFormat::GetMin
int64_t GetMin() const
return the min possible of the pixel
gdcm::TransferSyntax
Class to manipulate Transfer Syntax.
Definition gdcmTransferSyntax.h:40
gdcm_assert
#define gdcm_assert(cond)
Definition gdcmException.h:104
gdcmAssertMacro
#define gdcmAssertMacro(arg)
Assert.
Definition gdcmTrace.h:189
gdcmTypes.h
GDCM_EXPORT
#define GDCM_EXPORT
Definition gdcmWin32.h:34
gdcm
Definition gdcmASN1.h:21
gdcm::operator<<
std::ostream & operator<<(std::ostream &os, const Directory &d)
Definition gdcmDirectory.h:88