]> Creatis software - gdcm.git/blob - src/gdcmBinEntry.cxx
STYLE: Remove old comments
[gdcm.git] / src / gdcmBinEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmBinEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/09/21 09:44:59 $
7   Version:   $Revision: 1.78 $
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       if (GetGroup() == 0x7fe0 && GetVR() == "OW")
104       {     
105          uint16_t *binArea16 = (uint16_t*)binArea8;
106          binary_write (*fp, binArea16, lgr );
107       }
108       else
109       { 
110          // For any other VR, BinEntry is re-written as-is
111          binary_write (*fp, binArea8, lgr );
112       }
113 #else
114       binary_write ( *fp, binArea8, lgr ); // Elem value
115 #endif //GDCM_WORDS_BIGENDIAN
116
117    }
118    else
119    {
120       // nothing was loaded, but we need to skip space on disc
121       fp->seekp(lgr, std::ios::cur);
122    }
123 }
124
125 /**
126  * \brief Sets the value (non string) of the current Dicom Header Entry
127  */
128 void BinEntry::SetBinArea( uint8_t *area, bool self )  
129
130    if (BinArea && SelfArea)
131       delete[] BinArea;
132
133    BinArea = area;
134    SelfArea=self;
135 }
136
137 //-----------------------------------------------------------------------------
138 // Protected
139
140 //-----------------------------------------------------------------------------
141 // Private
142    
143 //-----------------------------------------------------------------------------
144 // Print
145 /**
146  * \brief   Prints a BinEntry (Dicom entry)
147  * @param   os ostream we want to print in
148  * @param indent Indentation string to be prepended during printing
149  */
150 void BinEntry::Print(std::ostream &os, std::string const & )
151 {
152    os << "B ";
153    DocEntry::Print(os);
154    std::ostringstream s;
155    void* binArea = GetBinArea();
156    if (binArea)
157    {
158       if ( GetVR() == "FL" )
159       {
160          int l = GetReadLength()/4 - 1;
161          float *beg = (float *)GetBinArea();
162          s << " [" << *beg;
163          if ( l!= 0)
164             for (int i=0;i<l;i++)
165             {
166                beg++;
167                s << "\\" << *beg;
168             }
169             s << "]";
170       }
171       else if ( GetVR() == "FD" )
172       {
173          int l = GetReadLength()/8 - 1;
174          double *beg = (double *)GetBinArea();
175          s << " [" << *beg;
176          if ( l!= 0)
177             for (int i=0;i<l;i++)
178             {
179                beg++;
180                s << "\\" << *beg;
181             }
182             s << "]";
183       }
184       else
185       { 
186          if ( Util::IsCleanArea( GetBinArea(),GetLength()  ) )
187          {
188             std::string cleanString = 
189                    Util::CreateCleanString( GetBinArea(),GetLength()  );
190             s << " [" << cleanString << "]";
191          }
192          else
193          {
194             //s << " [" << GetValue()
195             s << " [" << GDCM_BINLOADED << ";"
196               << "length = " << GetLength() << "]";
197          } 
198       }
199    }
200    else
201    {
202       if ( GetLength() == 0 )
203       {
204          s << " []";
205       }
206       else 
207       {
208          s << " [" <<GetValue() << "]";
209       }         
210    }
211    os << s.str();
212 }
213
214 //-----------------------------------------------------------------------------
215 } // end namespace gdcm