Main Page | File List | Related Pages

gdcmParser.h

00001 // gdcmParser.h
00002 //-----------------------------------------------------------------------------
00003 #ifndef GDCMPARSER_H
00004 #define GDCMPARSER_H
00005 
00006 #include "gdcmCommon.h"
00007 #include "gdcmVR.h"
00008 #include "gdcmTS.h"
00009 #include "gdcmException.h"
00010 #include "gdcmDictSet.h"
00011 #include "gdcmHeaderEntry.h"
00012 
00013 #include <map>
00014 #include <list>       // for linking together *all* the Dicom Elements
00015 
00016 //-----------------------------------------------------------------------------
00017 typedef std::string VRKey;
00018 typedef std::string VRAtr;
00019 typedef std::map<VRKey, VRAtr> VRHT;    // Value Representation Hash Table
00020 
00021 typedef std::multimap<TagKey, gdcmHeaderEntry *> TagHeaderEntryHT;
00022 typedef std::pair<TagKey, gdcmHeaderEntry *> PairHT;
00023 typedef std::pair<TagHeaderEntryHT::iterator,TagHeaderEntryHT::iterator> IterHT; 
00024 
00025 typedef std::list<gdcmHeaderEntry *> ListTag; // for linking together the Elements
00026 
00027 typedef std::string GroupKey;
00028 typedef std::map<GroupKey, int> GroupHT;
00029 
00030 //-----------------------------------------------------------------------------
00031 /*
00032  * \defgroup gdcmParser
00033  * \brief used by both gdcmHeader and gdcmDicomDir
00034  */
00035 class GDCM_EXPORT gdcmParser
00036 {
00037 public:
00038    gdcmParser(bool exception_on_error  = false);
00039    gdcmParser(const char *filename, 
00040               bool  exception_on_error = false, 
00041               bool  enable_sequences   = false,
00042               bool  ignore_shadow      = false);
00043    virtual ~gdcmParser(void);
00044 
00045 // Print
00051    void SetPrintLevel(int level) { printLevel = level; };
00052    virtual void Print        (std::ostream &os = std::cout) {PrintEntry(os);};
00053    virtual void PrintEntry   (std::ostream &os = std::cout);
00054    virtual void PrintPubDict (std::ostream &os = std::cout);
00055    virtual void PrintShaDict (std::ostream &os = std::cout);
00056 
00057 // Standard values
00058    inline std::string GetFileName(void) {return filename;}
00059 
00060 // Dictionnaries
00061    gdcmDict *GetPubDict(void);
00062    gdcmDict *GetShaDict(void);
00063    bool SetShaDict(gdcmDict *dict);
00064    bool SetShaDict(DictKey dictName);
00065 
00066 // Informations contained in the parser
00067    virtual bool IsReadable(void);
00068    bool IsImplicitVRLittleEndianTransferSyntax(void);
00069    bool IsExplicitVRLittleEndianTransferSyntax(void);
00070    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
00071    bool IsExplicitVRBigEndianTransferSyntax(void);
00072    FileType GetFileType(void);
00073 
00074 // Entries
00080    inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
00081 
00087    inline ListTag &GetListEntry(void) { return listEntries; };
00088 
00089 // Read (used in gdcmFile)
00090    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
00091    bool CloseFile(void);
00092 
00093 // Write (used in gdcmFile)
00094    virtual bool Write(FILE *, FileType);
00095 
00096    bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
00097    bool ReplaceOrCreateByNumber(     char  *Value, guint16 Group, guint16 Elem);
00098    bool ReplaceIfExistByNumber (     char  *Value, guint16 Group, guint16 Elem);
00099 
00100 // System access
00101    inline int GetSwapCode(void) { return sw; }
00102    guint16 GetGrPixel(void)  {return GrPixel;}
00103    guint16 GetNumPixel(void) {return NumPixel;}
00104    
00105    guint16 SwapShort(guint16);   // needed by gdcmFile
00106    guint32 SwapLong(guint32);    // needed by gdcmFile
00107    guint16 UnswapShort(guint16); // needed by gdcmFile
00108    guint32 UnswapLong(guint32);  // needed by gdcmFile
00109 
00110 protected:
00111 // Entry
00112    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
00113    virtual std::string GetEntryByName    (std::string tagName);
00114    virtual std::string GetEntryVRByName  (std::string tagName);
00115    virtual std::string GetEntryByNumber  (guint16 group, guint16 element);
00116    virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
00117 
00118    virtual bool SetEntryByName  (std::string content, std::string tagName);
00119    virtual bool SetEntryByNumber(std::string content, guint16 group, guint16 element);
00120    virtual bool SetEntryLengthByNumber(guint32 length, guint16 group, guint16 element);
00121 
00122    virtual size_t GetEntryOffsetByNumber  (guint16 Group, guint16 Elem);
00123    virtual void  *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);   
00124    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
00125    virtual bool   SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
00126 
00127    virtual void UpdateShaEntries(void);
00128 
00129 // Header entry
00130    gdcmHeaderEntry *GetHeaderEntryByNumber  (guint16 group, guint16 element); 
00131    gdcmHeaderEntry *GetHeaderEntryByName    (std::string Name);
00132    IterHT           GetHeaderEntrySameNumber(guint16 group, guint16 element); 
00133 // IterHT           GetHeaderEntrySameName  (std::string Name); 
00134 
00135    void LoadHeaderEntrySafe(gdcmHeaderEntry *);
00136 
00137    void UpdateGroupLength(bool SkipSequence = false, FileType type = ImplicitVR);
00138    void WriteEntries(FILE *_fp,FileType type);
00139 
00140 // Variables
00141    FILE *fp;
00142    FileType filetype; // ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
00143 
00144    static const unsigned int HEADER_LENGTH_TO_READ; 
00145    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
00146    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
00147 
00148 protected:
00149    TagHeaderEntryHT tagHT; // H Table (multimap), to provide fast access
00150    ListTag listEntries;    // chained list, to keep the 'spacial' ordering
00151     
00152    int enableSequences;
00153    int printLevel;
00154    
00155    // For some ACR-NEMA images, it's *not* 7fe0, 0010 ...    
00156    guint16 GrPixel;
00157    guint16 NumPixel;
00158    // some files may contain icons; GrPixel,NumPixel appears several times
00159    // Let's remember how many times!
00160    int countGrPixel;
00161       
00162 private:
00163    // Read
00164    bool ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
00165 
00166    void LoadHeaderEntries    (void);
00167    void LoadHeaderEntry      (gdcmHeaderEntry *);
00168    void AddHeaderEntry       (gdcmHeaderEntry *);
00169    void FindHeaderEntryLength(gdcmHeaderEntry *);
00170    void FindHeaderEntryVR    (gdcmHeaderEntry *);
00171    bool CheckHeaderEntryVR   (gdcmHeaderEntry *, VRKey);
00172 
00173    std::string GetHeaderEntryValue  (gdcmHeaderEntry *);
00174    std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
00175 
00176    void SkipHeaderEntry          (gdcmHeaderEntry *);
00177    void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
00178    bool IsHeaderEntryAnInteger   (gdcmHeaderEntry *);
00179 
00180    guint32 FindHeaderEntryLengthOB(void);
00181 
00182    guint16 ReadInt16(void);
00183    guint32 ReadInt32(void);
00184    void    SkipBytes(guint32);
00185 
00186    void Initialise(void);
00187    bool CheckSwap(void);
00188    void SwitchSwapToBigEndian(void);
00189    void SetMaxSizeLoadEntry(long);
00190    void SetMaxSizePrintEntry(long);
00191 
00192    // DictEntry  related utilities
00193    gdcmDictEntry *GetDictEntryByName  (std::string Name);
00194    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
00195    gdcmDictEntry *NewVirtualDictEntry(guint16 group, 
00196                                       guint16 element,
00197                                       std::string vr     = "unkn",
00198                                       std::string fourth = "unkn",
00199                                       std::string name   = "unkn");
00200    gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *);
00201    
00202    // HeaderEntry related utilities
00203    gdcmHeaderEntry *ReadNextHeaderEntry   (void);
00204    gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group, 
00205                                            guint16 element);
00206    gdcmHeaderEntry *NewHeaderEntryByName  (std::string Name);
00207 
00208 
00209    // Deprecated (Not used) --> commented out
00210    //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
00211    //                                               std::string VR);
00212    guint32 GenerateFreeTagKeyInGroup(guint16 group);
00213 
00214    // Refering underlying filename.
00215    std::string filename; 
00216 
00217    // Public dictionary used to parse this header
00218    gdcmDict *RefPubDict;
00219    // Optional "shadow dictionary" (private elements) used to parse this header
00220    gdcmDict *RefShaDict;
00221 
00222    // = 1 if a gdcmHeaderEntry was added post parsing 
00223    int wasUpdated;
00224    
00225    // =1 if user wants to skip shadow groups while parsing (to save space)
00226    int ignoreShadow;
00227 
00228    // Swap code e.g. little, big, bad-big, bad-little endian). Warning:
00229    // this code is not fixed during header parsing.
00230    int sw;
00231 
00232    // Size treshold above which an element value will NOT be loaded in 
00233    // memory (to avoid loading the image/volume itself). By default,
00234    // this upper bound is fixed to 1024 bytes (which might look reasonable
00235    // when one considers the definition of the various VR contents).
00236    guint32 MaxSizeLoadEntry;
00237    // Size treshold above which an element value will NOT be *printed* in 
00238    // order no to polute the screen output. By default,
00239    // this upper bound is fixed to 64 bytes.   
00240    guint32 MaxSizePrintEntry;
00241    
00242 };
00243 
00244 //-----------------------------------------------------------------------------
00245 #endif

Generated on Mon Feb 14 16:13:44 2005 for gdcm by doxygen 1.3.6