GDCM
2.2.3
|
00001 /*========================================================================= 00002 00003 Program: GDCM (Grassroots DICOM). A DICOM library 00004 00005 Copyright (c) 2006-2011 Mathieu Malaterre 00006 All rights reserved. 00007 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details. 00008 00009 This software is distributed WITHOUT ANY WARRANTY; without even 00010 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00011 PURPOSE. See the above copyright notice for more information. 00012 00013 =========================================================================*/ 00014 00015 #ifndef GDCMPIXELFORMAT_H 00016 #define GDCMPIXELFORMAT_H 00017 00018 #include "gdcmTypes.h" 00019 #include <iostream> 00020 #include <assert.h> 00021 00022 namespace gdcm 00023 { 00024 00036 class GDCM_EXPORT PixelFormat 00037 { 00038 friend class Bitmap; 00039 friend std::ostream& operator<<(std::ostream &_os, const PixelFormat &pf); 00040 public: 00041 // When adding a type please add its dual type (its unsigned conterpart) 00042 typedef enum { 00043 UINT8, 00044 INT8, 00045 UINT12, 00046 INT12, 00047 UINT16, 00048 INT16, 00049 UINT32, // For some DICOM files (RT or SC) 00050 INT32, // " " 00051 FLOAT16, // sure why not... 00052 FLOAT32, // good ol' 'float' 00053 FLOAT64, // aka 'double' 00054 SINGLEBIT, // bool / monochrome 00055 UNKNOWN // aka BitsAllocated == 0 && PixelRepresentation == 0 00056 } ScalarType; 00057 00058 // default cstor: 00059 explicit PixelFormat ( 00060 unsigned short samplesperpixel = 1, 00061 unsigned short bitsallocated = 8, 00062 unsigned short bitsstored = 8, 00063 unsigned short highbit = 7, 00064 unsigned short pixelrepresentation = 0 ) : 00065 SamplesPerPixel(samplesperpixel), 00066 BitsAllocated(bitsallocated), 00067 BitsStored(bitsstored), 00068 HighBit(highbit), 00069 PixelRepresentation(pixelrepresentation) {} 00070 // helper, for the common case 00071 PixelFormat(ScalarType st); 00072 ~PixelFormat() {} 00073 00074 // For transparency of use 00075 operator ScalarType() const { return GetScalarType(); } 00076 00079 unsigned short GetSamplesPerPixel() const; 00080 void SetSamplesPerPixel(unsigned short spp) 00081 { 00082 gdcmAssertMacro( spp <= 4 ); 00083 SamplesPerPixel = spp; 00084 assert( SamplesPerPixel == 1 || SamplesPerPixel == 3 || SamplesPerPixel == 4 ); 00085 } 00086 00088 unsigned short GetBitsAllocated() const 00089 { 00090 return BitsAllocated; 00091 } 00092 void SetBitsAllocated(unsigned short ba) 00093 { 00094 if( ba ) 00095 { 00096 BitsAllocated = ba; 00097 BitsStored = ba; 00098 HighBit = (unsigned short)(ba - 1); 00099 } 00100 else // Make the PixelFormat as UNKNOWN 00101 { 00102 BitsAllocated = 0; 00103 PixelRepresentation = 0; 00104 } 00105 } 00106 00108 unsigned short GetBitsStored() const 00109 { 00110 assert( BitsStored <= BitsAllocated ); 00111 return BitsStored; 00112 } 00113 void SetBitsStored(unsigned short bs) 00114 { 00115 if( bs <= BitsAllocated && bs ) 00116 { 00117 BitsStored = bs; 00118 SetHighBit( (unsigned short) (bs - 1) ); 00119 } 00120 } 00121 00123 unsigned short GetHighBit() const 00124 { 00125 assert( HighBit < BitsStored ); 00126 return HighBit; 00127 } 00128 void SetHighBit(unsigned short hb) 00129 { 00130 if( hb < BitsStored ) 00131 HighBit = hb; 00132 } 00133 00135 unsigned short GetPixelRepresentation() const 00136 { 00137 return (unsigned short)(PixelRepresentation ? 1 : 0); 00138 } 00139 void SetPixelRepresentation(unsigned short pr) 00140 { 00141 PixelRepresentation = (unsigned short)(pr ? 1 : 0); 00142 } 00143 00145 ScalarType GetScalarType() const; 00146 00149 void SetScalarType(ScalarType st); 00150 const char *GetScalarTypeAsString() const; 00151 00157 uint8_t GetPixelSize() const; 00158 00160 void Print(std::ostream &os) const; 00161 00163 int64_t GetMin() const; 00164 00166 int64_t GetMax() const; 00167 00169 bool IsValid() const; 00170 00171 bool operator==(ScalarType st) const 00172 { 00173 return GetScalarType() == st; 00174 } 00175 bool operator!=(ScalarType st) const 00176 { 00177 return GetScalarType() != st; 00178 } 00179 bool operator==(const PixelFormat &pf) const 00180 { 00181 return 00182 SamplesPerPixel == pf.SamplesPerPixel && 00183 BitsAllocated == pf.BitsAllocated && 00184 BitsStored == pf.BitsStored && 00185 HighBit == pf.HighBit && 00186 PixelRepresentation == pf.PixelRepresentation; 00187 } 00188 bool operator!=(const PixelFormat &pf) const 00189 { 00190 return 00191 SamplesPerPixel != pf.SamplesPerPixel || 00192 BitsAllocated != pf.BitsAllocated || 00193 BitsStored != pf.BitsStored || 00194 HighBit != pf.HighBit || 00195 PixelRepresentation != pf.PixelRepresentation; 00196 } 00197 00198 protected: 00200 bool Validate(); 00201 00202 private: 00203 // D 0028|0002 [US] [Samples per Pixel] [1] 00204 unsigned short SamplesPerPixel; 00205 // D 0028|0100 [US] [Bits Allocated] [8] 00206 unsigned short BitsAllocated; 00207 // D 0028|0101 [US] [Bits Stored] [8] 00208 unsigned short BitsStored; 00209 // D 0028|0102 [US] [High Bit] [7] 00210 unsigned short HighBit; 00211 // D 0028|0103 [US] [Pixel Representation] [0] 00212 unsigned short PixelRepresentation; 00213 }; 00214 //----------------------------------------------------------------------------- 00215 inline std::ostream& operator<<(std::ostream &os, const PixelFormat &pf) 00216 { 00217 pf.Print( os ); 00218 return os; 00219 } 00220 00221 } // end namespace gdcm 00222 00223 #endif //GDCMPIXELFORMAT_H