]> Creatis software - gdcm.git/blob - src/gdcmBinEntry.cxx
ENH: Adding new option in CMake: GDCM_FORCE_BIGENDIAN_EMULATION. You are now able...
[gdcm.git] / src / gdcmBinEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmBinEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/11 20:04:07 $
7   Version:   $Revision: 1.71 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmBinEntry.h"
20 #include "gdcmContentEntry.h"
21 #include "gdcmUtil.h"
22
23 #include <fstream>
24 #include <sstream>
25 #include <iostream> // for std::ios_base, since <ios> does not exist on gcc/Solaris
26
27 namespace gdcm 
28 {
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
31 /**
32  * \brief   Constructor from a given BinEntry
33  */
34 BinEntry::BinEntry(DictEntry *e) 
35          :ContentEntry(e)
36 {
37    BinArea = 0;
38    SelfArea = true;
39 }
40
41 /**
42  * \brief   Constructor from a given BinEntry
43  * @param   e Pointer to existing Doc entry
44  */
45 BinEntry::BinEntry(DocEntry *e) 
46         : ContentEntry(e->GetDictEntry())
47 {
48    Copy(e);
49
50    BinArea = 0;
51    SelfArea = true;
52 }
53
54 /**
55  * \brief   Canonical destructor.
56  */
57 BinEntry::~BinEntry()
58 {
59    if (BinArea && SelfArea)
60    {
61       delete[] BinArea;
62       BinArea = 0; // let's be carefull !
63    }
64 }
65
66 //-----------------------------------------------------------------------------
67 // Public
68 /**
69  * \brief   canonical Writer
70  * @param fp already open file pointer
71  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
72 */
73 void BinEntry::WriteContent(std::ofstream *fp, FileType filetype)
74
75    DocEntry::WriteContent(fp, filetype);
76    uint8_t* binArea8 = BinArea; //safe notation
77    size_t lgr = GetLength();
78    if (BinArea) // the binArea was *actually* loaded
79    {
80    /// \todo  Probabely, the same operation will have to be done when we want 
81    ///        to write image with Big Endian Transfert Syntax, 
82    ///        and we are working on Little Endian Processor
83
84 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
85       /// \todo FIXME Right now, we only care of Pixels element
86       ///       we should deal with *all* the BinEntries
87       ///       well not really since we are not interpreting values read...
88
89       // 8 Bits Pixels *are* OB, 16 Bits Pixels *are* OW
90       // -value forced while Reading process-
91       if (GetGroup() == 0x7fe0 && GetVR() == "OW")
92       {     
93          uint16_t *binArea16 = (uint16_t*)binArea8;
94          binary_write (*fp, binArea16, lgr );
95       }
96       else
97       { 
98          // For any other VR, BinEntry is re-written as-is
99          binary_write (*fp, binArea8, lgr );
100       }
101 #else
102       binary_write ( *fp, binArea8, lgr ); // Elem value
103 #endif //GDCM_WORDS_BIGENDIAN
104
105    }
106    else
107    {
108       // nothing was loaded, but we need to skip space on disc
109       fp->seekp(lgr, std::ios::cur);
110    }
111 }
112
113 /**
114  * \brief Sets the value (non string) of the current Dicom Header Entry
115  */
116 void BinEntry::SetBinArea( uint8_t *area, bool self )  
117
118    if (BinArea && SelfArea)
119       delete[] BinArea;
120
121    BinArea = area;
122    SelfArea=self;
123 }
124
125 //-----------------------------------------------------------------------------
126 // Protected
127
128 //-----------------------------------------------------------------------------
129 // Private
130    
131 //-----------------------------------------------------------------------------
132 // Print
133 /**
134  * \brief   Prints a BinEntry (Dicom entry)
135  * @param   os ostream we want to print in
136  * @param indent Indentation string to be prepended during printing
137  */
138 void BinEntry::Print(std::ostream &os, std::string const & )
139 {
140    os << "B ";
141    DocEntry::Print(os);
142    std::ostringstream s;
143    void* binArea = GetBinArea();
144    if (binArea)
145    {
146       s << " [" << GetValue()
147         << "; length = " << GetLength() << "]";
148    }
149    else
150    {
151       if ( GetLength() == 0 )
152       {
153          s << " []";
154       }
155       else 
156       {
157          s << " [" <<GetValue() << "]";
158       }         
159    }
160    os << s.str();
161 }
162
163 //-----------------------------------------------------------------------------
164 } // end namespace gdcm