]> Creatis software - gdcm.git/blob - src/gdcmBinEntry.cxx
The way we dealt with 16 bits 'Pixel Data' on Big Endian processors
[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 08:55:58 $
7   Version:   $Revision: 1.79 $
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       if (GetGroup() == 0x7fe0 && GetVR() == "OW")
114       {  
115          uint16_t *binArea16 = (uint16_t*)binArea8;
116          binary_write (*fp, binArea16, lgr );
117       }
118       else
119       { 
120          // For any other VR, BinEntry is re-written as-is
121          binary_write (*fp, binArea8, lgr );
122       }
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 #else
133       binary_write ( *fp, binArea8, lgr ); // Elem value
134 #endif //GDCM_WORDS_BIGENDIAN
135
136    }
137    else
138    {
139       // nothing was loaded, but we need to skip space on disc
140       fp->seekp(lgr, std::ios::cur);
141    }
142 }
143
144 /**
145  * \brief Sets the value (non string) of the current Dicom Header Entry
146  */
147 void BinEntry::SetBinArea( uint8_t *area, bool self )  
148
149    if (BinArea && SelfArea)
150       delete[] BinArea;
151
152    BinArea = area;
153    SelfArea=self;
154 }
155
156 //-----------------------------------------------------------------------------
157 // Protected
158
159 //-----------------------------------------------------------------------------
160 // Private
161    
162 //-----------------------------------------------------------------------------
163 // Print
164 /**
165  * \brief   Prints a BinEntry (Dicom entry)
166  * @param   os ostream we want to print in
167  * @param indent Indentation string to be prepended during printing
168  */
169 void BinEntry::Print(std::ostream &os, std::string const & )
170 {
171    os << "B ";
172    DocEntry::Print(os);
173    std::ostringstream s;
174    void* binArea = GetBinArea();
175    if (binArea)
176    {
177       if ( GetVR() == "FL" )
178       {
179          int l = GetReadLength()/4 - 1;
180          float *beg = (float *)GetBinArea();
181          s << " [" << *beg;
182          if ( l!= 0)
183             for (int i=0;i<l;i++)
184             {
185                beg++;
186                s << "\\" << *beg;
187             }
188             s << "]";
189       }
190       else if ( GetVR() == "FD" )
191       {
192          int l = GetReadLength()/8 - 1;
193          double *beg = (double *)GetBinArea();
194          s << " [" << *beg;
195          if ( l!= 0)
196             for (int i=0;i<l;i++)
197             {
198                beg++;
199                s << "\\" << *beg;
200             }
201             s << "]";
202       }
203       else
204       { 
205          if ( Util::IsCleanArea( GetBinArea(),GetLength()  ) )
206          {
207             std::string cleanString = 
208                    Util::CreateCleanString( GetBinArea(),GetLength()  );
209             s << " [" << cleanString << "]";
210          }
211          else
212          {
213             //s << " [" << GetValue()
214             s << " [" << GDCM_BINLOADED << ";"
215               << "length = " << GetLength() << "]";
216          } 
217       }
218    }
219    else
220    {
221       if ( GetLength() == 0 )
222       {
223          s << " []";
224       }
225       else 
226       {
227          s << " [" <<GetValue() << "]";
228       }         
229    }
230    os << s.str();
231 }
232
233 //-----------------------------------------------------------------------------
234 } // end namespace gdcm