]> Creatis software - gdcm.git/blob - src/gdcmBinEntry.cxx
fdbda3f1a9b4dd4cc33a3a8b81e564bde90123c8
[gdcm.git] / src / gdcmBinEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmBinEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/08/25 14:59:49 $
7   Version:   $Revision: 1.74 $
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  Probably, the same operation should be done if we wanted 
81    ///        to write image with Big Endian Transfer Syntax, 
82    ///        while 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       if ( GetVR() == "FL" )
147       {
148          int l = GetReadLength()/4 - 1;
149          float *beg = (float *)GetBinArea();
150          s << " [" << *beg;
151          if ( l!= 0)
152             for (int i=0;i<l;i++)
153             {
154                beg++;
155                s << "\\" << *beg;
156             }
157             s << "]";
158       }
159       else if ( GetVR() == "FD" )
160       {
161          int l = GetReadLength()/8 - 1;
162          double *beg = (double *)GetBinArea();
163          s << " [" << *beg;
164          if ( l!= 0)
165             for (int i=0;i<l;i++)
166             {
167                beg++;
168                s << "\\" << *beg;
169             }
170             s << "]";
171       }
172       else
173       {
174          if ( Util::IsCleanArea( GetBinArea(),GetLength()  ) )
175          {
176             std::string cleanString = 
177                    Util::CreateCleanString( GetBinArea(),GetLength()  );
178             s << " [" << cleanString << "]";
179          }
180          else
181          {
182             s << " [" << GetValue()
183               << "; length = " << GetLength() << "]";
184          }
185  
186       }
187    }
188    else
189    {
190       if ( GetLength() == 0 )
191       {
192          s << " []";
193       }
194       else 
195       {
196          s << " [" <<GetValue() << "]";
197       }         
198    }
199    os << s.str();
200 }
201
202 //-----------------------------------------------------------------------------
203 } // end namespace gdcm