]> Creatis software - gdcm.git/blob - src/gdcmBinEntry.cxx
33a302b73ea33184f8fcd3ef28d1131349b9452f
[gdcm.git] / src / gdcmBinEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmBinEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/11 14:48:19 $
7   Version:   $Revision: 1.80 $
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
81    //  The same operation should be done if we wanted 
82    //  to write image with Big Endian Transfer Syntax, 
83    //  while working on Little Endian Processor
84    // --> forget Big Endian Transfer Syntax writting!
85    //     Next DICOM version will give it up ...
86
87    // -->
88    // --> FIXME 
89    // -->
90    // The stuff looks nice, but it's probably bugged,
91    // since troubles occur on big endian processors (SunSparc, Motorola)
92    // while reading the pixels of a 
93    // gdcm-written Little-Endian 16 bits per pixel image
94
95 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
96
97       /// \todo FIXME : Right now, we only care of Pixels element
98       ///       we should deal with *all* the BinEntries
99       ///       Well, not really since we are not interpreting values read...
100
101       // 8 Bits Pixels *are* OB, 16 Bits Pixels *are* OW
102       // -value forced while Reading process-
103       
104       //-->
105       // -->
106       // -->  WARNING
107       // -->        the following lines *looked* very clever, 
108       // -->        but they don't work on big endian processors.
109       // -->        since I've no access for the moment to a big endian proc :-(
110       // -->        I comment them out, to see the result on the dash board 
111       // -->     
112       
113       // --> Revert to initial code : TestWriteSimple hangs on Darwin :-(     
114       if (GetGroup() == 0x7fe0 && GetVR() == "OW")
115       {  
116          uint16_t *binArea16 = (uint16_t*)binArea8;
117          binary_write (*fp, binArea16, lgr );
118       }
119       else
120       { 
121          // For any other VR, BinEntry is re-written as-is
122          binary_write (*fp, binArea8, lgr );
123       }
124             
125       //-->
126       // -->
127       // -->  WARNING      
128       // -->         remove the following line, an uncomment the previous ones, 
129       // -->         if it doesn't work better
130       // -->     
131       /*binary_write ( *fp, binArea8, lgr ); // Elem value*/
132       
133 #else
134       binary_write ( *fp, binArea8, lgr ); // Elem value
135 #endif //GDCM_WORDS_BIGENDIAN
136
137    }
138    else
139    {
140       // nothing was loaded, but we need to skip space on disc
141       
142       //  --> WARNING : nothing is written; 
143       //  --> the initial data (on the the source image) is lost
144       //  --> user is *not* informed !
145       
146       fp->seekp(lgr, std::ios::cur);
147    }
148 }
149
150 /**
151  * \brief Sets the value (non string) of the current Dicom Header Entry
152  */
153 void BinEntry::SetBinArea( uint8_t *area, bool self )  
154
155    if (BinArea && SelfArea)
156       delete[] BinArea;
157
158    BinArea = area;
159    SelfArea=self;
160 }
161
162 //-----------------------------------------------------------------------------
163 // Protected
164
165 //-----------------------------------------------------------------------------
166 // Private
167    
168 //-----------------------------------------------------------------------------
169 // Print
170 /**
171  * \brief   Prints a BinEntry (Dicom entry)
172  * @param   os ostream we want to print in
173  * @param indent Indentation string to be prepended during printing
174  */
175 void BinEntry::Print(std::ostream &os, std::string const & )
176 {
177    os << "B ";
178    DocEntry::Print(os);
179    std::ostringstream s;
180    void* binArea = GetBinArea();
181    if (binArea)
182    {
183       if ( GetVR() == "FL" )
184       {
185          int l = GetReadLength()/4 - 1;
186          float *beg = (float *)GetBinArea();
187          s << " [" << *beg;
188          if ( l!= 0)
189             for (int i=0;i<l;i++)
190             {
191                beg++;
192                s << "\\" << *beg;
193             }
194             s << "]";
195       }
196       else if ( GetVR() == "FD" )
197       {
198          int l = GetReadLength()/8 - 1;
199          double *beg = (double *)GetBinArea();
200          s << " [" << *beg;
201          if ( l!= 0)
202             for (int i=0;i<l;i++)
203             {
204                beg++;
205                s << "\\" << *beg;
206             }
207             s << "]";
208       }
209       else
210       { 
211          if ( Util::IsCleanArea( GetBinArea(),GetLength()  ) )
212          {
213             std::string cleanString = 
214                    Util::CreateCleanString( GetBinArea(),GetLength()  );
215             s << " [" << cleanString << "]";
216          }
217          else
218          {
219             //s << " [" << GetValue()
220             s << " [" << GDCM_BINLOADED << ";"
221               << "length = " << GetLength() << "]";
222          } 
223       }
224    }
225    else
226    {
227       if ( GetLength() == 0 )
228       {
229          s << " []";
230       }
231       else 
232       {
233          s << " [" <<GetValue() << "]";
234       }         
235    }
236    os << s.str();
237 }
238
239 //-----------------------------------------------------------------------------
240 } // end namespace gdcm