# gdcmPixelFormat.h

Go to the documentation of this file.

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