GDCM: gdcmPixelFormat.h Source File

GDCM 3.2.8

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
22namespace gdcm
23{
24
25class TransferSyntax;
26
45 class GDCM_EXPORT PixelFormat
46{
47 friend class Bitmap;
48 friend std::ostream& operator<<(std::ostream &_os, const PixelFormat &pf);
49public:
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( BitsStored > 1 && hb == 0 )
169 HighBit = BitsStored - 1;
170 else if( hb < BitsStored )
171 HighBit = hb;
172 }
173
175 unsigned short GetPixelRepresentation() const
176 {
177 return (unsigned short)(PixelRepresentation ? 1 : 0);
178 }
179 void SetPixelRepresentation(unsigned short pr)
180 {
181 PixelRepresentation = (unsigned short)(pr ? 1 : 0);
182 }
183
185 ScalarType GetScalarType() const;
186
189 void SetScalarType(ScalarType st);
190 const char *GetScalarTypeAsString() const;
191
197 uint8_t GetPixelSize() const;
198
200 void Print(std::ostream &os) const;
201
203 int64_t GetMin() const;
204
206 int64_t GetMax() const;
207
209 bool IsValid() const;
210
211 bool operator==(ScalarType st) const
212 {
213 return GetScalarType() == st;
214 }
215 bool operator!=(ScalarType st) const
216 {
217 return GetScalarType() != st;
218 }
219 bool operator==(const PixelFormat &pf) const
220 {
221 return
222 SamplesPerPixel == pf.SamplesPerPixel &&
223 BitsAllocated == pf.BitsAllocated &&
224 BitsStored == pf.BitsStored &&
225 HighBit == pf.HighBit &&
226 PixelRepresentation == pf.PixelRepresentation;
227 }
228 bool operator!=(const PixelFormat &pf) const
229 {
230 return
231 SamplesPerPixel != pf.SamplesPerPixel ||
232 BitsAllocated != pf.BitsAllocated ||
233 BitsStored != pf.BitsStored ||
234 HighBit != pf.HighBit ||
235 PixelRepresentation != pf.PixelRepresentation;
236 }
237
238 bool IsCompatible(const TransferSyntax & ts ) const;
239protected:
241 bool Validate();
242
243private:
244 // D 0028|0002 [US] [Samples per Pixel] [1]
245 unsigned short SamplesPerPixel;
246 // D 0028|0100 [US] [Bits Allocated] [8]
247 unsigned short BitsAllocated;
248 // D 0028|0101 [US] [Bits Stored] [8]
249 unsigned short BitsStored;
250 // D 0028|0102 [US] [High Bit] [7]
251 unsigned short HighBit;
252 // D 0028|0103 [US] [Pixel Representation] [0]
253 unsigned short PixelRepresentation;
254};
255//-----------------------------------------------------------------------------
256 inline std::ostream& operator<<(std::ostream &os, const PixelFormat &pf)
257{
258 pf.Print( os );
259 return os;
260}
261
262} // end namespace gdcm
263
264#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:215
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:175
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:228
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:256
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:219
gdcm::PixelFormat::operator==
bool operator==(ScalarType st) const
Definition gdcmPixelFormat.h:211
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:179
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